Skip to content

Commit f3bf641

Browse files
committed
Init for ua gateway example
1 parent 59b2baa commit f3bf641

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

examples/ua gateway example.py

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# -------------------------------------------------------------------------
2+
# Copyright (c) PTC Inc. and/or all its affiliates. All rights reserved.
3+
# See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
7+
# UA Gateway Example - Simple example on how to manage a connection and
8+
# execute various calls for the UA Gateway components of the Kepware
9+
# Configuration API
10+
11+
from kepconfig import connection, error, ua_gateway
12+
13+
14+
def ErrorHandler(err):
15+
# Generic Handler for exception errors
16+
if isinstance(err, error.KepHTTPError):
17+
print(err.code)
18+
print(err.msg)
19+
print(err.url)
20+
print(err.hdrs)
21+
print(err.payload)
22+
elif isinstance(err, error.KepURLError):
23+
print(err.url)
24+
print(err.reason)
25+
elif isinstance(err, error.KepError):
26+
print(err.msg)
27+
else:
28+
print('Different Exception Received: {}'.format(err))
29+
30+
# This creates a server reference that is used to target all modifications of
31+
# the Kepware configuration
32+
server = connection.server(host = '127.0.0.1', port = 57412, user = 'Administrator', pw = '')
33+
34+
# Print UAG Instance Certificate information
35+
try:
36+
print("{} - {}".format("Read UA Gateway Instance Certificate properties", ua_gateway.certificates.get_instance_certificate(server)))
37+
except Exception as err:
38+
ErrorHandler(err)
39+
40+
# Reissue self-signed UAG Instance Certificate
41+
try:
42+
print("{} - {}".format("Reissue UA Gateway self-signed Instance Certificate", ua_gateway.certificates.reissue_self_signed_instance_certificate(server)))
43+
except Exception as err:
44+
ErrorHandler(err)
45+
46+
# Get the UAG Server Interface properties. These properties expose User Identify
47+
# Policy, Security Policy and other Communication properties that are applied across
48+
# all UAG server endpoints.
49+
50+
try:
51+
print("{} - {}".format("Get properties from UAG Server Interface configuration", ua_gateway.server.get_uag_server_interface_properties(server)))
52+
except Exception as err:
53+
ErrorHandler(err)
54+
55+
# Modify the properties of a server interface properties in UAG. In this example, we are enabling to allow anonymous user
56+
# identification for clients connecting to UAG server endpoints.
57+
UAGServerInterfaceChange = {
58+
"ua_gateway.UA_SERVER_INTERFACE_USER_IDENTITY_POLICY_ANONYMOUS": True,
59+
}
60+
61+
try:
62+
print("{} - {}".format("Update the properties of a UAG Server Interface", ua_gateway.server.modify_uag_server_interface_properties(server, UAGServerInterfaceChange, force= True) ))
63+
except Exception as err:
64+
ErrorHandler(err)
65+
66+
67+
# Create a new Server Endpoint for the UAG. This will create an endpoint for client applications to conenct to UAG and
68+
# access all downstream OPC UA servers that the UAG Client Connections will aggregate.
69+
70+
UAGServerEndpoint = {
71+
"common.ALLTYPES_NAME": "UAGServerEndpointTest",
72+
"common.ALLTYPES_DESCRIPTION": "",
73+
"ua_gateway.UA_SERVER_ENDPOINT_NETWORK_ADAPTER": 16777343,
74+
"ua_gateway.UA_SERVER_ENDPOINT_PORT": 58221,
75+
"ua_gateway.UA_SERVER_ENDPOINT_PROTOCOL": 0,
76+
"ua_gateway.UA_SERVER_ENDPOINT_ENABLED": True
77+
}
78+
79+
try:
80+
print("{} {} - {}".format("Create new UAG Server Endpoint for UA Gateway", UAGServerEndpoint["common.ALLTYPES_DESCRIPTION"], ua_gateway.server.add_ua_server_endpoint(server, UAGServerEndpoint)))
81+
except Exception as err:
82+
ErrorHandler(err)
83+
84+
# List the certificates in the UAG Server Endpoint trust store.
85+
server_endpoints_cert_list = None
86+
try:
87+
server_endpoints_cert_list = ua_gateway.server.get_all_certificates(server)
88+
print("{} - {}".format("List of certificates in the UAG Client connection trust store", server_endpoints_cert_list ))
89+
except Exception as err:
90+
ErrorHandler(err)
91+
92+
# Assuming that the first in the list is a new/untrusted instance certificate from a UA client, trust this certificate to allow
93+
# the client connection to be established to the UAG server endpoint.
94+
try:
95+
print("{} - {}".format("Trust a certificate in UAG Server Endpoint trust store", ua_gateway.server.trust_certificate(server, server_endpoints_cert_list[0]["common.ALLTYPES_NAME"]) ))
96+
except Exception as err:
97+
ErrorHandler(err)
98+
99+
# Modify the properties of a server endpoint in UAG. In this example, we are disabling the endpoint.
100+
UAGServerEndpointChange = {
101+
"ua_gateway.UA_SERVER_ENDPOINT_ENABLED": False
102+
}
103+
104+
try:
105+
print("{} - {}".format("Update the properties of a UAG Server Endpoint", ua_gateway.server.modify_ua_server_endpoint(server, UAGServerEndpointChange, ua_server_endpoint= UAGServerEndpoint["common.ALLTYPES_NAME"], force= True) ))
106+
except Exception as err:
107+
ErrorHandler(err)
108+
109+
# Delete a server endpoint in the UAG
110+
try:
111+
print("{} - {}".format("Delete a UAG Server Endpoint", ua_gateway.server.del_ua_server_endpoint(server, ua_server_endpoint= UAGServerEndpoint["common.ALLTYPES_NAME"]) ))
112+
except Exception as err:
113+
ErrorHandler(err)
114+
115+
116+
117+
# Create a new Client connection for the UAG. This will create a connection from UAG to the target UA server.
118+
UAGClientConnection = {
119+
"common.ALLTYPES_NAME": "LocalKepwareConnectionSecure",
120+
"common.ALLTYPES_DESCRIPTION": "",
121+
"ua_gateway.UA_CLIENT_CONNECTION_STATUS": "Disconnected",
122+
"ua_gateway.UA_CLIENT_CONNECTION_URL_NAME": "opc.tcp://localhost:49320",
123+
"ua_gateway.UA_CLIENT_CONNECTION_IDENTITY_POLICY": 0,
124+
"ua_gateway.UA_CLIENT_CONNECTION_USER_NAME": "",
125+
"ua_gateway.UA_CLIENT_CONNECTION_USER_PASSWORD": "",
126+
"ua_gateway.UA_CLIENT_CONNECTION_SECURITY_POLICY": 3,
127+
"ua_gateway.UA_CLIENT_CONNECTION_MESSAGE_MODE": 2,
128+
"ua_gateway.UA_CLIENT_CONNECTION_ENABLED": True,
129+
"ua_gateway.UA_CLIENT_CONNECTION_PUBLISHING_INTERVAL": 500,
130+
"ua_gateway.UA_CLIENT_CONNECTION_SUBSCRIPTION_LIFETIME": 500000,
131+
"ua_gateway.UA_CLIENT_CONNECTION_SESSION_TIMEOUT": 60000,
132+
"ua_gateway.UA_CLIENT_CONNECTION_PASSTHROUGH_ENABLED": True,
133+
"ua_gateway.UA_CLIENT_CONNECTION_MONITORED_ITEM_QUEUE_SIZE_OVERRIDE": 1,
134+
"ua_gateway.UA_CLIENT_CONNECTION_DISCARD_OLDEST_OVERRIDE": 0
135+
}
136+
137+
try:
138+
print("{} {} - {}".format("Create new UAG Client connection for UA Gateway to server", UAGClientConnection["common.ALLTYPES_DESCRIPTION"], ua_gateway.client.add_ua_client_connection(server, UAGClientConnection)))
139+
except Exception as err:
140+
ErrorHandler(err)
141+
142+
# List the certificates in the UAG Client Connections trust store.
143+
client_connection_cert_list = None
144+
try:
145+
client_connection_cert_list = ua_gateway.client.get_all_certificates(server)
146+
print("{} - {}".format("List of certificates in the UAG Client connection trust store", client_connection_cert_list ))
147+
except Exception as err:
148+
ErrorHandler(err)
149+
150+
# Assuming that the first in the list is a new/untrusted instance certificate from a UA server, trust this certificate to allow
151+
# the client connection to be established.
152+
try:
153+
print("{} - {}".format("Trust a certificate in UAG Client connection trust store", ua_gateway.client.trust_certificate(server, client_connection_cert_list[0]["common.ALLTYPES_NAME"]) ))
154+
except Exception as err:
155+
ErrorHandler(err)
156+
157+
# Modify the properties of a client connection in UAG. In this example, we are disabling the client connection.
158+
UAGClientConnectionChange = {
159+
"ua_gateway.UA_CLIENT_CONNECTION_ENABLED": False
160+
}
161+
162+
try:
163+
print("{} - {}".format("Update the properties of a UAG Client connection", ua_gateway.client.modify_ua_client_connection(server, UAGClientConnectionChange, ua_client_connection= UAGClientConnection["common.ALLTYPES_NAME"], force= True) ))
164+
except Exception as err:
165+
ErrorHandler(err)
166+
167+
# Delete a client connection in the UAG
168+
try:
169+
print("{} - {}".format("Delete a UAG Client connection", ua_gateway.client.del_ua_client_connection(server, ua_client_connection= UAGClientConnection["common.ALLTYPES_NAME"]) ))
170+
except Exception as err:
171+
ErrorHandler(err)

0 commit comments

Comments
 (0)