blob: 05b09ba17e7136f1ffcd39a2e06f9402a9226c64 [file] [log] [blame]
rajendrajaiswal6a61ad82019-12-16 14:24:02 +00001import gzip
2import json
3import os
4import shutil
5import time
6import xml.etree.ElementTree as ET
7from random import randint
8import requests
9import pnfconfig
10
11
12class PNF:
13 """ Handle update on xml and send file ready event to ves collector """
14 def __init__(self):
15 pass
16
17 @staticmethod
18 def create_job_id(jobid, change_list):
19 """
20 create new measinfo tag and add new sub element in existing xml.
21 :param jobid: create unique job id within xml sub element.
22 :param change_list: list to create sub elements itmes.
23 """
24 try:
25 measurement_type = []
26 meas_object_dn = []
27 for items in range(len(change_list)):
28 if "/measurementType =" in change_list[items]:
29 measurement_type.append(((change_list[items].rsplit('/', 1))[1].rsplit('=', 1))[1].strip())
30 if "/DN =" in change_list[items]:
31 meas_object_dn.append(((change_list[items].rsplit('/', 1))[1].rsplit('=', 1))[1].strip())
32 script_dir = os.path.dirname(__file__)
33 pm_rel_file_path = "sftp/"
34 pm_location = os.path.join(script_dir, pm_rel_file_path)
35 ET.register_namespace('', "http://www.3gpp.org/ftp/specs/archive/32_series/32.435#measCollec")
36 tree = ET.parse(pm_location + "pm.xml")
37 root = tree.getroot()
38 attrib = {}
39 measinfo = ET.SubElement(root[1], 'measInfo', attrib)
40 attrib = {'jobId': jobid}
41 ET.SubElement(measinfo, 'job', attrib)
42 ET.SubElement(measinfo, 'granPeriod', {'duration': 'PT900S', 'endTime': '2000-03-01T14:14:30+02:00'})
43 ET.SubElement(measinfo, 'repPeriod', {'duration': 'PT1800S'})
44 for items in range(len(measurement_type)):
45 meastype = ET.SubElement(measinfo, 'measType', {'p': (items + 1).__str__()})
46 meastype.text = measurement_type[items]
47 for items in range(len(meas_object_dn)):
48 measvalue = ET.SubElement(measinfo, 'measValue', {'measObjLdn': meas_object_dn[items]})
49 for item in range(len(measurement_type)):
50 value = ET.SubElement(measvalue, 'r', {'p': (item + 1).__str__()})
51 value.text = randint(100, 900).__str__()
52 tree.write(pm_location + "pm.xml", encoding="utf-8", xml_declaration=True)
53 except Exception as error:
54 print(error)
55
56 @staticmethod
57 def delete_job_id(jobid):
58 """
59 delete measinfo tag from existing xml pm file based on jobid.
60 :param jobid: element within measinfo tag.
61 """
62 try:
63 script_dir = os.path.dirname(__file__)
64 pm_rel_file_path = "sftp/"
65 pm_location = os.path.join(script_dir, pm_rel_file_path)
66 ET.register_namespace('', "http://www.3gpp.org/ftp/specs/archive/32_series/32.435#measCollec")
67 tree = ET.parse(pm_location + "pm.xml")
68 root = tree.getroot()
69 for measinfo in root[1].findall(
70 '{http://www.3gpp.org/ftp/specs/archive/32_series/32.435#measCollec}measInfo'):
71 xml_id = measinfo.find('{http://www.3gpp.org/ftp/specs/archive/32_series/32.435#measCollec}job').attrib
72 if xml_id["jobId"] == jobid:
73 root[1].remove(measinfo)
74 tree.write(pm_location + "pm.xml", encoding="utf-8", xml_declaration=True)
75 except Exception as error:
76 print(error)
77
78 @staticmethod
79 def pm_job():
80 """
81 create timestemp based gunzip xml file and send file ready event to ves collector.
82 """
83 try:
84 script_dir = os.path.dirname(__file__)
85 timestemp = time.time()
86 pm_rel_file_path = "sftp/"
87 pm_location = os.path.join(script_dir, pm_rel_file_path)
88 shutil.copy(pm_location + "pm.xml", pm_location + "A{}.xml".format(timestemp))
89 with open(pm_location + "A{}.xml".format(timestemp), 'rb') as f_in:
90 with gzip.open(pm_location + "A{}.xml.gz".format(timestemp), 'wb') as f_out:
91 shutil.copyfileobj(f_in, f_out)
92 os.remove(pm_location + "A{}.xml".format(timestemp))
93 rel_path = "FileReadyEvent.json"
94 file_ready_event_path = os.path.join(script_dir, rel_path)
95 with open(file_ready_event_path) as json_file:
96 data = json_file.read().replace("pmfilename", str(timestemp))
97 eventdata = json.loads(data)
98 url = "http://{}:{}/eventListener/v7".format(pnfconfig.VES_IP, pnfconfig.VES_PORT)
99 print("Sending File Ready Event to VES Collector " + url + " -- data @" + data)
100 headers = {'content-type': 'application/json'}
101 requests.post(url, json=eventdata, headers=headers)
102 except Exception as error:
103 print(error)