blob: b43ee29e2902fabc01a7af1a66644d46c90067a6 [file] [log] [blame]
Marco Platania22337772018-04-03 16:52:13 -04001'''
2Created on Aug 18, 2017
3
4@author: sw6830
5'''
6from robot.api import logger
7from Queue import Queue
Mariusz Wagner831baff2018-09-14 14:24:02 +02008import uuid
9import time
10import datetime
11import json
12import threading
13import os
14import platform
15import subprocess
16import paramiko
Marco Platania22337772018-04-03 16:52:13 -040017import DcaeVariables
18import DMaaP
19
Mariusz Wagner831baff2018-09-14 14:24:02 +020020
Marco Platania22337772018-04-03 16:52:13 -040021class DcaeLibrary(object):
22
23 def __init__(self):
24 pass
25
Mariusz Wagner831baff2018-09-14 14:24:02 +020026 @staticmethod
27 def setup_dmaap_server(port_num=3904):
28 if DcaeVariables.HttpServerThread is not None:
29 DMaaP.clean_up_event()
Marco Platania22337772018-04-03 16:52:13 -040030 logger.console("Clean up event from event queue before test")
31 logger.info("DMaaP Server already started")
32 return "true"
33
34 DcaeVariables.IsRobotRun = True
Mariusz Wagner831baff2018-09-14 14:24:02 +020035 DMaaP.test(port=port_num)
Marco Platania22337772018-04-03 16:52:13 -040036 try:
37 DcaeVariables.VESEventQ = Queue()
38 DcaeVariables.HttpServerThread = threading.Thread(name='DMAAP_HTTPServer', target=DMaaP.DMaaPHttpd.serve_forever)
39 DcaeVariables.HttpServerThread.start()
40 logger.console("DMaaP Mockup Sever started")
41 time.sleep(2)
42 return "true"
43 except Exception as e:
44 print (str(e))
45 return "false"
46
Mariusz Wagner831baff2018-09-14 14:24:02 +020047 @staticmethod
48 def shutdown_dmaap():
49 if DcaeVariables.HTTPD is not None:
Marco Platania22337772018-04-03 16:52:13 -040050 DcaeVariables.HTTPD.shutdown()
51 logger.console("DMaaP Server shut down")
52 time.sleep(3)
53 return "true"
54 else:
55 return "false"
56
Mariusz Wagner831baff2018-09-14 14:24:02 +020057 @staticmethod
58 def cleanup_ves_events():
59 if DcaeVariables.HttpServerThread is not None:
60 DMaaP.clean_up_event()
Marco Platania22337772018-04-03 16:52:13 -040061 logger.console("DMaaP event queue is cleaned up")
62 return "true"
63 logger.console("DMaaP server not started yet")
64 return "false"
65
Mariusz Wagner831baff2018-09-14 14:24:02 +020066 @staticmethod
67 def enable_vesc_https_auth():
68 global client
Marco Platania22337772018-04-03 16:52:13 -040069 if 'Windows' in platform.system():
70 try:
71 client = paramiko.SSHClient()
72 client.load_system_host_keys()
Mariusz Wagner831baff2018-09-14 14:24:02 +020073 # client.set_missing_host_key_policy(paramiko.WarningPolicy)
Marco Platania22337772018-04-03 16:52:13 -040074 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
75
76 client.connect(os.environ['CSIT_IP'], port=22, username=os.environ['CSIT_USER'], password=os.environ['CSIT_PD'])
77 stdin, stdout, stderr = client.exec_command('%{WORKSPACE}/test/csit/tests/dcaegen2/testcases/resources/vesc_enable_https_auth.sh')
78 logger.console(stdout.read())
79 finally:
80 client.close()
81 return
82 ws = os.environ['WORKSPACE']
83 script2run = ws + "/test/csit/tests/dcaegen2/testcases/resources/vesc_enable_https_auth.sh"
84 logger.info("Running script: " + script2run)
85 logger.console("Running script: " + script2run)
86 subprocess.call(script2run)
87 time.sleep(5)
88 return
89
Mariusz Wagner831baff2018-09-14 14:24:02 +020090 @staticmethod
91 def dmaap_message_receive(evtobj, action='contain'):
Marco Platania22337772018-04-03 16:52:13 -040092
Mariusz Wagner831baff2018-09-14 14:24:02 +020093 evt_str = DMaaP.deque_event()
94 while evt_str != None:
95 logger.console("DMaaP receive VES Event:\n" + evt_str)
Marco Platania22337772018-04-03 16:52:13 -040096 if action == 'contain':
Mariusz Wagner831baff2018-09-14 14:24:02 +020097 if evtobj in evt_str:
98 logger.info("DMaaP Receive Expected Publish Event:\n" + evt_str)
Marco Platania22337772018-04-03 16:52:13 -040099 return 'true'
100 if action == 'sizematch':
Mariusz Wagner831baff2018-09-14 14:24:02 +0200101 if len(evtobj) == len(evt_str):
Marco Platania22337772018-04-03 16:52:13 -0400102 return 'true'
103 if action == 'dictmatch':
Mariusz Wagner831baff2018-09-14 14:24:02 +0200104 evt_dict = json.loads(evt_str)
105 if cmp(evtobj, evt_dict) == 0:
Marco Platania22337772018-04-03 16:52:13 -0400106 return 'true'
Mariusz Wagner831baff2018-09-14 14:24:02 +0200107 evt_str = DMaaP.deque_event()
Marco Platania22337772018-04-03 16:52:13 -0400108 return 'false'
Mariusz Wagner831baff2018-09-14 14:24:02 +0200109
110 @staticmethod
111 def is_json_empty(resp):
Marco Platania22337772018-04-03 16:52:13 -0400112 logger.info("Enter is_json_empty: resp.text: " + resp.text)
Mariusz Wagner831baff2018-09-14 14:24:02 +0200113 if resp.text is None or len(resp.text) < 2:
Marco Platania22337772018-04-03 16:52:13 -0400114 return 'True'
115 return 'False'
116
Mariusz Wagner831baff2018-09-14 14:24:02 +0200117 @staticmethod
118 def generate_uuid():
Marco Platania22337772018-04-03 16:52:13 -0400119 """generate a uuid"""
120 return uuid.uuid4()
121
Mariusz Wagner831baff2018-09-14 14:24:02 +0200122 @staticmethod
123 def get_json_value_list(jsonstr, keyval):
Marco Platania22337772018-04-03 16:52:13 -0400124 logger.info("Enter Get_Json_Key_Value_List")
Mariusz Wagner831baff2018-09-14 14:24:02 +0200125 if jsonstr is None or len(jsonstr) < 2:
Marco Platania22337772018-04-03 16:52:13 -0400126 logger.info("No Json data found")
127 return []
128 try:
129 data = json.loads(jsonstr)
130 nodelist = []
131 for item in data:
132 nodelist.append(item[keyval])
133 return nodelist
134 except Exception as e:
135 logger.info("Json data parsing fails")
136 print str(e)
137 return []
138
Mariusz Wagner831baff2018-09-14 14:24:02 +0200139 @staticmethod
140 def generate_millitimestamp_uuid():
Marco Platania22337772018-04-03 16:52:13 -0400141 """generate a millisecond timestamp uuid"""
142 then = datetime.datetime.now()
143 return int(time.mktime(then.timetuple())*1e3 + then.microsecond/1e3)
144
Mariusz Wagner831baff2018-09-14 14:24:02 +0200145 @staticmethod
146 def test():
Marco Platania22337772018-04-03 16:52:13 -0400147 import json
148 from pprint import pprint
149
150 with open('robot/assets/dcae/ves_volte_single_fault_event.json') as data_file:
151 data = json.load(data_file)
152
153 data['event']['commonEventHeader']['version'] = '5.0'
154 pprint(data)
155
156
Marco Platania22337772018-04-03 16:52:13 -0400157if __name__ == '__main__':
158 '''
159 dictStr = "action=getTable,Accept=application/json,Content-Type=application/json,X-FromAppId=1234908903284"
160 cls = DcaeLibrary()
161 #dict = cls.create_header_from_string(dictStr)
162 #print str(dict)
163 jsonStr = "[{'Node': 'onapfcnsl00', 'CheckID': 'serfHealth', 'Name': 'Serf Health Status', 'ServiceName': '', 'Notes': '', 'ModifyIndex': 6, 'Status': 'passing', 'ServiceID': '', 'ServiceTags': [], 'Output': 'Agent alive and reachable', 'CreateIndex': 6}]"
164 lsObj = cls.get_json_value_list(jsonStr, 'Status')
165 print lsObj
166 '''
167
168 lib = DcaeLibrary()
169 lib.enable_vesc_https_auth()
170
171 ret = lib.setup_dmaap_server()
172 print ret
173 time.sleep(100000)