2 ################################################################################
3 # Copyright 2021 highstreet technologies GmbH
5 # Licensed under the Apache License, Version 2.0 (the 'License');
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # https://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an 'AS IS' BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 ################################################################################
19 # A selection of common methods
28 from pathlib import Path
29 from _datetime import timezone
31 def sendVesEvent(data):
32 url = data['config']['vesEndpoint']['url']
33 username = data['config']['vesEndpoint']['username']
34 password = data['config']['vesEndpoint']['password']
35 headers = {'content-type': 'application/json'}
36 verify = data['config']['vesEndpoint']['verify']
38 response = requests.post(url, json=data['body'], auth=(
39 username, password), headers=headers, verify=verify)
40 except requests.exceptions.Timeout:
41 sys.exit('HTTP request failed, please check you internet connection.')
42 except requests.exceptions.TooManyRedirects:
43 sys.exit('HTTP request failed, please check your proxy settings.')
44 except requests.exceptions.RequestException as e:
45 # catastrophic error. bail.
48 if response.status_code >= 200 and response.status_code < 300:
51 sys.exit('Reading VES "stndDefined" message template failed.')
55 response = requests.get(url)
56 except requests.exceptions.Timeout:
57 sys.exit('HTTP request failed, please check you internet connection.')
58 except requests.exceptions.TooManyRedirects:
59 sys.exit('HTTP request failed, please check your proxy settings.')
60 except requests.exceptions.RequestException as e:
61 # catastrophic error. bail.
64 if response.status_code >= 200 and response.status_code < 300:
65 return response.json()
67 sys.exit('Reading VES "stndDefined" message template failed.')
69 def getInitData(domain, stndBody=''):
70 currentTime = datetime.datetime.now(tz=timezone.utc)
71 dir = os.path.dirname(os.path.realpath(__file__))
74 result['domain']= domain
75 result['directory']= dir
76 result['outdir']= dir + '/json/examples'
77 result['fqdn']= socket.getfqdn()
78 result['timestamp']= int(currentTime.timestamp()*1000000)
79 result['eventTime']= currentTime.isoformat() + 'Z'
80 result['interface']= "urn:ietf:params:xml:ns:yang:ietf-interfaces:interfaces/interface/name='O-RAN-SC-OAM'"
83 with open('config.yml', 'r') as stream:
85 result['config']= yaml.safe_load(stream)
86 except yaml.YAMLError as exc:
90 if domain == 'stndDefined':
91 url = 'https://raw.githubusercontent.com/onap/testsuite/master/robot/assets/dcae/ves_stdnDefined_' + stndBody + '.json'
92 result['body']= sendHttpGet(url)
94 templateFileName = dir + '/json/templates/' + domain + '.json'
95 with open(templateFileName) as f:
96 result['body']= json.load(f)
98 Path(result["outdir"]).mkdir(parents=True, exist_ok=True)
101 def saveExample(data):
102 if 'directory' in data and 'domain' in data and 'body' in data:
103 name = data['domain']
104 if 'pnfId' in data: name = '-'.join( (data['pnfId'], data['domain']) )
105 outputFileName = data['directory'] + '/json/examples/' + name + '.json'
106 with open(outputFileName, 'w') as f:
107 json.dump(data['body'], f, indent=2, sort_keys=True)
109 print("Example could not been saved:\n" + json.dump(data, f, indent=2, sort_keys=True))