blob: 3fcd3800c4313e748352265cae3001c44584374d [file] [log] [blame]
Scott Bakerb146a852017-11-29 16:36:05 -08001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
17import unittest
18from mock import patch, call, Mock, MagicMock, PropertyMock
19import mock
20
21import os, sys
22
23test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
24service_dir=os.path.join(test_path, "../../../..")
25xos_dir=os.path.join(test_path, "../../..")
26if not os.path.exists(os.path.join(test_path, "new_base")):
27 xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
28 services_dir=os.path.join(xos_dir, "../../xos_services")
29
30# chosen to match https://github.com/opencord/ecord/blob/master/examples/vnaasglobal-service-reference.yaml
31ONOS_NAME = "onfGlobalONOS"
32ONOS_IP = "onos-cord"
33ONOS_PORT = 8182
34ONOS_USERNAME = "onos"
35ONOS_PASSWORD = "rocks"
36ONOS_TYPE = "global"
37
38BWP_GOLD_CBS = 2000
39BWP_GOLD_EBS = 2700
40BWP_GOLD_CIR = 20000
41BWP_GOLD_EIR = 5000
42BWP_GOLD_NAME = "gold"
43
Scott Bakerd76123f2018-01-23 14:26:54 -080044CONNECT_POINT_1_TENANT = "onf"
45CONNECT_POINT_1_NAME = "uni1"
46CONNECT_POINT_1_LATLNG = "[37.973535, -122.531087]"
47CONNECT_POINT_1_CPE_ID = "domain:10.90.1.30-cord-onos/1"
48
49CONNECT_POINT_2_TENANT = "onf"
50CONNECT_POINT_2_NAME = "uni2"
51CONNECT_POINT_2_LATLNG = "[37.773972, -122.431297]"
52CONNECT_POINT_2_CPE_ID = "domain:10.90.1.30-cord-onos/1"
53
Scott Bakerb146a852017-11-29 16:36:05 -080054ELINE_VLANIDS = "100"
Scott Bakerb146a852017-11-29 16:36:05 -080055ELINE_NAME = "testeline"
56
Scott Baker14dcd2f2018-01-24 14:55:21 -080057# While transitioning from static to dynamic load, the path to find neighboring xproto files has changed. So check
58# both possible locations...
59def get_models_fn(service_name, xproto_name):
60 name = os.path.join(service_name, "xos", xproto_name)
61 if os.path.exists(os.path.join(services_dir, name)):
62 return name
63 else:
64 name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
65 if os.path.exists(os.path.join(services_dir, name)):
66 return name
67 raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
68
Scott Bakerb146a852017-11-29 16:36:05 -080069class TestSyncvNaaSEline(unittest.TestCase):
70 def setUp(self):
71 global SyncvNaaSEline, MockObjectList
72
73 self.sys_path_save = sys.path
74 sys.path.append(xos_dir)
75 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
76
77 config = os.path.join(test_path, "test_config.yaml")
78 from xosconfig import Config
79 Config.clear()
80 Config.init(config, 'synchronizer-config-schema.yaml')
81
82 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
Scott Baker14dcd2f2018-01-24 14:55:21 -080083 build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("vnaas", "vnaas.xproto")])
Scott Bakerb146a852017-11-29 16:36:05 -080084
85 import synchronizers.new_base.modelaccessor
86 import synchronizers.new_base.model_policies.model_policy_tenantwithcontainer
87 import sync_vnaaseline
88 from sync_vnaaseline import SyncvNaaSEline, model_accessor
89
90 from mock_modelaccessor import MockObjectList
91
92 # import all class names to globals
93 for (k, v) in model_accessor.all_model_classes.items():
94 globals()[k] = v
95
96 # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of
97 # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world.
98 model_accessor.reset_all_object_stores()
99
100 self.syncstep = SyncvNaaSEline()
101
102 self.onosModel = OnosModel(name=ONOS_NAME,
103 onos_ip=ONOS_IP,
104 onos_port=ONOS_PORT,
105 onos_username=ONOS_USERNAME,
106 onos_password=ONOS_PASSWORD,
107 onos_type=ONOS_TYPE)
108 self.bandwidthProfile = BandwidthProfile(cbs=BWP_GOLD_CBS,
109 ebs=BWP_GOLD_EBS,
110 cir=BWP_GOLD_CIR,
111 eir=BWP_GOLD_EIR,
112 name=BWP_GOLD_NAME)
Scott Bakerd76123f2018-01-23 14:26:54 -0800113 self.connect_point_1 = UserNetworkInterface(tenant=CONNECT_POINT_1_TENANT,
114 name=CONNECT_POINT_1_NAME,
115 latlng=CONNECT_POINT_1_LATLNG,
116 cpe_id=CONNECT_POINT_1_CPE_ID)
117 self.connect_point_2 = UserNetworkInterface(tenant=CONNECT_POINT_2_TENANT,
118 name=CONNECT_POINT_2_NAME,
119 latlng=CONNECT_POINT_2_LATLNG,
120 cpe_id=CONNECT_POINT_2_CPE_ID)
Scott Bakerb146a852017-11-29 16:36:05 -0800121
122 self.eline = ELine(name=ELINE_NAME,
Scott Bakerd76123f2018-01-23 14:26:54 -0800123 connect_point_1=self.connect_point_1,
124 connect_point_2=self.connect_point_2,
Scott Bakerb146a852017-11-29 16:36:05 -0800125 vlanids=ELINE_VLANIDS,
126 cord_site_name=ONOS_NAME,
Scott Bakerd76123f2018-01-23 14:26:54 -0800127 bwp=self.bandwidthProfile)
Scott Bakerb146a852017-11-29 16:36:05 -0800128
129 def tearDown(self):
130 sys.path = self.sys_path_save
131
132 def test_sync_record(self):
133 with patch.object(BandwidthProfile.objects, "get_items") as bwp_objects, \
134 patch.object(OnosModel.objects, "get_items") as onos_objects, \
135 patch("requests.post") as requests_post:
136
137 bwp_objects.return_value = [self.bandwidthProfile]
138 onos_objects.return_value = [self.onosModel]
139
140 requests_post.return_value = Mock(status_code=200)
141
142 self.syncstep.sync_record(self.eline)
143
144 requests_post.assert_called()
145
146 attrs = requests_post.call_args[1]["data"]
147 attrs = eval(attrs) # convert POST string back into a dict
148
149 desired_attrs = {"evcCfgId": ELINE_NAME,
150 "eir": BWP_GOLD_EIR,
151 "cir": BWP_GOLD_CIR,
Scott Bakerd76123f2018-01-23 14:26:54 -0800152 "uniList": [CONNECT_POINT_1_CPE_ID, CONNECT_POINT_2_CPE_ID],
Scott Bakerb146a852017-11-29 16:36:05 -0800153 "ebs": BWP_GOLD_EBS,
154 "vlanId": int(ELINE_VLANIDS),
155 "cbs": BWP_GOLD_CBS,
156 "evcId": ELINE_NAME,
157 "evcType": "POINT_TO_POINT"}
158
159 self.assertDictContainsSubset(desired_attrs, attrs)
160
161 def test_delete_record(self):
162 with patch.object(BandwidthProfile.objects, "get_items") as bwp_objects, \
163 patch.object(OnosModel.objects, "get_items") as onos_objects, \
164 patch("requests.delete") as requests_delete:
165
166 bwp_objects.return_value = [self.bandwidthProfile]
167 onos_objects.return_value = [self.onosModel]
168
169 requests_delete.return_value = Mock(status_code=200)
170
171 self.syncstep.delete_record(self.eline)
172
173 requests_delete.assert_called()
174
175 url = requests_delete.call_args[0][0]
176 self.assertEqual(url, "http://%s:%d/carrierethernet/evc/testeline" % (ONOS_IP, ONOS_PORT))
177
178 def test_get_onos_global_addr(self):
179 addr = self.syncstep.get_onos_global_addr(self.onosModel)
180 self.assertEqual(addr, 'http://%s:%d/carrierethernet/evc' % (ONOS_IP, ONOS_PORT))
181
182 def test_get_onos_global_auth(self):
183 auth = self.syncstep.get_onos_global_auth(self.onosModel)
184 self.assertEqual(auth.username, ONOS_USERNAME)
185 self.assertEqual(auth.password, ONOS_PASSWORD)
186
187if __name__ == '__main__':
188 unittest.main()
189
190