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
30 def sendVesEvent(data):
31 url = data['config']['vesEndpoint']['url']
32 username = data['config']['vesEndpoint']['username']
33 password = data['config']['vesEndpoint']['password']
34 headers = {'content-type': 'application/json'}
35 verify = data['config']['vesEndpoint']['verify']
37 response = requests.post(url, json=data['body'], auth=(
38 username, password), headers=headers, verify=verify)
39 except requests.exceptions.Timeout:
40 sys.exit('HTTP request failed, please check you internet connection.')
41 except requests.exceptions.TooManyRedirects:
42 sys.exit('HTTP request failed, please check your proxy settings.')
43 except requests.exceptions.RequestException as e:
44 # catastrophic error. bail.
47 if response.status_code >= 200 and response.status_code < 300:
50 sys.exit('Reading VES "stndDefined" message template failed.')
54 response = requests.get(url)
55 except requests.exceptions.Timeout:
56 sys.exit('HTTP request failed, please check you internet connection.')
57 except requests.exceptions.TooManyRedirects:
58 sys.exit('HTTP request failed, please check your proxy settings.')
59 except requests.exceptions.RequestException as e:
60 # catastrophic error. bail.
63 if response.status_code >= 200 and response.status_code < 300:
64 return response.json()
66 sys.exit('Reading VES "stndDefined" message template failed.')
68 def getInitData(domain, stndBody=''):
69 currentTime = datetime.datetime.utcnow()
70 dir = os.path.dirname(os.path.realpath(__file__))
73 result['domain']= domain
74 result['directory']= dir
75 result['outdir']= dir + '/json/examples'
76 result['fqdn']= socket.getfqdn()
77 result['timestamp']= int(currentTime.timestamp()*1000000)
78 result['eventTime']= currentTime.isoformat() + 'Z'
79 result['interface']= "urn:ietf:params:xml:ns:yang:ietf-interfaces:interfaces/interface/name='O-RAN-SC-OAM'"
82 with open('config.yml', 'r') as stream:
84 result['config']= yaml.safe_load(stream)
85 except yaml.YAMLError as exc:
89 if domain == 'stndDefined':
90 url = 'https://raw.githubusercontent.com/onap/testsuite/master/robot/assets/dcae/ves_stdnDefined_' + stndBody + '.json'
91 result['body']= sendHttpGet(url)
93 templateFileName = dir + '/json/templates/' + domain + '.json'
94 with open(templateFileName) as f:
95 result['body']= json.load(f)
97 Path(result["outdir"]).mkdir(parents=True, exist_ok=True)
100 def saveExample(data):
101 if 'directory' in data and 'domain' in data and 'body' in data:
102 name = data['domain']
103 if 'pnfId' in data: name = '-'.join( (data['pnfId'], data['domain']) )
104 outputFileName = data['directory'] + '/json/examples/' + name + '.json'
105 with open(outputFileName, 'w') as f:
106 json.dump(data['body'], f, indent=2, sort_keys=True)
108 print("Example could not been saved:\n" + json.dump(data, f, indent=2, sort_keys=True))