blob: 4d785b5fa249fbeb1daa3aa0fde586aa34b87496 [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'))
Luca Preteca974c82018-05-01 18:06:16 -070029# END of hack to load synchronizer framework
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080030
Luca Preteca974c82018-05-01 18:06:16 -070031# Generate model from xproto
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070032def 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):
Luca Preteca974c82018-05-01 18:06:16 -070044 request = req.json()['devices']
45 if not request['of:0000000ce2314000']:
Matteo Scandolob8621cd2018-04-04 17:12:37 -070046 return False
47 else:
Luca Preteca974c82018-05-01 18:06:16 -070048 if not request['of:0000000ce2314000']['basic']['driver'] == 'pmc-olt':
Matteo Scandolob8621cd2018-04-04 17:12:37 -070049 return False
Luca Preteca974c82018-05-01 18:06:16 -070050 if not request['of:0000000ce2314000']['accessDevice']['vlan'] == "s_tag" or not request['of:0000000ce2314000']['accessDevice']['uplink'] == "129":
Matteo Scandolob8621cd2018-04-04 17:12:37 -070051 return False
52 return True
53
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080054class TestSyncOLTDevice(unittest.TestCase):
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080055 def setUp(self):
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070056 global DeferredException
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070057 self.sys_path_save = sys.path
58 sys.path.append(xos_dir)
59 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
60
61 # Setting up the config module
62 from xosconfig import Config
63 config = os.path.join(test_path, "../model_policies/test_config.yaml")
64 Config.clear()
65 Config.init(config, "synchronizer-config-schema.yaml")
Luca Preteca974c82018-05-01 18:06:16 -070066 # END setting up the config module
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070067
68 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
Matteo Scandolo19466a02018-05-16 17:43:39 -070069 # build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")])
70
71 # FIXME this is to get jenkins to pass the tests, somehow it is running tests in a different order
72 # and apparently it is not overriding the generated model accessor
73 build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto"),
74 get_models_fn("vsg", "vsg.xproto"),
75 get_models_fn("../profiles/rcord", "rcord.xproto")])
76
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070077 import synchronizers.new_base.modelaccessor
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070078 from sync_olt_device import SyncOLTDevice, DeferredException
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070079 self.sync_step = SyncOLTDevice
80
Matteo Scandolob8621cd2018-04-04 17:12:37 -070081 pon_port = Mock()
82 pon_port.port_id = "00ff00"
83 pon_port.s_tag = "s_tag"
84
Luca Preteca974c82018-05-01 18:06:16 -070085 # Create a mock service instance
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080086 o = Mock()
87 o.volt_service.voltha_url = "voltha_url"
Luca Preteca974c82018-05-01 18:06:16 -070088 o.volt_service.voltha_port = 1234
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080089 o.volt_service.voltha_user = "voltha_user"
90 o.volt_service.voltha_pass = "voltha_pass"
Luca Preteca974c82018-05-01 18:06:16 -070091 o.volt_service.onos_voltha_url = "onos_voltha_url"
92 o.volt_service.onos_voltha_port = 4321
93 o.volt_service.onos_voltha_user = "onos_voltha_user"
94 o.volt_service.onos_voltha_pass = "onos_voltha_pass"
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080095
96 o.device_type = "ponsim_olt"
97 o.host = "172.17.0.1"
98 o.port = "50060"
99 o.uplink = "129"
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800100 o.driver = "pmc-olt"
101
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700102 # feedback state
103 o.device_id = None
104 o.admin_state = None
105 o.oper_status = None
106 o.of_id = None
107
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800108 o.tologdict.return_value = {'name': "Mock VOLTServiceInstance"}
109
110 o.save.return_value = "Saved"
111
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700112 o.pon_ports.all.return_value = [pon_port]
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700113
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800114 self.o = o
115
116 def tearDown(self):
117 self.o = None
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700118 sys.path = self.sys_path_save
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800119
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800120 @requests_mock.Mocker()
121 def test_get_of_id_from_device(self, m):
122 logical_devices = {
123 "items": [
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700124 {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"},
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800125 {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"}
126 ]
127 }
Luca Preteca974c82018-05-01 18:06:16 -0700128 m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800129 self.o.device_id = "123"
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700130 self.o = self.sync_step.get_ids_from_logical_device(self.o)
131 self.assertEqual(self.o.of_id, "0001000ce2314000")
132 self.assertEqual(self.o.dp_id, "of:0000000ce2314000")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800133
134 with self.assertRaises(Exception) as e:
135 self.o.device_id = "idonotexist"
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700136 self.sync_step.get_ids_from_logical_device(self.o)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700137 self.assertEqual(e.exception.message, "Can't find a logical_device for OLT device id: idonotexist")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800138
139 @requests_mock.Mocker()
140 def test_sync_record_fail_add(self, m):
141 """
142 Should print an error if we can't add the device in VOLTHA
143 """
Luca Preteca974c82018-05-01 18:06:16 -0700144 m.post("http://voltha_url:1234/api/v1/devices", status_code=500, text="MockError")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800145
146 with self.assertRaises(Exception) as e:
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700147 self.sync_step().sync_record(self.o)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700148 self.assertEqual(e.exception.message, "Failed to add OLT device: MockError")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800149
150 @requests_mock.Mocker()
151 def test_sync_record_fail_no_id(self, m):
152 """
153 Should print an error if VOLTHA does not return the device id
154 """
Luca Preteca974c82018-05-01 18:06:16 -0700155 m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json={"id": ""})
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800156
157 with self.assertRaises(Exception) as e:
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700158 self.sync_step().sync_record(self.o)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700159 self.assertEqual(e.exception.message, "VOLTHA Device Id is empty. This probably means that the OLT device is already provisioned in VOLTHA")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800160
161 @requests_mock.Mocker()
162 def test_sync_record_fail_enable(self, m):
163 """
164 Should print an error if device.enable fails
165 """
Luca Preteca974c82018-05-01 18:06:16 -0700166 m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json={"id": "123"})
167 m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=500, text="EnableError")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800168
169 with self.assertRaises(Exception) as e:
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700170 self.sync_step().sync_record(self.o)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700171
172 self.assertEqual(e.exception.message, "Failed to enable OLT device: EnableError")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800173
174 @requests_mock.Mocker()
175 def test_sync_record_success(self, m):
176 """
177 If device.enable succed should fetch the state, retrieve the of_id and push it to ONOS
178 """
Luca Preteca974c82018-05-01 18:06:16 -0700179 m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json={"id": "123"})
180 m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=200)
181 m.get("http://voltha_url:1234/api/v1/devices/123", json={"oper_status": "ENABLED", "admin_state": "ACTIVE"})
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800182 logical_devices = {
183 "items": [
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700184 {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"},
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800185 {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"}
186 ]
187 }
Luca Preteca974c82018-05-01 18:06:16 -0700188 m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800189
Luca Preteca974c82018-05-01 18:06:16 -0700190 m.post("http://onos_voltha_url:4321/onos/v1/network/configuration/", status_code = 200, additional_matcher=match_onos_req, json={})
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800191
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700192 self.sync_step().sync_record(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800193 self.assertEqual(self.o.admin_state, "ACTIVE")
194 self.assertEqual(self.o.oper_status, "ENABLED")
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700195 self.assertEqual(self.o.of_id, "0001000ce2314000")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800196 self.o.save.assert_called_once()
197
198 @requests_mock.Mocker()
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700199 def test_sync_record_already_existing_in_voltha(self, m):
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700200 # mock device feedback state
201 self.o.device_id = "123"
202 self.o.admin_state = "ACTIVE"
203 self.o.oper_status = "ENABLED"
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700204 self.o.dp_id = "of:0000000ce2314000"
Matteo Scandolo2c144932018-05-04 14:06:24 -0700205 self.o.of_id = "0001000ce2314000"
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700206
Luca Preteca974c82018-05-01 18:06:16 -0700207 m.post("http://onos_voltha_url:4321/onos/v1/network/configuration/", status_code = 200, additional_matcher=match_onos_req, json={})
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700208
209 self.sync_step().sync_record(self.o)
210 self.o.save.assert_not_called()
211
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700212 @requests_mock.Mocker()
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800213 def test_delete_record(self, m):
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700214 self.o.of_id = "0001000ce2314000"
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800215 self.o.device_id = "123"
216
Matteo Scandolo19466a02018-05-16 17:43:39 -0700217 m.delete("http://onos_voltha_url:4321/onos/v1/network/configuration/devices/0001000ce2314000", status_code=204)
Luca Preteca974c82018-05-01 18:06:16 -0700218 m.post("http://voltha_url:1234/api/v1/devices/123/disable", status_code=200)
219 m.delete("http://voltha_url:1234/api/v1/devices/123/delete", status_code=200)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800220
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700221 self.sync_step().delete_record(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800222
Luca Preteca974c82018-05-01 18:06:16 -0700223 # We don't need to assert here if there are no exceptions happening
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800224
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700225 def test_deferred_for_port(self):
226 self.o.pon_ports.all.side_effect = Exception
227 with self.assertRaises(DeferredException) as e:
228 self.sync_step().configure_onos(self.o)
229 self.assertEqual(e.exception.message, "Waiting for pon_ports to come up")
230
231 self.o.pon_ports.all.return_value = []
232 with self.assertRaises(DeferredException) as e:
233 self.sync_step().configure_onos(self.o)
234 self.assertEqual(e.exception.message, "Waiting for pon_ports to come up")
235
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800236if __name__ == "__main__":
Luca Preteca974c82018-05-01 18:06:16 -0700237 unittest.main()