blob: 650f8fef84b78ee698979d344d1fb28fa5c6adf1 [file] [log] [blame]
pswangdaef3492017-09-21 22:04:20 -05001'''
2Created on Aug 18, 2017
3
4@author: sw6830
5'''
6from robot.api import logger
7from Queue import Queue
8import uuid, time, datetime,json, threading
9import DcaeVariables
10import DMaaP
11
12class DcaeLibrary(object):
13
14 def __init__(self):
15 pass
16
17 def setup_dmaap_server(self, portNum=3904):
18 if DcaeVariables.HttpServerThread != None:
19 DMaaP.cleanUpEvent()
20 logger.console("Clean up event from event queue before test")
21 logger.info("DMaaP Server already started")
22 return "true"
23
24 DcaeVariables.IsRobotRun = True
25 DMaaP.test(port=portNum)
26 try:
27 DcaeVariables.VESEventQ = Queue()
28 DcaeVariables.HttpServerThread = threading.Thread(name='DMAAP_HTTPServer', target=DMaaP.DMaaPHttpd.serve_forever)
29 DcaeVariables.HttpServerThread.start()
30 logger.console("DMaaP Mockup Sever started")
31 time.sleep(2)
32 return "true"
33 except Exception as e:
34 print (str(e))
35 return "false"
36
37 def shutdown_dmaap(self):
38 if DcaeVariables.HTTPD != None:
39 DcaeVariables.HTTPD.shutdown()
40 logger.console("DMaaP Server shut down")
41 time.sleep(3)
42 return "true"
43 else:
44 return "false"
45
46 def cleanup_ves_events(self):
47 if DcaeVariables.HttpServerThread != None:
48 DMaaP.cleanUpEvent()
49 logger.console("DMaaP event queue is cleaned up")
50 return "true"
51 logger.console("DMaaP server not started yet")
52 return "false"
53
54 def dmaap_message_receive(self, evtobj, action='contain'):
55
56 evtStr = DMaaP.dequeEvent()
57 while evtStr != None:
58 logger.console("DMaaP receive VES Event:\n" + evtStr)
59 if action == 'contain':
60 if evtobj in evtStr:
61 logger.info("DMaaP Receive Expected Publish Event:\n" + evtStr)
62 return 'true'
63 if action == 'sizematch':
64 if len(evtobj) == len(evtStr):
65 return 'true'
66 if action == 'dictmatch':
67 evtDict = json.loads(evtStr)
68 if cmp(evtobj, evtDict) == 0:
69 return 'true'
70 evtStr = DMaaP.dequeEvent()
71 return 'false'
72
73 def create_header_from_string(self, dictStr):
74 logger.info("Enter create_header_from_string: dictStr")
75 return dict(u.split("=") for u in dictStr.split(","))
76
77 def is_json_empty(self, resp):
78 logger.info("Enter is_json_empty: resp.text: " + resp.text)
79 if resp.text == None or len(resp.text) < 2:
80 return 'True'
81 return 'False'
82
83 def Generate_UUID(self):
84 """generate a uuid"""
85 return uuid.uuid4()
86
87 def get_json_value_list(self, jsonstr, keyval):
88 logger.info("Enter Get_Json_Key_Value_List")
89 if jsonstr == None or len(jsonstr) < 2:
90 logger.info("No Json data found")
91 return []
92 try:
93 data = json.loads(jsonstr)
94 nodelist = []
95 for item in data:
96 nodelist.append(item[keyval])
97 return nodelist
98 except Exception as e:
99 logger.info("Json data parsing fails")
100 print str(e)
101 return []
102
103 def generate_MilliTimestamp_UUID(self):
104 """generate a millisecond timestamp uuid"""
105 then = datetime.datetime.now()
106 return int(time.mktime(then.timetuple())*1e3 + then.microsecond/1e3)
107
108 def test (self):
109 import json
110 from pprint import pprint
111
112 with open('robot/assets/dcae/ves_volte_single_fault_event.json') as data_file:
113 data = json.load(data_file)
114
115 data['event']['commonEventHeader']['version'] = '5.0'
116 pprint(data)
117
118
119
120if __name__ == '__main__':
121 '''
122 dictStr = "action=getTable,Accept=application/json,Content-Type=application/json,X-FromAppId=1234908903284"
123 cls = DcaeLibrary()
124 #dict = cls.create_header_from_string(dictStr)
125 #print str(dict)
126 jsonStr = "[{'Node': 'onapfcnsl00', 'CheckID': 'serfHealth', 'Name': 'Serf Health Status', 'ServiceName': '', 'Notes': '', 'ModifyIndex': 6, 'Status': 'passing', 'ServiceID': '', 'ServiceTags': [], 'Output': 'Agent alive and reachable', 'CreateIndex': 6}]"
127 lsObj = cls.get_json_value_list(jsonStr, 'Status')
128 print lsObj
129 '''
130
131 lib = DcaeLibrary()
132 ret = lib.setup_dmaap_server()
133 print ret
134 time.sleep(10000000000)
135