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