blob: cd4906f6cd14a0bd80259cc498ba1c5cda602057 [file] [log] [blame]
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -08001# 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
16from mock import patch, call, Mock, PropertyMock
17import requests_mock
18
19import os, sys
20
21# Hack to load synchronizer framework
22test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
23xos_dir=os.path.join(test_path, "../../..")
24if not os.path.exists(os.path.join(test_path, "new_base")):
25 xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070026 services_dir = os.path.join(xos_dir, "../../xos_services")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080027sys.path.append(xos_dir)
28sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
29# END Hack to load synchronizer framework
30
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070031# generate model from xproto
32def get_models_fn(service_name, xproto_name):
33 name = os.path.join(service_name, "xos", xproto_name)
34 if os.path.exists(os.path.join(services_dir, name)):
35 return name
36 else:
37 name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
38 if os.path.exists(os.path.join(services_dir, name)):
39 return name
40 raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
41# END generate model from xproto
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080042
Matteo Scandolob8621cd2018-04-04 17:12:37 -070043def match_onos_req(req):
44 r = req.json()['devices']
45 if not r['abc']:
46 return False
47 else:
48 if not r['abc']['basic']['driver'] == 'pmc-olt':
49 return False
50 if not r['abc']['accessDevice']['vlan'] == "s_tag" or not r['abc']['accessDevice']['uplink'] == "129":
51 return False
52 return True
53
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080054class TestSyncOLTDevice(unittest.TestCase):
55
56 def setUp(self):
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070057
58 self.sys_path_save = sys.path
59 sys.path.append(xos_dir)
60 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
61
62 # Setting up the config module
63 from xosconfig import Config
64 config = os.path.join(test_path, "../model_policies/test_config.yaml")
65 Config.clear()
66 Config.init(config, "synchronizer-config-schema.yaml")
67 # END Setting up the config module
68
69 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
70 build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")])
71 import synchronizers.new_base.modelaccessor
72 from sync_olt_device import SyncOLTDevice
73 self.sync_step = SyncOLTDevice
74
Matteo Scandolob8621cd2018-04-04 17:12:37 -070075 pon_port = Mock()
76 pon_port.port_id = "00ff00"
77 pon_port.s_tag = "s_tag"
78
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080079 # create a mock service instance
80 o = Mock()
81 o.volt_service.voltha_url = "voltha_url"
82 o.volt_service.voltha_user = "voltha_user"
83 o.volt_service.voltha_pass = "voltha_pass"
84 o.volt_service.p_onos_url = "p_onos_url"
85 o.volt_service.p_onos_user = "p_onos_user"
86 o.volt_service.p_onos_pass = "p_onos_pass"
87
88 o.device_type = "ponsim_olt"
89 o.host = "172.17.0.1"
90 o.port = "50060"
91 o.uplink = "129"
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080092 o.driver = "pmc-olt"
93
Matteo Scandolob8621cd2018-04-04 17:12:37 -070094 # feedback state
95 o.device_id = None
96 o.admin_state = None
97 o.oper_status = None
98 o.of_id = None
99
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800100 o.tologdict.return_value = {'name': "Mock VOLTServiceInstance"}
101
102 o.save.return_value = "Saved"
103
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700104 o.ports.all.return_value = [pon_port]
105
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800106 self.o = o
107
108 def tearDown(self):
109 self.o = None
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700110 sys.path = self.sys_path_save
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800111
112 def test_format_url(self):
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700113 url = self.sync_step.format_url("onf.com")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800114 self.assertEqual(url, "http://onf.com")
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700115 url = self.sync_step.format_url("http://onf.com")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800116 self.assertEqual(url, "http://onf.com")
117
118 def test_get_voltha_info(self):
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700119 voltha_dict = self.sync_step.get_voltha_info(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800120
121 self.assertEqual(voltha_dict["url"], "http://voltha_url")
122 self.assertEqual(voltha_dict["user"], "voltha_user")
123 self.assertEqual(voltha_dict["pass"], "voltha_pass")
124
125 def test_get_onos_info(self):
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700126 p_onos_dict = self.sync_step.get_p_onos_info(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800127
128 self.assertEqual(p_onos_dict["url"], "http://p_onos_url")
129 self.assertEqual(p_onos_dict["user"], "p_onos_user")
130 self.assertEqual(p_onos_dict["pass"], "p_onos_pass")
131
132 @requests_mock.Mocker()
133 def test_get_of_id_from_device(self, m):
134 logical_devices = {
135 "items": [
136 {"root_device_id": "123", "id": "abc"},
137 {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"}
138 ]
139 }
140 m.get("http://voltha_url/api/v1/logical_devices", status_code=200, json=logical_devices)
141 self.o.device_id = "123"
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700142 of_id = self.sync_step.get_of_id_from_device(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800143 self.assertEqual(of_id, "abc")
144
145 with self.assertRaises(Exception) as e:
146 self.o.device_id = "idonotexist"
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700147 self.sync_step.get_of_id_from_device(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800148 self.assertEqual(e.exception.message, "Can't find a logical device for device id: idonotexist")
149
150 @requests_mock.Mocker()
151 def test_sync_record_fail_add(self, m):
152 """
153 Should print an error if we can't add the device in VOLTHA
154 """
155 m.post("http://voltha_url/api/v1/devices", status_code=500, text="MockError")
156
157 with self.assertRaises(Exception) as e:
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700158 self.sync_step().sync_record(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800159 self.assertEqual(e.exception.message, "Failed to add device: MockError")
160
161 @requests_mock.Mocker()
162 def test_sync_record_fail_no_id(self, m):
163 """
164 Should print an error if VOLTHA does not return the device id
165 """
166 m.post("http://voltha_url/api/v1/devices", status_code=200, json={"id": ""})
167
168 with self.assertRaises(Exception) as e:
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700169 self.sync_step().sync_record(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800170 self.assertEqual(e.exception.message, "VOLTHA Device Id is empty, this probably means that the device is already provisioned in VOLTHA")
171
172 @requests_mock.Mocker()
173 def test_sync_record_fail_enable(self, m):
174 """
175 Should print an error if device.enable fails
176 """
177 m.post("http://voltha_url/api/v1/devices", status_code=200, json={"id": "123"})
178 m.post("http://voltha_url/api/v1/devices/123/enable", status_code=500, text="EnableError")
179
180 with self.assertRaises(Exception) as e:
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700181 self.sync_step().sync_record(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800182 self.assertEqual(e.exception.message, "Failed to enable device: EnableError")
183
184 @requests_mock.Mocker()
185 def test_sync_record_success(self, m):
186 """
187 If device.enable succed should fetch the state, retrieve the of_id and push it to ONOS
188 """
189 m.post("http://voltha_url/api/v1/devices", status_code=200, json={"id": "123"})
190 m.post("http://voltha_url/api/v1/devices/123/enable", status_code=200)
191 m.get("http://voltha_url/api/v1/devices/123", json={"oper_status": "ENABLED", "admin_state": "ACTIVE"})
192 logical_devices = {
193 "items": [
194 {"root_device_id": "123", "id": "abc"},
195 {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"}
196 ]
197 }
198 m.get("http://voltha_url/api/v1/logical_devices", status_code=200, json=logical_devices)
199
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800200 m.post("http://p_onos_url/onos/v1/network/configuration/", status_code=200, additional_matcher=match_onos_req, json={})
201
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700202 self.sync_step().sync_record(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800203 self.assertEqual(self.o.admin_state, "ACTIVE")
204 self.assertEqual(self.o.oper_status, "ENABLED")
205 self.assertEqual(self.o.of_id, "abc")
206 self.o.save.assert_called_once()
207
208 @requests_mock.Mocker()
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700209 def test_sync_record_already_existing_in_voltha(self, m):
210
211 # mock device feedback state
212 self.o.device_id = "123"
213 self.o.admin_state = "ACTIVE"
214 self.o.oper_status = "ENABLED"
215 self.o.of_id = "abc"
216
217 m.post("http://p_onos_url/onos/v1/network/configuration/", status_code=200, additional_matcher=match_onos_req, json={})
218
219 self.sync_step().sync_record(self.o)
220 self.o.save.assert_not_called()
221
222
223 @requests_mock.Mocker()
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800224 def test_delete_record(self, m):
225 self.o.of_id = "abc"
226 self.o.device_id = "123"
227
228 m.delete("http://p_onos_url/onos/v1/network/configuration/devices/abc", status_code=200)
229 m.post("http://voltha_url/api/v1/devices/123/disable", status_code=200)
230 m.delete("http://voltha_url/api/v1/devices/123/delete", status_code=200)
231
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700232 self.sync_step().delete_record(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800233
234 # we don't need to assert here, if there are no exceptions it succeded
235
236if __name__ == "__main__":
237 unittest.main()