Coverage for osm_pla/test/test_mznPlacementConductor.py: 100%

31 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2024-06-22 10:12 +0000

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. 

15import logging 

16 

17# from collections import Counter 

18from unittest import TestCase, mock 

19 

20# import osm_pla 

21from osm_pla.placement.mznplacement import MznPlacementConductor, MznModelGenerator 

22 

23test_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 

29enum Vims = { 

30vimaaaaaaaa_38f5_438d_b8ee_3f93b3531f87, 

31vimbbbbbbbb_38f5_438d_b8ee_3f93b3531f87, 

32vimcccccccc_ed84_4e49_b5df_a9d117bd731f, 

33vimdddddddd_ed84_4e49_b5df_a9d117bd731f, 

34vimeeeeeeee_38f5_438d_b8ee_3f93b3531f87}; % The vim-accounts 

35array[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 

41array[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 

47array[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 

53array[Vims] of int: vim_price_list_1 = [500,51,52,53,54]; 

54array[Vims] of int: vim_price_list_2 = [20,21,22,23,24]; 

55array[Vims] of int: vim_price_list_3 = [70,71,72,73,74]; 

56array[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 

64var Vims: VNF1; 

65var Vims: VNF2; 

66var Vims: VNF3; 

67var Vims: VNF4; 

68 

69 

70% These are the set of rules for selecting DCs to VNFs 

71constraint trp_link_latency[VNF1, VNF2] <= 150; 

72constraint trp_link_latency[VNF2, VNF3] <= 140; 

73constraint trp_link_latency[VNF3, VNF4] <= 130; 

74constraint trp_link_jitter[VNF1, VNF2] <= 30; 

75constraint trp_link_jitter[VNF2, VNF3] <= 30; 

76constraint trp_link_jitter[VNF3, VNF4] <= 30; 

77 

78% Calculate the cost for VNFs and cost for transport link and total cost 

79var int: used_transport_cost =trp_link_price_list[VNF1, VNF2]+ 

80trp_link_price_list[VNF2, VNF3]+ 

81trp_link_price_list[VNF3, VNF4]; 

82 

83var int: used_vim_cost =vim_price_list_1[VNF1]+ 

84vim_price_list_2[VNF2]+ 

85vim_price_list_3[VNF3]+ 

86vim_price_list_4[VNF4]; 

87 

88var int: total_cost = used_transport_cost + used_vim_cost; 

89 

90solve minimize total_cost; 

91 

92""" 

93 

94test_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 

100enum Vims = { 

101vimaaaaaaaa_38f5_438d_b8ee_3f93b3531f87, 

102vimbbbbbbbb_38f5_438d_b8ee_3f93b3531f87, 

103vimcccccccc_ed84_4e49_b5df_a9d117bd731f, 

104vimdddddddd_ed84_4e49_b5df_a9d117bd731f, 

105vimeeeeeeee_38f5_438d_b8ee_3f93b3531f87}; % The vim-accounts 

106array[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 

112array[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 

118array[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 

124array[Vims] of int: vim_price_list_1 = [500,51,52,53,54]; 

125array[Vims] of int: vim_price_list_2 = [20,21,22,23,24]; 

126array[Vims] of int: vim_price_list_3 = [70,71,72,73,74]; 

127array[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 

135Vims: VNF1 = vimbbbbbbbb_38f5_438d_b8ee_3f93b3531f87; 

136var Vims: VNF2; 

137Vims: VNF3 = vimbbbbbbbb_38f5_438d_b8ee_3f93b3531f87; 

138var Vims: VNF4; 

139 

140 

141% These are the set of rules for selecting DCs to VNFs 

142constraint trp_link_latency[VNF1, VNF2] <= 150; 

143constraint trp_link_latency[VNF2, VNF3] <= 140; 

144constraint trp_link_latency[VNF3, VNF4] <= 130; 

145constraint trp_link_jitter[VNF1, VNF2] <= 30; 

146constraint trp_link_jitter[VNF2, VNF3] <= 30; 

147constraint trp_link_jitter[VNF3, VNF4] <= 30; 

148 

149% Calculate the cost for VNFs and cost for transport link and total cost 

150var int: used_transport_cost =trp_link_price_list[VNF1, VNF2]+ 

151trp_link_price_list[VNF2, VNF3]+ 

152trp_link_price_list[VNF3, VNF4]; 

153 

154var int: used_vim_cost =vim_price_list_1[VNF1]+ 

155vim_price_list_2[VNF2]+ 

156vim_price_list_3[VNF3]+ 

157vim_price_list_4[VNF4]; 

158 

159var int: total_cost = used_transport_cost + used_vim_cost; 

160 

161solve minimize total_cost; 

162 

163""" 

164 

165test_mzn_unsatisfiable_model = """ 

166var 1..2: item1; 

167var 1..2: item2; 

168constraint item1 + item2 == 5; 

169 

170solve satisfy; 

171""" 

172 

173 

174class 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", {})