Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
#
# 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.
from typing import Dict
import json, logging, pytest
from common.Constants import DEFAULT_CONTEXT_NAME, DEFAULT_TOPOLOGY_NAME
from common.proto.context_pb2 import ContextId, TopologyId
from common.tools.descriptor.Loader import DescriptorLoader, check_descriptor_load_results, validate_empty_scenario
from common.tools.object_factory.Context import json_context_id
from common.tools.object_factory.Topology import json_topology_id
from context.client.ContextClient import ContextClient
from nbi.service.rest_server import RestServer
from .PrepareTestScenario import do_rest_delete_request, do_rest_post_request, do_rest_get_request, do_rest_put_request, do_rest_patch_request, mock_service, nbi_service_rest, context_client
LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.DEBUG)
DESCRIPTOR_FILE = 'nbi/tests/data/topology-dummy.json'
JSON_ADMIN_CONTEXT_ID = json_context_id(DEFAULT_CONTEXT_NAME)
ADMIN_CONTEXT_ID = ContextId(**JSON_ADMIN_CONTEXT_ID)
ADMIN_TOPOLOGY_ID = TopologyId(**json_topology_id(DEFAULT_TOPOLOGY_NAME, context_id=JSON_ADMIN_CONTEXT_ID))
BASE_URL = '/restconf/bwm/v1'
@pytest.fixture(scope='session')
def storage() -> Dict:
yield dict()
def compare_dicts(dict1, dict2):
# Function to recursively sort dictionaries
def recursively_sort(d):
if isinstance(d, dict):
return {k: recursively_sort(v) for k, v in sorted(d.items())}
if isinstance(d, list):
return [recursively_sort(item) for item in d]
return d
# Sort dictionaries to ignore the order of fields
sorted_dict1 = recursively_sort(dict1)
sorted_dict2 = recursively_sort(dict2)
if sorted_dict1 != sorted_dict2:
LOGGER.error(sorted_dict1)
LOGGER.error(sorted_dict2)
return sorted_dict1 != sorted_dict2
def check_timestamps(bwm_service):
assert 'timeStamp' in bwm_service
assert 'seconds' in bwm_service['timeStamp']
assert 'nanoseconds' in bwm_service['timeStamp']
def test_prepare_environment(context_client : ContextClient) -> None: # pylint: disable=redefined-outer-name
validate_empty_scenario(context_client)
descriptor_loader = DescriptorLoader(descriptors_file=DESCRIPTOR_FILE, context_client=context_client)
results = descriptor_loader.process()
check_descriptor_load_results(results, descriptor_loader)
descriptor_loader.validate()
# Verify the scenario has no services/slices
response = context_client.GetContext(ADMIN_CONTEXT_ID)
assert len(response.topology_ids) == 3
assert len(response.service_ids ) == 0
assert len(response.slice_ids ) == 0
def test_get_allocations_empty(nbi_service_rest : RestServer, storage : Dict): # pylint: disable=redefined-outer-name, unused-argument
URL = BASE_URL + '/bw_allocations'
retrieved_data = do_rest_get_request(URL, logger=LOGGER, expected_status_codes={200})
LOGGER.debug('retrieved_data={:s}'.format(json.dumps(retrieved_data, sort_keys=True)))
assert len(retrieved_data) == 0
def test_allocation(nbi_service_rest : RestServer, storage : Dict):
URL = BASE_URL + '/bw_allocations'
data = {
"allocationDirection":"string",
"appInsId":"service_uuid_01",
"fixedAllocation":"123000.0",
"fixedBWPriority":"SEE_DESCRIPTION",
"requestType":0,
"sessionFilter":[
{
"dstAddress":"192.168.3.2",
"dstPort":["b"],
"protocol":"string",
"sourceIp":"192.168.1.2",
"sourcePort":["a"]
}
]
}
retrieved_data = do_rest_post_request(URL, body=data, logger=LOGGER, expected_status_codes={200})
LOGGER.debug('retrieved_data={:s}'.format(json.dumps(retrieved_data, sort_keys=True)))
storage['service_uuid_01'] = 'service_uuid_01'
def test_get_allocations(nbi_service_rest : RestServer, storage : Dict): # pylint: disable=redefined-outer-name, unused-argument
assert 'service_uuid_01' in storage
URL = BASE_URL + '/bw_allocations'
retrieved_data = do_rest_get_request(URL, logger=LOGGER, expected_status_codes={200})
LOGGER.debug('retrieved_data={:s}'.format(json.dumps(retrieved_data, sort_keys=True)))
assert len(retrieved_data) == 1
good_result = [
{
"appInsId":"service_uuid_01",
"fixedAllocation":"123000.0",
"allocationDirection":"string",
"fixedBWPriority":"SEE_DESCRIPTION",
"requestType":"0",
"sessionFilter":[
{
"dstAddress":"192.168.3.2",
"dstPort":["b"],
"protocol":"string",
"sourceIp":"192.168.1.2",
"sourcePort":["a"]
}
],
}
]
compare_dicts(retrieved_data, good_result)
check_timestamps(retrieved_data[0])
def test_get_allocation(nbi_service_rest : RestServer, storage : Dict): # pylint: disable=redefined-outer-name, unused-argument
assert 'service_uuid_01' in storage
URL = BASE_URL + '/bw_allocations/service_uuid_01'
retrieved_data = do_rest_get_request(URL, logger=LOGGER, expected_status_codes={200})
LOGGER.debug('retrieved_data={:s}'.format(json.dumps(retrieved_data, sort_keys=True)))
good_result = {
"appInsId":"service_uuid_01",
"fixedAllocation":"123000.0",
"allocationDirection":"string",
"fixedBWPriority":"SEE_DESCRIPTION",
"requestType":"0",
"sessionFilter":[
{
"dstAddress":"192.168.3.2",
"dstPort":["b"],
"protocol":"string",
"sourceIp":"192.168.1.2",
"sourcePort":["a"]
}
]
}
compare_dicts(retrieved_data, good_result)
check_timestamps(retrieved_data)
def test_put_allocation(nbi_service_rest : RestServer, storage : Dict): # pylint: disable=redefined-outer-name, unused-argument
assert 'service_uuid_01' in storage
URL = BASE_URL + '/bw_allocations/service_uuid_01'
changed_allocation = {
"appInsId":"service_uuid_01",
"fixedAllocation":"200.0",
"allocationDirection":"parriba",
"fixedBWPriority":"NOPRIORITY",
"requestType":"0",
"sessionFilter":[
{
"dstAddress":"192.168.3.2",
"dstPort":["b"],
"protocol":"string",
"sourceIp":"192.168.1.2",
"sourcePort":["a"]
}
]
}
retrieved_data = do_rest_put_request(URL, body=json.dumps(changed_allocation), logger=LOGGER, expected_status_codes={200})
compare_dicts(retrieved_data, changed_allocation)
check_timestamps(retrieved_data)
def test_patch_allocation(nbi_service_rest : RestServer, storage : Dict): # pylint: disable=redefined-outer-name, unused-argument
assert 'service_uuid_01' in storage
URL = BASE_URL + '/bw_allocations/service_uuid_01'
difference = {
"fixedBWPriority":"FULLPRIORITY",
}
changed_allocation = {
"appInsId":"service_uuid_01",
"fixedAllocation":"200.0",
"allocationDirection":"parriba",
"fixedBWPriority":"FULLPRIORITY",
"requestType":"0",
"sessionFilter":[
{
"dstAddress":"192.168.3.2",
"dstPort":["b"],
"protocol":"string",
"sourceIp":"192.168.1.2",
"sourcePort":["a"]
}
]
}
retrieved_data = do_rest_patch_request(URL, body=changed_allocation, logger=LOGGER, expected_status_codes={200})
compare_dicts(retrieved_data, changed_allocation)
check_timestamps(retrieved_data)
def test_delete_allocation(nbi_service_rest : RestServer, storage : Dict): # pylint: disable=redefined-outer-name, unused-argument
assert 'service_uuid_01' in storage
URL = BASE_URL + '/bw_allocations/service_uuid_01'
do_rest_delete_request(URL, logger=LOGGER, expected_status_codes={200})
def test_get_allocations_empty_final(nbi_service_rest : RestServer, storage : Dict): # pylint: disable=redefined-outer-name, unused-argument
URL = BASE_URL + '/bw_allocations'
retrieved_data = do_rest_get_request(URL, logger=LOGGER, expected_status_codes={200})
LOGGER.debug('retrieved_data={:s}'.format(json.dumps(retrieved_data, sort_keys=True)))
assert len(retrieved_data) == 0