blob: 44554974aac68eb6b7a68d5f175bded9db823a38 [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
128
129 @requests_mock.Mocker()
130 def test_sync(self, m):
131 with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
132 patch.object(BNGPortMapping.objects, "get_items") as bng_objects, \
133 patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
134
135 fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service)
136
137 si = self.mock_westbound(fsi, s_tag=111, switch_datapath_id = "of:0000000000000201", switch_port = 3)
138 serviceinstance_objects.return_value = [si]
139
140 bngmapping = BNGPortMapping(s_tag=111, switch_port=4)
141 bng_objects.return_value = [bngmapping]
142
143 desired_data = {"deviceId": "of:0000000000000201",
144 "vlanId": 111,
145 "ports": [3, 4]}
146
147 m.post("http://onos-fabric:8181/onos/segmentrouting/xconnect",
148 status_code=200,
149 additional_matcher=functools.partial(match_json, desired_data))
150
151 self.sync_step().sync_record(fsi)
152 self.assertTrue(m.called)
153
154 self.assertEqual(fsi.backend_handle, "111/of:0000000000000201")
155 fcsi_save.assert_called()
156
157 def test_sync_no_bng_mapping(self):
158 with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
159 patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
160
161 fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service)
162
163 si = self.mock_westbound(fsi, s_tag=111, switch_datapath_id = "of:0000000000000201", switch_port = 3)
164 serviceinstance_objects.return_value = [si]
165
166 with self.assertRaises(Exception) as e:
167 self.sync_step().sync_record(fsi)
168
169 self.assertEqual(e.exception.message, "Unable to determine BNG port for s_tag 111")
170
171 @requests_mock.Mocker()
172 def test_delete(self, m):
173 with patch.object(FabricCrossconnectServiceInstance.objects, "get_items") as fcsi_objects, \
174 patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
175 fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service,
176 backend_handle="111/of:0000000000000201",
177 enacted=True)
178
179 fcsi_objects.return_value=[fsi]
180
181 desired_data = {"deviceId": "of:0000000000000201",
182 "vlanId": 111}
183
184 m.delete("http://onos-fabric:8181/onos/segmentrouting/xconnect",
185 status_code=204,
186 additional_matcher=functools.partial(match_json, desired_data))
187
188 self.sync_step().delete_record(fsi)
189 self.assertTrue(m.called)
190
191 @requests_mock.Mocker()
192 def test_delete_in_use(self, m):
193 with patch.object(FabricCrossconnectServiceInstance.objects, "get_items") as fcsi_objects:
194 # The subscriber we want to delete
195 fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service,
196 backend_handle="111/of:0000000000000201",
197 enacted=True)
198
199 # Another subscriber using the same (s_tag, dpid) pair
200 fsi2 = FabricCrossconnectServiceInstance(id=7778, owner=self.service,
201 backend_handle="111/of:0000000000000201",
202 enacted=True)
203
204 fcsi_objects.return_value=[fsi, fsi2]
205
206 desired_data = {"deviceId": "of:0000000000000201",
207 "vlanId": 111}
208
209 m.delete("http://onos-fabric:8181/onos/segmentrouting/xconnect",
210 status_code=204,
211 additional_matcher=functools.partial(match_json, desired_data))
212
213 self.sync_step().delete_record(fsi)
214 self.assertFalse(m.called)
Scott Bakera6c687c2018-07-16 15:08:49 -0700215
216 def tearDown(self):
217 self.o = None
218 sys.path = self.sys_path_save
219
220if __name__ == '__main__':
221 unittest.main()