X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=solution%2Fdev%2Fves-test-collector%2Fclient-scripts-ves-v7%2FsendVesNotification.py;fp=solution%2Fdev%2Fves-test-collector%2Fclient-scripts-ves-v7%2FsendVesNotification.py;h=6c4d52187038278e001d30afcb564f18ff370151;hb=6bc11bbce334d492295ad966650debef910efb39;hp=99b77841746a066e408da21fa3f968dfc3c95618;hpb=f9e11bbaa0d9db27bd418a1e7b2634a54cdf054f;p=oam.git diff --git a/solution/dev/ves-test-collector/client-scripts-ves-v7/sendVesNotification.py b/solution/dev/ves-test-collector/client-scripts-ves-v7/sendVesNotification.py index 99b7784..6c4d521 100644 --- a/solution/dev/ves-test-collector/client-scripts-ves-v7/sendVesNotification.py +++ b/solution/dev/ves-test-collector/client-scripts-ves-v7/sendVesNotification.py @@ -2,14 +2,14 @@ ################################################################################ # Copyright 2021 highstreet technologies GmbH # -# Licensed under the Apache License, Version 2.0 (the "License"); +# 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, +# 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. @@ -18,67 +18,72 @@ ################################################################################ # Send a VES event for domain 'notification' -# importing the datetime, json, requests, os socket and yaml library -import datetime +# importing the sys, json, requests library +import sys +import getopt import json import requests -import os -import socket -import yaml -from pathlib import Path +from globalVesEventEmitter import getInitData, saveExample -# Globals -dir = os.path.dirname(os.path.realpath(__file__)) -domain = "notification" -fqdn = socket.getfqdn() +# Construct VES body and send +def performJob(domain, pnfId): + initData = getInitData(domain) + initData['pnfId'] = pnfId -# time formats -currentTime = datetime.datetime.utcnow() -timestamp = int(currentTime.timestamp()*1000000) -eventTime = currentTime.isoformat() + "Z" + print('################################################################################') + print('# send VES ' + domain) -# Create output path -Path(dir + "/json/examples").mkdir(parents=True, exist_ok=True) + initData['body']['event']['commonEventHeader']['domain'] = initData['domain'] + initData['body']['event']['commonEventHeader']['eventId'] = initData['fqdn'] + \ + '_' + initData['eventTime'] + initData['body']['event']['commonEventHeader']['eventName'] = initData['domain'] + \ + '_' + initData['config']['settings']['eventType'] + initData['body']['event']['commonEventHeader']['eventType'] = initData['config']['settings']['eventType'] + initData['body']['event']['commonEventHeader']['sequence'] = initData['config']['settings']['sequence'] + initData['body']['event']['commonEventHeader']['reportingEntityName'] = initData['fqdn'] + initData['body']['event']['commonEventHeader']['sourceName'] = initData['pnfId'] + initData['body']['event']['commonEventHeader']['startEpochMicrosec'] = initData['timestamp'] + initData['body']['event']['commonEventHeader']['lastEpochMicrosec'] = initData['timestamp'] + initData['body']['event']['commonEventHeader']['nfNamingCode'] = initData['pnfId'] + initData['body']['event']['commonEventHeader']['nfVendorName'] = 'O-RAN-SC OAM' -# Read settings -with open("config.yml", 'r') as stream: - try: - cfg = yaml.safe_load(stream) - except yaml.YAMLError as exc: - print(exc) + initData['body']['event']['notificationFields']['additionalFields']['eventTime'] = initData['eventTime'] + initData['body']['event']['notificationFields']['changeContact'] = initData['fqdn'] + initData['body']['event']['notificationFields']['changeIdentifier'] = initData['pnfId'] + initData['body']['event']['notificationFields']['stateInterface'] = initData['interface'] + + # Save example body + saveExample(initData) -print("################################################################################") -print("# send SDN-Controller " + domain) + # Send VES Event + url = initData['config']['vesEndpoint']['url'] + username = initData['config']['vesEndpoint']['username'] + password = initData['config']['vesEndpoint']['password'] + verify = initData['config']['vesEndpoint']['verify'] + response = requests.post(url, json=initData['body'], auth=( + username, password), verify=verify) + print(response) -# Read template body -templateFileName = dir + "/json/templates/" + domain + ".json" -with open(templateFileName) as f: - data = json.load(f) +# Analysing command line parameters -data["event"]["commonEventHeader"]["domain"] = domain -data["event"]["commonEventHeader"]["eventId"] = fqdn + "_" + eventTime -data["event"]["commonEventHeader"]["eventName"] = domain + \ - "_" + cfg["settings"]["eventType"] -data["event"]["commonEventHeader"]["eventType"] = cfg["settings"]["eventType"] -data["event"]["commonEventHeader"]["sequence"] = cfg["settings"]["sequence"] -data["event"]["commonEventHeader"]["reportingEntityName"] = fqdn -data["event"]["commonEventHeader"]["sourceName"] = fqdn -data["event"]["commonEventHeader"]["startEpochMicrosec"] = timestamp -data["event"]["commonEventHeader"]["lastEpochMicrosec"] = timestamp -data["event"]["commonEventHeader"]["nfNamingCode"] = "SDN-Controller" -data["event"]["commonEventHeader"]["nfVendorName"] = "O-RAN-SC OAM" -data["event"]["heartbeatFields"]["additionalFields"]["eventTime"] = eventTime +def main(argv): + domain = 'notification' + usage = 'sendVesNotification.py --pnfId ' + pnfId = '' + try: + opts, args = getopt.getopt(argv, "hi:o:", ["pnfId="]) + except getopt.GetoptError: + print(usage) + sys.exit(2) + for opt, arg in opts: + if opt in ('-h', '--help'): + print(usage) + sys.exit() + elif opt in ("-p", "--pnfId"): + pnfId = arg + performJob(domain, pnfId) -# save example body -outputFileName = dir + "/json/examples/" + domain + ".json" -with open(outputFileName, 'w') as f: - json.dump(data, f, indent=2, sort_keys=True) -# Send VES Event -url = cfg["vesEndpoint"]["url"] -username = cfg["vesEndpoint"]["username"] -password = cfg["vesEndpoint"]["password"] -verify = cfg["vesEndpoint"]["verify"] -response = requests.post(url, json=data, auth=(username, password), verify=verify) -print(response) +if __name__ == "__main__": + main(sys.argv[1:])