update link to upper-constraints.txt
[optf/osdf.git] / test / conductor / test_conductor_translation.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2017-2018 AT&T Intellectual Property
3 #   Copyright (C) 2020 Wipro Limited.
4 #
5 #   Licensed under the Apache License, Version 2.0 (the "License");
6 #   you may not use this file except in compliance with the License.
7 #   You may obtain a copy of the License at
8 #
9 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #   Unless required by applicable law or agreed to in writing, software
12 #   distributed under the License is distributed on an "AS IS" BASIS,
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #   See the License for the specific language governing permissions and
15 #   limitations under the License.
16 #
17 # -------------------------------------------------------------------------
18 #
19 import unittest
20
21 from osdf.adapters.local_data import local_policies
22 from osdf.adapters.conductor import translation as tr
23 from osdf.utils.interfaces import json_from_file
24
25
26 class TestConductorTranslation(unittest.TestCase):
27
28     def setUp(self):
29         self.main_dir = ""
30         self.conductor_api_template = self.main_dir + "osdf/templates/conductor_interface.json"
31         self.local_config_file = self.main_dir + "config/common_config.yaml"
32         policy_data_path = self.main_dir + "test/policy-local-files"
33
34         valid_policies_list_file = policy_data_path + '/' + 'meta-valid-policies.txt'
35         valid_policies_files = local_policies.get_policy_names_from_file(valid_policies_list_file)
36
37         parameter_data_file = self.main_dir + "test/placement-tests/request.json"
38         self.request_json = json_from_file(parameter_data_file)
39         parameter_data_file = self.main_dir + "test/placement-tests/request_vfmod.json"
40         self.request_vfmod_json = json_from_file(parameter_data_file)
41         self.policies = [json_from_file(policy_data_path + '/' + name) for name in valid_policies_files]
42
43         self.optimization_policies = [json_from_file(policy_data_path + '/'
44                                                      + "slice-selection-files/opt_policy_nsi_reuse.json")]
45
46     def tearDown(self):
47         pass
48
49     def test_gen_demands(self):
50         # need to run this only on vnf policies
51         vnf_policies = [x for x in self.policies if x[list(x.keys())[0]]["type"]
52                         == "onap.policies.optimization.VnfPolicy"]
53         res = tr.gen_demands(self.request_json['placementInfo']['placementDemands'], vnf_policies)
54
55         assert res is not None
56
57     def test_gen_vfmod_demands(self):
58         # need to run this only on vnf policies
59         vnf_policies = [x for x in self.policies if x[list(x.keys())[0]]["type"]
60                         == "onap.policies.optimization.VnfPolicy"]
61         res = tr.gen_demands(self.request_vfmod_json['placementInfo']['placementDemands'], vnf_policies)
62         assert res is not None
63
64     def test_gen_optimization_policy(self):
65         expected = [{
66             "goal": "minimize",
67             "operation_function": {
68                 "operator": "sum",
69                 "operands": [
70                     {
71                         "function": "attribute",
72                         "params": {
73                             "attribute": "creation_cost",
74                             "demand": "embb-nst"
75                         }
76                     }
77                 ]
78             }
79         }]
80         self.assertEqual(expected,
81                          tr.gen_optimization_policy(self.request_vfmod_json['placementInfo']['placementDemands'],
82                                                     self.optimization_policies))
83
84
85 if __name__ == "__main__":
86     unittest.main()
87