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