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