blob: cac697d7fbe3e4add658f8a9cea66f4adbb3e867 [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
43class TestSyncOLTDevice(unittest.TestCase):
44
45 def setUp(self):
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070046
47 self.sys_path_save = sys.path
48 sys.path.append(xos_dir)
49 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
50
51 # Setting up the config module
52 from xosconfig import Config
53 config = os.path.join(test_path, "../model_policies/test_config.yaml")
54 Config.clear()
55 Config.init(config, "synchronizer-config-schema.yaml")
56 # END Setting up the config module
57
58 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
59 build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")])
60 import synchronizers.new_base.modelaccessor
61 from sync_olt_device import SyncOLTDevice
62 self.sync_step = SyncOLTDevice
63
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080064 # create a mock service instance
65 o = Mock()
66 o.volt_service.voltha_url = "voltha_url"
67 o.volt_service.voltha_user = "voltha_user"
68 o.volt_service.voltha_pass = "voltha_pass"
69 o.volt_service.p_onos_url = "p_onos_url"
70 o.volt_service.p_onos_user = "p_onos_user"
71 o.volt_service.p_onos_pass = "p_onos_pass"
72
73 o.device_type = "ponsim_olt"
74 o.host = "172.17.0.1"
75 o.port = "50060"
76 o.uplink = "129"
77 o.vlan = "3"
78 o.driver = "pmc-olt"
79
80 o.tologdict.return_value = {'name': "Mock VOLTServiceInstance"}
81
82 o.save.return_value = "Saved"
83
84 self.o = o
85
86 def tearDown(self):
87 self.o = None
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070088 sys.path = self.sys_path_save
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080089
90 def test_format_url(self):
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070091 url = self.sync_step.format_url("onf.com")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080092 self.assertEqual(url, "http://onf.com")
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070093 url = self.sync_step.format_url("http://onf.com")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080094 self.assertEqual(url, "http://onf.com")
95
96 def test_get_voltha_info(self):
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070097 voltha_dict = self.sync_step.get_voltha_info(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080098
99 self.assertEqual(voltha_dict["url"], "http://voltha_url")
100 self.assertEqual(voltha_dict["user"], "voltha_user")
101 self.assertEqual(voltha_dict["pass"], "voltha_pass")
102
103 def test_get_onos_info(self):
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700104 p_onos_dict = self.sync_step.get_p_onos_info(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800105
106 self.assertEqual(p_onos_dict["url"], "http://p_onos_url")
107 self.assertEqual(p_onos_dict["user"], "p_onos_user")
108 self.assertEqual(p_onos_dict["pass"], "p_onos_pass")
109
110 @requests_mock.Mocker()
111 def test_get_of_id_from_device(self, m):
112 logical_devices = {
113 "items": [
114 {"root_device_id": "123", "id": "abc"},
115 {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"}
116 ]
117 }
118 m.get("http://voltha_url/api/v1/logical_devices", status_code=200, json=logical_devices)
119 self.o.device_id = "123"
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700120 of_id = self.sync_step.get_of_id_from_device(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800121 self.assertEqual(of_id, "abc")
122
123 with self.assertRaises(Exception) as e:
124 self.o.device_id = "idonotexist"
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700125 self.sync_step.get_of_id_from_device(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800126 self.assertEqual(e.exception.message, "Can't find a logical device for device id: idonotexist")
127
128 @requests_mock.Mocker()
129 def test_sync_record_fail_add(self, m):
130 """
131 Should print an error if we can't add the device in VOLTHA
132 """
133 m.post("http://voltha_url/api/v1/devices", status_code=500, text="MockError")
134
135 with self.assertRaises(Exception) as e:
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700136 self.sync_step().sync_record(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800137 self.assertEqual(e.exception.message, "Failed to add device: MockError")
138
139 @requests_mock.Mocker()
140 def test_sync_record_fail_no_id(self, m):
141 """
142 Should print an error if VOLTHA does not return the device id
143 """
144 m.post("http://voltha_url/api/v1/devices", status_code=200, json={"id": ""})
145
146 with self.assertRaises(Exception) as e:
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700147 self.sync_step().sync_record(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800148 self.assertEqual(e.exception.message, "VOLTHA Device Id is empty, this probably means that the device is already provisioned in VOLTHA")
149
150 @requests_mock.Mocker()
151 def test_sync_record_fail_enable(self, m):
152 """
153 Should print an error if device.enable fails
154 """
155 m.post("http://voltha_url/api/v1/devices", status_code=200, json={"id": "123"})
156 m.post("http://voltha_url/api/v1/devices/123/enable", status_code=500, text="EnableError")
157
158 with self.assertRaises(Exception) as e:
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700159 self.sync_step().sync_record(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800160 self.assertEqual(e.exception.message, "Failed to enable device: EnableError")
161
162 @requests_mock.Mocker()
163 def test_sync_record_success(self, m):
164 """
165 If device.enable succed should fetch the state, retrieve the of_id and push it to ONOS
166 """
167 m.post("http://voltha_url/api/v1/devices", status_code=200, json={"id": "123"})
168 m.post("http://voltha_url/api/v1/devices/123/enable", status_code=200)
169 m.get("http://voltha_url/api/v1/devices/123", json={"oper_status": "ENABLED", "admin_state": "ACTIVE"})
170 logical_devices = {
171 "items": [
172 {"root_device_id": "123", "id": "abc"},
173 {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"}
174 ]
175 }
176 m.get("http://voltha_url/api/v1/logical_devices", status_code=200, json=logical_devices)
177
178 def match_onos_req(req):
179 r = req.json()['devices']
180 if not r['abc']:
181 return False
182 else:
183 if not r['abc']['basic']['driver'] == 'pmc-olt':
184 return False
185 if not r['abc']['accessDevice']['vlan'] == "3" or not r['abc']['accessDevice']['uplink'] == "129":
186 return False
187 return True
188
189 m.post("http://p_onos_url/onos/v1/network/configuration/", status_code=200, additional_matcher=match_onos_req, json={})
190
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700191 self.sync_step().sync_record(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800192 self.assertEqual(self.o.admin_state, "ACTIVE")
193 self.assertEqual(self.o.oper_status, "ENABLED")
194 self.assertEqual(self.o.of_id, "abc")
195 self.o.save.assert_called_once()
196
197 @requests_mock.Mocker()
198 def test_delete_record(self, m):
199 self.o.of_id = "abc"
200 self.o.device_id = "123"
201
202 m.delete("http://p_onos_url/onos/v1/network/configuration/devices/abc", status_code=200)
203 m.post("http://voltha_url/api/v1/devices/123/disable", status_code=200)
204 m.delete("http://voltha_url/api/v1/devices/123/delete", status_code=200)
205
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700206 self.sync_step().delete_record(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800207
208 # we don't need to assert here, if there are no exceptions it succeded
209
210if __name__ == "__main__":
211 unittest.main()