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 | |
Zack Williams | 84a71e9 | 2019-11-15 09:00:19 -0700 | [diff] [blame] | 17 | from __future__ import absolute_import |
| 18 | from .mock_adapter_agent import MockProxyAddress, MockDevice |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 19 | from pyvoltha.adapters.extensions.omci.omci_cc import * |
| 20 | from pyvoltha.adapters.extensions.omci.omci_entities import entity_id_to_class_map |
| 21 | |
| 22 | |
| 23 | class MockOnuHandler(MockDevice): |
| 24 | """ |
| 25 | Minimal class to handle ONU needs in OpenOMCI testing |
| 26 | |
| 27 | So that we do not have to duplicate the IAdapter functionality, just |
| 28 | the handler, the OLT and ONU handlers are derived from a mock Device |
| 29 | base class so that we can access the _devices map and get either a |
| 30 | Device to play with (like the real thing) or the associated handler |
| 31 | """ |
| 32 | def __init__(self, adapter_agent, parent_id, device_id, pon_id, onu_id): |
| 33 | |
| 34 | self.proxy_address = MockProxyAddress(parent_id, pon_id, onu_id) |
| 35 | super(MockOnuHandler, self).__init__(device_id, self.proxy_address) |
| 36 | |
| 37 | self.device_id = device_id |
| 38 | self.device = self |
| 39 | self._adapter_agent = adapter_agent |
| 40 | |
| 41 | self.onu_mock = None |
| 42 | self.omci_cc = OMCI_CC(adapter_agent, device_id, me_map=entity_id_to_class_map) |
| 43 | |
| 44 | # Items that you can change to perform various test failures |
| 45 | |
| 46 | self._enabled = True |
| 47 | |
| 48 | def tearDown(self): |
| 49 | """Test case cleanup""" |
| 50 | if self.onu_mock is not None: |
| 51 | self.onu_mock.tearDown() |
| 52 | self.onu_mock = None |
| 53 | |
| 54 | @property |
| 55 | def enabled(self): |
| 56 | return self._enabled |
| 57 | |
| 58 | @enabled.setter |
| 59 | def enabled(self, value): |
| 60 | if self._enabled != value: |
| 61 | self._enabled = value |
| 62 | olt = self._adapter_agent.get_device(self.proxy_address.device_id) |
| 63 | if olt is not None and self.proxy_address.channel_id in olt.enabled_pons: |
| 64 | if self._enabled: |
| 65 | olt.activated_onus.add(self.serial_number) |
| 66 | else: |
| 67 | olt.activated_onus.discard(self.serial_number) |
| 68 | |
| 69 | # Begin minimal set of needed IAdapter interfaces |
| 70 | |
| 71 | # TODO: Implement minimal functionality |
| 72 | |
| 73 | def send_proxied_message(self, proxy_address, msg): |
| 74 | assert False, 'OpenOMCI will implement this for the MOCK ONU' |
| 75 | |
| 76 | def receive_proxied_message(self, _, msg): |
| 77 | # Rx of OMCI message from MOCK OLT |
| 78 | |
| 79 | if self.omci_cc is not None and self.enabled: |
| 80 | self.omci_cc.receive_message(msg.decode('hex')) |