blob: a9978c813cbeab09a413335273f463996c1cc649 [file] [log] [blame]
Matteo Scandolo564009f2019-01-16 14:11:02 -08001# 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
15from tinydb import TinyDB, Query
16from robot.api import logger
17
18class devices(object):
19
20 def __init__(self):
21 self.olts = TinyDB('olts.json')
22 self.pon_ports = TinyDB('pon_ports.json')
23 self.onus = TinyDB('onus.json')
24 self.uni_ports = TinyDB('uni_ports.json')
25
26 def get_mock_data(self):
27 """
28 Get all the mock data,
29 this method is mostly intended for debugging
30 :return: a dictionary containing all the mocked data
31 """
32 olts = self.olts.all()
33 pon_ports = self.pon_ports.all()
34 onus = self.onus.all()
35 uni_ports = self.uni_ports.all()
36 return {
37 'olts': olts,
38 'pon_ports': pon_ports,
39 'onus': onus,
40 'uni_ports': uni_ports,
41 }
42
43 def clean_storage(self):
44 self.olts.purge()
45 self.pon_ports.purge()
46 self.onus.purge()
47 self.uni_ports.purge()
48
49###############################################################
50# OLT #
51###############################################################
52
53 def create_mock_olts(self, num_olts, voltservice_id):
54 """
55 :param num_olts: Number of OLTs to be created
56 :param voltservice_id: ID if the vOLT service
57 :return:
58 """
59 olts = []
60 for index in range(1, int(num_olts) + 1):
61 olt = {
62 'name': 'Test OLT%s' % index,
63 'volt_service_id': voltservice_id,
64 'device_type': 'fake_olt',
65 'host': '127.0.0.1',
66 'port': index,
67 'uplink': '65536',
68 'switch_datapath_id': 'of:0000000000000001',
69 'switch_port': str(index),
70 'of_id': 'of:000000%s' % index,
Matteo Scandolo6ccc9902019-03-01 13:07:35 -080071 'dp_id': 'of:000000%s' % index,
Matteo Scandolo564009f2019-01-16 14:11:02 -080072 }
73 # logger.info('Created OLT %s' % olt, also_console=True)
74 olts.append(olt)
75 self.olts.insert(olt)
76
77 return olts
78
79 def get_rest_olts(self):
80 """
81 Get all the OLTs that have been created for the test
82 formatted for the XOS Rest API
83 :return: a list of OLTs
84 """
85 return self.olts.all()
86
87 def update_olt_id(self, olt, id):
88 """
89 Update in the memory storage the XOS ID
90 of a particular OLT as it's needed to create PON Ports
91 :param olt: The OLT object to update
92 :param id: The ID returned from XOS
93 :return: None
94 """
95 Olt = Query()
96 self.olts.update({'id': id}, Olt.name == olt['name'])
97
98###############################################################
99# PON PORT #
100###############################################################
101
102 def create_mock_pon_ports(self, num_pon):
103
104 ports = []
105 for olt in self.olts.all():
Matteo Scandolo6ccc9902019-03-01 13:07:35 -0800106 for index in range(1, int(num_pon) + 1):
Matteo Scandolo564009f2019-01-16 14:11:02 -0800107 port = {
Matteo Scandolo6ccc9902019-03-01 13:07:35 -0800108 'name': 'Test PonPort %s Olt %s' % (index, olt['id']),
Matteo Scandolo564009f2019-01-16 14:11:02 -0800109 'port_no': index,
110 'olt_device_id': olt['id']
111 }
112 ports.append(port)
113 self.pon_ports.insert(port)
114 return ports
115
116 def get_rest_pon_ports(self):
117 """
118 Get all the PON Ports that have been created for the test
119 formatted for the XOS Rest API
120 :return: a list of PON Ports
121 """
122 return self.pon_ports.all();
123
124 def update_pon_port_id(self, pon_port, id):
125 """
126 Update in the memory storage the XOS ID
127 of a particular PON Port as it's needed to create ONUs
128 :param pon_port: The PON Port object to update
129 :param id: The ID returned from XOS
130 :return: None
131 """
132 PonPort = Query()
133 self.pon_ports.update({'id': id}, PonPort.name == pon_port['name'])
134
135###############################################################
136# ONU #
137###############################################################
138
139 def create_mock_onus(self, num_onus):
140 onus = []
141 j = 0
142 for port in self.pon_ports.all():
143 j = j + 1
144 for index in range(1, int(num_onus) + 1):
145 onu = {
146 'serial_number': "ROBOT%s%s" % (j, index),
147 'vendor': 'Robot',
148 'pon_port_id': port['id']
149 }
150 onus.append(onu)
151 self.onus.insert(onu)
152 return onus
153
154 def get_rest_onus(self):
155 return self.onus.all();
156
157 def update_onu_id(self, onu, id):
158 Onu = Query()
159 self.onus.update({'id': id}, Onu.serial_number == onu['serial_number'])
160
161###############################################################
162# UNI #
163###############################################################
164
165 def create_mock_unis(self):
166 # NOTE I believe UNI port number must be unique across OLT
167 unis = []
168 i = 0
169 for onu in self.onus.all():
170 uni = {
171 'name': 'Test UniPort %s' % i,
172 'port_no': i,
173 'onu_device_id': onu['id']
174 }
175 unis.append(uni)
176 self.uni_ports.insert(uni)
177 i = i+1
178 return unis
179
180 def get_rest_unis(self):
181 return self.uni_ports.all();
182
183 def update_uni_id(self, uni, id):
184 UniPort = Query()
185 self.uni_ports.update({'id': id}, UniPort.name == uni['name'])
186
187###############################################################
188# WHITELIST #
189###############################################################
190
191 def create_mock_whitelist(self, attworkflowservice_id):
192 entries = []
193 for onu in self.onus.all():
194 e = {
195 'owner_id': attworkflowservice_id,
196 'serial_number': onu['serial_number'],
197 'pon_port_id': self._find_pon_port_by_onu(onu)['port_no'],
198 'device_id': self._find_olt_by_onu(onu)['of_id']
199 }
200 entries.append(e)
201
202 return entries
203
204###############################################################
205# EVENTS #
206###############################################################
207
208 def generate_onu_events(self):
209 events = []
210 for onu in self.onus.all():
211 ev = {
212 'status': 'activated',
213 'serial_number': onu['serial_number'],
214 'uni_port_id': self._find_uni_by_onu(onu)['port_no'],
215 'of_dpid': self._find_olt_by_onu(onu)['of_id'],
216 }
217 events.append(ev)
218 return events
219
Matteo Scandolo6ccc9902019-03-01 13:07:35 -0800220 def generate_auth_events(self):
221 events = []
222 for onu in self.onus.all():
223 ev = {
224 'authenticationState': "APPROVED",
225 'deviceId': self._find_olt_by_onu(onu)['dp_id'],
226 'portNumber': self._find_uni_by_onu(onu)['port_no'],
227 }
228 events.append(ev)
229 return events
230
231 def generate_dhcp_events(self):
232 events = []
233 for onu in self.onus.all():
234 ev = {
235 'deviceId': self._find_olt_by_onu(onu)['dp_id'],
236 'portNumber': self._find_uni_by_onu(onu)['port_no'],
237 "macAddress": "aa:bb:cc:ee:ff",
238 "ipAddress": "10.10.10.10",
239 "messageType": "DHCPACK"
240 }
241 events.append(ev)
242 return events
243
Matteo Scandolo564009f2019-01-16 14:11:02 -0800244###############################################################
245# HELPERS #
246###############################################################
247
248 def _find_uni_by_onu(self, onu):
249 Uni = Query()
250 # NOTE there's an assumption that 1 ONU has 1 UNI Port
251 return self.uni_ports.search(Uni.onu_device_id == onu['id'])[0]
252
253 def _find_pon_port_by_onu(self, onu):
Matteo Scandolo6ccc9902019-03-01 13:07:35 -0800254 # this does not care about the olt id...
Matteo Scandolo564009f2019-01-16 14:11:02 -0800255 PonPort = Query()
256 return self.pon_ports.search(PonPort.id == onu['pon_port_id'])[0]
257
258 def _find_olt_by_onu(self, onu):
259 pon_port = self._find_pon_port_by_onu(onu)
260 Olt = Query()
261 return self.olts.search(Olt.id == pon_port['olt_device_id'])[0]