NST selection changes- filenotfounderror
[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 import os
21 BASE_DIR = os.path.dirname(__file__)
22 """
23 This application generates NST SELECTION API calls using the information received from SO
24 """
25
26
27 def get_nst_solution(request_json):
28 # the file is in the same folder for now will move it to the conf folder of the has once its integrated there...
29     config_input_json = os.path.join(BASE_DIR, 'conf/configIinputs.json')
30     try:
31         with open(config_input_json, 'r') as openfile:
32             serviceProfile = request_json["serviceProfile"]
33             nstSolutionList = []
34             resourceName = "NST"
35             serviceProfileParameters = serviceProfile["serviceProfileParameters"]
36             nst_object = json.load(openfile)
37             for nst in nst_object[resourceName]:
38                 [(nstName, nstList)] = nst.items()
39                 individual_nst = dict()
40                 matchall = False
41                 for constraint_name in serviceProfileParameters:
42                     value = serviceProfileParameters[constraint_name]
43                     constraint_value = nstList.get(constraint_name)
44                     if (not constraint_value):
45                         matchall = False
46                         break
47                     else:
48                         matchall = True
49                 if matchall:
50                     individual_nst["NSTName"] = nstList.get("name")
51                     individual_nst["UUID"] = nstList.get("modeluuid")
52                     individual_nst["invariantUUID"] = nstList.get("modelinvariantuuid")
53                     individual_nst["individual_nst"] = 1
54                     nstSolutionList.append(individual_nst)
55
56         return nstSolutionList
57     except Exception as err:
58         raise err
59
60
61 def process_nst_selection( request_json, osdf_config):
62     """
63     Process a PCI request from a Client (build config-db, policy and  API call, make the call, return result)
64     :param req_object: Request parameters from the client
65     :param osdf_config: Configuration specific to OSDF application (core + deployment)
66     :return: response from NST Opt
67     """
68     solution = get_nst_solution(request_json)
69
70     return {
71         "requestId" : request_json['requestInfo']['requestId'],
72         "transactionId" : request_json['requestInfo']['transactionId'],
73         "statusMessage" : " ",
74         "requestStatus" : "accepted",
75         "solutions" : solution
76     }