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 # http://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
26 from pathlib import Path
28 def getInitData(domain):
29 currentTime = datetime.datetime.utcnow()
30 dir = os.path.dirname(os.path.realpath(__file__))
33 result['domain']= domain
34 result['directory']= dir
35 result['outdir']= dir + '/json/examples'
36 result['fqdn']= socket.getfqdn()
37 result['timestamp']= int(currentTime.timestamp()*1000000)
38 result['eventTime']= currentTime.isoformat() + 'Z'
39 result['interface']= "urn:ietf:params:xml:ns:yang:ietf-interfaces:interfaces/interface/name='O-RAN-SC-OAM'"
42 with open('config.yml', 'r') as stream:
44 result['config']= yaml.safe_load(stream)
45 except yaml.YAMLError as exc:
49 templateFileName = dir + '/json/templates/' + domain + '.json'
50 with open(templateFileName) as f:
51 result['body']= json.load(f)
54 Path(result["outdir"]).mkdir(parents=True, exist_ok=True)
57 def saveExample(data):
58 if 'directory' in data and 'domain' in data and 'body' in data:
60 if 'pnfId' in data: name = '-'.join( (data['pnfId'], data['domain']) )
61 outputFileName = data['directory'] + '/json/examples/' + name + '.json'
62 with open(outputFileName, 'w') as f:
63 json.dump(data['body'], f, indent=2, sort_keys=True)
65 print("Example could not been saved:\n" + json.dump(data, f, indent=2, sort_keys=True))