take-1 for the NST selection function
[optf/osdf.git] / apps / nst / optimizers / nst_select_processor.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2020 Huawei Intellectual Property
3 #
4 #   Licensed under the Apache License, Version 2.0 (the "License");
5 #   you may not use this file except in compliance with the License.
6 #   You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #   Unless required by applicable law or agreed to in writing, software
11 #   distributed under the License is distributed on an "AS IS" BASIS,
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #   See the License for the specific language governing permissions and
14 #   limitations under the License.
15 #
16 # -------------------------------------------------------------------------
17
18
19 import json
20 from osdf.logging.osdf_logging import MH, audit_log
21 """
22 This application generates NST SELECTION API calls using the information received from SO
23 """
24
25 def buildSolution(request_json):
26     return {
27         "NSTsolution" : getNSTSolution(request_json)
28     }
29
30 def getNSTSolution(request_json):
31 # the file is in the same folder for now will move it to the conf folder o fthe has once its integrated there...
32     with open('./conf/configIinputs.json', 'r') as openfile:
33         serviceProfile = request_json["serviceProfile"]
34         resourceName = "NST"
35         serviceProfileParameters = serviceProfile["serviceProfileParameters"]
36         nst_object = json.load(openfile)
37         foundNst = False
38         for nst in nst_object[resourceName]:
39             [(nstName, nstList)] = nst.items()
40             matchall = False
41             for constraint_name in serviceProfileParameters:
42                 value = serviceProfileParameters[constraint_name]
43                 constraint_value= nstList[constraint_name]
44                 if constraint_value != value:
45                     matchall = False
46                     break
47                 else:
48                     matchall = True
49             if matchall:
50                 foundNst = True
51                 NSTName = nstList["name"]
52                 matchlevel = 1
53     if not(foundNst):
54         NSTName = None
55         matchlevel = 0
56     return {
57         "invariantUUID" : "INvariant UUID",
58         "UUID" : "uuid",
59         "NSTName" : NSTName,
60         "matchLevel" : matchlevel
61     }
62
63
64 def process_nst_selection( request_json, osdf_config):
65     """
66     Process a PCI request from a Client (build config-db, policy and  API call, make the call, return result)
67     :param req_object: Request parameters from the client
68     :param osdf_config: Configuration specific to OSDF application (core + deployment)
69     :return: response from NST Opt
70     """
71     solution = buildSolution(request_json)
72
73     return {
74         "requestId" : request_json['requestInfo']['requestId'],
75         "transactionId" : request_json['requestInfo']['transactionId'],
76         "statusMessage" : " ",
77         "requestStatus" : "accepted",
78         "solutions" : solution
79     }