blob: 19e246cd86ebf82303be133a6ed86530cf8db9be [file] [log] [blame]
A R Karthick35495c32017-05-11 14:58:32 -07001import requests
2import json
3import time
A.R Karthick57fa9372017-05-24 12:47:03 -07004import os
5import signal
6from CordTestUtils import log_test as log, getstatusoutput
7from CordContainer import Container
8
9class VolthaService(object):
10 services = ('consul', 'kafka', 'zookeeper', 'registrator', 'fluentd')
11 compose_file = 'docker-compose-system-test.yml'
12 service_map = {}
13
14 def __init__(self, voltha_loc, controller, interface = 'eth0'):
15 if not os.access(voltha_loc, os.F_OK):
16 raise Exception('Voltha location %s not found' %voltha_loc)
17 compose_file_loc = os.path.join(voltha_loc, 'compose', self.compose_file)
18 if not os.access(compose_file_loc, os.F_OK):
19 raise Exception('Voltha compose file %s not found' %compose_file_loc)
20 self.voltha_loc = voltha_loc
21 self.controller = controller
22 self.interface = interface
23 self.compose_file_loc = compose_file_loc
24
25 def start(self):
26 start_cmd = 'docker-compose -f {} up -d {} {} {} {} {}'.format(self.compose_file_loc,
27 *self.services)
28 ret = os.system(start_cmd)
29 if ret != 0:
30 raise Exception('Failed to start voltha services. Failed with code %d' %ret)
31
32 for service in self.services:
33 name = 'compose_{}_1'.format(service)
34 network = 'compose_default'
35 cnt = Container(name, name)
36 ip = cnt.ip(network = network)
37 if not ip:
38 raise Exception('IP not found for container %s' %name)
39 print('IP %s for service %s' %(ip, service))
40 self.service_map[service] = dict(name = name, network = network, ip = ip)
41
42 #first start chameleon
43 chameleon_start_cmd = "cd {} && sh -c '. ./env.sh && \
44 nohup python chameleon/main.py -v --consul=localhost:8500 \
45 --fluentd={}:24224 --grpc-endpoint=localhost:50555 \
46 >/tmp/chameleon.log 2>&1 &'".format(self.voltha_loc,
47 self.service_map['fluentd']['ip'])
48 ret = os.system(chameleon_start_cmd)
49 if ret != 0:
50 raise Exception('VOLTHA chameleon service not started. Failed with return code %d' %ret)
51
52 #now start voltha and ofagent
53 voltha_start_cmd = "cd {} && sh -c '. ./env.sh && \
54 nohup python voltha/main.py -v --consul=localhost:8500 --kafka={}:9092 -I {} \
55 --fluentd={}:24224 --rest-port=8880 --grpc-port=50555 \
56 >/tmp/voltha.log 2>&1 &'".format(self.voltha_loc,
57 self.service_map['kafka']['ip'],
58 self.interface,
59 self.service_map['fluentd']['ip'])
60 ret = os.system(voltha_start_cmd)
61 if ret != 0:
62 raise Exception('Failed to start VOLTHA. Return code %d' %ret)
63
64
65 ofagent_start_cmd = "cd {} && sh -c '. ./env.sh && \
66 nohup python ofagent/main.py -v --consul=localhost:8500 \
67 --fluentd={}:24224 --controller={}:6653 --grpc-endpoint=localhost:50555 \
68 >/tmp/ofagent.log 2>&1 &'".format(self.voltha_loc,
69 self.service_map['fluentd']['ip'],
70 self.controller)
71 ret = os.system(ofagent_start_cmd)
72 if ret != 0:
73 raise Exception('VOLTHA ofagent not started. Failed with return code %d' %ret)
74
75 def kill_service(self, pattern):
76 st, output = getstatusoutput('pgrep -f "{}"'.format(pattern))
77 if st == 0 and output:
78 pids = output.strip().splitlines()
79 for pid in pids:
80 try:
81 os.kill(int(pid), signal.SIGKILL)
82 except:
83 pass
84
85 def stop(self):
86 self.kill_service('python voltha/main.py')
87 self.kill_service('python ofagent/main.py')
88 self.kill_service('python chameleon/main.py')
89 service_stop_cmd = 'docker-compose -f {} down'.format(self.compose_file_loc)
90 os.system(service_stop_cmd)
A R Karthick35495c32017-05-11 14:58:32 -070091
92class VolthaCtrl(object):
93
94 def __init__(self, host, rest_port = 8881):
95 self.host = host
96 self.rest_port = rest_port
97 self.rest_url = 'http://{}:{}/api/v1'.format(host, rest_port)
98
99 def get_devices(self):
100 url = '{}/local/devices'.format(self.rest_url)
101 resp = requests.get(url)
102 if resp.ok is not True or resp.status_code != 200:
103 return None
104 return resp.json()
105
106 def enable_device(self, olt_type, olt_mac):
107 url = '{}/local/devices'.format(self.rest_url)
108 device_config = { 'type' : olt_type, 'mac_address' : olt_mac }
109 #pre-provision
110 log.info('Pre-provisioning %s with mac %s' %(olt_type, olt_mac))
111 resp = requests.post(url, data = json.dumps(device_config))
112 if resp.ok is not True or resp.status_code != 200:
113 return False
114 device_id = resp.json()['id']
115 log.info('Enabling device %s' %(device_id))
116 enable_url = '{}/{}/enable'.format(url, device_id)
117 resp = requests.post(enable_url)
118 if resp.ok is not True or resp.status_code != 200:
119 return False
120 #get operational status
121 time.sleep(5)
122 log.info('Checking operational status for device %s' %(device_id))
123 resp = requests.get('{}/{}'.format(url, device_id))
124 if resp.ok is not True or resp.status_code != 200:
125 return False
126 device_info = resp.json()
127 if device_info['oper_status'] != 'ACTIVE' or \
128 device_info['admin_state'] != 'ENABLED' or \
129 device_info['connect_status'] != 'REACHABLE':
130 return False
131
132 return True
Chetan Gaonker3620a112017-05-23 06:10:15 +0000133
134 def get_operational_status(self, device_id):
135 url = '{}/local/devices'.format(self.rest_url)
136 log.info('Checking operational status for device %s' %(device_id))
137 resp = requests.get('{}/{}'.format(url, device_id))
138 if resp.ok is not True or resp.status_code != 200:
139 return False
140 device_info = resp.json()
141 if device_info['oper_status'] != 'ACTIVE' or \
142 device_info['admin_state'] != 'ENABLED' or \
143 device_info['connect_status'] != 'REACHABLE':
144 return False
145 return True
146
147 def check_preprovision_status(self, device_id):
148 url = '{}/local/devices'.format(self.rest_url)
149 log.info('Check if device %s is in Preprovisioning state'%(device_id))
150 resp = requests.get('{}/{}'.format(url, device_id))
151 if resp.ok is not True or resp.status_code != 200:
152 return False
153 device_info = resp.json()
154 if device_info['admin_status'] == 'PREPROVISIONED':
155 return True
156 return False