aboutsummaryrefslogtreecommitdiffstats
path: root/src/onapsdk/aai/business/service.py
blob: d0f787621be1fbf449a27b01aada4efb2edd8f49 (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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
"""Service instance module."""
#   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 typing import Iterator, Type, Union, Iterable, Optional

from onapsdk.exceptions import StatusError, ParameterError
from onapsdk.sdc.service import Service
from onapsdk.so.deletion import ServiceDeletionRequest
from onapsdk.so.instantiation import NetworkInstantiation, VnfInstantiation, PnfInstantiation
from onapsdk.utils.jinja import jinja_env

from .instance import Instance
from .network import NetworkInstance
from .pnf import PnfInstance
from .vnf import VnfInstance


class ServiceInstance(Instance):  # pylint: disable=too-many-instance-attributes
    """Service instanve class."""

    ACTIVE_STATUS_MESSAGE = 'Service orchestration status must be "Active"'

    def __init__(self,  # NOSONAR  # pylint: disable=too-many-arguments, too-many-locals
                 service_subscription: "ServiceSubscription",
                 instance_id: str,
                 instance_name: str = None,
                 service_type: str = None,
                 service_role: str = None,
                 environment_context: str = None,
                 workload_context: str = None,
                 created_at: str = None,
                 updated_at: str = None,
                 resource_version: str = None,
                 description: str = None,
                 model_invariant_id: str = None,
                 model_version_id: str = None,
                 persona_model_version: str = None,
                 widget_model_id: str = None,
                 widget_model_version: str = None,
                 bandwith_total: str = None,
                 vhn_portal_url: str = None,
                 service_instance_location_id: str = None,
                 selflink: str = None,
                 orchestration_status: str = None,
                 input_parameters: str = None) -> None:
        """Service instance object initialization.

        Args:
            service_subscription (ServiceSubscription): service subscription which is belongs to
            instance_id (str): Uniquely identifies this instance of a service
            instance_name (str, optional): This field will store a name assigned to
                the service-instance. Defaults to None.
            service_type (str, optional): String capturing type of service. Defaults to None.
            service_role (str, optional): String capturing the service role. Defaults to None.
            environment_context (str, optional): This field will store the environment context
                assigned to the service-instance. Defaults to None.
            workload_context (str, optional): This field will store the workload context assigned to
                the service-instance. Defaults to None.
            created_at (str, optional): Create time of Network Service. Defaults to None.
            updated_at (str, optional): Last update of Network Service. Defaults to None.
            description (str, optional): Short description for service-instance. Defaults to None.
            model_invariant_id (str, optional): The ASDC model id for this resource or
                service model. Defaults to None.
            model_version_id (str, optional): The ASDC model version for this resource or
                service model. Defaults to None.
            persona_model_version (str, optional): The ASDC model version for this resource or
                service model. Defaults to None.
            widget_model_id (str, optional): The ASDC data dictionary widget model. This maps
                directly to the A&AI widget. Defaults to None.
            widget_model_version (str, optional): The ASDC data dictionary version of the widget
                model. This maps directly to the A&AI version of the widget. Defaults to None.
            bandwith_total (str, optional): Indicates the total bandwidth to be used for this
                service. Defaults to None.
            vhn_portal_url (str, optional): URL customers will use to access the vHN Portal.
                Defaults to None.
            service_instance_location_id (str, optional): An identifier that customers assign to
                the location where this service is being used. Defaults to None.
            resource_version (str, optional): Used for optimistic concurrency. Must be empty on
                create, valid on update and delete. Defaults to None.
            selflink (str, optional): Path to the controller object. Defaults to None.
            orchestration_status (str, optional): Orchestration status of this service.
                Defaults to None.
            input_parameters (str, optional): String capturing request parameters from SO to
                pass to Closed Loop. Defaults to None.
        """
        super().__init__(resource_version=resource_version,
                         model_invariant_id=model_invariant_id,
                         model_version_id=model_version_id)
        self.service_subscription: "ServiceSubscription" = service_subscription
        self.instance_id: str = instance_id
        self.instance_name: str = instance_name
        self.service_type: str = service_type
        self.service_role: str = service_role
        self.environment_context: str = environment_context
        self.workload_context: str = workload_context
        self.created_at: str = created_at
        self.updated_at: str = updated_at
        self.description: str = description
        self.bandwith_total: str = bandwith_total
        self.vhn_portal_url: str = vhn_portal_url
        self.service_instance_location_id: str = service_instance_location_id
        self.selflink: str = selflink
        self.orchestration_status: str = orchestration_status
        self.input_parameters: str = input_parameters
        self.persona_model_version: str = persona_model_version
        self.widget_model_id: str = widget_model_id
        self.widget_model_version: str = widget_model_version
        self._sdc_service: Optional[Service] = None

    def __repr__(self) -> str:
        """Service instance object representation.

        Returns:
            str: Human readable service instance representation

        """
        return (f"ServiceInstance(instance_id={self.instance_id}, "
                f"instance_name={self.instance_name})")

    def _get_related_instance(self,
                              related_instance_class: Union[Type[NetworkInstance],
                              Type[VnfInstance]],
                              relationship_related_to_type: str) -> Iterator[ \
            Union[NetworkInstance,
            VnfInstance]]:
        """Iterate through related service instances.

        This is method which for given `relationship_related_to_type` creates iterator
            it iterate through objects which are related with service.

        Args:
            related_instance_class (Union[Type[NetworkInstance], Type[VnfInstance]]): Class object
                to create required object instances
            relationship_related_to_type (str): Has to be "generic-vnf" or "l3-network"

        Raises:
            ParameterError: relationship_related_to_type does not satisfy the requirements

        Yields:
            Iterator[ Union[NetworkInstance, VnfInstance]]: [description]

        """
        if relationship_related_to_type not in ["l3-network", "generic-vnf", "pnf"]:
            msg = (
                f'Invalid "relationship_related_to_type" value. '
                f'Provided "{relationship_related_to_type}". '
                f'Has to be "l3-network" or "generic-vnf".'
            )
            raise ParameterError(msg)
        for relationship in self.relationships:
            if relationship.related_to == relationship_related_to_type:
                yield related_instance_class.create_from_api_response( \
                    self.send_message_json("GET",
                                           (f"Get {self.instance_id} "
                                            f"{related_instance_class.__class__.__name__}"),
                                           f"{self.base_url}{relationship.related_link}"),
                    self)

    @classmethod
    def create(cls, service_subscription: "ServiceSubscription",
               # NOSONAR  # pylint: disable=too-many-arguments, too-many-locals
               instance_id: str,
               instance_name: str = None,
               service_type: str = None,
               service_role: str = None,
               environment_context: str = None,
               workload_context: str = None,
               created_at: str = None,
               updated_at: str = None,
               resource_version: str = None,
               description: str = None,
               model_invariant_id: str = None,
               model_version_id: str = None,
               persona_model_version: str = None,
               widget_model_id: str = None,
               widget_model_version: str = None,
               bandwith_total: str = None,
               vhn_portal_url: str = None,
               service_instance_location_id: str = None,
               selflink: str = None,
               orchestration_status: str = None,
               input_parameters: str = None):
        """Service instance creation.

        Args:
            service_subscription (ServiceSubscription): service subscription which is belongs to
            instance_id (str): Uniquely identifies this instance of a service
            instance_name (str, optional): This field will store a name assigned to
                the service-instance. Defaults to None.
            service_type (str, optional): String capturing type of service. Defaults to None.
            service_role (str, optional): String capturing the service role. Defaults to None.
            environment_context (str, optional): This field will store the environment context
                assigned to the service-instance. Defaults to None.
            workload_context (str, optional): This field will store the workload context assigned to
                the service-instance. Defaults to None.
            created_at (str, optional): Create time of Network Service. Defaults to None.
            updated_at (str, optional): Last update of Network Service. Defaults to None.
            description (str, optional): Short description for service-instance. Defaults to None.
            model_invariant_id (str, optional): The ASDC model id for this resource or
                service model. Defaults to None.
            model_version_id (str, optional): The ASDC model version for this resource or
                service model. Defaults to None.
            persona_model_version (str, optional): The ASDC model version for this resource or
                service model. Defaults to None.
            widget_model_id (str, optional): The ASDC data dictionary widget model. This maps
                directly to the A&AI widget. Defaults to None.
            widget_model_version (str, optional): The ASDC data dictionary version of the widget
                model. This maps directly to the A&AI version of the widget. Defaults to None.
            bandwith_total (str, optional): Indicates the total bandwidth to be used for this
                service. Defaults to None.
            vhn_portal_url (str, optional): URL customers will use to access the vHN Portal.
                Defaults to None.
            service_instance_location_id (str, optional): An identifier that customers assign to
                the location where this service is being used. Defaults to None.
            resource_version (str, optional): Used for optimistic concurrency. Must be empty on
                create, valid on update and delete. Defaults to None.
            selflink (str, optional): Path to the controller object. Defaults to None.
            orchestration_status (str, optional): Orchestration status of this service.
                Defaults to None.
            input_parameters (str, optional): String capturing request parameters from SO to
                pass to Closed Loop. Defaults to None.
        """
        service_instance: "ServiceInstance" = cls(
            service_subscription,
            instance_id,
            instance_name,
            service_type,
            service_role,
            environment_context,
            workload_context,
            created_at,
            updated_at,
            resource_version,
            description,
            model_invariant_id,
            model_version_id,
            persona_model_version,
            widget_model_id,
            widget_model_version,
            bandwith_total,
            vhn_portal_url,
            service_instance_location_id,
            selflink,
            orchestration_status,
            input_parameters
        )
        cls.send_message("PUT",
                         f"Create service instance {instance_id} for " \
                         f"{service_subscription.service_type} service subscription",
                         f"{service_subscription.url}/service-instances/service-instance/" \
                         f"{instance_id}",
                         data=jinja_env()
                         .get_template("aai_service_instance_create.json.j2")
                         .render(
                             service_instance=service_instance
                         ))
        return service_instance

    @classmethod
    def get_all_url(cls, service_subscription: "ServiceSubscription") -> str:  # pylint: disable=arguments-differ
        """Return an url to get all service instances for service subscription.

        Args:
            service_subscription (ServiceSubscription): Service subscription object

        Returns:
            str: Url to get all service instances for service subscription

        """
        return f"{service_subscription.url}/service-instances/"

    @property
    def url(self) -> str:
        """Service instance resource URL.

        Returns:
            str: Service instance url

        """
        return (
            f"{self.service_subscription.url}/service-instances/service-instance/{self.instance_id}"
        )

    @property
    def vnf_instances(self) -> Iterator[VnfInstance]:
        """Vnf instances associated with service instance.

        Returns iterator of VnfInstances representing VNF instantiated for that service

        Yields:
            VnfInstance: VnfInstance object

        """
        return self._get_related_instance(VnfInstance, "generic-vnf")

    @property
    def network_instances(self) -> Iterator[NetworkInstance]:
        """Network instances associated with service instance.

        Returns iterator of NetworkInstance representing network instantiated for that service

        Yields:
            NetworkInstance: NetworkInstance object

        """
        return self._get_related_instance(NetworkInstance, "l3-network")

    @property
    def pnfs(self) -> Iterator[PnfInstance]:
        """Pnfs associated with service instance.

        Returns iterator of PnfInstance representing pnfs instantiated for that service

        Yields:
            PnfInstance: PnfInstance object

        """
        return self._get_related_instance(PnfInstance, "pnf")

    @property
    def sdc_service(self) -> Service:
        """Sdc service related with that instance.

        Sdc service model which was used to create that instance.

        Raises:
            ResourceNotFound: Service model not found

        """
        if not self._sdc_service:
            self._sdc_service = Service.get_by_unique_uuid(self.model_invariant_id)
        return self._sdc_service

    @property
    def active(self) -> bool:
        """Information if service instance's orchestration status is active."""
        return self.orchestration_status == "Active"

    def add_vnf(self,  # pylint: disable=too-many-arguments
                vnf: "Vnf",
                line_of_business: "LineOfBusiness",
                platform: "Platform",
                cloud_region: "CloudRegion" = None,
                tenant: "Tenant" = None,
                vnf_instance_name: str = None,
                vnf_parameters: Iterable["InstantiationParameter"] = None,
                so_vnf: "SoServiceVnf" = None,
                a_la_carte: bool = True
                ) -> "VnfInstantiation":
        """Add vnf into service instance.

        Instantiate VNF.

        Args:
            vnf (Vnf): Vnf from service configuration to instantiate
            line_of_business (LineOfBusiness): LineOfBusiness to use in instantiation request
            platform (Platform): Platform to use in instantiation request
            cloud_region (CloudRegion, optional): Cloud region to use in instantiation request.
                Defaults to None.
                THAT PROPERTY WILL BE REQUIRED IN ONE OF THE FUTURE RELEASE. REFACTOR YOUR CODE
                TO USE IT!.
            tenant (Tenant, optional): Tenant to use in instnatiation request.
                Defaults to None.
                THAT PROPERTY WILL BE REQUIRED IN ONE OF THE FUTURE RELEASE. REFACTOR YOUR CODE
                TO USE IT!.
            vnf_instance_name (str, optional): VNF instantion name.
                If no value is provided it's going to be
                "Python_ONAP_SDK_vnf_instance_{str(uuid4())}".
                Defaults to None.
            vnf_parameters (Iterable[InstantiationParameter], optional): InstantiationParameter to
                be passed as "userParams". Defaults to None.
            so_vnf: (SoServiceVnf, optional): object with vnf instance parameters. Defaults to None.
            a_la_carte (bool): instantiation type for vnf. Defaults to True.

        Raises:
            StatusError: Service orchestration status is not "Active".

        Returns:
            VnfInstantiation: VnfInstantiation request object

        """
        if not self.active:
            raise StatusError(self.ACTIVE_STATUS_MESSAGE)

        if a_la_carte:
            return VnfInstantiation.instantiate_ala_carte(
                self,
                vnf,
                line_of_business,
                platform,
                cloud_region=cloud_region,
                tenant=tenant,
                vnf_instance_name=vnf_instance_name,
                vnf_parameters=vnf_parameters,
                sdc_service=self.sdc_service
            )

        return VnfInstantiation.instantiate_macro(
            self,
            vnf,
            line_of_business,
            platform,
            cloud_region=cloud_region,
            tenant=tenant,
            vnf_instance_name=vnf_instance_name,
            vnf_parameters=vnf_parameters,
            so_vnf=so_vnf,
            sdc_service=self.sdc_service
        )

    def add_pnf(self,  # pylint: disable=too-many-arguments
                pnf: "Pnf",
                line_of_business: "LineOfBusiness",
                platform: "Platform",
                pnf_instance_name: str = None,
                customer: "Customer" = None,
                service_subscription: "ServiceSubscription" = None,
                sdc_service: "SdcService" = None,
                ) -> "PnfInstantiation":
        """Add pnf into service instance.

        Instantiate PNF.

        Args:
            pnf (Pnf): Pnf from service configuration to instantiate
            line_of_business (LineOfBusiness): LineOfBusiness to use in instantiation request
            platform (Platform): Platform to use in instantiation request
            customer (Customer): Customer to use in instantiation request
            service_subscription(ServiceSubscription):  ServiceSubscription
            pnf_instance_name (str): PNF instantiation name
            sdc_service (SdcService): service model from sdc

        Raises:
            StatusError: Service orchestration status is not "Active".

        Returns:
            PnfInstantiation: PnfInstantiation request object

        """
        if not self.active:
            raise StatusError(self.ACTIVE_STATUS_MESSAGE)

        return PnfInstantiation.instantiate_macro(
            self,
            pnf,
            line_of_business,
            customer,
            service_subscription,
            platform,
            pnf_instance_name,
            sdc_service
        )

    def add_network(self,  # pylint: disable=too-many-arguments
                    network: "Network",
                    line_of_business: "LineOfBusiness",
                    platform: "Platform",
                    cloud_region: "CloudRegion" = None,
                    tenant: "Tenant" = None,
                    network_instance_name: str = None,
                    subnets: Iterator["Subnet"] = None) -> "NetworkInstantiation":
        """Add network into service instance.

        Instantiate vl.

        Args:
            network (Network): Network from service configuration to instantiate
            line_of_business (LineOfBusiness): LineOfBusiness to use in instantiation request
            platform (Platform): Platform to use in instantiation request
            cloud_region (CloudRegion, optional): Cloud region to use in instantiation request.
                Defaults to None.
                THAT PROPERTY WILL BE REQUIRED IN ONE OF THE FUTURE RELEASE. REFACTOR YOUR CODE
                TO USE IT!.
            tenant (Tenant, optional): Tenant to use in instnatiation request.
                Defaults to None.
                THAT PROPERTY WILL BE REQUIRED IN ONE OF THE FUTURE RELEASE. REFACTOR YOUR CODE
                TO USE IT!.
            network_instance_name (str, optional): Network instantion name.
                If no value is provided it's going to be
                "Python_ONAP_SDK_network_instance_{str(uuid4())}".
                Defaults to None.

        Raises:
            StatusError: Service orchestration status is not "Active"

        Returns:
            NetworkInstantiation: NetworkInstantiation request object

        """
        if not self.active:
            raise StatusError(self.ACTIVE_STATUS_MESSAGE)

        return NetworkInstantiation.instantiate_ala_carte(
            self,
            network,
            line_of_business,
            platform,
            cloud_region=cloud_region,
            tenant=tenant,
            network_instance_name=network_instance_name,
            subnets=subnets
        )

    def delete(self, a_la_carte: bool = True) -> "ServiceDeletionRequest":
        """Create service deletion request.

        Send a request to delete service instance

        Args:
            a_la_carte (boolean): deletion mode

        Returns:
            ServiceDeletionRequest: Deletion request object

        """
        self._logger.debug("Delete %s service instance", self.instance_id)
        return ServiceDeletionRequest.send_request(self, a_la_carte)

    def delete_from_aai(self,
                        service_subscription: "ServiceSubscription",
                        instance_id: str,
                        service_type: str = None) -> "ServiceInstance":
        """Send request to AAI to delete service instance.

        Args:
            service_subscription (ServiceSubscription): service subscription which is belongs to
            instance_id (str): Uniquely identifies this instance of a service
            service_type (str, optional): String capturing type of service.Defaults to None.

        """
        customer_instance = service_subscription.customer
        customer_id = customer_instance.global_customer_id

        # calling GET api to get resource_version of service instance
        response = self.send_message_json("GET",
                                          f"GET service instance {instance_id} for ",
                                          f"{self.base_url}{self.api_version}/business/"
                                          f"customers/customer/"
                                          f"{customer_id}/service-subscriptions/"
                                          f"service-subscription/"
                                          f"{service_type}/service-instances/service-instance/"
                                          f"{instance_id}")

        resource_version = str(response.get('resource-version', ''))

        # calling delete api to delete service instance from AAI
        self.send_message("DELETE",
                          f"Delete service instance {instance_id} for ",
                          f"{self.base_url}{self.api_version}/business/customers/customer/"
                          f"{customer_id}/service-subscriptions/"
                          f"service-subscription/"
                          f"{service_type}/service-instances/service-instance/"
                          f"{instance_id}?"
                          f"resource-version={resource_version}")