aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--apps/slice_selection/optimizers/conductor/remote_opt_processor.py45
-rw-r--r--apps/slice_selection/optimizers/conductor/response_processor.py39
-rw-r--r--test/apps/slice_selection/new_solution_nsi_response.json27
-rw-r--r--test/apps/slice_selection/no_rec.json18
-rw-r--r--test/apps/slice_selection/no_recomm_conductor_response.json18
-rw-r--r--test/apps/slice_selection/no_recomm_nsi_response.json37
-rw-r--r--test/apps/slice_selection/not_shared_nsi_request.json31
-rw-r--r--test/apps/slice_selection/not_shared_nsi_response.json37
-rw-r--r--test/apps/slice_selection/nsi_request.json5
-rw-r--r--test/apps/slice_selection/shared_solution_nsi_response.json20
-rw-r--r--test/apps/slice_selection/test_remote_opt_processor.py223
11 files changed, 348 insertions, 152 deletions
diff --git a/apps/slice_selection/optimizers/conductor/remote_opt_processor.py b/apps/slice_selection/optimizers/conductor/remote_opt_processor.py
index edad9ba..b34c8b1 100644
--- a/apps/slice_selection/optimizers/conductor/remote_opt_processor.py
+++ b/apps/slice_selection/optimizers/conductor/remote_opt_processor.py
@@ -25,7 +25,7 @@ import traceback
from requests import RequestException
from apps.slice_selection.optimizers.conductor.response_processor \
- import conductor_response_processor, conductor_error_response_processor
+ import conductor_response_processor, conductor_error_response_processor, solution_with_only_slice_profile, get_nsi_selection_response
from osdf.adapters.conductor import conductor
from osdf.adapters.policy.interface import get_policies
from osdf.adapters.policy.utils import group_policies_gen
@@ -46,29 +46,40 @@ def process_nsi_selection_opt(request_json, osdf_config):
overall_recommendations = dict()
nst_info_map = dict()
+ new_nsi_solutions = list()
for nst_info in request_json["NSTInfoList"]:
nst_name = nst_info["modelName"]
- nst_info_map["nst_name"] = {"NSTName": nst_name,
- "UUID": nst_info["modelVersionId"],
- "invariantUUID": nst_info["modelInvariantId"]}
+ nst_info_map[nst_name] = {"NSTName": nst_name,
+ "UUID": nst_info["modelVersionId"],
+ "invariantUUID": nst_info["modelInvariantId"]}
- policy_request_json = request_json.copy()
- policy_request_json['serviceInfo']['serviceName'] = nst_name
+ if request_json["serviceProfile"]["resourceSharingLevel"] == "non-shared":
+ new_nsi_solution = solution_with_only_slice_profile(request_json['serviceProfile'], nst_info_map.get(nst_name))
+ new_nsi_solutions.append(new_nsi_solution)
+ else:
+ policy_request_json = request_json.copy()
+ policy_request_json['serviceInfo']['serviceName'] = nst_name
+ policies = get_policies(policy_request_json, "slice_selection")
- policies = get_policies(policy_request_json, "slice_selection")
+ demands = get_slice_demands(nst_name, policies, osdf_config.core)
- demands = get_slice_demands(nst_name, policies, osdf_config.core)
-
- request_parameters = request_json.get('serviceProfile',{})
- service_info = {}
- req_info['numSolutions'] = 'all'
- resp = conductor.request(req_info, demands, request_parameters, service_info, False,
+ request_parameters = request_json.get('serviceProfile',{})
+ service_info = {}
+ req_info['numSolutions'] = 'all'
+ resp = conductor.request(req_info, demands, request_parameters, service_info, False,
osdf_config, policies)
- debug_log.debug("Response from conductor {}".format(str(resp)))
- overall_recommendations[nst_name] = resp["plans"][0].get("recommendations")
-
- return conductor_response_processor(overall_recommendations, nst_info_map, req_info)
+ if resp["plans"][0].get("status") == "not found":
+ resp["recommendations"] = list()
+ debug_log.debug("Response from conductor {}".format(str(resp)))
+ overall_recommendations[nst_name] = resp["plans"][0].get("recommendations")
+ if request_json["serviceProfile"]["resourceSharingLevel"] == "non-shared":
+ solutions = dict()
+ solutions['newNSISolutions'] = new_nsi_solutions
+ solutions['sharedNSISolutions'] = []
+ return get_nsi_selection_response(req_info, solutions)
+ else:
+ return conductor_response_processor(overall_recommendations, nst_info_map, req_info, request_json["serviceProfile"])
except Exception as ex:
error_log.error("Error for {} {}".format(req_info.get('requestId'),
traceback.format_exc()))
diff --git a/apps/slice_selection/optimizers/conductor/response_processor.py b/apps/slice_selection/optimizers/conductor/response_processor.py
index c175c1a..9b57092 100644
--- a/apps/slice_selection/optimizers/conductor/response_processor.py
+++ b/apps/slice_selection/optimizers/conductor/response_processor.py
@@ -23,15 +23,15 @@ Module for processing response from conductor for slice selection
from osdf.logging.osdf_logging import debug_log
-SLICE_PROFILE_FIELDS = ["latency", "max_number_of_ues", "coverage_area_ta_list",
- "ue_mobility_level", "resource_sharing_level", "exp_data_rate_ul",
- "exp_data_rate_dl", "area_traffic_cap_ul", "area_traffic_cap_dl",
- "activity_factor", "e2e_latency", "jitter", "survival_time",
- "exp_data_rate", "payload_size", "traffic_density", "conn_density",
- "reliability", "service_area_dimension", "cs_availability"]
+SLICE_PROFILE_FIELDS = {"latency":"latency", "max_number_of_ues":"maxNumberOfUEs", "coverage_area_ta_list": "coverageAreaTAList",
+ "ue_mobility_level":"uEMobilityLevel", "resource_sharing_level":"resourceSharingLevel", "exp_data_rate_ul": "expDataRateUL",
+ "exp_data_rate_dl":"expDataRateDL", "area_traffic_cap_ul":"areaTrafficCapUL", "area_traffic_cap_dl": "areaTrafficCapDL",
+ "activity_factor":"activityFactor", "e2e_latency":"e2eLatency", "jitter":"jitter", "survival_time": "survivalTime",
+ "exp_data_rate":"expDataRate", "payload_size":"payloadSize", "traffic_density":"trafficDensity", "conn_density":"connDensity",
+ "reliability":"reliability", "service_area_dimension":"serviceAreaDimension", "cs_availability": "csAvailability"}
-def conductor_response_processor(overall_recommendations, nst_info_map, request_info):
+def conductor_response_processor(overall_recommendations, nst_info_map, request_info, service_profile):
"""Process conductor response to form the response for the API request
:param overall_recommendations: recommendations from conductor
:param nst_info_map: NST info from the request
@@ -40,8 +40,12 @@ def conductor_response_processor(overall_recommendations, nst_info_map, request_
"""
shared_nsi_solutions = list()
new_nsi_solutions = list()
-
for nst_name, recommendations in overall_recommendations.items():
+ if not (recommendations):
+ new_nsi_solution = solution_with_only_slice_profile(service_profile, nst_info_map.get(nst_name))
+ new_nsi_solutions.append(new_nsi_solution)
+ continue
+
for recommendation in recommendations:
nsi_set = set(values['candidate']['nsi_id'] for key, values in recommendation.items())
if len(nsi_set) == 1:
@@ -82,6 +86,14 @@ def conductor_response_processor(overall_recommendations, nst_info_map, request_
return get_nsi_selection_response(request_info, solutions)
+def solution_with_only_slice_profile(service_profile, nst_info):
+ nssi_solutions = get_slice_profile_from_service_profile(service_profile)
+ new_nsi_solution = dict()
+ new_nsi_solution['matchLevel'] = ""
+ new_nsi_solution['NSTInfo'] = nst_info
+ new_nsi_solution['NSSISolutions'] = nssi_solutions
+ return new_nsi_solution
+
def conductor_error_response_processor(request_info, error_message):
"""Form response message from the error message
:param request_info: request info
@@ -94,6 +106,14 @@ def conductor_error_response_processor(request_info, error_message):
'statusMessage': error_message}
+def get_slice_profile_from_service_profile(service_profile):
+ nssi_solutions = list()
+ service_profile["domainType"] = "cn"
+ nssi_solution = {"sliceProfile": service_profile}
+ nssi_solutions.append(nssi_solution)
+ return nssi_solutions
+
+
def get_nssi_solutions(recommendation):
"""Get nssi solutions from recommendation
:param recommendation: recommendation from conductor
@@ -123,7 +143,7 @@ def get_solution_from_candidate(candidate):
for field in SLICE_PROFILE_FIELDS:
if candidate[field]:
- slice_profile[field] = candidate[field]
+ slice_profile[SLICE_PROFILE_FIELDS[field]] = candidate[field]
return nssi_info, slice_profile
@@ -139,3 +159,4 @@ def get_nsi_selection_response(request_info, solutions):
'requestStatus': 'completed',
'statusMessage': '',
'solutions': solutions}
+
diff --git a/test/apps/slice_selection/new_solution_nsi_response.json b/test/apps/slice_selection/new_solution_nsi_response.json
index de248c6..02023ce 100644
--- a/test/apps/slice_selection/new_solution_nsi_response.json
+++ b/test/apps/slice_selection/new_solution_nsi_response.json
@@ -10,16 +10,19 @@
"newNSISolutions":[
{
"matchLevel":"",
- "NSTInfo":null,
+ "NSTInfo":{"invariantUUID": "fda3c1e8-7653-4acd-80ef-f5755c1d3859",
+ "UUID": "a6906768-1cae-4e78-acd1-d753ac61f3e8",
+ "NSTName": "URLLC_1"
+ },
"NSSISolutions":[
{
"sliceProfile":{
"latency":20,
- "coverage_area_ta_list":"[{\"province\":\"??\",\"city\":\"???\",\"county\":\"???\",\"street\":\"?????\"}]",
- "ue_mobility_level":"stationary",
- "resource_sharing_level":"0",
- "exp_data_rate_ul":100,
- "exp_data_rate_dl":100
+ "coverageAreaTAList":"[{\"province\":\"??\",\"city\":\"???\",\"county\":\"???\",\"street\":\"?????\"}]",
+ "uEMobilityLevel":"stationary",
+ "resourceSharingLevel":"0",
+ "expDataRateUL":100,
+ "expDataRateDL":100
},
"NSSTInfo":{
"NSSTName":"URLLC_Core_1"
@@ -32,11 +35,11 @@
{
"sliceProfile":{
"latency":15,
- "coverage_area_ta_list":"[{\"province\":\"??\",\"city\":\"???\",\"county\":\"???\",\"street\":\"?????\"}]",
- "ue_mobility_level":"stationary",
- "resource_sharing_level":"0",
- "exp_data_rate_ul":100,
- "exp_data_rate_dl":100
+ "coverageAreaTAList":"[{\"province\":\"??\",\"city\":\"???\",\"county\":\"???\",\"street\":\"?????\"}]",
+ "uEMobilityLevel":"stationary",
+ "resourceSharingLevel":"0",
+ "expDataRateUL":100,
+ "expDataRateDL":100
},
"NSSTInfo":{
"NSSTName":"URLLC_Ran_1"
@@ -50,4 +53,4 @@
}
]
}
-} \ No newline at end of file
+}
diff --git a/test/apps/slice_selection/no_rec.json b/test/apps/slice_selection/no_rec.json
new file mode 100644
index 0000000..855afac
--- /dev/null
+++ b/test/apps/slice_selection/no_rec.json
@@ -0,0 +1,18 @@
+{
+ "plans": [
+ {
+ "id": "d8c07237-5f66-4aa6-871c-a04221d99458",
+ "links": [
+ [
+ {
+ "href": "https://oof-has-api:8091/v1/plans/d8c07237-5f66-4aa6-871c-a04221d99458",
+ "rel": "self"
+ }
+ ]
+ ],
+ "message": "Plan d8c07237-5f66-4aa6-871c-a04221d99458 search failed, no recommendations found by machine dev-oof-has-solver-65d478b6d-ql5fp",
+ "name": "d290f1ee-6c54-4b01-90e6-d701748f0851",
+ "status": "not found"
+ }
+ ]
+}
diff --git a/test/apps/slice_selection/no_recomm_conductor_response.json b/test/apps/slice_selection/no_recomm_conductor_response.json
new file mode 100644
index 0000000..f23af87
--- /dev/null
+++ b/test/apps/slice_selection/no_recomm_conductor_response.json
@@ -0,0 +1,18 @@
+{
+ "plans":[
+ {
+ "status":"done",
+ "id":"plan_id",
+ "name":"Plan Name 1",
+ "links":[
+ [
+ {
+ "href":"http://conductor:8091/v1/plans/plan_id",
+ "rel":"self"
+ }
+ ]
+ ],
+ "recommendations": []
+ }
+ ]
+}
diff --git a/test/apps/slice_selection/no_recomm_nsi_response.json b/test/apps/slice_selection/no_recomm_nsi_response.json
new file mode 100644
index 0000000..daf151d
--- /dev/null
+++ b/test/apps/slice_selection/no_recomm_nsi_response.json
@@ -0,0 +1,37 @@
+{
+ "requestId":"d290f1ee-6c54-4b01-90e6-d701748f0851",
+ "transactionId":"d290f1ee-6c54-4b01-90e6-d701748f0851",
+ "requestStatus":"completed",
+ "statusMessage":"",
+ "solutions":{
+ "sharedNSISolutions":[
+
+ ],
+ "newNSISolutions":[
+ {
+ "matchLevel":"",
+ "NSTInfo":{"invariantUUID": "fda3c1e8-7653-4acd-80ef-f5755c1d3859",
+ "UUID": "a6906768-1cae-4e78-acd1-d753ac61f3e8",
+ "NSTName": "URLLC_1"
+ },
+
+ "NSSISolutions":[
+ {
+ "sliceProfile": {
+ "latency": 2,
+ "security": "High",
+ "reliability": 99.9999,
+ "trafficDensity": 1,
+ "connDensity": 100000,
+ "expDataRate": 50,
+ "jitter": 1,
+ "survivalTime": 0,
+ "domainType":"cn",
+ "resourceSharingLevel":"shared"
+ }
+ }
+ ]
+ }
+ ]
+ }
+}
diff --git a/test/apps/slice_selection/not_shared_nsi_request.json b/test/apps/slice_selection/not_shared_nsi_request.json
new file mode 100644
index 0000000..1e22f41
--- /dev/null
+++ b/test/apps/slice_selection/not_shared_nsi_request.json
@@ -0,0 +1,31 @@
+{
+ "serviceProfile": {
+ "latency": 2,
+ "security": "High",
+ "reliability": 99.9999,
+ "trafficDensity": 1,
+ "connDensity": 100000,
+ "expDataRate": 50,
+ "jitter": 1,
+ "survivalTime": 0,
+ "resourceSharingLevel": "non-shared"
+ },
+ "serviceInfo":{
+ "serviceInstanceId": "209fb01e-60ca-4325-b074-c5ad4e0499f8",
+ "serviceName": ""
+ },
+ "requestInfo": {
+ "transactionId": "d290f1ee-6c54-4b01-90e6-d701748f0851",
+ "requestId": "d290f1ee-6c54-4b01-90e6-d701748f0851",
+ "callbackUrl": "http://0.0.0.0:9000/osdfCallback/",
+ "sourceId": "SO",
+ "timeout": 5
+ },
+ "NSTInfoList": [
+ {
+ "modelInvariantId": "fda3c1e8-7653-4acd-80ef-f5755c1d3859",
+ "modelVersionId": "a6906768-1cae-4e78-acd1-d753ac61f3e8",
+ "modelName": "URLLC_1"
+ }
+ ]
+}
diff --git a/test/apps/slice_selection/not_shared_nsi_response.json b/test/apps/slice_selection/not_shared_nsi_response.json
new file mode 100644
index 0000000..873ad2e
--- /dev/null
+++ b/test/apps/slice_selection/not_shared_nsi_response.json
@@ -0,0 +1,37 @@
+{
+ "requestId":"d290f1ee-6c54-4b01-90e6-d701748f0851",
+ "transactionId":"d290f1ee-6c54-4b01-90e6-d701748f0851",
+ "requestStatus":"completed",
+ "statusMessage":"",
+ "solutions":{
+ "sharedNSISolutions":[
+
+ ],
+ "newNSISolutions":[
+ {
+ "matchLevel":"",
+ "NSTInfo":{"invariantUUID": "fda3c1e8-7653-4acd-80ef-f5755c1d3859",
+ "UUID": "a6906768-1cae-4e78-acd1-d753ac61f3e8",
+ "NSTName": "URLLC_1"
+ },
+
+ "NSSISolutions":[
+ {
+ "sliceProfile": {
+ "latency": 2,
+ "security": "High",
+ "reliability": 99.9999,
+ "trafficDensity": 1,
+ "connDensity": 100000,
+ "expDataRate": 50,
+ "jitter": 1,
+ "survivalTime": 0,
+ "domainType": "cn",
+ "resourceSharingLevel": "non-shared"
+ }
+ }
+ ]
+ }
+ ]
+ }
+}
diff --git a/test/apps/slice_selection/nsi_request.json b/test/apps/slice_selection/nsi_request.json
index 69d6e80..72b2c8b 100644
--- a/test/apps/slice_selection/nsi_request.json
+++ b/test/apps/slice_selection/nsi_request.json
@@ -7,7 +7,8 @@
"connDensity": 100000,
"expDataRate": 50,
"jitter": 1,
- "survivalTime": 0
+ "survivalTime": 0,
+ "resourceSharingLevel":"shared"
},
"serviceInfo":{
"serviceInstanceId": "209fb01e-60ca-4325-b074-c5ad4e0499f8",
@@ -27,4 +28,4 @@
"modelName": "URLLC_1"
}
]
-} \ No newline at end of file
+}
diff --git a/test/apps/slice_selection/shared_solution_nsi_response.json b/test/apps/slice_selection/shared_solution_nsi_response.json
index 2c74196..46257dd 100644
--- a/test/apps/slice_selection/shared_solution_nsi_response.json
+++ b/test/apps/slice_selection/shared_solution_nsi_response.json
@@ -15,12 +15,12 @@
"invariantUUID": "",
"sliceProfile": [
{
- "coverage_area_ta_list": "[{\"province\":\"??\",\"city\":\"???\",\"county\":\"???\",\"street\":\"?????\"}]",
- "exp_data_rate_dl": 100,
- "exp_data_rate_ul": 100,
+ "coverageAreaTAList": "[{\"province\":\"??\",\"city\":\"???\",\"county\":\"???\",\"street\":\"?????\"}]",
+ "expDataRateDL": 100,
+ "expDataRateUL": 100,
"latency": 20,
- "resource_sharing_level": "0",
- "ue_mobility_level": "stationary"
+ "resourceSharingLevel": "0",
+ "uEMobilityLevel": "stationary"
}
]
},
@@ -31,12 +31,12 @@
"invariantUUID": "",
"sliceProfile": [
{
- "coverage_area_ta_list": "[{\"province\":\"??\",\"city\":\"???\",\"county\":\"???\",\"street\":\"?????\"}]",
- "exp_data_rate_dl": 100,
- "exp_data_rate_ul": 100,
+ "coverageAreaTAList": "[{\"province\":\"??\",\"city\":\"???\",\"county\":\"???\",\"street\":\"?????\"}]",
+ "expDataRateDL": 100,
+ "expDataRateUL": 100,
"latency": 15,
- "resource_sharing_level": "0",
- "ue_mobility_level": "stationary"
+ "resourceSharingLevel": "0",
+ "uEMobilityLevel": "stationary"
}
]
}
diff --git a/test/apps/slice_selection/test_remote_opt_processor.py b/test/apps/slice_selection/test_remote_opt_processor.py
index d9b4f24..136bb71 100644
--- a/test/apps/slice_selection/test_remote_opt_processor.py
+++ b/test/apps/slice_selection/test_remote_opt_processor.py
@@ -1,102 +1,121 @@
-# -------------------------------------------------------------------------
-# Copyright (C) 2020 Wipro Limited.
-#
-# 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 json
-import unittest
-from requests import RequestException
-
-from apps.slice_selection.optimizers.conductor.remote_opt_processor import process_nsi_selection_opt
-from osdf.adapters.local_data import local_policies
-from osdf.utils.interfaces import json_from_file, yaml_from_file
-from osdf.utils.programming_utils import DotDict
-import osdf.config.loader as config_loader
-from mock import patch, MagicMock
-import json
-from osdf.logging.osdf_logging import error_log, debug_log
-from osdf.adapters.policy.interface import get_policies
-
-
-class TestRemoteOptProcessor(unittest.TestCase):
- def setUp(self):
- self.config_spec = {
- "deployment": "config/osdf_config.yaml",
- "core": "config/common_config.yaml"
- }
- self.osdf_config = DotDict(config_loader.all_configs(**self.config_spec))
-
- def tearDown(self):
- patch.stopall()
-
- def test_process_nsi_selection_opt(self):
- main_dir = ""
- request_file = main_dir + 'test/apps/slice_selection/nsi_request.json'
- new_solution_response_file = main_dir + 'test/apps/slice_selection/new_solution_nsi_response.json'
- shared_solution_response_file = main_dir + 'test/apps/slice_selection/shared_solution_nsi_response.json'
- error_response_file = main_dir + 'test/apps/slice_selection/nsi_error_response.json'
-
- request_json = json_from_file(request_file)
- new_solution_response_json = json_from_file(new_solution_response_file)
- shared_solution_response_json = json_from_file(shared_solution_response_file)
- error_response_json = json_from_file(error_response_file)
-
- policies_path = main_dir + 'test/policy-local-files'
- slice_policies_file = main_dir + 'test/apps/slice_selection/slice_policies.txt'
-
- valid_policies_files = local_policies.get_policy_names_from_file(slice_policies_file)
- policies = [json_from_file(policies_path + '/' + name) for name in valid_policies_files]
- self.patcher_get_policies = patch('osdf.adapters.policy.interface.remote_api',
- return_value=policies)
- self.Mock_get_policies = self.patcher_get_policies.start()
-
- new_solution_conductor_response_file = 'test/apps/slice_selection/new_solution_conductor_response.json'
- new_solution_conductor_response = json_from_file(new_solution_conductor_response_file)
- self.patcher_req = patch('osdf.adapters.conductor.conductor.request',
- return_value=new_solution_conductor_response)
- self.Mock_req = self.patcher_req.start()
- self.assertEquals(new_solution_response_json, process_nsi_selection_opt(request_json, self.osdf_config))
- self.patcher_req.stop()
-
- shared_solution_conductor_response_file = 'test/apps/slice_selection/shared_solution_conductor_response.json'
- shared_solution_conductor_response = json_from_file(shared_solution_conductor_response_file)
- self.patcher_req = patch('osdf.adapters.conductor.conductor.request',
- return_value=shared_solution_conductor_response)
- self.Mock_req = self.patcher_req.start()
- self.assertEquals(shared_solution_response_json,
- process_nsi_selection_opt(request_json, self.osdf_config))
- self.patcher_req.stop()
-
- conductor_error_response_file = 'test/apps/slice_selection/conductor_error_response.json'
- conductor_error_response = json_from_file(conductor_error_response_file)
-
- self.patcher_req = patch('osdf.adapters.conductor.conductor.request',
- side_effect=RequestException(response=json.dumps(conductor_error_response)))
- self.Mock_req = self.patcher_req.start()
- self.assertEquals(error_response_json, process_nsi_selection_opt(request_json, self.osdf_config))
- self.patcher_req.stop()
-
- self.patcher_req = patch('osdf.adapters.conductor.conductor.request',
- side_effect=Exception("test_exception"))
- self.Mock_req = self.patcher_req.start()
- self.assertEquals('test_exception',
- process_nsi_selection_opt(request_json, self.osdf_config).get('statusMessage'))
- self.patcher_req.stop()
-
-
-if __name__ == "__main__":
- unittest.main()
-
+# -------------------------------------------------------------------------
+# Copyright (C) 2020 Wipro Limited.
+#
+# 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 json
+import unittest
+from requests import RequestException
+
+from apps.slice_selection.optimizers.conductor.remote_opt_processor import process_nsi_selection_opt
+from osdf.adapters.local_data import local_policies
+from osdf.utils.interfaces import json_from_file, yaml_from_file
+from osdf.utils.programming_utils import DotDict
+import osdf.config.loader as config_loader
+from mock import patch, MagicMock
+import json
+from osdf.logging.osdf_logging import error_log, debug_log
+from osdf.adapters.policy.interface import get_policies
+
+
+class TestRemoteOptProcessor(unittest.TestCase):
+ def setUp(self):
+ self.config_spec = {
+ "deployment": "config/osdf_config.yaml",
+ "core": "config/common_config.yaml"
+ }
+ self.osdf_config = DotDict(config_loader.all_configs(**self.config_spec))
+
+ def tearDown(self):
+ patch.stopall()
+
+ def test_process_nsi_selection_opt(self):
+ main_dir = ""
+ request_file = main_dir + 'test/apps/slice_selection/nsi_request.json'
+ not_shared_request_file = main_dir + 'test/apps/slice_selection/not_shared_nsi_request.json'
+ #response files
+ new_solution_response_file = main_dir + 'test/apps/slice_selection/new_solution_nsi_response.json'
+ shared_solution_response_file = main_dir + 'test/apps/slice_selection/shared_solution_nsi_response.json'
+ no_solution_response_file = main_dir + 'test/apps/slice_selection/no_recomm_nsi_response.json'
+ not_shared_response_file = main_dir + 'test/apps/slice_selection/not_shared_nsi_response.json'
+ error_response_file = main_dir + 'test/apps/slice_selection/nsi_error_response.json'
+
+ not_shared_request_json = json_from_file(not_shared_request_file)
+ not_shared_response_json = json_from_file(not_shared_response_file)
+ request_json = json_from_file(request_file)
+ new_solution_response_json = json_from_file(new_solution_response_file)
+ shared_solution_response_json = json_from_file(shared_solution_response_file)
+ no_solution_response_json = json_from_file(no_solution_response_file)
+ error_response_json = json_from_file(error_response_file)
+
+ policies_path = main_dir + 'test/policy-local-files'
+ slice_policies_file = main_dir + 'test/apps/slice_selection/slice_policies.txt'
+
+ valid_policies_files = local_policies.get_policy_names_from_file(slice_policies_file)
+ policies = [json_from_file(policies_path + '/' + name) for name in valid_policies_files]
+ self.patcher_get_policies = patch('osdf.adapters.policy.interface.remote_api',
+ return_value=policies)
+ self.Mock_get_policies = self.patcher_get_policies.start()
+ # new solution
+ new_solution_conductor_response_file = 'test/apps/slice_selection/new_solution_conductor_response.json'
+ new_solution_conductor_response = json_from_file(new_solution_conductor_response_file)
+ self.patcher_req = patch('osdf.adapters.conductor.conductor.request',
+ return_value=new_solution_conductor_response)
+ self.Mock_req = self.patcher_req.start()
+ self.assertEquals(new_solution_response_json, process_nsi_selection_opt(request_json, self.osdf_config))
+ self.patcher_req.stop()
+ # shared solution
+ shared_solution_conductor_response_file = 'test/apps/slice_selection/shared_solution_conductor_response.json'
+ shared_solution_conductor_response = json_from_file(shared_solution_conductor_response_file)
+ self.patcher_req = patch('osdf.adapters.conductor.conductor.request',
+ return_value=shared_solution_conductor_response)
+ self.Mock_req = self.patcher_req.start()
+ self.assertEquals(shared_solution_response_json,
+ process_nsi_selection_opt(request_json, self.osdf_config))
+ self.patcher_req.stop()
+ # not-shared solution
+ self.assertEquals(not_shared_response_json,
+ process_nsi_selection_opt(not_shared_request_json, self.osdf_config))
+ # no recommendation
+ no_solution_conductor_response_file = 'test/apps/slice_selection/no_rec.json'
+ no_solution_conductor_response = json_from_file(no_solution_conductor_response_file)
+ self.patcher_req = patch('osdf.adapters.conductor.conductor.request',
+ return_value=no_solution_conductor_response)
+ self.Mock_req = self.patcher_req.start()
+ self.assertEquals(no_solution_response_json,
+ process_nsi_selection_opt(request_json, self.osdf_config))
+ self.patcher_req.stop()
+
+ conductor_error_response_file = 'test/apps/slice_selection/conductor_error_response.json'
+ conductor_error_response = json_from_file(conductor_error_response_file)
+
+ self.patcher_req = patch('osdf.adapters.conductor.conductor.request',
+ side_effect=RequestException(response=json.dumps(conductor_error_response)))
+ self.Mock_req = self.patcher_req.start()
+ self.assertEquals(error_response_json, process_nsi_selection_opt(request_json, self.osdf_config))
+ self.patcher_req.stop()
+
+ self.patcher_req = patch('osdf.adapters.conductor.conductor.request',
+ side_effect=Exception("test_exception"))
+ self.Mock_req = self.patcher_req.start()
+ self.assertEquals('test_exception',
+ process_nsi_selection_opt(request_json, self.osdf_config).get('statusMessage'))
+ self.patcher_req.stop()
+
+
+if __name__ == "__main__":
+ unittest.main()
+