blob: 15bbd6027f57fc42632eb8895e9d0880d819a8c9 [file] [log] [blame]
Chip Boling67b674a2019-02-08 11:42:18 -06001#
2# Copyright 2017 the original author or authors.
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# import binascii
Zack Williams84a71e92019-11-15 09:00:19 -070017from __future__ import absolute_import
Chip Boling67b674a2019-02-08 11:42:18 -060018import structlog
Zack Williams84a71e92019-11-15 09:00:19 -070019import six
Chip Boling67b674a2019-02-08 11:42:18 -060020# from twisted.internet.defer import Deferred
21# from voltha.core.config.config_root import ConfigRoot
22# from pyvoltha.protos.voltha_pb2 import VolthaInstance
23# from pyvoltha.adapters.extensions.omci.omci_frame import OmciFrame
24
Chip Bolingce2daf62019-02-12 13:53:39 -060025
Chip Boling67b674a2019-02-08 11:42:18 -060026class MockProxyAddress(object):
27 def __init__(self, device_id, pon_id, onu_id):
28 self.device_id = device_id # Device ID of proxy (OLT)
29 self.onu_id = onu_id
30 self.onu_session_id = onu_id
31
32 self.channel_group_id = pon_id # close enough for mock
33 self.channel_id = pon_id
34 self.channel_termination = pon_id
35
36
37class MockDevice(object):
38 def __init__(self, device_id, proxy_address=None, serial_number=None):
39 from pyvoltha.adapters.extensions.omci.omci_entities import entity_id_to_class_map
40 self.id = device_id
41 self.parent_id = None
42 self.proxy_address = proxy_address
43 self.serial_number = serial_number
44 self.me_map = entity_id_to_class_map
45
46
47class MockCore(object):
48 def __init__(self):
49 self.root = None # ConfigRoot(VolthaInstance())
Chip Bolingce2daf62019-02-12 13:53:39 -060050 if self.root is None:
51 from nose import SkipTest
52 msg = "TODO: Unit tests involving the kv-store are not yet supported by pyvoltha"
53 raise SkipTest(msg)
Chip Boling67b674a2019-02-08 11:42:18 -060054
55 def get_proxy(self, path):
56 return self.root.get_proxy(path)
57
58
59class MockAdapterAgent(object):
60 """
61 Minimal class to handle adapter-agent needs in OpenOMCI. It can be
62 used by a mock OLT or ONU.
63
64 So that we do not have to duplicate the IAdapter functionality, just
65 the handler, the OLT and ONU handlers are derived from a mock Device
66 base class so that we can access the _devices map and get either a
67 Device to play with (like the real thing) or the associated handler
68 """
69 def __init__(self, d=None):
70 self.log = structlog.get_logger()
71 self._devices = dict() # device-id -> mock device
72 self.core = MockCore()
73 self.deferred = d
74 self.timeout_the_message = False
75
76 @property
77 def send_omci_defer(self):
78 return self.deferred
79
80 @send_omci_defer.setter
81 def send_omci_defer(self, value):
82 self.deferred = value
83
84 @property
85 def name(self):
86 return "cig_mock_ont"
87
88 def tearDown(self):
89 """Test case cleanup"""
Zack Williams84a71e92019-11-15 09:00:19 -070090 for device in six.itervalues(self._devices):
Chip Boling67b674a2019-02-08 11:42:18 -060091 device.tearDown()
92 self._devices.clear()
93
94 def add_device(self, device):
95 self._devices[device.id] = device
96
97 def add_child_device(self, parent_device, child_device):
98 # Set parent
99 child_device.parent_id = parent_device.id
100
101 # Add ONU serial number if PON and ONU enabled
102
103 if (child_device.enabled and
104 child_device.serial_number is not None and
105 child_device.proxy_address.channel_id in parent_device.enabled_pons):
106 parent_device.activated_onus.add(child_device.serial_number)
107
108 self.add_device(child_device)
109
110 def get_device(self, device_id):
111 return self._devices[device_id]
112
113 def get_child_device(self, parent_device_id, **kwargs):
114 onu_id = kwargs.pop('onu_id', None)
115 pon_id = kwargs.pop('pon_id', None)
116 if onu_id is None and pon_id is None:
117 return None
118
119 # Get all child devices with the same parent ID
Zack Williams84a71e92019-11-15 09:00:19 -0700120 children_ids = set(d.id for d in six.itervalues(self._devices)
Chip Boling67b674a2019-02-08 11:42:18 -0600121 if d.parent_id == parent_device_id)
122
123 # Loop through all the child devices with this parent ID
124 for child_id in children_ids:
125 device = self.get_device(child_id)
126
127 # Does this child device match the passed in ONU ID?
128 found_onu_id = False
129 if onu_id is not None:
130 if device.proxy_address.onu_id == onu_id:
131 found_onu_id = True
132
133 # Does this child device match the passed in SERIAL NUMBER?
134 found_pon_id = False
135 if pon_id is not None:
136 if device.proxy_address.channel_id == pon_id:
137 found_pon_id = True
138 # Match ONU ID and PON ID
139 if onu_id is not None and pon_id is not None:
140 found = found_onu_id & found_pon_id
141 # Otherwise ONU ID or PON ID
142 else:
143 found = found_onu_id | found_pon_id
144
145 # Return the matched child device
146 if found:
147 return device
148
149 return None
150
151 def send_proxied_message(self, proxy_address, msg):
152 # Look up ONU handler and forward the message
153 self.log.debug("--> send_proxied_message", message=msg)
154
155 # if proxy_address is None:
156 if self.deferred is not None and not self.timeout_the_message:
157 self.deferred.callback(msg)
158 # return None
159
160 # olt_handler = self.get_device(proxy_address.device_id)
161
162 # if olt_handler is not None:
163 # olt_handler.send_proxied_message(proxy_address, msg)
164
165 def receive_proxied_message(self, proxy_address, msg):
166 # Look up ONU handler and forward the message
167
168 onu_handler = self.get_child_device(proxy_address.device_id,
169 onu_id=proxy_address.onu_id,
170 pon_id=proxy_address.channel_id)
171 if onu_handler is not None:
172 onu_handler.receive_proxied_message(proxy_address, msg)