summaryrefslogtreecommitdiffstats
path: root/mock-so/resources/service_instance.py
blob: 60880c2e4e6ee2c19e76df5beda5ec654a6c08b0 (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
"""SO mock instance resources."""
"""
   Copyright 2023 Deutsche Telekom AG, Orange

   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 Callable, Dict, List
from uuid import uuid4

import requests
from flask_restful import Resource, request


SERVICE_INSTANCES = {}


class SoResource(Resource):
    """Base SO resource class."""

    AAI_MOCK_URL = ""

    @classmethod
    def set_aai_mock(cls, aai_mock_url: str) -> None:
        """Set A&AI mock address.

        Instance resources needs to communicate with A&AI mock
            to update it's state.

        Args:
            aai_mock_url (str): A&AI mock url

        """
        cls.AAI_MOCK_URL = aai_mock_url

    @classmethod
    def reset(cls) -> None:
        """Reset SO resource.

        Clean SERVICE_INSTANCES dictionary
            and A&AI mock url address.

        """
        global SERVICE_INSTANCES
        SERVICE_INSTANCES = {}
        cls.AAI_MOCK_URL = ""

    def check_aai_mock_address(method: Callable) -> Callable:
        """Decorate method to check if A&AI address is set.

        If A&AI mock address is not set it returns 500 HTTP
            response for resource's method

        Args:
            method (Callable): method to decorate

        """

        def decorator(self, *args, **kwargs):
            if not self.AAI_MOCK_URL:
                return "A&AI mock address not set", 500
            return method(self, *args, **kwargs)

        return decorator


class ServiceInstance(SoResource):
    """Service instance resource class."""

    def delete(self, service_instance_id: str) -> Dict[str, Dict[str, str]]:
        """Delete service instance.

        Remove service instance data from SERVICE_INSTANCES
            dictionary.

        Args:
            service_instance_id (str): Service instance id key value

        Returns:
            Dict[str, Dict[str, str]]: Deletion request dictionary

        """
        service_instance = SERVICE_INSTANCES[service_instance_id]
        requests.delete(
            (
                f"{self.AAI_MOCK_URL}/aai/v16/business/customers/customer/"
                f"{service_instance['customerId']}/service-subscriptions/service-subscription/"
                f"{service_instance['serviceSubscriptionId']}/service-instances/service-instance/"
                f"{service_instance_id}"
            )
        )
        del SERVICE_INSTANCES[service_instance_id]
        return {"requestReferences": {"requestId": str(uuid4())}}


class ServiceInstanceList(SoResource):
    """Service instances list resource."""

    @SoResource.check_aai_mock_address
    def post(self) -> Dict[str, Dict[str, str]]:
        """Create service instance.

        Create service instance data dictionary.
            Call request to A&AI mock to create service instance there.

        Returns:
            Dict[str, Dict[str, str]]: Creation request dictionary

        """
        instance_id = str(uuid4())
        request_data = request.get_json()
        customer_id = request_data["requestDetails"]["subscriberInfo"][
            "globalSubscriberId"
        ]
        service_subscription_id = request_data["requestDetails"]["requestParameters"][
            "subscriptionServiceType"
        ]
        instance_name = request_data["requestDetails"]["requestInfo"]["instanceName"]
        service_instance = {
            "requestId": str(uuid4()),
            "instanceId": instance_id,
            "customerId": customer_id,
            "serviceSubscriptionId": service_subscription_id,
            "instanceName": instance_name,
        }
        requests.post(
            (
                f"{self.AAI_MOCK_URL}/aai/v16/business/customers/customer/{customer_id}/"
                f"service-subscriptions/service-subscription/{service_subscription_id}/"
                "service-instances"
            ),
            json={
                "service-instance-name": instance_name,
                "service-instance-id": instance_id,
            },
        )
        SERVICE_INSTANCES[service_instance["instanceId"]] = service_instance
        return {"requestReferences": service_instance}


class VnfInstance(SoResource):
    """Vnf instance resource."""

    @SoResource.check_aai_mock_address
    def delete(
        self, service_instance_id: str, vnf_instance_id: str
    ) -> Dict[str, Dict[str, str]]:
        """Delete vnf instance.

        Remove vnf instanca data from SERVICE_INSTANCES dictionary.
            Call DELETE request to A&AI mock.

        Args:
            service_instance_id (str): Service instance id key value
            vnf_instance_id (str): Vnf instance id key value

        Returns:
            Dict[str, Dict[str, str]]: Deletion request dictionary.

        """
        related_service = SERVICE_INSTANCES[service_instance_id]
        requests.delete(
            (
                f"{self.AAI_MOCK_URL}/aai/v16/business/customers/customer/"
                f"{related_service['customerId']}/service-subscriptions/service-subscription/"
                f"{related_service['serviceSubscriptionId']}/service-instances/service-instance/"
                f"{service_instance_id}/relationship-list"
            )
        )
        return {"requestReferences": {"requestId": str(uuid4())}}


class VnfInstanceList(SoResource):
    """Vnf instances list resource."""

    @SoResource.check_aai_mock_address
    def post(self, service_instance_id: str) -> Dict[str, Dict[str, str]]:
        """Create vnf instance.

        Create vnf instance data dictionary.
            Call request to A&AI mock to create vnf instance there.

        Returns:
            Dict[str, Dict[str, str]]: Creation request dictionary

        """
        instance_id = str(uuid4())
        request_data = request.get_json()
        related_instance_id = request_data["requestDetails"]["relatedInstanceList"][0][
            "relatedInstance"
        ]["instanceId"]
        related_service = SERVICE_INSTANCES[related_instance_id]
        requests.post(
            (
                f"{self.AAI_MOCK_URL}/aai/v16/business/customers/customer/{related_service['customerId']}/"
                f"service-subscriptions/service-subscription/{related_service['serviceSubscriptionId']}/"
                f"service-instances/service-instance/{related_instance_id}/relationship-list"
            ),
            json={
                "related-to": "generic-vnf",
                "related-link": f"/aai/v16/network/generic-vnfs/generic-vnf/{instance_id}",
            },
        )
        return {
            "requestReferences": {"requestId": str(uuid4()), "instanceId": instance_id}
        }


class VfModuleInstance(SoResource):
    """Vf module instance resource class."""

    @SoResource.check_aai_mock_address
    def delete(
        self, service_instance_id: str, vnf_instance_id: str, vf_module_instance_id: str
    ) -> Dict[str, Dict[str, str]]:
        """Delete vf module instance.

        Call DELETE request to A&AI mock to delete vf module instance.

        Args:
            service_instance_id (str): Service instance id key value.
            vnf_instance_id (str): Vnf instance id key value.
            vf_module_instance_id (str): Vf module instance id key value.

        Returns:
            Dict[str, Dict[str, str]]: Deletion request dictionary

        """
        requests.delete(
            (
                f"{self.AAI_MOCK_URL}/aai/v16/network/generic-vnfs/generic-vnf/"
                f"{vnf_instance_id}/vf-modules/{vf_module_instance_id}"
            )
        )
        return {"requestReferences": {"requestId": str(uuid4())}}


class VfModuleInstanceList(SoResource):
    """Vf module instances list resource."""

    @SoResource.check_aai_mock_address
    def post(
        self, service_instance_id: str, vnf_instance_id: str
    ) -> Dict[str, Dict[str, str]]:
        """Create vf module instance.

        Call POST request to A&AI mock to create vf module instance.

        Args:
            service_instance_id (str): Service instance id key value
            vnf_instance_id (str): Vnf instance id key value

        Returns:
            Dict[str, Dict[str, str]]: Creation request dictionary

        """
        instance_id = str(uuid4())
        requests.post(
            (
                f"{self.AAI_MOCK_URL}/aai/v16/network/generic-vnfs/generic-vnf/"
                f"{vnf_instance_id}/vf-modules"
            ),
            json={"vf-module-id": instance_id},
        )
        return {
            "requestReferences": {"requestId": str(uuid4()), "instanceId": instance_id}
        }


class NetworkInstance(SoResource):
    """Network instance resource."""

    @SoResource.check_aai_mock_address
    def delete(
        self, service_instance_id: str, network_instance_id: str
    ) -> Dict[str, Dict[str, str]]:
        """Delete network instance.

        Remove network instanca data from SERVICE_INSTANCES dictionary.
            Call DELETE request to A&AI mock.

        Args:
            service_instance_id (str): Service instance id key value
            network_instance_id (str): Network instance id key value

        Returns:
            Dict[str, Dict[str, str]]: Deletion request dictionary.

        """
        related_service = SERVICE_INSTANCES[service_instance_id]
        requests.delete(
            (
                f"{self.AAI_MOCK_URL}/aai/v16/business/customers/customer/"
                f"{related_service['customerId']}/service-subscriptions/service-subscription/"
                f"{related_service['serviceSubscriptionId']}/service-instances/service-instance/"
                f"{service_instance_id}/relationship-list"
            )
        )
        return {"requestReferences": {"requestId": str(uuid4())}}


class NetworkInstanceList(SoResource):
    """Network instances list resource."""

    @SoResource.check_aai_mock_address
    def post(self, service_instance_id: str) -> Dict[str, Dict[str, str]]:
        """Create network instance.

        Create network instance data dictionary.
            Call request to A&AI mock to create network instance there.

        Returns:
            Dict[str, Dict[str, str]]: Creation request dictionary

        """
        instance_id = str(uuid4())
        request_data = request.get_json()
        related_instance_id = request_data["requestDetails"]["relatedInstanceList"][0][
            "relatedInstance"
        ]["instanceId"]
        related_service = SERVICE_INSTANCES[related_instance_id]
        requests.post(
            (
                f"{self.AAI_MOCK_URL}/aai/v16/business/customers/customer/{related_service['customerId']}/"
                f"service-subscriptions/service-subscription/{related_service['serviceSubscriptionId']}/"
                f"service-instances/service-instance/{related_instance_id}/relationship-list"
            ),
            json={
                "related-to": "l3-network",
                "related-link": f"/aai/v16/network/l3-networks/l3-network/{instance_id}",
            },
        )
        return {
            "requestReferences": {"requestId": str(uuid4()), "instanceId": instance_id}
        }