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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
# -------------------------------------------------------------------------
# Copyright (c) 2020 Huawei Intellectual Property
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# -------------------------------------------------------------------------
#
import requests
from requests.auth import HTTPBasicAuth
from osdf.utils.mdc_utils import mdc_from_json
from osdf.logging.osdf_logging import MH, audit_log, error_log, debug_log
import pymzn
from sklearn import preprocessing
import os
BASE_DIR = os.path.dirname(__file__)
class RouteOpt:
"""
This values will need to deleted..
only added for the debug purpose
"""
# DNS server and standard port of AAI..
# TODO: read the port from the configuration and add to DNS
aai_host = "https://aai.api.simpledemo.onap.org:8443"
audit_log.info("base directory")
audit_log.info(BASE_DIR)
aai_headers = {
"X-TransactionId": "9999",
"X-FromAppId": "OOF",
"Accept": "application/json",
"Content-Type": "application/json",
"Real-Time": "true"
}
def isCrossONAPLink(self, logical_link):
"""
This method checks if cross link is cross onap
:param logical_link:
:return:
"""
for relationship in logical_link["relationship-list"]["relationship"]:
if relationship["related-to"] == "ext-aai-network":
return True
return False
def getLinksName(self, routes,initial_start_edge,initial_end_edge, mappingTable):
routes=list(routes)
arr=routes[0]['x']
listOfLinks=[]
for i in range(0, len(routes[0]['x'])):
if arr[i] == 1 :
# listOfLinks.append(self.fetchLogicalLinks(initial_start_edge[i], initial_end_edge[i], mappingTable))
listOfLinks.append(mappingTable[initial_start_edge[i] + ":" + initial_end_edge[i]])
return listOfLinks
# def search(self, ip1, ip2, dic):
# if ip1 == "" or ip2 == "":
# return ""
# else:
# string = ip1 + ":" + ip2
# return dic[string]
#
# def fetchLogicalLinks(self, initial_start_edge, initial_end_edge, mappingTable):
# link_name=self.search(initial_start_edge, initial_end_edge, mappingTable)
# return link_name
# def fetchLogicalLinks(self, initial_start_edge, initial_end_edge, mappingTable):
# return mappingTable[initial_start_edge + ":" + initial_end_edge]
def solve(self, mzn_model, dzn_data):
return pymzn.minizinc(mzn=mzn_model, data=dzn_data)
def getLinks(self, mzn_model, dzn_data, initial_start_edge,initial_end_edge, mappingTable):
routes = self.solve(mzn_model, dzn_data)
audit_log.info("mocked minizinc solution====>")
audit_log.info(routes)
converted_links=self.getLinksName(routes, initial_start_edge,initial_end_edge, mappingTable)
audit_log.info("converted links===>")
audit_log.info(converted_links)
return converted_links
def addition(self, data):
relationship = data["relationship-list"]["relationship"]
res = ""
for index, eachItem in enumerate(relationship):
if index == len(relationship) - 1:
res += eachItem["accessNodeId"]
else:
res += eachItem["accessNodeId"] + ":"
return data["link-name"], res
def createMapTable(self, logical_links):
result = map(self.addition, logical_links)
parseTemplate = {}
for eachItem in result:
parseTemplate[eachItem[1]] = eachItem[0]
audit_log.info("mapping table")
audit_log.info(parseTemplate)
return parseTemplate
def build_dzn_data(self, src_access_node_id, dst_access_node_id):
Edge_Start = []
Edge_End = []
logical_links = self.get_logical_links()
audit_log.info("mocked response of AAI received (logical links) successful===>")
audit_log.info(logical_links)
# prepare map table
mappingTable = self.createMapTable(logical_links)
# take the logical link where both the p-interface in same onap
if logical_links is not None:
for logical_link in logical_links:
if not self.isCrossONAPLink(logical_link):
# link is in local ONAP
relationship = logical_link["relationship-list"]["relationship"]
relationshipStartNode = relationship[0]
relationshipStartNodeID = relationshipStartNode["related-link"].split("/")[-1]
start_accessNodeId = relationshipStartNodeID.split("-")[-3]
Edge_Start.append(start_accessNodeId)
relationshipEndtNode = relationship[1]
relationshipEndNodeID = relationshipEndtNode["related-link"].split("/")[-1]
end_accessNodeId = relationshipEndNodeID.split("-")[-3]
Edge_End.append(end_accessNodeId)
audit_log.info("edge start and end array of i/p address are===>")
audit_log.info(Edge_Start)
audit_log.info(Edge_End)
# labeling ip to number for mapping
le = preprocessing.LabelEncoder()
le.fit(Edge_Start + Edge_End)
# print(le.classes_)
dzn_start_edge = le.transform(Edge_Start)
final_dzn_start_arr = []
for i in range(0, len(dzn_start_edge)):
final_dzn_start_arr.append(dzn_start_edge[i])
final_dzn_end_arr = []
dzn_end_edge = le.transform(Edge_End)
for j in range(0, len(dzn_end_edge)):
final_dzn_end_arr.append(dzn_end_edge[j])
audit_log.info("start and end array that passed in dzn_data===>")
audit_log.info(final_dzn_start_arr)
audit_log.info(final_dzn_end_arr)
link_cost = []
for k in range(0, len(final_dzn_start_arr)):
link_cost.append(1)
audit_log.info("src_access_node_id")
audit_log.info(src_access_node_id)
source= le.transform([src_access_node_id])
audit_log.info("vallue of source===>")
audit_log.info(source)
if source in final_dzn_start_arr :
start = source[0]
audit_log.info("source node")
audit_log.info(start)
audit_log.info("dst_access_node_id")
audit_log.info(dst_access_node_id)
destination= le.transform([dst_access_node_id])
if destination in final_dzn_end_arr :
end = destination[0]
audit_log.info("destination node")
audit_log.info(end)
# data to be prepared in the below format:
dzn_data = {
'N': self.total_node(final_dzn_start_arr + final_dzn_end_arr),
'M': len(final_dzn_start_arr),
'Edge_Start': final_dzn_start_arr,
'Edge_End': final_dzn_end_arr,
'L': link_cost,
'Start': start,
'End': end,
}
# can not do reverse mapping outside of this scope, so doing here
audit_log.info("reverse mapping after prepared dzn_data")
initial_start_edge=le.inverse_transform(final_dzn_start_arr)
initial_end_edge=le.inverse_transform(final_dzn_end_arr)
audit_log.info(initial_start_edge)
audit_log.info(initial_end_edge)
return dzn_data, initial_start_edge,initial_end_edge, mappingTable
def total_node(self, node):
nodeSet = set()
for i in range(0, len(node)):
nodeSet.add(node[i])
total_node = len(nodeSet)
return total_node
def getRoute(self, request):
"""
This method checks
:param logical_link:
:return:
"""
routeInfo = request["routeInfo"]["routeRequests"]
routeRequest = routeInfo[0]
src_access_node_id = routeRequest["srcPort"]["accessNodeId"]
dst_access_node_id = routeRequest["dstPort"]["accessNodeId"]
dzn_data, initial_start_edge, initial_end_edge, mappingTable = self.build_dzn_data(src_access_node_id, dst_access_node_id )
#mzn_model = "/home/root1/Videos/projects/osdf/test/functest/simulators/osdf/optimizers/routeopt/route_opt.mzn"
mzn_model = os.path.join(BASE_DIR, 'route_opt.mzn')
routeSolutions = self.getLinks(mzn_model, dzn_data, initial_start_edge,initial_end_edge, mappingTable)
return {
"requestId": request["requestInfo"]["requestId"],
"transactionId": request["requestInfo"]["transactionId"],
"statusMessage": " ",
"requestStatus": "accepted",
"solutions": routeSolutions
}
def get_logical_links(self):
"""
This method returns list of all cross ONAP links
from /aai/v14/network/logical-links?operation-status="Up"
:return: logical-links[]
"""
logical_link_url = "/aai/v13/network/logical-links?operational-status=up"
aai_req_url = self.aai_host + logical_link_url
response = requests.get(aai_req_url,headers=self.aai_headers,auth=HTTPBasicAuth("AAI", "AAI"),verify=False)
if response.status_code == 200:
return response.json()
|