1 # Copyright 2020 ArctosLabs Scandinavia AB
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # from collections import Counter
17 from unittest
import TestCase
, mock
20 from osm_pla
.placement
.mznplacement
import MznPlacementConductor
, MznModelGenerator
23 % This minizinc model is generated using
24 % C:/Users/LG/PycharmProjects/dynamic_jijna2_mzn/osm_pla/placement/mznplacement.py
25 % at 2019-10-24 11:12:02.058905.
27 %This is the NETWORK RESOURCE MODEL
29 vimaaaaaaaa_38f5_438d_b8ee_3f93b3531f87,
30 vimbbbbbbbb_38f5_438d_b8ee_3f93b3531f87,
31 vimcccccccc_ed84_4e49_b5df_a9d117bd731f,
32 vimdddddddd_ed84_4e49_b5df_a9d117bd731f,
33 vimeeeeeeee_38f5_438d_b8ee_3f93b3531f87}; % The vim-accounts
34 array[Vims, Vims] of int: trp_link_latency = [|0,50,100,150,200,
39 |]; % Transport link latency between data centers
40 array[Vims, Vims] of int: trp_link_jitter = [|0,50,100,150,200,
45 |]; % Transport link jitter between data centers
46 array[Vims, Vims] of int: trp_link_price_list = [|0,5,6,6,7,
51 |]; % Transport link price list
52 array[Vims] of int: vim_price_list_1 = [500,51,52,53,54];
53 array[Vims] of int: vim_price_list_2 = [20,21,22,23,24];
54 array[Vims] of int: vim_price_list_3 = [70,71,72,73,74];
55 array[Vims] of int: vim_price_list_4 = [40,41,42,43,44];
58 % This is the NETWORK BASIC LOAD MODEL (CONSUMED)
59 % NOTE. This is not applicable in OSM Release 7
61 % This is the SERVICE CONSUMPTION MODEL
62 % These are the variables, i.e. which DC to select for each VNF
69 % These are the set of rules for selecting DCs to VNFs
70 constraint trp_link_latency[VNF1, VNF2] <= 150;
71 constraint trp_link_latency[VNF2, VNF3] <= 140;
72 constraint trp_link_latency[VNF3, VNF4] <= 130;
73 constraint trp_link_jitter[VNF1, VNF2] <= 30;
74 constraint trp_link_jitter[VNF2, VNF3] <= 30;
75 constraint trp_link_jitter[VNF3, VNF4] <= 30;
77 % Calculate the cost for VNFs and cost for transport link and total cost
78 var int: used_transport_cost =trp_link_price_list[VNF1, VNF2]+
79 trp_link_price_list[VNF2, VNF3]+
80 trp_link_price_list[VNF3, VNF4];
82 var int: used_vim_cost =vim_price_list_1[VNF1]+
83 vim_price_list_2[VNF2]+
84 vim_price_list_3[VNF3]+
85 vim_price_list_4[VNF4];
87 var int: total_cost = used_transport_cost + used_vim_cost;
89 solve minimize total_cost;
93 test_mzn_model_w_pinning
= """
94 % This minizinc model is generated using
95 % C:/Users/LG/PycharmProjects/dynamic_jijna2_mzn/osm_pla/placement/mznplacement.py
96 % at 2019-10-24 11:12:02.058905.
98 %This is the NETWORK RESOURCE MODEL
100 vimaaaaaaaa_38f5_438d_b8ee_3f93b3531f87,
101 vimbbbbbbbb_38f5_438d_b8ee_3f93b3531f87,
102 vimcccccccc_ed84_4e49_b5df_a9d117bd731f,
103 vimdddddddd_ed84_4e49_b5df_a9d117bd731f,
104 vimeeeeeeee_38f5_438d_b8ee_3f93b3531f87}; % The vim-accounts
105 array[Vims, Vims] of int: trp_link_latency = [|0,50,100,150,200,
110 |]; % Transport link latency between data centers
111 array[Vims, Vims] of int: trp_link_jitter = [|0,50,100,150,200,
116 |]; % Transport link jitter between data centers
117 array[Vims, Vims] of int: trp_link_price_list = [|0,5,6,6,7,
122 |]; % Transport link price list
123 array[Vims] of int: vim_price_list_1 = [500,51,52,53,54];
124 array[Vims] of int: vim_price_list_2 = [20,21,22,23,24];
125 array[Vims] of int: vim_price_list_3 = [70,71,72,73,74];
126 array[Vims] of int: vim_price_list_4 = [40,41,42,43,44];
129 % This is the NETWORK BASIC LOAD MODEL (CONSUMED)
130 % NOTE. This is not applicable in OSM Release 7
132 % This is the SERVICE CONSUMPTION MODEL
133 % These are the variables, i.e. which DC to select for each VNF
134 Vims: VNF1 = vimbbbbbbbb_38f5_438d_b8ee_3f93b3531f87;
136 Vims: VNF3 = vimbbbbbbbb_38f5_438d_b8ee_3f93b3531f87;
140 % These are the set of rules for selecting DCs to VNFs
141 constraint trp_link_latency[VNF1, VNF2] <= 150;
142 constraint trp_link_latency[VNF2, VNF3] <= 140;
143 constraint trp_link_latency[VNF3, VNF4] <= 130;
144 constraint trp_link_jitter[VNF1, VNF2] <= 30;
145 constraint trp_link_jitter[VNF2, VNF3] <= 30;
146 constraint trp_link_jitter[VNF3, VNF4] <= 30;
148 % Calculate the cost for VNFs and cost for transport link and total cost
149 var int: used_transport_cost =trp_link_price_list[VNF1, VNF2]+
150 trp_link_price_list[VNF2, VNF3]+
151 trp_link_price_list[VNF3, VNF4];
153 var int: used_vim_cost =vim_price_list_1[VNF1]+
154 vim_price_list_2[VNF2]+
155 vim_price_list_3[VNF3]+
156 vim_price_list_4[VNF4];
158 var int: total_cost = used_transport_cost + used_vim_cost;
160 solve minimize total_cost;
164 test_mzn_unsatisfiable_model
= """
167 constraint item1 + item2 == 5;
173 class TestMznPlacementConductor(TestCase
):
174 def test__run_placement_model(self
):
175 expected_result
= [{'vimAccountId': 'bbbbbbbb-38f5-438d-b8ee-3f93b3531f87', 'member-vnf-index': '1'},
176 {'vimAccountId': 'aaaaaaaa-38f5-438d-b8ee-3f93b3531f87', 'member-vnf-index': '2'},
177 {'vimAccountId': 'aaaaaaaa-38f5-438d-b8ee-3f93b3531f87', 'member-vnf-index': '3'},
178 {'vimAccountId': 'aaaaaaaa-38f5-438d-b8ee-3f93b3531f87', 'member-vnf-index': '4'}]
180 mpc
= MznPlacementConductor(logging
.getLogger(__name__
))
181 placement
= mpc
._run
_placement
_model
(mzn_model
=test_mzn_model
, ns_desc
={})
182 # sort the result to ease assert with expected result
183 sorted_placement
= sorted(placement
, key
=lambda k
: k
['member-vnf-index'])
184 self
.assertEqual(expected_result
, sorted_placement
, 'Faulty syntax or content')
186 def test__run_placement_model_w_pinning(self
):
187 expected_result
= [{'vimAccountId': 'bbbbbbbb-38f5-438d-b8ee-3f93b3531f87', 'member-vnf-index': '1'},
188 {'vimAccountId': 'bbbbbbbb-38f5-438d-b8ee-3f93b3531f87', 'member-vnf-index': '2'},
189 {'vimAccountId': 'bbbbbbbb-38f5-438d-b8ee-3f93b3531f87', 'member-vnf-index': '3'},
190 {'vimAccountId': 'aaaaaaaa-38f5-438d-b8ee-3f93b3531f87', 'member-vnf-index': '4'}]
192 ns_desc
= [{'vnf_price_per_vim': [10, 9, 7, 8], 'vnf_id': '2'},
193 {'vim_account': 'vimbbbbbbbb_38f5_438d_b8ee_3f93b3531f87', 'vnf_price_per_vim': [10, 9, 7, 8],
195 {'vnf_price_per_vim': [10, 9, 7, 8], 'vnf_id': '4'},
196 {'vim_account': 'vimbbbbbbbb_38f5_438d_b8ee_3f93b3531f87', 'vnf_price_per_vim': [10, 9, 7, 8],
200 mpc
= MznPlacementConductor(logging
.getLogger(__name__
))
201 placement
= mpc
._run
_placement
_model
(mzn_model
=test_mzn_model_w_pinning
, ns_desc
=ns_desc
)
202 # sort the result to ease assert with expected result
203 sorted_placement
= sorted(placement
, key
=lambda k
: k
['member-vnf-index'])
204 self
.assertEqual(expected_result
, sorted_placement
, 'Faulty syntax or content')
206 def test__run_placement_model_unsatisfiable(self
):
207 mpc
= MznPlacementConductor(logging
.getLogger(__name__
))
208 self
.assertEqual([{}], mpc
._run
_placement
_model
(mzn_model
=test_mzn_unsatisfiable_model
, ns_desc
={}),
209 "Faulty syntax or content for unsatisfiable model")
211 @mock.patch
.object(MznModelGenerator
, 'create_model', side_effect
=['%model'])
212 @mock.patch
.object(MznPlacementConductor
, '_run_placement_model')
213 def test_do_placement_computation(self
, mock_run
, mock_create
):
214 mpc
= MznPlacementConductor(logging
.getLogger(__name__
))
215 dummy_nspd
= {'ns_desc': {}}
216 _
= mpc
.do_placement_computation(dummy_nspd
)
217 mock_create
.assert_called_with(dummy_nspd
)
218 mock_run
.assert_called_with('%model', {})