blob: af5e50971be2015ac4388e6a6ba21b572d71fd8d [file] [log] [blame]
Scott Baker91bf91b2019-03-15 16:13:51 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Scott Bakere9855012019-04-01 15:01:34 -070015#from __future__ import absolute_import
16
Scott Bakercabc3de2019-04-03 17:23:08 -070017import datetime
Scott Bakere9855012019-04-01 15:01:34 -070018import imp
Scott Baker91bf91b2019-03-15 16:13:51 -070019import unittest
20import json
Scott Bakercabc3de2019-04-03 17:23:08 -070021import time
22from mock import patch, Mock, MagicMock, ANY
Scott Baker91bf91b2019-03-15 16:13:51 -070023
24import os
25import sys
26
27test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
28
29
30class TestOnosPortEvent(unittest.TestCase):
31
32 def setUp(self):
33 global DeferredException
34
35 self.sys_path_save = sys.path
36
37 # Setting up the config module
38 from xosconfig import Config
39 config = os.path.join(test_path, "../test_config.yaml")
40 Config.clear()
41 Config.init(config, "synchronizer-config-schema.yaml")
42 # END Setting up the config module
43
Scott Bakercabc3de2019-04-03 17:23:08 -070044 # Mock the kafka producer
45 self.mockxoskafka = MagicMock()
46 modules = {
47 'xoskafka': self.mockxoskafka,
48 'xoskafka.XOSKafkaProducer': self.mockxoskafka.XOSKafkaProducer,
49 }
50 self.module_patcher = patch.dict('sys.modules', modules)
51 self.module_patcher.start()
52
Scott Baker91bf91b2019-03-15 16:13:51 -070053 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
54 mock_modelaccessor_config(test_path, [("fabric", "fabric.xproto"),
55 ("onos-service", "onos.xproto")])
56
57 import xossynchronizer.modelaccessor
58 import mock_modelaccessor
Scott Bakere9855012019-04-01 15:01:34 -070059 imp.reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
60 imp.reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
Scott Baker91bf91b2019-03-15 16:13:51 -070061
62 from xossynchronizer.modelaccessor import model_accessor
63 self.model_accessor = model_accessor
64
65 from mock_modelaccessor import MockObjectList
Scott Bakercabc3de2019-04-03 17:23:08 -070066
67 # necessary to reset XOSKafkaProducer's call_count
68 import onos_event
69 reload(onos_event)
70
71 from onos_event import OnosPortEventStep, XOSKafkaProducer
72 self.XOSKafkaProducer = XOSKafkaProducer
Scott Baker91bf91b2019-03-15 16:13:51 -070073
74 # import all class names to globals
75 for (k, v) in model_accessor.all_model_classes.items():
76 globals()[k] = v
77
78 self.event_step = OnosPortEventStep
79
80 self.fabric_service = FabricService(name="fabric",
81 id=1112,
82 backend_code=1,
83 backend_status="succeeded")
84
85 self.switch = Switch(name="switch1",
86 ofId="of:0000000000000001",
87 backend_code=1,
88 backend_status="succeeded")
89
90 self.port1 = SwitchPort(name="switch1port1",
91 switch=self.switch,
92 switch_id=self.switch.id,
93 portId="1",
94 oper_status=None,
95 backend_code=1,
96 backend_status="succeeded")
97
98 self.port2 = SwitchPort(name="switch1port2",
Scott Bakercabc3de2019-04-03 17:23:08 -070099 kind="access",
Scott Baker91bf91b2019-03-15 16:13:51 -0700100 switch=self.switch,
101 switch_id=self.switch.id,
102 portId="2",
103 oper_status=None,
104 backend_code=1,
105 backend_status="succeeded")
106
107 self.switch.ports = MockObjectList([self.port1, self.port2])
108
109 self.log = Mock()
110
111 def tearDown(self):
112 sys.path = self.sys_path_save
113
114 def test_process_event_enable(self):
115 with patch.object(Switch.objects, "get_items") as switch_objects, \
Scott Bakere9855012019-04-01 15:01:34 -0700116 patch.object(SwitchPort.objects, "get_items") as switchport_objects:
Scott Baker91bf91b2019-03-15 16:13:51 -0700117 switch_objects.return_value = [self.switch]
118 switchport_objects.return_value = [self.port1, self.port2]
119
Scott Bakercabc3de2019-04-03 17:23:08 -0700120 event_dict = {"timestamp":"2019-03-21T18:00:26.613Z",
121 "deviceId": self.switch.ofId,
Scott Baker91bf91b2019-03-15 16:13:51 -0700122 "portId": self.port1.portId,
123 "enabled": True}
124 event = Mock()
125 event.value = json.dumps(event_dict)
126
127 step = self.event_step(model_accessor=self.model_accessor, log=self.log)
128 step.process_event(event)
129
130 self.assertEqual(self.port1.oper_status, "enabled")
131
132 def test_process_event_disable(self):
133 with patch.object(Switch.objects, "get_items") as switch_objects, \
Scott Bakere9855012019-04-01 15:01:34 -0700134 patch.object(SwitchPort.objects, "get_items") as switchport_objects:
Scott Baker91bf91b2019-03-15 16:13:51 -0700135 switch_objects.return_value = [self.switch]
136 switchport_objects.return_value = [self.port1, self.port2]
137
Scott Bakercabc3de2019-04-03 17:23:08 -0700138 event_dict = {"timestamp":"2019-03-21T18:00:26.613Z",
139 "deviceId": self.switch.ofId,
Scott Baker91bf91b2019-03-15 16:13:51 -0700140 "portId": self.port1.portId,
141 "enabled": False}
142 event = Mock()
143 event.value = json.dumps(event_dict)
144
145 step = self.event_step(model_accessor=self.model_accessor, log=self.log)
146 step.process_event(event)
147
148 self.assertEqual(self.port1.oper_status, "disabled")
149
150 def test_process_event_no_switch(self):
151 with patch.object(Switch.objects, "get_items") as switch_objects, \
Scott Bakere9855012019-04-01 15:01:34 -0700152 patch.object(SwitchPort.objects, "get_items") as switchport_objects:
Scott Baker91bf91b2019-03-15 16:13:51 -0700153 switch_objects.return_value = [self.switch]
154 switchport_objects.return_value = [self.port1, self.port2]
155
Scott Bakercabc3de2019-04-03 17:23:08 -0700156 event_dict = {"timestamp":"2019-03-21T18:00:26.613Z",
157 "deviceId": "doesnotexist",
Scott Baker91bf91b2019-03-15 16:13:51 -0700158 "portId": self.port1.portId,
159 "enabled": True}
160 event = Mock()
161 event.value = json.dumps(event_dict)
162
163 step = self.event_step(model_accessor=self.model_accessor, log=self.log)
164
165 step.process_event(event)
166
167 # should not have changed
168 self.assertEqual(self.port1.oper_status, None)
169
170 def test_process_event_no_port(self):
171 with patch.object(Switch.objects, "get_items") as switch_objects, \
Scott Bakere9855012019-04-01 15:01:34 -0700172 patch.object(SwitchPort.objects, "get_items") as switchport_objects:
Scott Baker91bf91b2019-03-15 16:13:51 -0700173 switch_objects.return_value = [self.switch]
174 switchport_objects.return_value = [self.port1, self.port2]
175
Scott Bakercabc3de2019-04-03 17:23:08 -0700176 event_dict = {"timestamp":"2019-03-21T18:00:26.613Z",
177 "deviceId": self.switch.ofId,
Scott Baker91bf91b2019-03-15 16:13:51 -0700178 "portId": "doesnotexist",
179 "enabled": True}
180 event = Mock()
181 event.value = json.dumps(event_dict)
182
183 step = self.event_step(model_accessor=self.model_accessor, log=self.log)
184
185 step.process_event(event)
186
187 # should not have changed
188 self.assertEqual(self.port1.oper_status, None)
189
Scott Bakercabc3de2019-04-03 17:23:08 -0700190 def test_send_alarm(self):
191 self.port2.oper_status = "disabled"
192 value = {"timestamp":"2019-03-21T18:00:26.613Z",
193 "deviceId":"of:0000000000000001",
194 "portId":"2",
195 "enabled":False,
196 "speed":10000,
197 "type":"COPPER"}
198
199 step = self.event_step(model_accessor=self.model_accessor, log=self.log)
200 step.send_alarm(self.switch, self.port2, value)
201
202 self.assertEqual(self.XOSKafkaProducer.produce.call_count, 1)
203 topic = self.XOSKafkaProducer.produce.call_args[0][0]
204 key = self.XOSKafkaProducer.produce.call_args[0][1]
205 event = json.loads(self.XOSKafkaProducer.produce.call_args[0][2])
206
207 self.assertEqual(topic, "xos.alarms.fabric-service")
208 self.assertEqual(key, "of:0000000000000001:2")
209
210 raised_ts = time.mktime(datetime.datetime.strptime(value["timestamp"], "%Y-%m-%dT%H:%M:%S.%fZ").timetuple())
211
212 self.maxDiff = None
213
214 expected_alarm = {
215 u"category": u"SWITCH",
216 u"reported_ts": ANY,
217 u"raised_ts": raised_ts,
218 u"state": u"RAISED",
219 u"alarm_type_name": u"SWITCH.PORT_LOS",
220 u"severity": u"MAJOR",
221 u"resource_id": unicode(self.switch.ofId),
222 u"context": {u"portId": u"2", u"portKind": u"access",
223 u'switch.name': u'switch1'},
224 u"type": u"COMMUNICATION",
225 u"id": u"xos.fabricservice.%s.SWITCH_PORT_LOS" % self.switch.ofId,
226 u"description": u"xos.fabricservice.%s - SWITCH PORT LOS Alarm - SWITCH_PORT_LOS - RAISED" % self.switch.ofId}
227
228 self.assertDictEqual(expected_alarm, event)
229
230 def test_clear_alarm(self):
231 self.port2.oper_status = "enabled"
232 value = {"timestamp":"2019-03-21T18:00:26.613Z",
233 "deviceId":"of:0000000000000001",
234 "portId":"2",
235 "enabled":False,
236 "speed":10000,
237 "type":"COPPER"}
238
239 step = self.event_step(model_accessor=self.model_accessor, log=self.log)
240 step.send_alarm(self.switch, self.port2, value)
241
242 self.assertEqual(self.XOSKafkaProducer.produce.call_count, 1)
243 topic = self.XOSKafkaProducer.produce.call_args[0][0]
244 key = self.XOSKafkaProducer.produce.call_args[0][1]
245 event = json.loads(self.XOSKafkaProducer.produce.call_args[0][2])
246
247 self.assertEqual(topic, "xos.alarms.fabric-service")
248 self.assertEqual(key, "of:0000000000000001:2")
249
250 raised_ts = time.mktime(datetime.datetime.strptime(value["timestamp"], "%Y-%m-%dT%H:%M:%S.%fZ").timetuple())
251
252 self.maxDiff = None
253
254 expected_alarm = {
255 u"category": u"SWITCH",
256 u"reported_ts": ANY,
257 u"raised_ts": raised_ts,
258 u"state": u"CLEARED",
259 u"alarm_type_name": u"SWITCH.PORT_LOS",
260 u"severity": u"MAJOR",
261 u"resource_id": unicode(self.switch.ofId),
262 u"context": {u"portId": u"2", u"portKind": u"access",
263 u'switch.name': u'switch1'},
264 u"type": u"COMMUNICATION",
265 u"id": u"xos.fabricservice.%s.SWITCH_PORT_LOS" % self.switch.ofId,
266 u"description": u"xos.fabricservice.%s - SWITCH PORT LOS Alarm - SWITCH_PORT_LOS - CLEARED" % self.switch.ofId}
267
268 self.assertDictEqual(expected_alarm, event)
269
Scott Baker91bf91b2019-03-15 16:13:51 -0700270
271if __name__ == '__main__':
272 unittest.main()