pswang | daef349 | 2017-09-21 22:04:20 -0500 | [diff] [blame] | 1 | '''
|
| 2 | Created on Aug 18, 2017
|
| 3 |
|
| 4 | @author: sw6830
|
| 5 | '''
|
| 6 | from robot.api import logger
|
| 7 | from Queue import Queue
|
pswang | 212b7d9 | 2017-09-27 12:45:03 -0500 | [diff] [blame] | 8 | import uuid, time, datetime,json, threading,os, platform, subprocess,paramiko
|
pswang | daef349 | 2017-09-21 22:04:20 -0500 | [diff] [blame] | 9 | import DcaeVariables
|
| 10 | import DMaaP
|
| 11 |
|
| 12 | class DcaeLibrary(object):
|
| 13 |
|
| 14 | def __init__(self):
|
pswang | 212b7d9 | 2017-09-27 12:45:03 -0500 | [diff] [blame] | 15 | pass
|
pswang | daef349 | 2017-09-21 22:04:20 -0500 | [diff] [blame] | 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"
|
pswang | 212b7d9 | 2017-09-27 12:45:03 -0500 | [diff] [blame] | 53 |
|
| 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
|
pswang | daef349 | 2017-09-21 22:04:20 -0500 | [diff] [blame] | 75 |
|
| 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 |
|
| 142 | if __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()
|
pswang | 212b7d9 | 2017-09-27 12:45:03 -0500 | [diff] [blame] | 154 | lib.enable_vesc_https_auth()
|
| 155 |
|
pswang | daef349 | 2017-09-21 22:04:20 -0500 | [diff] [blame] | 156 | ret = lib.setup_dmaap_server()
|
| 157 | print ret
|
pswang | 212b7d9 | 2017-09-27 12:45:03 -0500 | [diff] [blame] | 158 | time.sleep(100000)
|
| 159 |
|