blob: 0242ad7abb99a97c8c877ef05b6e0f62b54602f6 [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
pswang212b7d92017-09-27 12:45:03 -05008import uuid, time, datetime,json, threading,os, platform, subprocess,paramiko
pswangdaef3492017-09-21 22:04:20 -05009import DcaeVariables
10import DMaaP
11
12class DcaeLibrary(object):
13
14 def __init__(self):
pswang212b7d92017-09-27 12:45:03 -050015 pass
pswangdaef3492017-09-21 22:04:20 -050016
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"
pswang212b7d92017-09-27 12:45:03 -050053
54 def enable_vesc_https_auth(self):
55 if 'Windows' in platform.system():
56 try:
57 client = paramiko.SSHClient()
58 client.load_system_host_keys()
59 #client.set_missing_host_key_policy(paramiko.WarningPolicy)
60 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
61
62 client.connect(os.environ['CSIT_IP'], port=22, username=os.environ['CSIT_USER'], password=os.environ['CSIT_PD'])
63 stdin, stdout, stderr = client.exec_command('%{WORKSPACE}/test/csit/tests/dcaegen2/testcases/resources/vesc_enable_https_auth.sh')
64 logger.console(stdout.read())
65 finally:
66 client.close()
67 return
68 ws = os.environ['WORKSPACE']
69 script2run = ws + "/test/csit/tests/dcaegen2/testcases/resources/vesc_enable_https_auth.sh"
70 logger.info("Running script: " + script2run)
71 logger.console("Running script: " + script2run)
72 subprocess.call(script2run)
73 time.sleep(5)
74 return
pswangdaef3492017-09-21 22:04:20 -050075
76 def dmaap_message_receive(self, evtobj, action='contain'):
77
78 evtStr = DMaaP.dequeEvent()
79 while evtStr != None:
80 logger.console("DMaaP receive VES Event:\n" + evtStr)
81 if action == 'contain':
82 if evtobj in evtStr:
83 logger.info("DMaaP Receive Expected Publish Event:\n" + evtStr)
84 return 'true'
85 if action == 'sizematch':
86 if len(evtobj) == len(evtStr):
87 return 'true'
88 if action == 'dictmatch':
89 evtDict = json.loads(evtStr)
90 if cmp(evtobj, evtDict) == 0:
91 return 'true'
92 evtStr = DMaaP.dequeEvent()
93 return 'false'
94
95 def create_header_from_string(self, dictStr):
96 logger.info("Enter create_header_from_string: dictStr")
97 return dict(u.split("=") for u in dictStr.split(","))
98
99 def is_json_empty(self, resp):
100 logger.info("Enter is_json_empty: resp.text: " + resp.text)
101 if resp.text == None or len(resp.text) < 2:
102 return 'True'
103 return 'False'
104
105 def Generate_UUID(self):
106 """generate a uuid"""
107 return uuid.uuid4()
108
109 def get_json_value_list(self, jsonstr, keyval):
110 logger.info("Enter Get_Json_Key_Value_List")
111 if jsonstr == None or len(jsonstr) < 2:
112 logger.info("No Json data found")
113 return []
114 try:
115 data = json.loads(jsonstr)
116 nodelist = []
117 for item in data:
118 nodelist.append(item[keyval])
119 return nodelist
120 except Exception as e:
121 logger.info("Json data parsing fails")
122 print str(e)
123 return []
124
125 def generate_MilliTimestamp_UUID(self):
126 """generate a millisecond timestamp uuid"""
127 then = datetime.datetime.now()
128 return int(time.mktime(then.timetuple())*1e3 + then.microsecond/1e3)
129
130 def test (self):
131 import json
132 from pprint import pprint
133
134 with open('robot/assets/dcae/ves_volte_single_fault_event.json') as data_file:
135 data = json.load(data_file)
136
137 data['event']['commonEventHeader']['version'] = '5.0'
138 pprint(data)
139
140
141
142if __name__ == '__main__':
143 '''
144 dictStr = "action=getTable,Accept=application/json,Content-Type=application/json,X-FromAppId=1234908903284"
145 cls = DcaeLibrary()
146 #dict = cls.create_header_from_string(dictStr)
147 #print str(dict)
148 jsonStr = "[{'Node': 'onapfcnsl00', 'CheckID': 'serfHealth', 'Name': 'Serf Health Status', 'ServiceName': '', 'Notes': '', 'ModifyIndex': 6, 'Status': 'passing', 'ServiceID': '', 'ServiceTags': [], 'Output': 'Agent alive and reachable', 'CreateIndex': 6}]"
149 lsObj = cls.get_json_value_list(jsonStr, 'Status')
150 print lsObj
151 '''
152
153 lib = DcaeLibrary()
pswang212b7d92017-09-27 12:45:03 -0500154 lib.enable_vesc_https_auth()
155
pswangdaef3492017-09-21 22:04:20 -0500156 ret = lib.setup_dmaap_server()
157 print ret
pswang212b7d92017-09-27 12:45:03 -0500158 time.sleep(100000)
159