A R Karthick | ed3a2ca | 2017-07-06 15:50:03 -0700 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import time |
| 4 | from nose.tools import * |
| 5 | from CordTestUtils import log_test as log |
| 6 | from OnosCtrl import OnosCtrl |
| 7 | |
| 8 | class XosUtils(object): |
| 9 | |
| 10 | head_node = os.getenv('HEAD_NODE', 'prod') |
| 11 | HEAD_NODE = head_node + '.cord.lab' if len(head_node.split('.')) == 1 else head_node |
| 12 | CONTROLLER_PORT = '9000' |
| 13 | our_path = os.path.dirname(os.path.realpath(__file__)) |
| 14 | cord_api_path = os.path.join(our_path, '..', 'cord-api') |
| 15 | framework_path = os.path.join(cord_api_path, 'Framework') |
| 16 | utils_path = os.path.join(framework_path, 'utils') |
| 17 | sys.path.append(utils_path) |
| 18 | sys.path.append(framework_path) |
| 19 | |
| 20 | @classmethod |
| 21 | def getCredentials(cls): |
| 22 | onos_cfg = OnosCtrl.get_config() |
| 23 | if onos_cfg is None: |
| 24 | return None |
| 25 | if 'apps' in onos_cfg and \ |
| 26 | 'org.opencord.vtn' in onos_cfg['apps'] and \ |
| 27 | 'cordvtn' in onos_cfg['apps']['org.opencord.vtn'] and \ |
| 28 | 'xos' in onos_cfg['apps']['org.opencord.vtn']['cordvtn']: |
| 29 | xos_cfg = onos_cfg['apps']['org.opencord.vtn']['cordvtn']['xos'] |
| 30 | endpoint = xos_cfg['endpoint'] |
| 31 | user = xos_cfg['user'] |
| 32 | password = xos_cfg['password'] |
| 33 | xos_endpoints = endpoint.split(':') |
| 34 | xos_host = xos_endpoints[1][len('//'):] |
| 35 | xos_port = xos_endpoints[2][:-1] |
| 36 | #log.info('xos_host: %s, port: %s, user: %s, password: %s' %(xos_host, xos_port, user, password)) |
| 37 | return dict(host = xos_host, port = xos_port, user = user, password = password) |
| 38 | |
| 39 | return None |
| 40 | |
| 41 | @classmethod |
| 42 | def getRestApi(cls): |
| 43 | try: |
| 44 | from restApi import restApi |
| 45 | restApiXos = restApi() |
| 46 | xos_credentials = cls.getCredentials() |
| 47 | if xos_credentials is None: |
| 48 | restApiXos.controllerIP = cls.HEAD_NODE |
| 49 | restApiXos.controllerPort = cls.CONTROLLER_PORT |
| 50 | else: |
| 51 | restApiXos.controllerIP = xos_credentials['host'] |
| 52 | restApiXos.controllerPort = xos_credentials['port'] |
| 53 | restApiXos.user = xos_credentials['user'] |
| 54 | restApiXos.password = xos_credentials['password'] |
| 55 | |
| 56 | return restApiXos |
| 57 | except: |
| 58 | return None |
| 59 | |
| 60 | def __init__(self): |
| 61 | self.restApi = self.getRestApi() |
| 62 | |
| 63 | def subscriberCreate(self, subscriber_info, volt_subscriber_info): |
| 64 | subId = '' |
| 65 | try: |
| 66 | result = self.restApi.ApiPost('TENANT_SUBSCRIBER', subscriber_info) |
| 67 | assert_equal(result, True) |
| 68 | result = self.restApi.ApiGet('TENANT_SUBSCRIBER') |
| 69 | assert_not_equal(result, None) |
| 70 | subId = self.restApi.getSubscriberId(result, volt_subscriber_info['account_num']) |
| 71 | assert_not_equal(subId, '0') |
| 72 | log.info('Subscriber ID for account num %s = %s' %(str(volt_subscriber_info['account_num']), subId)) |
| 73 | volt_tenant = volt_subscriber_info['voltTenant'] |
| 74 | #update the subscriber id in the tenant info before making the rest |
| 75 | volt_tenant['subscriber'] = subId |
| 76 | result = self.restApi.ApiPost('TENANT_VOLT', volt_tenant) |
| 77 | assert_equal(result, True) |
| 78 | finally: |
| 79 | return subId |
| 80 | |
| 81 | def subscriberDelete(self, account_num, subId = '', voltId = ''): |
| 82 | if not subId: |
| 83 | #get the subscriber id first |
| 84 | result = self.restApi.ApiGet('TENANT_SUBSCRIBER') |
| 85 | assert_not_equal(result, None) |
| 86 | subId = self.restApi.getSubscriberId(result, account_num) |
| 87 | assert_not_equal(subId, '0') |
| 88 | if not voltId: |
| 89 | #get the volt id for the subscriber |
| 90 | result = self.restApi.ApiGet('TENANT_VOLT') |
| 91 | assert_not_equal(result, None) |
| 92 | voltId = CordSubscriberUtils.getVoltId(result, subId) |
| 93 | assert_not_equal(voltId, None) |
| 94 | log.info('Deleting subscriber ID %s for account num %s' %(subId, str(account_num))) |
| 95 | status = self.restApi.ApiDelete('TENANT_SUBSCRIBER', subId) |
| 96 | assert_equal(status, True) |
| 97 | #Delete the tenant |
| 98 | log.info('Deleting VOLT Tenant ID %s for subscriber %s' %(voltId, subId)) |
| 99 | self.restApi.ApiDelete('TENANT_VOLT', voltId) |
| 100 | |
| 101 | def subscriberId(self, account_num): |
| 102 | result = self.restApi.ApiGet('TENANT_SUBSCRIBER') |
| 103 | assert_not_equal(result, None) |
| 104 | subId = self.restApi.getSubscriberId(result, account_num) |
| 105 | return subId |
| 106 | |
| 107 | class CordSubscriberUtils(object): |
| 108 | |
| 109 | SUBSCRIBER_ACCOUNT_NUM = 100 |
| 110 | SUBSCRIBER_S_TAG = 500 |
| 111 | SUBSCRIBER_C_TAG = 500 |
| 112 | SUBSCRIBERS_PER_S_TAG = 8 |
| 113 | |
| 114 | def __init__(self, |
| 115 | num_subscribers, |
| 116 | account_num = SUBSCRIBER_ACCOUNT_NUM, |
| 117 | s_tag = SUBSCRIBER_S_TAG, |
| 118 | c_tag = SUBSCRIBER_C_TAG, |
| 119 | subscribers_per_s_tag = SUBSCRIBERS_PER_S_TAG): |
| 120 | self.num_subscribers = num_subscribers |
| 121 | self.account_num = account_num |
| 122 | self.s_tag = s_tag |
| 123 | self.c_tag = c_tag |
| 124 | self.subscribers_per_s_tag = subscribers_per_s_tag |
| 125 | self.subscriber_map = {} |
| 126 | self.subscriber_info = self.getConfig() |
| 127 | self.volt_subscriber_info = self.getVoltConfig() |
| 128 | self.xos = XosUtils() |
| 129 | |
| 130 | def getCredentials(self, subId): |
| 131 | """Generate our own account num, s_tag and c_tags""" |
| 132 | if subId in self.subscriber_map: |
| 133 | return self.subscriber_map[subId] |
| 134 | account_num = self.account_num |
| 135 | self.account_num += 1 |
| 136 | s_tag, c_tag = self.s_tag, self.c_tag |
| 137 | self.c_tag += 1 |
| 138 | if self.c_tag % self.subscribers_per_s_tag == 0: |
| 139 | self.s_tag += 1 |
| 140 | self.subscriber_map[subId] = account_num, s_tag, c_tag |
| 141 | return self.subscriber_map[subId] |
| 142 | |
| 143 | def getConfig(self): |
| 144 | features = { |
| 145 | 'cdn': True, |
| 146 | 'uplink_speed': 1000000000, |
| 147 | 'downlink_speed': 1000000000, |
| 148 | 'uverse': True, |
| 149 | 'status': 'enabled' |
| 150 | } |
| 151 | subscriber_map = [] |
| 152 | for i in xrange(self.num_subscribers): |
| 153 | subId = 'sub{}'.format(i) |
| 154 | account_num, _, _ = self.getCredentials(subId) |
| 155 | identity = { 'account_num' : str(account_num), |
| 156 | 'name' : 'My House {}'.format(i) |
| 157 | } |
| 158 | sub_info = { 'features' : features, |
| 159 | 'identity' : identity |
| 160 | } |
| 161 | subscriber_map.append(sub_info) |
| 162 | |
| 163 | return subscriber_map |
| 164 | |
| 165 | def getVoltConfig(self): |
| 166 | voltSubscriberMap = [] |
| 167 | for i in xrange(self.num_subscribers): |
| 168 | subId = 'sub{}'.format(i) |
| 169 | account_num, s_tag, c_tag = self.getCredentials(subId) |
| 170 | voltSubscriberInfo = {} |
| 171 | voltSubscriberInfo['voltTenant'] = dict(s_tag = str(s_tag), |
| 172 | c_tag = str(c_tag), |
| 173 | subscriber = '') |
| 174 | voltSubscriberInfo['account_num'] = account_num |
| 175 | voltSubscriberMap.append(voltSubscriberInfo) |
| 176 | |
| 177 | return voltSubscriberMap |
| 178 | |
| 179 | @classmethod |
| 180 | def getVoltId(cls, result, subId): |
| 181 | if type(result) is not type([]): |
| 182 | return None |
| 183 | for tenant in result: |
| 184 | if str(tenant['subscriber']) == str(subId): |
| 185 | return str(tenant['id']) |
| 186 | return None |
| 187 | |
| 188 | def subscriberCreate(self, index, subscriber_info = None, volt_subscriber_info = None): |
| 189 | if subscriber_info is None: |
| 190 | subscriber_info = self.subscriber_info[index] |
| 191 | if volt_subscriber_info is None: |
| 192 | volt_subscriber_info = self.volt_subscriber_info[index] |
| 193 | s_tag = int(volt_subscriber_info['voltTenant']['s_tag']) |
| 194 | c_tag = int(volt_subscriber_info['voltTenant']['c_tag']) |
| 195 | log.info('Creating tenant with s_tag: %d, c_tag: %d' %(s_tag, c_tag)) |
| 196 | subId = self.xos.subscriberCreate(subscriber_info, volt_subscriber_info) |
| 197 | return subId |
| 198 | |
| 199 | def subscriberDelete(self, index, subId = '', voltId = '', subscriber_info = None, volt_subscriber_info = None): |
| 200 | if subscriber_info is None: |
| 201 | subscriber_info = self.subscriber_info[index] |
| 202 | if volt_subscriber_info is None: |
| 203 | volt_subscriber_info = self.volt_subscriber_info[index] |
| 204 | s_tag = int(volt_subscriber_info['voltTenant']['s_tag']) |
| 205 | c_tag = int(volt_subscriber_info['voltTenant']['c_tag']) |
| 206 | log.info('Deleting tenant with s_tag: %d, c_tag: %d' %(s_tag, c_tag)) |
| 207 | self.xos.subscriberDelete(volt_subscriber_info['account_num'], subId = subId, voltId = voltId) |
| 208 | |
| 209 | def subscriberId(self, index): |
| 210 | volt_subscriber_info = self.volt_subscriber_info[index] |
| 211 | return self.xos.subscriberId(volt_subscriber_info['account_num']) |