CI: Update RTD configuration file
[sim/o1-interface.git] / ntsimulator / deploy / smo-nts-ng-topology-server / callhomeConfig.py
1 #!/usr/bin/python3
2
3 ################################################################################
4 # Copyright 2022 highstreet technologies GmbH
5 #
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
9 #
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #
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
20
21 import http.client
22 import time
23 import base64
24 import os
25
26 TIMEOUT=1000
27 INTERVAL=30
28
29 # default ODL credentials
30 username = os.getenv("SDN_CONTROLLER_USERNAME")
31 password = os.getenv("SDN_CONTROLLER_PASSWORD")
32 cred_string = username + ":" + password
33
34 certreadyCmd="GET"
35 certreadyUrl="/rests/data/odl-netconf-callhome-server:netconf-callhome-server"
36 timePassed=0
37
38 headers = {'Authorization':'Basic %s' % base64.b64encode(cred_string.encode()).decode(),
39            'Accept':"application/json",
40            'Content-type':"application/yang-data+json"}
41
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!\"]}}}]}"
46
47
48 # checking if RESTCONF and NETCONF CallHome feature are functional in ODL
49 def makeHealthcheckCall(headers, timePassed):
50     connected = False
51     # WAIT 10 minutes maximum and test every 30 seconds if HealthCheck API is returning 200
52     while timePassed < TIMEOUT:
53         try:
54             conn = http.client.HTTPConnection("127.0.0.1",8181)
55             req = conn.request(certreadyCmd, certreadyUrl,headers=headers)
56             res = conn.getresponse()
57             res.read()
58             httpStatus = res.status
59             if httpStatus == 200:
60                 print("Healthcheck Passed in %d seconds." %timePassed)
61                 connected = True
62                 break
63             else:
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))
65         except:
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)
68
69     if timePassed > TIMEOUT:
70         print("TIME OUT: Healthcheck not passed in  %d seconds... Could cause problems for testing activities..." %TIMEOUT)
71
72     return connected
73
74
75 def timeIncrement(timePassed):
76     time.sleep(INTERVAL)
77     timePassed = timePassed + INTERVAL
78     return timePassed
79
80 # add current NTS in allowed devices list for NETCONF CallHome
81 def configureNetconfCallhome():
82     connected = makeHealthcheckCall(headers, timePassed)
83     if connected:
84         with open('/home/netconf/.ssh/melacon.server.key.pub', 'r') as file:
85             data = file.read().rstrip()
86             words = data.split()
87             publicKey = words[1]
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()
92             res.read()
93             httpStatus = res.status
94             if httpStatus >= 200 and httpStatus < 300:
95                 print("Successfully enabled CallHome for device with key=%s" % publicKey)
96             else:
97                 print("Could not allow device...")
98
99 print("Starting NETCONF Callhome configuration...")
100 configureNetconfCallhome()