-
Notifications
You must be signed in to change notification settings - Fork 1
/
AlgorithmFour.py
200 lines (158 loc) · 9.01 KB
/
AlgorithmFour.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from graph_tool.all import *
import numpy as np
import random
import TotalNetwork as tn
def algorithmFour(totalNetwork, resCapList, vnfCncList, greedy_method=True):
vnfCncList = tn.getUpdatedCncList(totalNetwork)
resCapList = tn.getUpdatedResList(totalNetwork, layer="Substrate")
sortedVnfCncList = sorted(vnfCncList, reverse=greedy_method)
sortedSbsValList = sorted(resCapList, reverse=greedy_method)
sbsFoundVertex = 0
numOfMappings = 0
ranFoundVertex = 0
for vnfCncVal in sortedVnfCncList:
# print("Num of Mapping Al")
foundSbsVert = False
ranFoundVertex = find_vertex(totalNetwork, totalNetwork.vp.degree, vnfCncVal)
optimalRan = []
maxVnfRes = 0
for vnfFunction in ranFoundVertex:
if totalNetwork.vertex_properties.binaryMappingVar[vnfFunction] == 0:
optimalRan.append(vnfFunction)
for vnfFunction in optimalRan:
if totalNetwork.vp.resources[vnfFunction] > maxVnfRes:
ranFoundVertex = vnfFunction
break
if not ranFoundVertex or type(ranFoundVertex) == list:
# print("Empty List of Found VNF Functions")
continue
else:
isNeighborMapped = False
for neighbor in ranFoundVertex.all_neighbors():
if totalNetwork.vp.binaryMappingVar[neighbor] == 1:
isNeighborMapped = True
break
else:
continue
# Case One
if isNeighborMapped == False:
if sortedSbsValList[0] >= totalNetwork.vp.resourceCapacity[ranFoundVertex]:
sbsFoundVertex = find_vertex(totalNetwork, totalNetwork.vp.resourceCapacity, sortedSbsValList[0])
foundSbsVert = True
else:
foundSbsVert = False
if foundSbsVert == False or not sbsFoundVertex:
print("Failed VNF Mapping Case One")
totalNetwork.vp.binaryMappingVar[ranFoundVertex] = 2
continue
else: # Mapping Case One
totalNetwork.add_edge(sbsFoundVertex[0], ranFoundVertex)
totalNetwork.vp.binaryMappingVar[ranFoundVertex] = 1
numOfMappings += 1
totalNetwork.vp.resourceCapacity[sbsFoundVertex[0]] -= totalNetwork.vertex_properties.resources[ranFoundVertex]
sortedSbsValList[0] = totalNetwork.vp.resourceCapacity[sbsFoundVertex[0]]
sortedSbsValList = sorted(sortedSbsValList, reverse=greedy_method)
else: # Mapping Case Two
selectedSbsTower = []
selectedCounter = 0
iterCountBand = 0
vnfMappedNeighbor = []
vnfSbsMappedNeighbor = []
mappableSbsTowers = []
unmappableTowers = set()
for vnf_neighbor in ranFoundVertex.all_neighbors():
if totalNetwork.vertex_properties.binaryMappingVar[vnf_neighbor] == 1:
vnfMappedNeighbor.append(vnf_neighbor)
substrateEdgeList = find_edge(totalNetwork, totalNetwork.ep.bandwidth, 0)
for edges in substrateEdgeList:
if edges.source() == vnf_neighbor:
vnfSbsMappedNeighbor.append(edges.target())
break
elif edges.target() == vnf_neighbor:
vnfSbsMappedNeighbor.append(edges.source())
break
for sbsResourceValue in sortedSbsValList:
possibleSbsTower = find_vertex(totalNetwork, totalNetwork.vp.resourceCapacity, sbsResourceValue)
isFoundSbs = False
while True:
if not possibleSbsTower:
break
elif possibleSbsTower[0] in mappableSbsTowers or possibleSbsTower[0] in unmappableTowers:
possibleSbsTower.pop(0)
else:
break
if not possibleSbsTower:
# print("List of Possible is Empty")
continue
if totalNetwork.vp.resourceCapacity[possibleSbsTower[0]] >= totalNetwork.vp.resources[ranFoundVertex]:
for mappedSbsTower in vnfSbsMappedNeighbor:
if possibleSbsTower[0] not in mappedSbsTower.all_neighbors():
if possibleSbsTower[0] == mappedSbsTower:
isFoundSbs = True
else:
isFoundSbs = False
unmappableTowers.add(possibleSbsTower[0])
break
else:
isFoundSbs = True
if isFoundSbs == True:
foundSbsVert = True
mappableSbsTowers.append(possibleSbsTower[0])
selectedSbsTower.append(selectedCounter)
selectedCounter += 1
else:
print("Resource not Satisfied Case Two")
unmappableTowers.add(possibleSbsTower[0])
if greedy_method == True:
break
else:
continue
if foundSbsVert == False:
print("VNF Mapping Failure Rescource and Connection Issue")
totalNetwork.vp.binaryMappingVar[ranFoundVertex] = 2
continue
else:
# Checking for Bandwidth
for possibleSbs in mappableSbsTowers:
for mappedNeighborCounter in range(len(vnfMappedNeighbor)):
vnfBandwidthConnection = 0
sbsBandwidthConnection = 0
# Find the Substrate Edge First
if vnfSbsMappedNeighbor[mappedNeighborCounter] == possibleSbs:
sbsFoundVertex = possibleSbs
foundSbsVert = True
continue
for edge in possibleSbs.all_edges():
if edge.source() == vnfSbsMappedNeighbor[mappedNeighborCounter] or edge.target() == vnfSbsMappedNeighbor[mappedNeighborCounter]:
sbsBandwidthConnection = edge
for edge in ranFoundVertex.all_edges():
if edge.source() == vnfMappedNeighbor[mappedNeighborCounter] or edge.target() == vnfMappedNeighbor[mappedNeighborCounter]:
vnfBandwidthConnection = edge
if totalNetwork.ep.bandwidth[sbsBandwidthConnection] >= totalNetwork.ep.bandwidth[vnfBandwidthConnection]:
foundSbsVert = True
else:
foundSbsVert = False
if foundSbsVert == True:
sbsFoundVertex = possibleSbs
break
else:
continue
if foundSbsVert == False:
print("VNF Mapping Failure Bandwidth Connection")
totalNetwork.vp.binaryMappingVar[ranFoundVertex] = 2
continue
else:
sortCount = 0
totalNetwork.add_edge(sbsFoundVertex, ranFoundVertex)
totalNetwork.vp.binaryMappingVar[ranFoundVertex] = 1
numOfMappings += 1
totalNetwork.vp.resourceCapacity[sbsFoundVertex] -= totalNetwork.vertex_properties.resources[ranFoundVertex]
# Search for the appropraite Index
sbsNetwork = find_vertex(totalNetwork, totalNetwork.vp.graphName, "Substrate")
for ctrVar in range(len(sortedSbsValList)):
for sbsTower in sbsNetwork:
if totalNetwork.vp.resourceCapacity[sbsTower] == sortedSbsValList[ctrVar]:
sortCount = ctrVar
sortedSbsValList[sortCount] = totalNetwork.vp.resourceCapacity[sbsFoundVertex]
sortedSbsValList = sorted(sortedSbsValList, reverse=greedy_method)
return numOfMappings;