Matteo Scandolo | 48d3d2d | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 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 Karthick | ed3a2ca | 2017-07-06 15:50:03 -0700 | [diff] [blame] | 17 | import os |
| 18 | import sys |
| 19 | import time |
| 20 | from nose.tools import * |
| 21 | from CordTestUtils import log_test as log |
| 22 | from OnosCtrl import OnosCtrl |
| 23 | |
| 24 | class XosUtils(object): |
| 25 | |
A R Karthick | 705a5bf | 2017-10-27 11:17:04 -0700 | [diff] [blame] | 26 | head_node = os.getenv('HEAD_NODE', 'head1') |
A R Karthick | ed3a2ca | 2017-07-06 15:50:03 -0700 | [diff] [blame] | 27 | 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(':') |
| 50 | xos_host = xos_endpoints[1][len('//'):] |
| 51 | xos_port = xos_endpoints[2][:-1] |
| 52 | #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() |
| 78 | |
| 79 | def subscriberCreate(self, subscriber_info, volt_subscriber_info): |
| 80 | subId = '' |
| 81 | try: |
| 82 | result = self.restApi.ApiPost('TENANT_SUBSCRIBER', subscriber_info) |
| 83 | assert_equal(result, True) |
| 84 | result = self.restApi.ApiGet('TENANT_SUBSCRIBER') |
| 85 | assert_not_equal(result, None) |
| 86 | subId = self.restApi.getSubscriberId(result, volt_subscriber_info['account_num']) |
| 87 | assert_not_equal(subId, '0') |
| 88 | log.info('Subscriber ID for account num %s = %s' %(str(volt_subscriber_info['account_num']), subId)) |
| 89 | volt_tenant = volt_subscriber_info['voltTenant'] |
| 90 | #update the subscriber id in the tenant info before making the rest |
| 91 | volt_tenant['subscriber'] = subId |
| 92 | result = self.restApi.ApiPost('TENANT_VOLT', volt_tenant) |
| 93 | assert_equal(result, True) |
| 94 | finally: |
| 95 | return subId |
| 96 | |
| 97 | def subscriberDelete(self, account_num, subId = '', voltId = ''): |
| 98 | if not subId: |
| 99 | #get the subscriber id first |
| 100 | result = self.restApi.ApiGet('TENANT_SUBSCRIBER') |
| 101 | assert_not_equal(result, None) |
| 102 | subId = self.restApi.getSubscriberId(result, account_num) |
| 103 | assert_not_equal(subId, '0') |
| 104 | if not voltId: |
| 105 | #get the volt id for the subscriber |
| 106 | result = self.restApi.ApiGet('TENANT_VOLT') |
| 107 | assert_not_equal(result, None) |
| 108 | voltId = CordSubscriberUtils.getVoltId(result, subId) |
| 109 | assert_not_equal(voltId, None) |
| 110 | log.info('Deleting subscriber ID %s for account num %s' %(subId, str(account_num))) |
| 111 | status = self.restApi.ApiDelete('TENANT_SUBSCRIBER', subId) |
| 112 | assert_equal(status, True) |
| 113 | #Delete the tenant |
| 114 | log.info('Deleting VOLT Tenant ID %s for subscriber %s' %(voltId, subId)) |
| 115 | self.restApi.ApiDelete('TENANT_VOLT', voltId) |
| 116 | |
| 117 | def subscriberId(self, account_num): |
| 118 | result = self.restApi.ApiGet('TENANT_SUBSCRIBER') |
| 119 | assert_not_equal(result, None) |
| 120 | subId = self.restApi.getSubscriberId(result, account_num) |
| 121 | return subId |
| 122 | |
| 123 | class CordSubscriberUtils(object): |
| 124 | |
| 125 | SUBSCRIBER_ACCOUNT_NUM = 100 |
| 126 | SUBSCRIBER_S_TAG = 500 |
| 127 | SUBSCRIBER_C_TAG = 500 |
| 128 | SUBSCRIBERS_PER_S_TAG = 8 |
| 129 | |
| 130 | def __init__(self, |
| 131 | num_subscribers, |
| 132 | account_num = SUBSCRIBER_ACCOUNT_NUM, |
| 133 | s_tag = SUBSCRIBER_S_TAG, |
| 134 | c_tag = SUBSCRIBER_C_TAG, |
| 135 | subscribers_per_s_tag = SUBSCRIBERS_PER_S_TAG): |
| 136 | self.num_subscribers = num_subscribers |
| 137 | self.account_num = account_num |
| 138 | self.s_tag = s_tag |
| 139 | self.c_tag = c_tag |
| 140 | self.subscribers_per_s_tag = subscribers_per_s_tag |
| 141 | self.subscriber_map = {} |
| 142 | self.subscriber_info = self.getConfig() |
| 143 | self.volt_subscriber_info = self.getVoltConfig() |
| 144 | self.xos = XosUtils() |
| 145 | |
| 146 | def getCredentials(self, subId): |
| 147 | """Generate our own account num, s_tag and c_tags""" |
| 148 | if subId in self.subscriber_map: |
| 149 | return self.subscriber_map[subId] |
| 150 | account_num = self.account_num |
| 151 | self.account_num += 1 |
| 152 | s_tag, c_tag = self.s_tag, self.c_tag |
| 153 | self.c_tag += 1 |
| 154 | if self.c_tag % self.subscribers_per_s_tag == 0: |
| 155 | self.s_tag += 1 |
| 156 | self.subscriber_map[subId] = account_num, s_tag, c_tag |
| 157 | return self.subscriber_map[subId] |
| 158 | |
| 159 | def getConfig(self): |
| 160 | features = { |
| 161 | 'cdn': True, |
| 162 | 'uplink_speed': 1000000000, |
| 163 | 'downlink_speed': 1000000000, |
| 164 | 'uverse': True, |
| 165 | 'status': 'enabled' |
| 166 | } |
| 167 | subscriber_map = [] |
| 168 | for i in xrange(self.num_subscribers): |
| 169 | subId = 'sub{}'.format(i) |
| 170 | account_num, _, _ = self.getCredentials(subId) |
| 171 | identity = { 'account_num' : str(account_num), |
| 172 | 'name' : 'My House {}'.format(i) |
| 173 | } |
| 174 | sub_info = { 'features' : features, |
| 175 | 'identity' : identity |
| 176 | } |
| 177 | subscriber_map.append(sub_info) |
| 178 | |
| 179 | return subscriber_map |
| 180 | |
| 181 | def getVoltConfig(self): |
| 182 | voltSubscriberMap = [] |
| 183 | for i in xrange(self.num_subscribers): |
| 184 | subId = 'sub{}'.format(i) |
| 185 | account_num, s_tag, c_tag = self.getCredentials(subId) |
| 186 | voltSubscriberInfo = {} |
| 187 | voltSubscriberInfo['voltTenant'] = dict(s_tag = str(s_tag), |
| 188 | c_tag = str(c_tag), |
| 189 | subscriber = '') |
| 190 | voltSubscriberInfo['account_num'] = account_num |
| 191 | voltSubscriberMap.append(voltSubscriberInfo) |
| 192 | |
| 193 | return voltSubscriberMap |
| 194 | |
| 195 | @classmethod |
| 196 | def getVoltId(cls, result, subId): |
| 197 | if type(result) is not type([]): |
| 198 | return None |
| 199 | for tenant in result: |
| 200 | if str(tenant['subscriber']) == str(subId): |
| 201 | return str(tenant['id']) |
| 202 | return None |
| 203 | |
| 204 | def subscriberCreate(self, index, subscriber_info = None, volt_subscriber_info = None): |
| 205 | if subscriber_info is None: |
| 206 | subscriber_info = self.subscriber_info[index] |
| 207 | if volt_subscriber_info is None: |
| 208 | volt_subscriber_info = self.volt_subscriber_info[index] |
| 209 | s_tag = int(volt_subscriber_info['voltTenant']['s_tag']) |
| 210 | c_tag = int(volt_subscriber_info['voltTenant']['c_tag']) |
| 211 | log.info('Creating tenant with s_tag: %d, c_tag: %d' %(s_tag, c_tag)) |
| 212 | subId = self.xos.subscriberCreate(subscriber_info, volt_subscriber_info) |
| 213 | return subId |
| 214 | |
| 215 | def subscriberDelete(self, index, subId = '', voltId = '', subscriber_info = None, volt_subscriber_info = None): |
| 216 | if subscriber_info is None: |
| 217 | subscriber_info = self.subscriber_info[index] |
| 218 | if volt_subscriber_info is None: |
| 219 | volt_subscriber_info = self.volt_subscriber_info[index] |
| 220 | s_tag = int(volt_subscriber_info['voltTenant']['s_tag']) |
| 221 | c_tag = int(volt_subscriber_info['voltTenant']['c_tag']) |
| 222 | log.info('Deleting tenant with s_tag: %d, c_tag: %d' %(s_tag, c_tag)) |
| 223 | self.xos.subscriberDelete(volt_subscriber_info['account_num'], subId = subId, voltId = voltId) |
| 224 | |
| 225 | def subscriberId(self, index): |
| 226 | volt_subscriber_info = self.volt_subscriber_info[index] |
| 227 | return self.xos.subscriberId(volt_subscriber_info['account_num']) |