diff options
Diffstat (limited to 'test/mocks/pmsh-pnf-sim/docker-compose/pnf.py')
-rw-r--r-- | test/mocks/pmsh-pnf-sim/docker-compose/pnf.py | 70 |
1 files changed, 40 insertions, 30 deletions
diff --git a/test/mocks/pmsh-pnf-sim/docker-compose/pnf.py b/test/mocks/pmsh-pnf-sim/docker-compose/pnf.py index 05b09ba17..a187ff76b 100644 --- a/test/mocks/pmsh-pnf-sim/docker-compose/pnf.py +++ b/test/mocks/pmsh-pnf-sim/docker-compose/pnf.py @@ -1,12 +1,18 @@ import gzip import json +import logging import os import shutil import time -import xml.etree.ElementTree as ET +import xml.etree.ElementTree as ElementTree from random import randint + import requests -import pnfconfig +from requests.auth import HTTPBasicAuth + +from app_config import pnfconfig + +logger = logging.getLogger('dev') class PNF: @@ -32,26 +38,26 @@ class PNF: script_dir = os.path.dirname(__file__) pm_rel_file_path = "sftp/" pm_location = os.path.join(script_dir, pm_rel_file_path) - ET.register_namespace('', "http://www.3gpp.org/ftp/specs/archive/32_series/32.435#measCollec") - tree = ET.parse(pm_location + "pm.xml") + ElementTree.register_namespace('', "http://www.3gpp.org/ftp/specs/archive/32_series/32.435#measCollec") + tree = ElementTree.parse(pm_location + "pm.xml") root = tree.getroot() attrib = {} - measinfo = ET.SubElement(root[1], 'measInfo', attrib) + measinfo = ElementTree.SubElement(root[1], 'measInfo', attrib) attrib = {'jobId': jobid} - ET.SubElement(measinfo, 'job', attrib) - ET.SubElement(measinfo, 'granPeriod', {'duration': 'PT900S', 'endTime': '2000-03-01T14:14:30+02:00'}) - ET.SubElement(measinfo, 'repPeriod', {'duration': 'PT1800S'}) + ElementTree.SubElement(measinfo, 'job', attrib) + ElementTree.SubElement(measinfo, 'granPeriod', {'duration': 'PT900S', 'endTime': '2000-03-01T14:14:30+02:00'}) + ElementTree.SubElement(measinfo, 'repPeriod', {'duration': 'PT1800S'}) for items in range(len(measurement_type)): - meastype = ET.SubElement(measinfo, 'measType', {'p': (items + 1).__str__()}) + meastype = ElementTree.SubElement(measinfo, 'measType', {'p': (items + 1).__str__()}) meastype.text = measurement_type[items] for items in range(len(meas_object_dn)): - measvalue = ET.SubElement(measinfo, 'measValue', {'measObjLdn': meas_object_dn[items]}) + measvalue = ElementTree.SubElement(measinfo, 'measValue', {'measObjLdn': meas_object_dn[items]}) for item in range(len(measurement_type)): - value = ET.SubElement(measvalue, 'r', {'p': (item + 1).__str__()}) + value = ElementTree.SubElement(measvalue, 'r', {'p': (item + 1).__str__()}) value.text = randint(100, 900).__str__() tree.write(pm_location + "pm.xml", encoding="utf-8", xml_declaration=True) except Exception as error: - print(error) + logger.debug(error) @staticmethod def delete_job_id(jobid): @@ -63,17 +69,19 @@ class PNF: script_dir = os.path.dirname(__file__) pm_rel_file_path = "sftp/" pm_location = os.path.join(script_dir, pm_rel_file_path) - ET.register_namespace('', "http://www.3gpp.org/ftp/specs/archive/32_series/32.435#measCollec") - tree = ET.parse(pm_location + "pm.xml") + ElementTree.register_namespace( + '', "http://www.3gpp.org/ftp/specs/archive/32_series/32.435#measCollec") + tree = ElementTree.parse(pm_location + "pm.xml") root = tree.getroot() for measinfo in root[1].findall( '{http://www.3gpp.org/ftp/specs/archive/32_series/32.435#measCollec}measInfo'): - xml_id = measinfo.find('{http://www.3gpp.org/ftp/specs/archive/32_series/32.435#measCollec}job').attrib + xml_id = measinfo.find( + '{http://www.3gpp.org/ftp/specs/archive/32_series/32.435#measCollec}job').attrib if xml_id["jobId"] == jobid: root[1].remove(measinfo) tree.write(pm_location + "pm.xml", encoding="utf-8", xml_declaration=True) except Exception as error: - print(error) + logger.debug(error) @staticmethod def pm_job(): @@ -83,21 +91,23 @@ class PNF: try: script_dir = os.path.dirname(__file__) timestemp = time.time() - pm_rel_file_path = "sftp/" - pm_location = os.path.join(script_dir, pm_rel_file_path) - shutil.copy(pm_location + "pm.xml", pm_location + "A{}.xml".format(timestemp)) - with open(pm_location + "A{}.xml".format(timestemp), 'rb') as f_in: - with gzip.open(pm_location + "A{}.xml.gz".format(timestemp), 'wb') as f_out: + pm_location = os.path.join(script_dir, 'sftp/') + shutil.copy(pm_location + 'pm.xml', pm_location + f'A{timestemp}.xml') + with open(pm_location + f'A{timestemp}.xml', 'rb') as f_in: + with gzip.open(pm_location + f'A{timestemp}.xml.gz', 'wb') as f_out: shutil.copyfileobj(f_in, f_out) - os.remove(pm_location + "A{}.xml".format(timestemp)) - rel_path = "FileReadyEvent.json" - file_ready_event_path = os.path.join(script_dir, rel_path) - with open(file_ready_event_path) as json_file: + os.remove(pm_location + f'A{timestemp}.xml') + with open(os.path.join(script_dir, 'FileReadyEvent.json')) as json_file: data = json_file.read().replace("pmfilename", str(timestemp)) eventdata = json.loads(data) - url = "http://{}:{}/eventListener/v7".format(pnfconfig.VES_IP, pnfconfig.VES_PORT) - print("Sending File Ready Event to VES Collector " + url + " -- data @" + data) - headers = {'content-type': 'application/json'} - requests.post(url, json=eventdata, headers=headers) + session = requests.Session() + url = f'https://{pnfconfig.VES_IP}:{pnfconfig.VES_PORT}/eventListener/v7' + logger.debug(f'Sending File Ready Event to VES Collector {url} -- data @{data}') + headers = {'content-type': 'application/json', + 'x-transactionid': '123456'} + response = session.post(url, json=eventdata, headers=headers, + auth=HTTPBasicAuth(pnfconfig.VES_USER, pnfconfig.VES_PASS), + verify=False) + response.raise_for_status() except Exception as error: - print(error) + logger.debug(f'Exception caught {error}', exc_info=True) |