#!/usr/bin/env python ################################################################################ # Copyright 2021 highstreet technologies GmbH # # Licensed under the Apache License, Version 2.0 (the 'License'); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an 'AS IS' BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ################################################################################ # A selection of common methods import datetime import json import os import socket import yaml from pathlib import Path def getInitData(domain): currentTime = datetime.datetime.utcnow() dir = os.path.dirname(os.path.realpath(__file__)) result = {} result['domain']= domain result['directory']= dir result['outdir']= dir + '/json/examples' result['fqdn']= socket.getfqdn() result['timestamp']= int(currentTime.timestamp()*1000000) result['eventTime']= currentTime.isoformat() + 'Z' result['interface']= "urn:ietf:params:xml:ns:yang:ietf-interfaces:interfaces/interface/name='O-RAN-SC-OAM'" # Read config with open('config.yml', 'r') as stream: try: result['config']= yaml.safe_load(stream) except yaml.YAMLError as exc: print(exc) # Read template body templateFileName = dir + '/json/templates/' + domain + '.json' with open(templateFileName) as f: result['body']= json.load(f) Path(result["outdir"]).mkdir(parents=True, exist_ok=True) return result def saveExample(data): if 'directory' in data and 'domain' in data and 'body' in data: name = data['domain'] if 'pnfId' in data: name = '-'.join( (data['pnfId'], data['domain']) ) outputFileName = data['directory'] + '/json/examples/' + name + '.json' with open(outputFileName, 'w') as f: json.dump(data['body'], f, indent=2, sort_keys=True) else: print("Example could not been saved:\n" + json.dump(data, f, indent=2, sort_keys=True))