blob: a1b3e2388ed386f8d7fb0d34ef533e5373e2dc49 [file] [log] [blame]
Scott Bakera6c687c2018-07-16 15:08:49 -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
15import unittest
16
17import functools
Scott Baker547dea02018-07-18 15:24:26 -070018from mock import patch, call, Mock, PropertyMock, MagicMock
Scott Bakera6c687c2018-07-16 15:08:49 -070019import requests_mock
20import multistructlog
21from multistructlog import create_logger
22
23import os, sys
24
25# Hack to load synchronizer framework
26test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
27xos_dir=os.path.join(test_path, "../../..")
28if not os.path.exists(os.path.join(test_path, "new_base")):
29 xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
30 services_dir = os.path.join(xos_dir, "../../xos_services")
31sys.path.append(xos_dir)
32sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
33# END Hack to load synchronizer framework
34
35# generate model from xproto
36def get_models_fn(service_name, xproto_name):
37 name = os.path.join(service_name, "xos", xproto_name)
38 if os.path.exists(os.path.join(services_dir, name)):
39 return name
40 else:
41 name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
42 if os.path.exists(os.path.join(services_dir, name)):
43 return name
44 raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
45# END generate model from xproto
46
Scott Baker547dea02018-07-18 15:24:26 -070047def mock_get_westbound_service_instance_properties(props, prop):
48 return props[prop]
Scott Bakera6c687c2018-07-16 15:08:49 -070049
50def match_json(desired, req):
51 if desired!=req.json():
52 raise Exception("Got request %s, but body is not matching" % req.url)
53 return False
54 return True
55
Scott Baker547dea02018-07-18 15:24:26 -070056class TestSyncFabricCrossconnectServiceInstance(unittest.TestCase):
Scott Bakera6c687c2018-07-16 15:08:49 -070057
58 def setUp(self):
59 global DeferredException
60
61 self.sys_path_save = sys.path
62 sys.path.append(xos_dir)
63 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
64
65 # Setting up the config module
66 from xosconfig import Config
67 config = os.path.join(test_path, "../test_fabric_crossconnect_config.yaml")
68 Config.clear()
69 Config.init(config, "synchronizer-config-schema.yaml")
70 # END Setting up the config module
71
72 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
73 build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("fabric-crossconnect", "fabric-crossconnect.xproto")])
74 import synchronizers.new_base.modelaccessor
75
76 from sync_fabric_crossconnect_service_instance import SyncFabricCrossconnectServiceInstance, model_accessor, DeferredException
77
78 # import all class names to globals
79 for (k, v) in model_accessor.all_model_classes.items():
80 globals()[k] = v
81
Scott Bakera6c687c2018-07-16 15:08:49 -070082 self.sync_step = SyncFabricCrossconnectServiceInstance
83 self.sync_step.log = Mock()
84
Scott Baker547dea02018-07-18 15:24:26 -070085 # mock onos-fabric
86 self.onos_fabric = Service(name = "onos-fabric",
87 rest_hostname = "onos-fabric",
88 rest_port = "8181",
89 rest_username = "onos",
90 rest_password = "rocks")
Scott Bakera6c687c2018-07-16 15:08:49 -070091
Scott Baker547dea02018-07-18 15:24:26 -070092 self.service = FabricCrossconnectService(name = "fcservice",
93 provider_services = [self.onos_fabric])
94
95 def mock_westbound(self, fsi, s_tag, switch_datapath_id, switch_port):
96 # Mock out a ServiceInstance so the syncstep can call get_westbound_service_instance_properties on it
97 si = ServiceInstance(id=fsi.id)
98 si.get_westbound_service_instance_properties = functools.partial(
99 mock_get_westbound_service_instance_properties,
100 {"s_tag": s_tag,
101 "switch_datapath_id": switch_datapath_id,
102 "switch_port": switch_port})
103 return si
104
105 def test_format_url(self):
106 url = self.sync_step().format_url("foo.com/bar")
107 self.assertEqual(url, "http://foo.com/bar")
108
109 url = self.sync_step().format_url("http://foo.com/bar")
110 self.assertEqual(url, "http://foo.com/bar")
111
112 def test_make_handle_extract_handle(self):
113 h = self.sync_step().make_handle(222, "of:0000000000000201")
114 (s_tag, dpid) = self.sync_step().extract_handle(h)
115
116 self.assertEqual(s_tag, 222)
117 self.assertEqual(dpid, "of:0000000000000201")
118
119 def test_get_fabric_onos_init(self):
120 fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service)
121
122 d = self.sync_step().get_fabric_onos_info(fsi)
123
124 self.assertEqual(d["url"], "http://onos-fabric:8181")
125 self.assertEqual(d["user"], "onos")
126 self.assertEqual(d["pass"], "rocks")
127
Scott Bakerd443ea72018-08-07 13:50:06 -0700128 def test_range_matches_single(self):
129 self.assertTrue(self.sync_step().range_matches(123, "123"))
130
131 def test_range_matches_single_incorrect(self):
132 self.assertFalse(self.sync_step().range_matches(123, "456"))
133
134 def test_range_matches_range(self):
135 self.assertTrue(self.sync_step().range_matches(123, "122-124"))
136
137 def test_range_matches_range_incorrect(self):
138 self.assertFalse(self.sync_step().range_matches(123, "110-113"))
139
140 def test_range_matches_any(self):
141 self.assertTrue(self.sync_step().range_matches(123, "ANY"))
142 self.assertTrue(self.sync_step().range_matches(123, "any"))
143
144 def test_find_bng_single(self):
145 with patch.object(BNGPortMapping.objects, "get_items") as bng_objects, \
146 patch.object(self.sync_step, "range_matches") as range_matches:
147 bngmapping = BNGPortMapping(s_tag="111", switch_port=4)
148 bng_objects.return_value = [bngmapping]
149
150 # this should not be called
151 range_matches.return_value = False
152
153 found_bng = self.sync_step().find_bng(111)
154 self.assertTrue(found_bng)
155 self.assertEqual(found_bng.switch_port, 4)
156
157 range_matches.assert_not_called()
158
159 def test_find_bng_any(self):
160 with patch.object(BNGPortMapping.objects, "get_items") as bng_objects:
161 bngmapping = BNGPortMapping(s_tag="ANY", switch_port=4)
162 bng_objects.return_value = [bngmapping]
163
164 found_bng = self.sync_step().find_bng(111)
165 self.assertTrue(found_bng)
166 self.assertEqual(found_bng.switch_port, 4)
167
168 def test_find_bng_range(self):
169 with patch.object(BNGPortMapping.objects, "get_items") as bng_objects:
170 bngmapping = BNGPortMapping(s_tag="100-200", switch_port=4)
171 bng_objects.return_value = [bngmapping]
172
173 found_bng = self.sync_step().find_bng(111)
174 self.assertTrue(found_bng)
175 self.assertEqual(found_bng.switch_port, 4)
Scott Baker547dea02018-07-18 15:24:26 -0700176
177 @requests_mock.Mocker()
178 def test_sync(self, m):
179 with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
180 patch.object(BNGPortMapping.objects, "get_items") as bng_objects, \
181 patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
182
183 fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service)
184
185 si = self.mock_westbound(fsi, s_tag=111, switch_datapath_id = "of:0000000000000201", switch_port = 3)
186 serviceinstance_objects.return_value = [si]
187
Scott Bakerd443ea72018-08-07 13:50:06 -0700188 bngmapping = BNGPortMapping(s_tag="111", switch_port=4)
Scott Baker547dea02018-07-18 15:24:26 -0700189 bng_objects.return_value = [bngmapping]
190
191 desired_data = {"deviceId": "of:0000000000000201",
192 "vlanId": 111,
193 "ports": [3, 4]}
194
195 m.post("http://onos-fabric:8181/onos/segmentrouting/xconnect",
196 status_code=200,
197 additional_matcher=functools.partial(match_json, desired_data))
198
199 self.sync_step().sync_record(fsi)
200 self.assertTrue(m.called)
201
202 self.assertEqual(fsi.backend_handle, "111/of:0000000000000201")
203 fcsi_save.assert_called()
204
205 def test_sync_no_bng_mapping(self):
206 with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
207 patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
208
209 fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service)
210
Scott Bakerd443ea72018-08-07 13:50:06 -0700211 si = self.mock_westbound(fsi, s_tag="111", switch_datapath_id = "of:0000000000000201", switch_port = 3)
Scott Baker547dea02018-07-18 15:24:26 -0700212 serviceinstance_objects.return_value = [si]
213
214 with self.assertRaises(Exception) as e:
215 self.sync_step().sync_record(fsi)
216
217 self.assertEqual(e.exception.message, "Unable to determine BNG port for s_tag 111")
218
219 @requests_mock.Mocker()
220 def test_delete(self, m):
221 with patch.object(FabricCrossconnectServiceInstance.objects, "get_items") as fcsi_objects, \
222 patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
223 fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service,
224 backend_handle="111/of:0000000000000201",
225 enacted=True)
226
227 fcsi_objects.return_value=[fsi]
228
229 desired_data = {"deviceId": "of:0000000000000201",
230 "vlanId": 111}
231
232 m.delete("http://onos-fabric:8181/onos/segmentrouting/xconnect",
233 status_code=204,
234 additional_matcher=functools.partial(match_json, desired_data))
235
236 self.sync_step().delete_record(fsi)
237 self.assertTrue(m.called)
238
239 @requests_mock.Mocker()
240 def test_delete_in_use(self, m):
241 with patch.object(FabricCrossconnectServiceInstance.objects, "get_items") as fcsi_objects:
242 # The subscriber we want to delete
243 fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service,
244 backend_handle="111/of:0000000000000201",
245 enacted=True)
246
247 # Another subscriber using the same (s_tag, dpid) pair
248 fsi2 = FabricCrossconnectServiceInstance(id=7778, owner=self.service,
249 backend_handle="111/of:0000000000000201",
250 enacted=True)
251
252 fcsi_objects.return_value=[fsi, fsi2]
253
254 desired_data = {"deviceId": "of:0000000000000201",
255 "vlanId": 111}
256
257 m.delete("http://onos-fabric:8181/onos/segmentrouting/xconnect",
258 status_code=204,
259 additional_matcher=functools.partial(match_json, desired_data))
260
261 self.sync_step().delete_record(fsi)
262 self.assertFalse(m.called)
Scott Bakera6c687c2018-07-16 15:08:49 -0700263
264 def tearDown(self):
265 self.o = None
266 sys.path = self.sys_path_save
267
268if __name__ == '__main__':
269 unittest.main()