3 ################################################################################
4 # Copyright 2022 highstreet technologies GmbH
6 # Licensed under the Apache License, Version 2.0 (the 'License');
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an 'AS IS' BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 ################################################################################
18 # Script for adding this running NTS Topology Server to the allowed-devices list of the
19 # local OpenDaylight instance, for enabling NETCONF CallHome
29 # default ODL credentials
30 username = os.getenv("SDN_CONTROLLER_USERNAME")
31 password = os.getenv("SDN_CONTROLLER_PASSWORD")
32 cred_string = username + ":" + password
35 certreadyUrl="/rests/data/odl-netconf-callhome-server:netconf-callhome-server"
38 headers = {'Authorization':'Basic %s' % base64.b64encode(cred_string.encode()).decode(),
39 'Accept':"application/json",
40 'Content-type':"application/yang-data+json"}
42 # ODL NETCONF CallHome allowed devices URL
43 callhomeConfigUrl = "/rests/data/odl-netconf-callhome-server:netconf-callhome-server/allowed-devices/device=o-ran-sc-topology-service"
44 # ODL NETCONF CallHome allowed devices payload; private key will be replaced with the one generated on the device at runtime
45 callhomeConfigPayload = "{\"odl-netconf-callhome-server:device\":[{\"odl-netconf-callhome-server:unique-id\":\"o-ran-sc-topology-service\", \"odl-netconf-callhome-server:ssh-client-params\": {\"odl-netconf-callhome-server:host-key\":\"@priv_key@\",\"odl-netconf-callhome-server:credentials\":{\"odl-netconf-callhome-server:username\":\"netconf\",\"odl-netconf-callhome-server:passwords\":[\"netconf!\"]}}}]}"
48 # checking if RESTCONF and NETCONF CallHome feature are functional in ODL
49 def makeHealthcheckCall(headers, timePassed):
51 # WAIT 10 minutes maximum and test every 30 seconds if HealthCheck API is returning 200
52 while timePassed < TIMEOUT:
54 conn = http.client.HTTPConnection("127.0.0.1",8181)
55 req = conn.request(certreadyCmd, certreadyUrl,headers=headers)
56 res = conn.getresponse()
58 httpStatus = res.status
60 print("Healthcheck Passed in %d seconds." %timePassed)
64 print("Sleep: %d seconds before testing if Healthcheck worked. Total wait time up now is: %d seconds. Timeout is: %d seconds. Problem code was: %d" %(INTERVAL, timePassed, TIMEOUT, httpStatus))
66 print("Cannot execute REST call. Sleep: %d seconds before testing if Healthcheck worked. Total wait time up now is: %d seconds. Timeout is: %d seconds." %(INTERVAL, timePassed, TIMEOUT))
67 timePassed = timeIncrement(timePassed)
69 if timePassed > TIMEOUT:
70 print("TIME OUT: Healthcheck not passed in %d seconds... Could cause problems for testing activities..." %TIMEOUT)
75 def timeIncrement(timePassed):
77 timePassed = timePassed + INTERVAL
80 # add current NTS in allowed devices list for NETCONF CallHome
81 def configureNetconfCallhome():
82 connected = makeHealthcheckCall(headers, timePassed)
84 with open('/home/netconf/.ssh/melacon.server.key.pub', 'r') as file:
85 data = file.read().rstrip()
88 payload = callhomeConfigPayload.replace("@priv_key@", publicKey)
89 conn = http.client.HTTPConnection("localhost",8181)
90 req = conn.request("PUT", callhomeConfigUrl,headers=headers, body=payload)
91 res = conn.getresponse()
93 httpStatus = res.status
94 if httpStatus >= 200 and httpStatus < 300:
95 print("Successfully enabled CallHome for device with key=%s" % publicKey)
97 print("Could not allow device...")
99 print("Starting NETCONF Callhome configuration...")
100 configureNetconfCallhome()