Some init version
[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
40   # Read config
41   with open('config.yml', 'r') as stream:
42     try:
43         result['config']= yaml.safe_load(stream)
44     except yaml.YAMLError as exc:
45         print(exc)
46
47   # Read template body
48   templateFileName = dir + '/json/templates/' + domain + '.json'
49   with open(templateFileName) as f:
50     result['body']= json.load(f)
51
52   
53   Path(result["outdir"]).mkdir(parents=True, exist_ok=True)
54   return result
55
56 def saveExample(data):
57   if 'directory' in data and 'domain' in data and 'body' in data:
58     outputFileName = data['directory'] + '/json/examples/' + data['domain'] + '.json'
59     with open(outputFileName, 'w') as f:
60       json.dump(data['body'], f, indent=2, sort_keys=True)
61   else:
62     print("Example could not been saved:\n" + json.dump(data, f, indent=2, sort_keys=True)) 
63