Create VES client example for domain 'notification'
[oam.git] / solution / dev / ves-test-collector / client-scripts-ves-v7 / globalVesEventEmitter.py
1 #!/usr/bin/env python
2 ################################################################################
3 # Copyright 2021 highstreet technologies GmbH
4 #
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
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16 #
17
18 ################################################################################
19 # A selection of common methods
20
21 import datetime
22 import json
23 import os
24 import socket
25 import yaml
26 from pathlib import Path
27
28 def getInitData(domain):
29   currentTime = datetime.datetime.utcnow()
30   dir = os.path.dirname(os.path.realpath(__file__))
31
32   result = {}
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'"
40
41   # Read config
42   with open('config.yml', 'r') as stream:
43     try:
44         result['config']= yaml.safe_load(stream)
45     except yaml.YAMLError as exc:
46         print(exc)
47
48   # Read template body
49   templateFileName = dir + '/json/templates/' + domain + '.json'
50   with open(templateFileName) as f:
51     result['body']= json.load(f)
52
53   
54   Path(result["outdir"]).mkdir(parents=True, exist_ok=True)
55   return result
56
57 def saveExample(data):
58   if 'directory' in data and 'domain' in data and 'body' in data:
59     name = data['domain']
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)
64   else:
65     print("Example could not been saved:\n" + json.dump(data, f, indent=2, sort_keys=True)) 
66