Enable black in tox.ini
[osm/PLA.git] / osm_pla / test / test_mznPlacementConductor.py
1 # Copyright 2020 ArctosLabs Scandinavia AB
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
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 import logging
16
17 # from collections import Counter
18 from unittest import TestCase, mock
19
20 # import osm_pla
21 from osm_pla.placement.mznplacement import MznPlacementConductor, MznModelGenerator
22
23 test_mzn_model = """
24 % This minizinc model is generated using
25 % C:/Users/LG/PycharmProjects/dynamic_jijna2_mzn/osm_pla/placement/mznplacement.py
26 % at 2019-10-24 11:12:02.058905.
27
28 %This is the NETWORK RESOURCE MODEL
29 enum Vims = {
30 vimaaaaaaaa_38f5_438d_b8ee_3f93b3531f87,
31 vimbbbbbbbb_38f5_438d_b8ee_3f93b3531f87,
32 vimcccccccc_ed84_4e49_b5df_a9d117bd731f,
33 vimdddddddd_ed84_4e49_b5df_a9d117bd731f,
34 vimeeeeeeee_38f5_438d_b8ee_3f93b3531f87}; % The vim-accounts
35 array[Vims, Vims] of int: trp_link_latency = [|0,50,100,150,200,
36 |0,0,100,150,200,
37 |0,0,0,150,200,
38 |0,0,0,0,200,
39 |0,0,0,0,0,
40 |]; % Transport link latency between data centers
41 array[Vims, Vims] of int: trp_link_jitter = [|0,50,100,150,200,
42 |0,0,100,150,200,
43 |0,0,0,150,200,
44 |0,0,0,0,200,
45 |0,0,0,0,0,
46 |]; % Transport link jitter between data centers
47 array[Vims, Vims] of int: trp_link_price_list = [|0,5,6,6,7,
48 |0,0,6,6,7,
49 |0,0,0,6,7,
50 |0,0,0,0,7,
51 |0,0,0,0,0,
52 |]; % Transport link price list
53 array[Vims] of int: vim_price_list_1 = [500,51,52,53,54];
54 array[Vims] of int: vim_price_list_2 = [20,21,22,23,24];
55 array[Vims] of int: vim_price_list_3 = [70,71,72,73,74];
56 array[Vims] of int: vim_price_list_4 = [40,41,42,43,44];
57
58
59 % This is the NETWORK BASIC LOAD MODEL (CONSUMED)
60 % NOTE. This is not applicable in OSM Release 7
61
62 % This is the SERVICE CONSUMPTION MODEL
63 % These are the variables, i.e. which DC to select for each VNF
64 var Vims: VNF1;
65 var Vims: VNF2;
66 var Vims: VNF3;
67 var Vims: VNF4;
68
69
70 % These are the set of rules for selecting DCs to VNFs
71 constraint trp_link_latency[VNF1, VNF2] <= 150;
72 constraint trp_link_latency[VNF2, VNF3] <= 140;
73 constraint trp_link_latency[VNF3, VNF4] <= 130;
74 constraint trp_link_jitter[VNF1, VNF2] <= 30;
75 constraint trp_link_jitter[VNF2, VNF3] <= 30;
76 constraint trp_link_jitter[VNF3, VNF4] <= 30;
77
78 % Calculate the cost for VNFs and cost for transport link and total cost
79 var int: used_transport_cost =trp_link_price_list[VNF1, VNF2]+
80 trp_link_price_list[VNF2, VNF3]+
81 trp_link_price_list[VNF3, VNF4];
82
83 var int: used_vim_cost =vim_price_list_1[VNF1]+
84 vim_price_list_2[VNF2]+
85 vim_price_list_3[VNF3]+
86 vim_price_list_4[VNF4];
87
88 var int: total_cost = used_transport_cost + used_vim_cost;
89
90 solve minimize total_cost;
91
92 """
93
94 test_mzn_model_w_pinning = """
95 % This minizinc model is generated using
96 % C:/Users/LG/PycharmProjects/dynamic_jijna2_mzn/osm_pla/placement/mznplacement.py
97 % at 2019-10-24 11:12:02.058905.
98
99 %This is the NETWORK RESOURCE MODEL
100 enum Vims = {
101 vimaaaaaaaa_38f5_438d_b8ee_3f93b3531f87,
102 vimbbbbbbbb_38f5_438d_b8ee_3f93b3531f87,
103 vimcccccccc_ed84_4e49_b5df_a9d117bd731f,
104 vimdddddddd_ed84_4e49_b5df_a9d117bd731f,
105 vimeeeeeeee_38f5_438d_b8ee_3f93b3531f87}; % The vim-accounts
106 array[Vims, Vims] of int: trp_link_latency = [|0,50,100,150,200,
107 |0,0,100,150,200,
108 |0,0,0,150,200,
109 |0,0,0,0,200,
110 |0,0,0,0,0,
111 |]; % Transport link latency between data centers
112 array[Vims, Vims] of int: trp_link_jitter = [|0,50,100,150,200,
113 |0,0,100,150,200,
114 |0,0,0,150,200,
115 |0,0,0,0,200,
116 |0,0,0,0,0,
117 |]; % Transport link jitter between data centers
118 array[Vims, Vims] of int: trp_link_price_list = [|0,5,6,6,7,
119 |0,0,6,6,7,
120 |0,0,0,6,7,
121 |0,0,0,0,7,
122 |0,0,0,0,0,
123 |]; % Transport link price list
124 array[Vims] of int: vim_price_list_1 = [500,51,52,53,54];
125 array[Vims] of int: vim_price_list_2 = [20,21,22,23,24];
126 array[Vims] of int: vim_price_list_3 = [70,71,72,73,74];
127 array[Vims] of int: vim_price_list_4 = [40,41,42,43,44];
128
129
130 % This is the NETWORK BASIC LOAD MODEL (CONSUMED)
131 % NOTE. This is not applicable in OSM Release 7
132
133 % This is the SERVICE CONSUMPTION MODEL
134 % These are the variables, i.e. which DC to select for each VNF
135 Vims: VNF1 = vimbbbbbbbb_38f5_438d_b8ee_3f93b3531f87;
136 var Vims: VNF2;
137 Vims: VNF3 = vimbbbbbbbb_38f5_438d_b8ee_3f93b3531f87;
138 var Vims: VNF4;
139
140
141 % These are the set of rules for selecting DCs to VNFs
142 constraint trp_link_latency[VNF1, VNF2] <= 150;
143 constraint trp_link_latency[VNF2, VNF3] <= 140;
144 constraint trp_link_latency[VNF3, VNF4] <= 130;
145 constraint trp_link_jitter[VNF1, VNF2] <= 30;
146 constraint trp_link_jitter[VNF2, VNF3] <= 30;
147 constraint trp_link_jitter[VNF3, VNF4] <= 30;
148
149 % Calculate the cost for VNFs and cost for transport link and total cost
150 var int: used_transport_cost =trp_link_price_list[VNF1, VNF2]+
151 trp_link_price_list[VNF2, VNF3]+
152 trp_link_price_list[VNF3, VNF4];
153
154 var int: used_vim_cost =vim_price_list_1[VNF1]+
155 vim_price_list_2[VNF2]+
156 vim_price_list_3[VNF3]+
157 vim_price_list_4[VNF4];
158
159 var int: total_cost = used_transport_cost + used_vim_cost;
160
161 solve minimize total_cost;
162
163 """
164
165 test_mzn_unsatisfiable_model = """
166 var 1..2: item1;
167 var 1..2: item2;
168 constraint item1 + item2 == 5;
169
170 solve satisfy;
171 """
172
173
174 class TestMznPlacementConductor(TestCase):
175 def test__run_placement_model(self):
176 expected_result = [
177 {
178 "vimAccountId": "bbbbbbbb-38f5-438d-b8ee-3f93b3531f87",
179 "member-vnf-index": "1",
180 },
181 {
182 "vimAccountId": "aaaaaaaa-38f5-438d-b8ee-3f93b3531f87",
183 "member-vnf-index": "2",
184 },
185 {
186 "vimAccountId": "aaaaaaaa-38f5-438d-b8ee-3f93b3531f87",
187 "member-vnf-index": "3",
188 },
189 {
190 "vimAccountId": "aaaaaaaa-38f5-438d-b8ee-3f93b3531f87",
191 "member-vnf-index": "4",
192 },
193 ]
194
195 mpc = MznPlacementConductor(logging.getLogger(__name__))
196 placement = mpc._run_placement_model(mzn_model=test_mzn_model, ns_desc={})
197 # sort the result to ease assert with expected result
198 sorted_placement = sorted(placement, key=lambda k: k["member-vnf-index"])
199 self.assertEqual(expected_result, sorted_placement, "Faulty syntax or content")
200
201 def test__run_placement_model_w_pinning(self):
202 expected_result = [
203 {
204 "vimAccountId": "bbbbbbbb-38f5-438d-b8ee-3f93b3531f87",
205 "member-vnf-index": "1",
206 },
207 {
208 "vimAccountId": "bbbbbbbb-38f5-438d-b8ee-3f93b3531f87",
209 "member-vnf-index": "2",
210 },
211 {
212 "vimAccountId": "bbbbbbbb-38f5-438d-b8ee-3f93b3531f87",
213 "member-vnf-index": "3",
214 },
215 {
216 "vimAccountId": "aaaaaaaa-38f5-438d-b8ee-3f93b3531f87",
217 "member-vnf-index": "4",
218 },
219 ]
220
221 ns_desc = [
222 {"vnf_price_per_vim": [10, 9, 7, 8], "vnf_id": "2"},
223 {
224 "vim_account": "vimbbbbbbbb_38f5_438d_b8ee_3f93b3531f87",
225 "vnf_price_per_vim": [10, 9, 7, 8],
226 "vnf_id": "1",
227 },
228 {"vnf_price_per_vim": [10, 9, 7, 8], "vnf_id": "4"},
229 {
230 "vim_account": "vimbbbbbbbb_38f5_438d_b8ee_3f93b3531f87",
231 "vnf_price_per_vim": [10, 9, 7, 8],
232 "vnf_id": "3",
233 },
234 ]
235
236 mpc = MznPlacementConductor(logging.getLogger(__name__))
237 placement = mpc._run_placement_model(
238 mzn_model=test_mzn_model_w_pinning, ns_desc=ns_desc
239 )
240 # sort the result to ease assert with expected result
241 sorted_placement = sorted(placement, key=lambda k: k["member-vnf-index"])
242 self.assertEqual(expected_result, sorted_placement, "Faulty syntax or content")
243
244 def test__run_placement_model_unsatisfiable(self):
245 mpc = MznPlacementConductor(logging.getLogger(__name__))
246 self.assertEqual(
247 [{}],
248 mpc._run_placement_model(
249 mzn_model=test_mzn_unsatisfiable_model, ns_desc={}
250 ),
251 "Faulty syntax or content for unsatisfiable model",
252 )
253
254 @mock.patch.object(MznModelGenerator, "create_model", side_effect=["%model"])
255 @mock.patch.object(MznPlacementConductor, "_run_placement_model")
256 def test_do_placement_computation(self, mock_run, mock_create):
257 mpc = MznPlacementConductor(logging.getLogger(__name__))
258 dummy_nspd = {"ns_desc": {}}
259 _ = mpc.do_placement_computation(dummy_nspd)
260 mock_create.assert_called_with(dummy_nspd)
261 mock_run.assert_called_with("%model", {})