blob: 72cc9b5591ea5f8d6064128c9f7528e8bc7b76df [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
A R Karthicked3a2ca2017-07-06 15:50:03 -070017import os
18import sys
19import time
20from nose.tools import *
21from CordTestUtils import log_test as log
22from OnosCtrl import OnosCtrl
23
24class XosUtils(object):
25
A R Karthick705a5bf2017-10-27 11:17:04 -070026 head_node = os.getenv('HEAD_NODE', 'head1')
A R Karthicked3a2ca2017-07-06 15:50:03 -070027 HEAD_NODE = head_node + '.cord.lab' if len(head_node.split('.')) == 1 else head_node
28 CONTROLLER_PORT = '9000'
29 our_path = os.path.dirname(os.path.realpath(__file__))
30 cord_api_path = os.path.join(our_path, '..', 'cord-api')
31 framework_path = os.path.join(cord_api_path, 'Framework')
32 utils_path = os.path.join(framework_path, 'utils')
33 sys.path.append(utils_path)
34 sys.path.append(framework_path)
35
36 @classmethod
37 def getCredentials(cls):
38 onos_cfg = OnosCtrl.get_config()
39 if onos_cfg is None:
40 return None
41 if 'apps' in onos_cfg and \
42 'org.opencord.vtn' in onos_cfg['apps'] and \
43 'cordvtn' in onos_cfg['apps']['org.opencord.vtn'] and \
44 'xos' in onos_cfg['apps']['org.opencord.vtn']['cordvtn']:
45 xos_cfg = onos_cfg['apps']['org.opencord.vtn']['cordvtn']['xos']
46 endpoint = xos_cfg['endpoint']
47 user = xos_cfg['user']
48 password = xos_cfg['password']
49 xos_endpoints = endpoint.split(':')
A R Karthick3eef13c2017-11-06 18:47:25 -080050 xos_host = xos_endpoints[0]
51 xos_port = xos_endpoints[1]
A R Karthicked3a2ca2017-07-06 15:50:03 -070052 #log.info('xos_host: %s, port: %s, user: %s, password: %s' %(xos_host, xos_port, user, password))
53 return dict(host = xos_host, port = xos_port, user = user, password = password)
54
55 return None
56
57 @classmethod
58 def getRestApi(cls):
59 try:
60 from restApi import restApi
61 restApiXos = restApi()
62 xos_credentials = cls.getCredentials()
63 if xos_credentials is None:
64 restApiXos.controllerIP = cls.HEAD_NODE
65 restApiXos.controllerPort = cls.CONTROLLER_PORT
66 else:
67 restApiXos.controllerIP = xos_credentials['host']
68 restApiXos.controllerPort = xos_credentials['port']
69 restApiXos.user = xos_credentials['user']
70 restApiXos.password = xos_credentials['password']
71
72 return restApiXos
73 except:
74 return None
75
76 def __init__(self):
77 self.restApi = self.getRestApi()
A R Karthickdfdcb902017-11-06 18:23:46 -080078
79 '''
80 @method search_dictionary
81 @Description: Searches for a key in the provided nested dictionary
82 @params: input_dict = dictionary to be searched
83 search_key = name of the key to be searched for
84 returns two values: search_key value and status of the search.
85 True if found (False when not found)
86
87 '''
88 def search_dictionary(self, input_dict, search_key):
89 input_keys = input_dict.keys()
90 key_value = ''
91 found = False
92 for key in input_keys:
93 if key == search_key:
94 key_value = input_dict[key]
95 found = True
96 break
97 elif type(input_dict[key]) == dict:
98 key_value, found = self.search_dictionary(input_dict[key],search_key)
99 if found == True:
100 break
101 elif type(input_dict[key]) == list:
102 if not input_dict[key]:
103 found = False
104 break
105 for item in input_dict[key]:
106 if isinstance(item, dict):
107 key_value, found = self.search_dictionary(item, search_key)
108 if found == True:
109 break
110 return key_value,found
111
112 '''
113 @method getFieldValueFromDict
114 @params : search_dict - Dictionary to be searched
115 field - Key to be searched for (ex: account_num)
116 @Returns: Returns the value of the Key that was provided
117 '''
118 def getFieldValueFromDict(self,search_dict, field):
119 results = ''
120 found = False
121 input_keys = search_dict.keys()
122 for key in input_keys:
123 print "key...", key
124 if key == field:
125 results = search_dict[key]
126 if not results:
127 found = True
128 break
129 elif type(search_dict[key]) == dict:
130 results, found = self.search_dictionary(search_dict[key],field)
131 if found == True:
132 break
133 elif type(search_dict[key]) == list:
134 if not search_dict[key]:
135 found = False
136 continue
137 for item in search_dict[key]:
138 if isinstance(item, dict):
139 results, found = self.search_dictionary(item, field)
140 if found == True:
141 break
142 if results:
143 break
144
145 return results
A R Karthick028edaf2017-11-06 16:34:57 -0800146
147 def getSubscriberId(self, subscriberList, account_num):
148 subscriberId = 0
149 subscriberInfo = None
150 for subscriber in subscriberList:
151 if str(subscriber['service_specific_id']) == str(account_num):
A R Karthickdfdcb902017-11-06 18:23:46 -0800152 subscriberId = self.getFieldValueFromDict(subscriber, 'id')
A R Karthick028edaf2017-11-06 16:34:57 -0800153 subscriberInfo = subscriber
154 break
155 return subscriberInfo, subscriberId
156
A R Karthick30c2d682017-11-07 10:39:23 -0800157 def getVoltId(self, result, subInfo, s_tag = None, c_tag = None):
A R Karthickdfdcb902017-11-06 18:23:46 -0800158 subscribed_link_ids_list = self.getFieldValueFromDict(subInfo,
A R Karthick699f5c52017-11-06 19:02:47 -0800159 'subscribed_links_ids')
A R Karthick30c2d682017-11-07 10:39:23 -0800160 if len(subscribed_link_ids_list) > 0:
161 subscribed_link_ids = subscribed_link_ids_list[0]
162 service_link = self.restApi.ApiChameleonGet('CH_CORE_SERVICELINK',
163 subscribed_link_ids)
164 assert_not_equal(service_link, None)
165 provider_service_instance_id = service_link.get('provider_service_instance_id',
166 None)
167 assert_not_equal(provider_service_instance_id, None)
168 return provider_service_instance_id
169
170 #find the tenant for the s_tag/c_tag
171 if s_tag is None or c_tag is None:
172 return None
173
174 if result is None:
175 result = self.restApi.ApiGet('VOLT_TENANT')
176 result = result['items']
177
178 tenant = filter(lambda t: int(t['s_tag']) == int(s_tag) and \
179 int(t['c_tag']) == int(c_tag), result)
180 if not tenant:
181 return None
182
183 return tenant[0]['id']
A R Karthick028edaf2017-11-06 16:34:57 -0800184
185 def getProviderInstance(self, info):
A R Karthick30c2d682017-11-07 10:39:23 -0800186 return info['id']
A R Karthickdfdcb902017-11-06 18:23:46 -0800187 provided_link_ids_list = self.getFieldValueFromDict(info,
A R Karthick699f5c52017-11-06 19:02:47 -0800188 'provided_links_ids')
A R Karthick028edaf2017-11-06 16:34:57 -0800189 assert_not_equal(provided_link_ids_list, None)
190 assert_not_equal(len(provided_link_ids_list), 0)
191 provided_link_ids = provided_link_ids_list[0]
192 service_link = self.restApi.ApiChameleonGet('CH_CORE_SERVICELINK',
193 provided_link_ids)
194 if service_link is None:
195 return None
196 provider_service_instance_id = service_link.get('provider_service_instance_id',
197 None)
198 assert_not_equal(provider_service_instance_id, None)
199 return provider_service_instance_id
200
201 def linkTenant(self, subId, tenant_info):
A R Karthick699f5c52017-11-06 19:02:47 -0800202 result = self.restApi.ApiGet('VOLT_TENANT')['items']
A R Karthick028edaf2017-11-06 16:34:57 -0800203 tenant = None
204 for volt in result:
205 if str(volt['c_tag']) == str(tenant_info['c_tag']):
206 tenant = volt
207 break
208 assert_not_equal(tenant, None)
A R Karthickdfdcb902017-11-06 18:23:46 -0800209 volt_id = self.getFieldValueFromDict(tenant, 'id')
210 provided_links_ids_list = self.getFieldValueFromDict(tenant,
A R Karthick699f5c52017-11-06 19:02:47 -0800211 'provided_links_ids')
A R Karthick028edaf2017-11-06 16:34:57 -0800212 assert_not_equal( len(provided_link_ids_list), 0)
213 provided_link_ids = provided_link_ids_list[0]
A R Karthickdfdcb902017-11-06 18:23:46 -0800214 subscribed_link_ids_list = self.getFieldValueFromDict(tenant,
A R Karthick699f5c52017-11-06 19:02:47 -0800215 'subscribed_links_ids')
A R Karthick028edaf2017-11-06 16:34:57 -0800216 assert_not_equal(len(subscribed_link_ids_list), 0)
217 subscribed_link_ids = subscribed_link_ids_list[0]
218 service_link = self.restApi.ApiChameleonGet('CH_CORE_SERVICELINK',
219 provided_link_ids)
220 assert_not_equal(service_link, None)
221 provider_service_instance_id = service_link.get('provider_service_instance_id',
222 None)
223 assert_not_equal(provider_service_instance_id, None)
224 service_dict = dict(subscriber_service_instance_id = subId)
225 result = self.restApi.ApiChameleonPut('CH_CORE_SERVICELINK',
226 service_dict,
227 provided_link_ids)
228 assert_equal(result, True)
229 return provider_service_instance_id
230 # service_link_dict = self.restApi.ApiChameleonGet('CH_CORE_SERVICELINK',
231 # subscribed_link_ids)
232 # assert_not_equal(service_link_dict, None)
233 # vsg_tenant = service_link_dict.get('provider_service_instance_id', None)
234 # assert_not_equal(vsg_tenant, None)
235 # vsg_result = self.restApi.ApiChameleonGet('VSG_TENANT',
236 # vsg_tenant)
237 # assert_not_equal(vsg_result, None)
238 # vsg_instance = vsg_result.get('instance_id', None)
239 # assert_not_equal(vsg_instance, None)
240 # instance_result = self.restApi.ApiChameleonGet('CH_CORE_INSTANCES',
241 # vsg_instance)
242 # assert_equal(instance_result, True)
A R Karthicked3a2ca2017-07-06 15:50:03 -0700243
244 def subscriberCreate(self, subscriber_info, volt_subscriber_info):
245 subId = ''
246 try:
A R Karthick028edaf2017-11-06 16:34:57 -0800247 result = self.restApi.ApiPost('VOLT_SUBSCRIBER', subscriber_info)
A R Karthicked3a2ca2017-07-06 15:50:03 -0700248 assert_equal(result, True)
A R Karthick028edaf2017-11-06 16:34:57 -0800249 result = self.restApi.ApiGet('VOLT_SUBSCRIBER')
A R Karthicked3a2ca2017-07-06 15:50:03 -0700250 assert_not_equal(result, None)
A R Karthick699f5c52017-11-06 19:02:47 -0800251 result = result['items']
A R Karthick028edaf2017-11-06 16:34:57 -0800252 _, subId = self.getSubscriberId(result,
253 volt_subscriber_info['service_specific_id'])
A R Karthicked3a2ca2017-07-06 15:50:03 -0700254 assert_not_equal(subId, '0')
A R Karthick028edaf2017-11-06 16:34:57 -0800255 log.info('Subscriber ID for account num %s = %s' %(str(volt_subscriber_info['service_specific_id']), subId))
A R Karthicked3a2ca2017-07-06 15:50:03 -0700256 volt_tenant = volt_subscriber_info['voltTenant']
A R Karthick028edaf2017-11-06 16:34:57 -0800257 result = self.restApi.ApiPost('VOLT_TENANT', volt_tenant)
A R Karthicked3a2ca2017-07-06 15:50:03 -0700258 assert_equal(result, True)
A R Karthick028edaf2017-11-06 16:34:57 -0800259 volt_id = self.linkTenant(subId, volt_tenant)
260 log.info('Subscriber create with ctag %s, stag %s, volt id %s' %(str(volt_tenant['c_tag']),
261 str(volt_tenant['s_tag']),
262 str(volt_id)))
A R Karthicked3a2ca2017-07-06 15:50:03 -0700263 finally:
264 return subId
265
A R Karthick30c2d682017-11-07 10:39:23 -0800266 def subscriberDelete(self, account_num, s_tag = None, c_tag = None, subId = '', voltId = ''):
A R Karthick028edaf2017-11-06 16:34:57 -0800267 result = self.restApi.ApiGet('VOLT_SUBSCRIBER')
268 assert_not_equal(result, None)
A R Karthick699f5c52017-11-06 19:02:47 -0800269 result = result['items']
A R Karthicked3a2ca2017-07-06 15:50:03 -0700270 if not subId:
271 #get the subscriber id first
A R Karthick028edaf2017-11-06 16:34:57 -0800272 subInfo, subId = self.getSubscriberId(result, account_num)
A R Karthicked3a2ca2017-07-06 15:50:03 -0700273 assert_not_equal(subId, '0')
A R Karthick028edaf2017-11-06 16:34:57 -0800274 else:
275 subInfo, currentSubId = self.getSubscriberId(result, account_num)
276 assert_not_equal(currentSubId, '0')
277 #assert_equal(subId, currentSubId)
A R Karthickdfdcb902017-11-06 18:23:46 -0800278 subId = self.getFieldValueFromDict(subInfo, 'id')
A R Karthicked3a2ca2017-07-06 15:50:03 -0700279 if not voltId:
280 #get the volt id for the subscriber
A R Karthick028edaf2017-11-06 16:34:57 -0800281 result = self.restApi.ApiGet('VOLT_TENANT')
A R Karthicked3a2ca2017-07-06 15:50:03 -0700282 assert_not_equal(result, None)
A R Karthick699f5c52017-11-06 19:02:47 -0800283 result = result['items']
A R Karthick30c2d682017-11-07 10:39:23 -0800284 voltId = self.getVoltId(result, subInfo, s_tag = s_tag, c_tag = c_tag)
A R Karthicked3a2ca2017-07-06 15:50:03 -0700285 assert_not_equal(voltId, None)
A R Karthicked3a2ca2017-07-06 15:50:03 -0700286 log.info('Deleting VOLT Tenant ID %s for subscriber %s' %(voltId, subId))
A R Karthick028edaf2017-11-06 16:34:57 -0800287 status = self.restApi.ApiChameleonDelete('VOLT_TENANT', voltId)
288 assert_equal(status, True)
289 log.info('Deleting subscriber ID %s for account num %s' %(subId, str(account_num)))
290 status = self.restApi.ApiChameleonDelete('VOLT_SUBSCRIBER', subId)
291 assert_equal(status, True)
A R Karthicked3a2ca2017-07-06 15:50:03 -0700292
293 def subscriberId(self, account_num):
A R Karthick028edaf2017-11-06 16:34:57 -0800294 result = self.restApi.ApiGet('VOLT_SUBSCRIBER')
A R Karthicked3a2ca2017-07-06 15:50:03 -0700295 assert_not_equal(result, None)
A R Karthick699f5c52017-11-06 19:02:47 -0800296 result = result['items']
A R Karthick028edaf2017-11-06 16:34:57 -0800297 _, subId = self.getSubscriberId(result, account_num)
A R Karthicked3a2ca2017-07-06 15:50:03 -0700298 return subId
299
300class CordSubscriberUtils(object):
301
302 SUBSCRIBER_ACCOUNT_NUM = 100
303 SUBSCRIBER_S_TAG = 500
304 SUBSCRIBER_C_TAG = 500
305 SUBSCRIBERS_PER_S_TAG = 8
306
307 def __init__(self,
308 num_subscribers,
309 account_num = SUBSCRIBER_ACCOUNT_NUM,
310 s_tag = SUBSCRIBER_S_TAG,
311 c_tag = SUBSCRIBER_C_TAG,
312 subscribers_per_s_tag = SUBSCRIBERS_PER_S_TAG):
313 self.num_subscribers = num_subscribers
314 self.account_num = account_num
315 self.s_tag = s_tag
316 self.c_tag = c_tag
317 self.subscribers_per_s_tag = subscribers_per_s_tag
318 self.subscriber_map = {}
A R Karthick30c2d682017-11-07 10:39:23 -0800319 self.tenant_map = {}
A R Karthicked3a2ca2017-07-06 15:50:03 -0700320 self.subscriber_info = self.getConfig()
321 self.volt_subscriber_info = self.getVoltConfig()
322 self.xos = XosUtils()
323
324 def getCredentials(self, subId):
325 """Generate our own account num, s_tag and c_tags"""
326 if subId in self.subscriber_map:
327 return self.subscriber_map[subId]
328 account_num = self.account_num
329 self.account_num += 1
330 s_tag, c_tag = self.s_tag, self.c_tag
331 self.c_tag += 1
332 if self.c_tag % self.subscribers_per_s_tag == 0:
333 self.s_tag += 1
334 self.subscriber_map[subId] = account_num, s_tag, c_tag
A R Karthick30c2d682017-11-07 10:39:23 -0800335 self.tenant_map[account_num] = (s_tag, c_tag)
A R Karthicked3a2ca2017-07-06 15:50:03 -0700336 return self.subscriber_map[subId]
337
338 def getConfig(self):
339 features = {
A R Karthick3eef13c2017-11-06 18:47:25 -0800340 'cdn_enable': True,
A R Karthicked3a2ca2017-07-06 15:50:03 -0700341 'uplink_speed': 1000000000,
342 'downlink_speed': 1000000000,
A R Karthick028edaf2017-11-06 16:34:57 -0800343 'enable_uverse': True,
A R Karthicked3a2ca2017-07-06 15:50:03 -0700344 'status': 'enabled'
345 }
346 subscriber_map = []
347 for i in xrange(self.num_subscribers):
348 subId = 'sub{}'.format(i)
349 account_num, _, _ = self.getCredentials(subId)
A R Karthick028edaf2017-11-06 16:34:57 -0800350 identity = { 'service_specific_id' : str(account_num),
A R Karthicked3a2ca2017-07-06 15:50:03 -0700351 'name' : 'My House {}'.format(i)
352 }
A R Karthick028edaf2017-11-06 16:34:57 -0800353 sub_data = [ (k, v) for d in (features, identity) \
354 for k, v in d.iteritems() ]
355 sub_info = dict(sub_data)
A R Karthicked3a2ca2017-07-06 15:50:03 -0700356 subscriber_map.append(sub_info)
357
358 return subscriber_map
359
A R Karthick30c2d682017-11-07 10:39:23 -0800360 def getVoltInfo(self, account_num):
361 num = int(account_num)
362 if num in self.tenant_map:
363 return self.tenant_map[num]
364 return None, None
365
A R Karthicked3a2ca2017-07-06 15:50:03 -0700366 def getVoltConfig(self):
367 voltSubscriberMap = []
368 for i in xrange(self.num_subscribers):
369 subId = 'sub{}'.format(i)
370 account_num, s_tag, c_tag = self.getCredentials(subId)
371 voltSubscriberInfo = {}
372 voltSubscriberInfo['voltTenant'] = dict(s_tag = str(s_tag),
A R Karthick028edaf2017-11-06 16:34:57 -0800373 c_tag = str(c_tag))
374 voltSubscriberInfo['service_specific_id'] = account_num
A R Karthicked3a2ca2017-07-06 15:50:03 -0700375 voltSubscriberMap.append(voltSubscriberInfo)
376
377 return voltSubscriberMap
378
A R Karthick028edaf2017-11-06 16:34:57 -0800379 def getVoltId(self, subInfo):
A R Karthick30c2d682017-11-07 10:39:23 -0800380 s_tag, c_tag = self.getVoltInfo(subInfo['service_specific_id'])
381 return self.xos.getVoltId(None, subInfo, s_tag = s_tag, c_tag = c_tag)
A R Karthick028edaf2017-11-06 16:34:57 -0800382
383 def getProviderInstance(self, tenant_info):
384 return self.xos.getProviderInstance(tenant_info)
A R Karthicked3a2ca2017-07-06 15:50:03 -0700385
386 def subscriberCreate(self, index, subscriber_info = None, volt_subscriber_info = None):
387 if subscriber_info is None:
388 subscriber_info = self.subscriber_info[index]
389 if volt_subscriber_info is None:
390 volt_subscriber_info = self.volt_subscriber_info[index]
391 s_tag = int(volt_subscriber_info['voltTenant']['s_tag'])
392 c_tag = int(volt_subscriber_info['voltTenant']['c_tag'])
393 log.info('Creating tenant with s_tag: %d, c_tag: %d' %(s_tag, c_tag))
394 subId = self.xos.subscriberCreate(subscriber_info, volt_subscriber_info)
395 return subId
396
397 def subscriberDelete(self, index, subId = '', voltId = '', subscriber_info = None, volt_subscriber_info = None):
398 if subscriber_info is None:
399 subscriber_info = self.subscriber_info[index]
400 if volt_subscriber_info is None:
401 volt_subscriber_info = self.volt_subscriber_info[index]
402 s_tag = int(volt_subscriber_info['voltTenant']['s_tag'])
403 c_tag = int(volt_subscriber_info['voltTenant']['c_tag'])
404 log.info('Deleting tenant with s_tag: %d, c_tag: %d' %(s_tag, c_tag))
A R Karthick30c2d682017-11-07 10:39:23 -0800405 self.xos.subscriberDelete(volt_subscriber_info['service_specific_id'], s_tag = s_tag, c_tag = c_tag, subId = subId, voltId = voltId)
A R Karthicked3a2ca2017-07-06 15:50:03 -0700406
407 def subscriberId(self, index):
408 volt_subscriber_info = self.volt_subscriber_info[index]
A R Karthick028edaf2017-11-06 16:34:57 -0800409 return self.xos.subscriberId(volt_subscriber_info['service_specific_id'])