aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_aai_service_instance.py
blob: 9f84dfd74a162ac59d5690e59e2a0b8efd2539a2 (plain)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#   Copyright 2022 Orange, Deutsche Telekom AG
#
#   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 unittest import mock

import pytest

from onapsdk.aai.aai_element import AaiElement
from onapsdk.aai.business import ServiceInstance
from onapsdk.so.deletion import ServiceDeletionRequest
from onapsdk.so.instantiation import NetworkInstantiation, VnfInstantiation
from onapsdk.exceptions import StatusError


RELATIONSHIPS_VNF = {
    "relationship": [
        {
            "related-to": "generic-vnf",
            "relationship_label": "anything",
            "related_link": "test_relationship_related_link",
            "relationship_data": []
        }
    ]
}


RELATIONSHIPS_NETWORK = {
    "relationship": [
        {
            "related-to": "l3-network",
            "relationship-label": "anything",
            "related-link": "related_link",
            "relationship-data": []
        }
    ]
}


NETWORK_INSTANCE = {
    'network-id': '49dab38b-3a5b-47e5-9cd6-b8d069d6109d',
    'network-name': 'Python_ONAP_SDK_network_instance_0b4308ca-3fe0-4af1-9c4e-ed2c816b9496',
    'is-bound-to-vpn': False,
    'resource-version': '1593162237842',
    'orchestration-status': 'Inventoried',
    'model-invariant-id': 'cdbb2169-e638-4aab-a4e9-b9d2d6d62b04',
    'model-version-id': '51789f7b-5ffc-4c12-ac87-02363fdb40b1',
    'model-customization-id': 'db9c9a6c-2a1c-4cdd-8fbc-e10448d0e4cc',
    'is-provider-network': False,
    'is-shared-network': False,
    'is-external-network': False,
    'relationship-list': {
        'relationship': [
            {
                'related-to': 'service-instance',
                'relationship-label': 'org.onap.relationships.inventory.ComposedOf',
                'related-link': '/aai/v19/business/customers/customer/TestCustomer/service-subscriptions/service-subscription/vFW_with_net/service-instances/service-instance/72fd9ee9-077f-4d3d-8e86-08ed24514802',
                'relationship-data': [
                    {
                        'relationship-key': 'customer.global-customer-id',
                        'relationship-value': 'TestCustomer'
                    },
                    {
                        'relationship-key': 'service-subscription.service-type',
                        'relationship-value': 'vFW_with_net'
                    },
                    {
                        'relationship-key': 'service-instance.service-instance-id',
                        'relationship-value': '72fd9ee9-077f-4d3d-8e86-08ed24514802'
                    }
                ],
                'related-to-property': [
                    {
                        'property-key': 'service-instance.service-instance-name',
                        'property-value': 'Python_ONAP_SDK_service_instance_7be66d06-c466-46cf-b84a-cd7af2d633ed'
                    }
                ]
            },
            {
                'related-to': 'cloud-region',
                'relationship-label': 'org.onap.relationships.inventory.Uses',
                'related-link': '/aai/v19/cloud-infrastructure/cloud-regions/cloud-region/TestCloudOwner/RegionOne',
                'relationship-data': [
                    {
                        'relationship-key': 'cloud-region.cloud-owner',
                        'relationship-value': 'TestCloudOwner'
                    },
                    {
                        'relationship-key': 'cloud-region.cloud-region-id',
                        'relationship-value': 'RegionOne'
                    }
                ],
                'related-to-property': [
                    {
                        'property-key': 'cloud-region.owner-defined-type',
                        'property-value': ''
                    }
                ]
            },
            {
                'related-to': 'line-of-business',
                'relationship-label': 'org.onap.relationships.inventory.Uses',
                'related-link': '/aai/v19/business/lines-of-business/line-of-business/Test-BusinessLine',
                'relationship-data': [
                    {
                        'relationship-key': 'line-of-business.line-of-business-name',
                        'relationship-value': 'Test-BusinessLine'
                    }
                ]
            },
            {
                'related-to': 'tenant',
                'relationship-label': 'org.onap.relationships.inventory.Uses',
                'related-link': '/aai/v19/cloud-infrastructure/cloud-regions/cloud-region/TestCloudOwner/RegionOne/tenants/tenant/89788fdf49514f94963b12a6c0cfdc71',
                'relationship-data': [
                    {
                        'relationship-key': 'cloud-region.cloud-owner',
                        'relationship-value': 'TestCloudOwner'
                    },
                    {
                        'relationship-key': 'cloud-region.cloud-region-id',
                        'relationship-value': 'RegionOne'
                    },
                    {
                        'relationship-key': 'tenant.tenant-id',
                        'relationship-value': '89788fdf49514f94963b12a6c0cfdc71'
                    }
                ],
                'related-to-property': [
                    {
                        'property-key': 'tenant.tenant-name',
                        'property-value': 'test-tenant'
                    }
                ]
            },
            {
                'related-to': 'platform',
                'relationship-label': 'org.onap.relationships.inventory.Uses',
                'related-link': '/aai/v19/business/platforms/platform/Test-Platform',
                'relationship-data': [
                    {
                        'relationship-key': 'platform.platform-name',
                        'relationship-value': 'Test-Platform'
                    }
                ]
            }
        ]
    }
}


COUNT = {
    "results":[
        {
            "service-instance":29
        }
    ]
}


def test_service_instance():
    service_subscription = mock.MagicMock()
    service_subscription.url = "test_url"
    service_instance = ServiceInstance(service_subscription=service_subscription,
                                       instance_id="test_service_instance_id")
    assert service_instance.url == (f"{service_instance.service_subscription.url}/service-instances/"
                                    f"service-instance/{service_instance.instance_id}")


@mock.patch.object(ServiceInstance, "send_message_json")
def test_service_instance_vnf_instances(mock_relationships_send_message_json):
    service_instance = ServiceInstance(service_subscription=mock.MagicMock(),
                                       instance_id="test_service_instance_id")
    mock_relationships_send_message_json.return_value = {"relationship": []}
    assert len(list(service_instance.vnf_instances)) == 0
    mock_relationships_send_message_json.return_value = RELATIONSHIPS_VNF
    assert len(list(service_instance.vnf_instances)) == 1


@mock.patch.object(AaiElement, "send_message_json")
def test_service_instance_network_instances(mock_aai_element_send_message_json):
    service_instance = ServiceInstance(service_subscription=mock.MagicMock(),
                                       instance_id="test_service_instance_id")
    mock_aai_element_send_message_json.side_effect = [RELATIONSHIPS_NETWORK, NETWORK_INSTANCE]
    assert len(list(service_instance.network_instances)) == 1


@mock.patch.object(VnfInstantiation, "instantiate_ala_carte")
@mock.patch.object(ServiceInstance, "sdc_service", new_callable=mock.PropertyMock)
def test_service_instance_add_vnf(mock_sdc_service, mock_vnf_instantiation):
    service_instance = ServiceInstance(service_subscription=mock.MagicMock(),
                                       instance_id="test_service_instance_id")
    service_instance.orchestration_status = "Inactive"
    with pytest.raises(StatusError) as exc:
        service_instance.add_vnf(mock.MagicMock(),
                                 mock.MagicMock(),
                                 mock.MagicMock())
    assert exc.type == StatusError
    service_instance.orchestration_status = "Active"
    service_instance.add_vnf(mock.MagicMock(),
                             mock.MagicMock(),
                             mock.MagicMock())
    mock_vnf_instantiation.assert_called_once()


@mock.patch.object(VnfInstantiation, "instantiate_macro")
@mock.patch.object(ServiceInstance, "sdc_service", new_callable=mock.PropertyMock)
def test_service_instance_add_vnf_macro(mock_sdc_service, mock_vnf_instantiation):
    service_instance = ServiceInstance(service_subscription=mock.MagicMock(),
                                       instance_id="test_service_instance_id")
    service_instance.orchestration_status = "Inactive"
    with pytest.raises(StatusError) as exc:
        service_instance.add_vnf(mock.MagicMock(),
                                 mock.MagicMock(),
                                 mock.MagicMock(),
                                 a_la_carte=False)
    assert exc.type == StatusError
    service_instance.orchestration_status = "Active"
    service_instance.add_vnf(mock.MagicMock(),
                             mock.MagicMock(),
                             mock.MagicMock(),
                             a_la_carte=False)
    mock_vnf_instantiation.assert_called_once()


@mock.patch.object(NetworkInstantiation, "instantiate_ala_carte")
@mock.patch.object(ServiceInstance, "sdc_service", new_callable=mock.PropertyMock)
def test_service_instance_add_network(mock_sdc_service, mock_network_instantiation):
    service_instance = ServiceInstance(service_subscription=mock.MagicMock(),
                                       instance_id="test_service_instance_id")
    service_instance.orchestration_status = "Inactive"
    with pytest.raises(StatusError) as exc:
        service_instance.add_network(mock.MagicMock(),
                                     mock.MagicMock(),
                                     mock.MagicMock())
    assert exc.type == StatusError
    service_instance.orchestration_status = "Active"
    service_instance.add_network(mock.MagicMock(),
                                 mock.MagicMock(),
                                 mock.MagicMock())
    mock_network_instantiation.assert_called_once()


@mock.patch.object(ServiceDeletionRequest, "send_request")
@mock.patch.object(ServiceInstance, "sdc_service", new_callable=mock.PropertyMock)
def test_service_instance_deletion(mock_sdc_service, mock_service_deletion_request):
    service_instance = ServiceInstance(service_subscription=mock.MagicMock(),
                                       instance_id="test_service_instance_id")
    service_instance.delete()
    mock_service_deletion_request.assert_called_once_with(service_instance, True)


@mock.patch("onapsdk.aai.business.service.Service.get_by_unique_uuid")
def test_service_instance_sdc_service(mock_service_get_by_unique_uuid):
    si = ServiceInstance(service_subscription=mock.MagicMock(),
                         instance_id="test_service_instance_id",
                         model_invariant_id="1234")
    si.sdc_service
    mock_service_get_by_unique_uuid.assert_called_once_with("1234")
    si.sdc_service
    mock_service_get_by_unique_uuid.assert_called_once_with("1234")

@mock.patch.object(ServiceInstance, "send_message_json")
def test_service_instance_count(mock_send_message_json):
    mock_send_message_json.return_value = COUNT
    assert ServiceInstance.count(service_subscription=mock.MagicMock())