update link to upper-constraints.txt
[optf/osdf.git] / test / test_inter_domain_route_opt.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2020 Fujitsu Limited 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 import unittest
18
19 from unittest.mock import patch
20 from apps.route.optimizers.inter_domain_route_opt import InterDomainRouteOpt
21 import osdf.config.loader as config_loader
22 from osdf.utils.interfaces import json_from_file
23 from osdf.utils.programming_utils import DotDict
24
25 count = 1 
26
27 def mocked_requests_get(*args, **kwargs):
28     class MockResponse:
29         def __init__(self, json_data, status_code):
30             self.json_data = json_data
31             self.status_code = status_code
32
33         def json(self):
34             return self.json_data
35   
36     main_dir = ""
37     response_data_file = main_dir + "test/inter_domain_route_opt/get_links.json"
38     bandwidth_attributes = main_dir + "test/inter_domain_route_opt/bandwidth_attributes.json"
39     bandwidth_attribute_values = json_from_file(bandwidth_attributes)
40     
41     controllers_list = main_dir + "test/inter_domain_route_opt/controllers_list.json"
42     
43     if args[0] == 'https://api.url:30233/aai/v19/network/logical-links?link-type=inter-domain&operational-status=up':
44         return MockResponse(json_from_file(response_data_file), 200)
45     elif args[0] == 'https://api.url:30233/aai/v19/network/pnfs/pnf/pnf1/p-interfaces/p-interface/int1?depth=all':
46         return MockResponse(bandwidth_attribute_values["int-1-bw"], 200)
47     elif args[0] == 'https://api.url:30233/aai/v19/network/pnfs/pnf/pnf2/p-interfaces/p-interface/int3?depth=all':
48         return MockResponse(bandwidth_attribute_values["int-3-bw"], 200)
49     elif args[0] == 'https://api.url:30233/aai/v19/network/pnfs/pnf/pnf2/p-interfaces/p-interface/int4?depth=all':
50         return MockResponse(bandwidth_attribute_values["int-4-bw"], 200)
51     elif args[0] == 'https://api.url:30233/aai/v19/network/pnfs/pnf/pnf3/p-interfaces/p-interface/int5?depth=all':
52         return MockResponse(bandwidth_attribute_values["int-5-bw"], 200)
53     elif args[0] == 'https://api.url:30233/aai/v19/network/pnfs/pnf/pnf3/p-interfaces/p-interface/int6?depth=all':
54         return MockResponse(bandwidth_attribute_values["int-6-bw"], 200)
55     elif args[0] == 'https://api.url:30233/aai/v19/network/pnfs/pnf/pnf4/p-interfaces/p-interface/int7?depth=all':
56         return MockResponse(bandwidth_attribute_values["int-7-bw"], 200)
57     elif args[0] == 'https://api.url:30233/aai/v19/external-system/esr-thirdparty-sdnc-list':
58         return MockResponse(json_from_file(controllers_list), 200)                                             
59     return MockResponse(None, 404)   
60     
61
62 def mocked_requests_put(*args, **kwargs):
63     class MockResponse:
64         def __init__(self, json_data, status_code):
65             self.json_data = json_data
66             self.status_code = status_code
67
68         def json(self):
69             return self.json_data
70     main_dir = ""
71     controllers_for_interfaces = main_dir + "test/inter_domain_route_opt/controllers_for_interfaces.json"
72     controllers_for_interfaces_values = json_from_file(controllers_for_interfaces)
73
74     global count
75       
76     if count == 1:
77         count += 1
78         return MockResponse(controllers_for_interfaces_values["int-1-cont"], 200)
79     elif count == 2:
80         count += 1
81         return MockResponse(controllers_for_interfaces_values["int-3-cont"], 200)
82     elif count == 3:
83         count += 1
84         return MockResponse(controllers_for_interfaces_values["int-4-cont"], 200)
85     elif count == 4:
86         count += 1
87         return MockResponse(controllers_for_interfaces_values["int-5-cont"], 200)
88     elif count == 5:
89       count += 1
90       return MockResponse(controllers_for_interfaces_values["int-6-cont"], 200)
91     elif count == 6:
92         count += 1
93         return MockResponse(controllers_for_interfaces_values["int-7-cont"], 200)
94             
95     return MockResponse(None, 404)            
96     
97             
98
99 class TestInterDomainRouteOpt(unittest.TestCase):
100     @patch('apps.route.optimizers.inter_domain_route_opt.requests.get', side_effect=mocked_requests_get)
101     @patch('apps.route.optimizers.inter_domain_route_opt.requests.put', side_effect=mocked_requests_put)
102     @patch('apps.route.optimizers.simple_route_opt.pymzn.minizinc')               
103     def test_process_get_route(self, mock_solve , mock_put, mock_get):      
104         main_dir = ""
105         mock_solve.return_value = [{'x': [1, 1, 0, 0, 0, 0]}]
106         self.config_spec = {
107             "deployment": "test/functest/simulators/simulated-config/osdf_config.yaml",
108             "core": "test/functest/simulators/simulated-config/common_config.yaml"
109         }
110         self.osdf_config = DotDict(config_loader.all_configs(**self.config_spec))
111         parameter_data_file = main_dir + "test/inter_domain_route_opt/request.json"
112         request_json = json_from_file(parameter_data_file)
113         routopt = InterDomainRouteOpt()
114         actual_response = routopt.get_route(request_json,self.osdf_config)
115         mock_response = {
116             "requestId":"789456",
117             "transactionId":"123456",
118             "statusMessage":"SUCCESS",
119             "requestStatus":"accepted",
120             "solutions":{
121                 "routeInfo":{
122                 "serviceRoute":[
123                     {
124                      "srcInterfaceId":"int19",
125                      "dstInterfaceId":"int1",
126                      "controllerId":"Controller1"
127                 },
128                     {
129                      "srcInterfaceId":"int3",
130                      "dstInterfaceId":"int4",
131                      "controllerId":"Controller2"
132                 },
133                     {
134                      "srcInterfaceId":"int5",
135                      "dstInterfaceId":"int20",
136                      "controllerId":"Controller3"
137                 }
138                 ],
139                "linkList":[
140                     "link1",
141                     "link2"
142                 ]
143                 }
144            }
145         }
146         self.assertEqual(mock_response, actual_response)
147         
148         
149 if __name__ == '__main__':
150     unittest.main()
151