blob: 142dbd81bcccd2ab4165806124bb123a4fe34644 [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
17import sys
18from mock_adapter_agent import MockDevice
19from nose.twistedtools import reactor
20
21
22class MockOltHandler(MockDevice):
23 """
24 VERY Minimal class to handle OLT needs in OpenOMCI testing
25
26 So that we do not have to duplicate the IAdapter functionality, just
27 the handler, the OLT and ONU handlers are derived from a mock Device
28 base class so that we can access the _devices map and get either a
29 Device to play with (like the real thing) or the associated handler
30 """
31 def __init__(self, adapter_agent, device_id):
32 super(MockOltHandler, self).__init__(device_id)
33
34 self.device_id = device_id
35 self.device = self
36 self._adapter_agent = adapter_agent
37 self._num_tx = 0
38
39 ####################################################################
40 # NOTE: The following can be manipulated in your test case to modify the behaviour
41 # of this mock.
42 #
43 # Note that activated ONUs are added during adapter add_child_device
44 # if the ONU handler associated is 'enabled'
45
46 self.enabled = True # OLT is enabled/active
47 self.activated_onus = set() # Activated ONU serial numbers
48 self.enabled_pons = range(0, 16) # Enabled PONs
49 self.max_tx = sys.maxint # Fail after this many tx requests
50 self.latency = 0.0 # OMCI response latency (keep small)
51
52 # TODO: Implement minimal functionality
53
54 # TODO: Implement minimal functionality
55
56 def tearDown(self):
57 """Test case cleanup"""
58 pass
59
60 # Begin minimal set of needed IAdapter interfaces
61
62 def send_proxied_message(self, proxy_address, msg):
63 """Check various enabled flags and status and send if okay"""
64
65 if not self.enabled:
66 return None
67
68 pon_id = proxy_address.channel_id
69
70 if pon_id not in self.enabled_pons:
71 return None
72
73 # Look up ONU device ID.
74 onu_id = proxy_address.onu_id
75 onu_handler = self._adapter_agent.get_child_device(proxy_address.device_id,
76 pon_id=pon_id,
77 onu_id=onu_id)
78
79 if onu_handler is None or not onu_handler.enabled:
80 return None
81
82 onu_mock = onu_handler.onu_mock
83 if onu_mock is None or onu_mock.serial_number not in self.activated_onus:
84 return None
85
86 # And Tx success (silent discard for OMCI timeout testing)
87 if self._num_tx >= self.max_tx:
88 return None
89 self._num_tx += 1
90
91 response = onu_mock.rx_omci_frame(msg)
92
93 # Make async and add any requested latency. Bound it to less
94 # than 5 seconds since this is a unit test that need to be
95 # somewhat responsive
96
97 assert 0.0 <= self.latency <= 5, 'Best practice is latency <= 5 seconds'
98 if response is not None:
99 reactor.callLater(self.latency, self._deliver_proxy_message, proxy_address, response)
100
101 def _deliver_proxy_message(self, proxy_address, response):
102 from common.frameio.frameio import hexify
103 self._adapter_agent.receive_proxied_message(proxy_address,
104 hexify(str(response)))
105
106 def receive_proxied_message(self, _, __):
107 assert False, 'This is never called on the OLT side of proxy messaging'
108