1 # ============LICENSE_START=======================================================
2 # Copyright (C) 2019 Nordix Foundation.
3 # ================================================================================
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # SPDX-License-Identifier: Apache-2.0
17 # ============LICENSE_END=========================================================
33 password = "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U"
39 postKeystore= "/restconf/operations/netconf-keystore:add-keystore-entry"
40 postPrivateKey= "/restconf/operations/netconf-keystore:add-private-key"
41 postTrustedCertificate= "/restconf/operations/netconf-keystore:add-trusted-certificate"
44 headers = {'Authorization':'Basic %s' % base64.b64encode(username + ":" + password),
45 'X-FromAppId': 'csit-sdnc',
46 'X-TransactionId': 'csit-sdnc',
47 'Accept':"application/json",
48 'Content-type':"application/json"}
50 def readFile(folder, file):
51 key = open(Path + "/" + folder + "/" + file, "r")
54 fileRead = "\n".join(fileRead.splitlines()[1:-1])
57 def readTrustedCertificate(folder, file):
62 key = open(Path + "/" + folder + "/" + file, "r")
63 lines = key.readlines()
65 if not "BEGIN CERTIFICATE CA.pem" in line and not "END CERTIFICATE CA.pem" in line and startCa:
67 elif "BEGIN CERTIFICATE CA.pem" in line:
69 elif "END CERTIFICATE CA.pem" in line:
72 if not "BEGIN CERTIFICATE Server.crt" in line and not "END CERTIFICATE Server.crt" in line and startCrt:
74 elif "BEGIN CERTIFICATE Server.crt" in line:
76 elif "END CERTIFICATE Server.crt" in line:
78 return caPem, serverCrt
80 def makeKeystoreKey(clientKey, count):
81 odl_private_key="ODL_private_key_%d" %count
83 json_keystore_key='{{\"input\": {{ \"key-credential\": {{\"key-id\": \"{odl_private_key}\", \"private-key\" : ' \
84 '\"{clientKey}\",\"passphrase\" : \"\"}}}}}}'.format(
85 odl_private_key=odl_private_key,
88 return json_keystore_key
92 def makePrivateKey(clientKey, clientCrt, caPem, count):
93 odl_private_key="ODL_private_key_%d" %count
95 json_private_key='{{\"input\": {{ \"private-key\":{{\"name\": \"{odl_private_key}\", \"data\" : ' \
96 '\"{clientKey}\",\"certificate-chain\":[\"{clientCrt}\",\"{caPem}\"]}}}}}}'.format(
97 odl_private_key=odl_private_key,
102 return json_private_key
104 def makeTrustedCertificate(serverCrt, caPem, count):
105 trusted_cert_name = "xNF_Server_certificate_%d" %count
106 trusted_name = "xNF_CA_certificate_%d" %count
108 json_trusted_cert='{{\"input\": {{ \"trusted-certificate\": [{{\"name\":\"{trusted_cert_name}\",\"certificate\" : ' \
109 '\"{serverCrt}\"}},{{\"name\": \"{trusted_name}\",\"certificate\":\"{caPem}\"}}]}}}}'.format(
110 trusted_cert_name=trusted_cert_name,
112 trusted_name=trusted_name,
115 return json_trusted_cert
118 def makeRestconfPost(conn, json_file, apiCall):
119 req = conn.request("POST", apiCall, json_file, headers=headers)
120 res = conn.getresponse()
122 if res.status != 200:
123 print "Error here, response back wasnt 200: Response was : %d , %s" % (res.status, res.reason)
125 print res.status, res.reason
127 def extractZipFiles(zipFileList, count):
128 for zipFolder in zipFileList:
129 with zipfile.ZipFile(Path + "/" + zipFolder.strip(),"r") as zip_ref:
130 zip_ref.extractall(Path)
131 folder = zipFolder.rsplit(".")[0]
132 processFiles(folder, count)
134 def processFiles(folder, count):
135 conn = httplib.HTTPConnection("localhost",8181)
136 for file in os.listdir(Path + "/" + folder):
137 if os.path.isfile(Path + "/" + folder + "/" + file.strip()):
139 clientKey = readFile(folder, file.strip())
140 elif "trustedCertificate" in file:
141 caPem, serverCrt = readTrustedCertificate(folder, file.strip())
143 clientCrt = readFile(folder, file.strip())
145 print "Could not find file %s" % file.strip()
146 shutil.rmtree(Path + "/" + folder)
147 json_keystore_key = makeKeystoreKey(clientKey, count)
148 json_private_key = makePrivateKey(clientKey, clientCrt, caPem, count)
149 json_trusted_cert = makeTrustedCertificate(serverCrt, caPem, count)
151 makeRestconfPost(conn, json_keystore_key, postKeystore)
152 makeRestconfPost(conn, json_private_key, postPrivateKey)
153 makeRestconfPost(conn, json_trusted_cert, postTrustedCertificate)
155 def makeHealthcheckCall(headers):
156 conn = httplib.HTTPConnection("localhost",8181)
157 req = conn.request("POST", "/restconf/operations/SLI-API:healthcheck",headers=headers)
158 res = conn.getresponse()
160 if res.status == 200:
161 print ("Healthcheck Passed in %d seconds." %TIME)
163 print ("Sleep: %d seconds before testing if Healtcheck worked. Total wait time up now is: %d seconds. Timeout is: %d seconds" %(INTERVAL, TIME, TIME_OUT))
167 def timeIncrement(TIME):
169 TIME = TIME + INTERVAL
172 def healthcheck(TIME):
173 # WAIT 10 minutes maximum and test every 30 seconds if HealthCheck API is returning 200
174 while TIME < TIME_OUT:
176 status = makeHealthcheckCall(headers)
181 print ("Sleep: %d seconds before testing if Healthcheck worked. Total wait time up now is: %d seconds. Timeout is: %d seconds" %(INTERVAL, TIME, TIME_OUT))
183 TIME = timeIncrement(TIME)
186 print ("TIME OUT: Healthcheck not passed in %d seconds... Could cause problems for testing activities..." %TIME_OUT)
190 if os.path.isfile(Path + "/certs.properties"):
191 with open(Path + "/certs.properties", "r") as f:
193 if not "*****" in line:
194 zipFileList.append(line)
196 extractZipFiles(zipFileList, count)
200 print "Error: File not found in path entered"
202 print "This was a problem here, Healthcheck never passed, please check is your instance up and running."