Matteo Scandolo | 564009f | 2019-01-16 14:11:02 -0800 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Zack Williams | 821c502 | 2020-01-15 15:11:46 -0700 | [diff] [blame^] | 15 | from __future__ import absolute_import |
| 16 | |
Matteo Scandolo | 564009f | 2019-01-16 14:11:02 -0800 | [diff] [blame] | 17 | from tinydb import TinyDB, Query |
Zack Williams | 821c502 | 2020-01-15 15:11:46 -0700 | [diff] [blame^] | 18 | # from robot.api import logger |
| 19 | |
Matteo Scandolo | 564009f | 2019-01-16 14:11:02 -0800 | [diff] [blame] | 20 | |
| 21 | class devices(object): |
| 22 | |
| 23 | def __init__(self): |
| 24 | self.olts = TinyDB('olts.json') |
| 25 | self.pon_ports = TinyDB('pon_ports.json') |
| 26 | self.onus = TinyDB('onus.json') |
| 27 | self.uni_ports = TinyDB('uni_ports.json') |
| 28 | |
| 29 | def get_mock_data(self): |
| 30 | """ |
| 31 | Get all the mock data, |
| 32 | this method is mostly intended for debugging |
| 33 | :return: a dictionary containing all the mocked data |
| 34 | """ |
| 35 | olts = self.olts.all() |
| 36 | pon_ports = self.pon_ports.all() |
| 37 | onus = self.onus.all() |
| 38 | uni_ports = self.uni_ports.all() |
| 39 | return { |
| 40 | 'olts': olts, |
| 41 | 'pon_ports': pon_ports, |
| 42 | 'onus': onus, |
| 43 | 'uni_ports': uni_ports, |
| 44 | } |
| 45 | |
| 46 | def clean_storage(self): |
| 47 | self.olts.purge() |
| 48 | self.pon_ports.purge() |
| 49 | self.onus.purge() |
| 50 | self.uni_ports.purge() |
| 51 | |
| 52 | ############################################################### |
| 53 | # OLT # |
| 54 | ############################################################### |
| 55 | |
| 56 | def create_mock_olts(self, num_olts, voltservice_id): |
| 57 | """ |
| 58 | :param num_olts: Number of OLTs to be created |
| 59 | :param voltservice_id: ID if the vOLT service |
| 60 | :return: |
| 61 | """ |
| 62 | olts = [] |
| 63 | for index in range(1, int(num_olts) + 1): |
| 64 | olt = { |
| 65 | 'name': 'Test OLT%s' % index, |
| 66 | 'volt_service_id': voltservice_id, |
| 67 | 'device_type': 'fake_olt', |
| 68 | 'host': '127.0.0.1', |
| 69 | 'port': index, |
| 70 | 'uplink': '65536', |
| 71 | 'switch_datapath_id': 'of:0000000000000001', |
| 72 | 'switch_port': str(index), |
| 73 | 'of_id': 'of:000000%s' % index, |
Matteo Scandolo | 6ccc990 | 2019-03-01 13:07:35 -0800 | [diff] [blame] | 74 | 'dp_id': 'of:000000%s' % index, |
Matteo Scandolo | 564009f | 2019-01-16 14:11:02 -0800 | [diff] [blame] | 75 | } |
| 76 | # logger.info('Created OLT %s' % olt, also_console=True) |
| 77 | olts.append(olt) |
| 78 | self.olts.insert(olt) |
| 79 | |
| 80 | return olts |
| 81 | |
| 82 | def get_rest_olts(self): |
| 83 | """ |
| 84 | Get all the OLTs that have been created for the test |
| 85 | formatted for the XOS Rest API |
| 86 | :return: a list of OLTs |
| 87 | """ |
| 88 | return self.olts.all() |
| 89 | |
| 90 | def update_olt_id(self, olt, id): |
| 91 | """ |
| 92 | Update in the memory storage the XOS ID |
| 93 | of a particular OLT as it's needed to create PON Ports |
| 94 | :param olt: The OLT object to update |
| 95 | :param id: The ID returned from XOS |
| 96 | :return: None |
| 97 | """ |
| 98 | Olt = Query() |
| 99 | self.olts.update({'id': id}, Olt.name == olt['name']) |
| 100 | |
| 101 | ############################################################### |
| 102 | # PON PORT # |
| 103 | ############################################################### |
| 104 | |
| 105 | def create_mock_pon_ports(self, num_pon): |
| 106 | |
| 107 | ports = [] |
| 108 | for olt in self.olts.all(): |
Matteo Scandolo | 6ccc990 | 2019-03-01 13:07:35 -0800 | [diff] [blame] | 109 | for index in range(1, int(num_pon) + 1): |
Matteo Scandolo | 564009f | 2019-01-16 14:11:02 -0800 | [diff] [blame] | 110 | port = { |
Matteo Scandolo | 6ccc990 | 2019-03-01 13:07:35 -0800 | [diff] [blame] | 111 | 'name': 'Test PonPort %s Olt %s' % (index, olt['id']), |
Matteo Scandolo | 564009f | 2019-01-16 14:11:02 -0800 | [diff] [blame] | 112 | 'port_no': index, |
| 113 | 'olt_device_id': olt['id'] |
| 114 | } |
| 115 | ports.append(port) |
| 116 | self.pon_ports.insert(port) |
| 117 | return ports |
| 118 | |
| 119 | def get_rest_pon_ports(self): |
| 120 | """ |
| 121 | Get all the PON Ports that have been created for the test |
| 122 | formatted for the XOS Rest API |
| 123 | :return: a list of PON Ports |
| 124 | """ |
Zack Williams | 821c502 | 2020-01-15 15:11:46 -0700 | [diff] [blame^] | 125 | return self.pon_ports.all() |
Matteo Scandolo | 564009f | 2019-01-16 14:11:02 -0800 | [diff] [blame] | 126 | |
| 127 | def update_pon_port_id(self, pon_port, id): |
| 128 | """ |
| 129 | Update in the memory storage the XOS ID |
| 130 | of a particular PON Port as it's needed to create ONUs |
| 131 | :param pon_port: The PON Port object to update |
| 132 | :param id: The ID returned from XOS |
| 133 | :return: None |
| 134 | """ |
| 135 | PonPort = Query() |
| 136 | self.pon_ports.update({'id': id}, PonPort.name == pon_port['name']) |
| 137 | |
| 138 | ############################################################### |
| 139 | # ONU # |
| 140 | ############################################################### |
| 141 | |
| 142 | def create_mock_onus(self, num_onus): |
| 143 | onus = [] |
| 144 | j = 0 |
| 145 | for port in self.pon_ports.all(): |
| 146 | j = j + 1 |
| 147 | for index in range(1, int(num_onus) + 1): |
| 148 | onu = { |
| 149 | 'serial_number': "ROBOT%s%s" % (j, index), |
| 150 | 'vendor': 'Robot', |
| 151 | 'pon_port_id': port['id'] |
| 152 | } |
| 153 | onus.append(onu) |
| 154 | self.onus.insert(onu) |
| 155 | return onus |
| 156 | |
| 157 | def get_rest_onus(self): |
Zack Williams | 821c502 | 2020-01-15 15:11:46 -0700 | [diff] [blame^] | 158 | return self.onus.all() |
Matteo Scandolo | 564009f | 2019-01-16 14:11:02 -0800 | [diff] [blame] | 159 | |
| 160 | def update_onu_id(self, onu, id): |
| 161 | Onu = Query() |
| 162 | self.onus.update({'id': id}, Onu.serial_number == onu['serial_number']) |
| 163 | |
| 164 | ############################################################### |
| 165 | # UNI # |
| 166 | ############################################################### |
| 167 | |
| 168 | def create_mock_unis(self): |
| 169 | # NOTE I believe UNI port number must be unique across OLT |
| 170 | unis = [] |
| 171 | i = 0 |
| 172 | for onu in self.onus.all(): |
| 173 | uni = { |
| 174 | 'name': 'Test UniPort %s' % i, |
| 175 | 'port_no': i, |
| 176 | 'onu_device_id': onu['id'] |
| 177 | } |
| 178 | unis.append(uni) |
| 179 | self.uni_ports.insert(uni) |
| 180 | i = i+1 |
| 181 | return unis |
| 182 | |
| 183 | def get_rest_unis(self): |
Zack Williams | 821c502 | 2020-01-15 15:11:46 -0700 | [diff] [blame^] | 184 | return self.uni_ports.all() |
Matteo Scandolo | 564009f | 2019-01-16 14:11:02 -0800 | [diff] [blame] | 185 | |
| 186 | def update_uni_id(self, uni, id): |
| 187 | UniPort = Query() |
| 188 | self.uni_ports.update({'id': id}, UniPort.name == uni['name']) |
| 189 | |
| 190 | ############################################################### |
| 191 | # WHITELIST # |
| 192 | ############################################################### |
| 193 | |
| 194 | def create_mock_whitelist(self, attworkflowservice_id): |
| 195 | entries = [] |
| 196 | for onu in self.onus.all(): |
| 197 | e = { |
| 198 | 'owner_id': attworkflowservice_id, |
| 199 | 'serial_number': onu['serial_number'], |
| 200 | 'pon_port_id': self._find_pon_port_by_onu(onu)['port_no'], |
| 201 | 'device_id': self._find_olt_by_onu(onu)['of_id'] |
| 202 | } |
| 203 | entries.append(e) |
| 204 | |
| 205 | return entries |
| 206 | |
| 207 | ############################################################### |
| 208 | # EVENTS # |
| 209 | ############################################################### |
| 210 | |
| 211 | def generate_onu_events(self): |
| 212 | events = [] |
| 213 | for onu in self.onus.all(): |
| 214 | ev = { |
| 215 | 'status': 'activated', |
Hardik Windlass | 3638c61 | 2019-03-30 20:46:02 +0530 | [diff] [blame] | 216 | 'serialNumber': onu['serial_number'], |
| 217 | 'portNumber': str(self._find_uni_by_onu(onu)['port_no']), |
| 218 | 'deviceId': self._find_olt_by_onu(onu)['of_id'], |
Matteo Scandolo | 564009f | 2019-01-16 14:11:02 -0800 | [diff] [blame] | 219 | } |
| 220 | events.append(ev) |
| 221 | return events |
| 222 | |
Matteo Scandolo | 6ccc990 | 2019-03-01 13:07:35 -0800 | [diff] [blame] | 223 | def generate_auth_events(self): |
| 224 | events = [] |
| 225 | for onu in self.onus.all(): |
| 226 | ev = { |
| 227 | 'authenticationState': "APPROVED", |
| 228 | 'deviceId': self._find_olt_by_onu(onu)['dp_id'], |
| 229 | 'portNumber': self._find_uni_by_onu(onu)['port_no'], |
| 230 | } |
| 231 | events.append(ev) |
| 232 | return events |
| 233 | |
| 234 | def generate_dhcp_events(self): |
| 235 | events = [] |
| 236 | for onu in self.onus.all(): |
| 237 | ev = { |
| 238 | 'deviceId': self._find_olt_by_onu(onu)['dp_id'], |
| 239 | 'portNumber': self._find_uni_by_onu(onu)['port_no'], |
| 240 | "macAddress": "aa:bb:cc:ee:ff", |
| 241 | "ipAddress": "10.10.10.10", |
| 242 | "messageType": "DHCPACK" |
| 243 | } |
| 244 | events.append(ev) |
| 245 | return events |
| 246 | |
Matteo Scandolo | 564009f | 2019-01-16 14:11:02 -0800 | [diff] [blame] | 247 | ############################################################### |
| 248 | # HELPERS # |
| 249 | ############################################################### |
| 250 | |
| 251 | def _find_uni_by_onu(self, onu): |
| 252 | Uni = Query() |
| 253 | # NOTE there's an assumption that 1 ONU has 1 UNI Port |
| 254 | return self.uni_ports.search(Uni.onu_device_id == onu['id'])[0] |
| 255 | |
| 256 | def _find_pon_port_by_onu(self, onu): |
Matteo Scandolo | 6ccc990 | 2019-03-01 13:07:35 -0800 | [diff] [blame] | 257 | # this does not care about the olt id... |
Matteo Scandolo | 564009f | 2019-01-16 14:11:02 -0800 | [diff] [blame] | 258 | PonPort = Query() |
| 259 | return self.pon_ports.search(PonPort.id == onu['pon_port_id'])[0] |
| 260 | |
| 261 | def _find_olt_by_onu(self, onu): |
| 262 | pon_port = self._find_pon_port_by_onu(onu) |
| 263 | Olt = Query() |
Hardik Windlass | 3638c61 | 2019-03-30 20:46:02 +0530 | [diff] [blame] | 264 | return self.olts.search(Olt.id == pon_port['olt_device_id'])[0] |