aboutsummaryrefslogtreecommitdiffstats
path: root/onap-client/onap_client/sdc/service.py
blob: 6aace73b7133de17fd6f325854327608a5add02c (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
# -*- coding: utf8 -*-
# ============LICENSE_START=======================================================
# org.onap.vvp/validation-scripts
# ===================================================================
# Copyright © 2020 AT&T Intellectual Property. All rights reserved.
# ===================================================================
#
# Unless otherwise specified, all software contained herein is licensed
# under the Apache License, Version 2.0 (the "License");
# you may not use this software 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.
#
#
#
# Unless otherwise specified, all documentation contained herein is licensed
# under the Creative Commons License, Attribution 4.0 Intl. (the "License");
# you may not use this documentation except in compliance with the License.
# You may obtain a copy of the License at
#
#             https://creativecommons.org/licenses/by/4.0/
#
# Unless required by applicable law or agreed to in writing, documentation
# 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.
#
# ============LICENSE_END============================================

from onap_client.lib import generate_dummy_string
from onap_client.resource import Resource
from onap_client import exceptions
from onap_client.client.clients import get_client as Client
from onap_client.sdc.vnf import get_vnf_id
from onap_client.util import utility

import base64
import time
import json
import random
import uuid


def normalize_category_icon(category_name):
    if category_name == "Network L1-3":
        return "network_l_4"
    elif category_name == "Mobility":
        return "mobility"
    elif category_name == "E2E Service":
        return "network_l_1-3"
    elif category_name == "Network L1-3":
        return "network_l_1-3"
    elif category_name == "Network Service":
        return "network_l_1-3"
    elif category_name == "Network 4+":
        return "network_l_4"
    elif category_name == "VoIP Call Control":
        return "call_controll"
    else:
        return "network_l_1-3"


class Service(Resource):
    resource_name = "SERVICE"
    spec = {
        "instantiation_type": {
            "type": str,
            "required": False,
            "default": "A-la-carte",
        },
        "service_name": {
            "type": str,
            "required": False,
            "default": generate_dummy_string("test_service_"),
        },
        "contact_id": {"type": str, "required": False, "default": "cs0008"},
        "category_name": {"type": str, "required": False, "default": "Network L1-3"},
        "tag": {"type": str, "required": False, "default": "robot-ete"},
        "project_code": {"type": str, "required": False, "default": ""},
        "environment_context": {
            "type": str,
            "required": False,
            "default": "General_Revenue-Bearing",
        },
        "ecomp_generated_naming": {"type": str, "required": False, "default": "true"},
        "description": {
            "type": str,
            "required": False,
            "default": "Brand New Service",
        },
        "service_type": {"type": str, "required": False, "default": ""},
        "service_role": {"type": str, "required": False, "default": ""},
        "naming_policy": {"type": str, "required": False, "default": ""},
        "resources": {
            "type": list,
            "list_item": dict,
            "required": False,
            "default": [],
            "nested": {
                "resource_name": {"type": str, "required": True},
                "resource_id": {"type": str, "required": False},
                "catalog_resource_name": {"type": str, "required": False},
                "origin_type": {"type": str, "required": False, "default": "VF"},
                "properties": {"type": dict, "required": False, "default": {}},
                "deployment_properties": {"type": dict, "required": False, "default": {}}
            },
        },
        "allow_update": {"type": bool, "required": False, "default": False},
        "wait_for_distribution": {"type": bool, "required": False, "default": False},
    }

    def _create(self, service_input):
        """Creates a service object in SDC"""
        service = None

        existing = get_service_id(service_input.get("service_name"))
        if existing is None:
            service = create_service(service_input)
        elif service_input.get("allow_update"):
            service = update_service(existing, service_input)
        else:
            raise exceptions.ResourceAlreadyExistsException(
                "Service resource {} already exists".format(
                    service_input.get("service_name")
                )
            )

        return service

    def _post_create(self):
        resources = self.resources

        for resource in resources:
            resource_name = resource.get("resource_name")
            catalog_resource_name = resource.get("catalog_resource_name")
            resource_id = resource.get("resource_id")
            resource_properties = resource.get("properties")
            if not resource_id:
                resource_id = get_vnf_id(catalog_resource_name)
                if not resource_id:
                    raise exceptions.ResourceIDNotFoundException(
                        "resource ID was not passed, and resource lookup by name was not found {}".format(
                            resource_name
                        )
                    )
            resource_origin = resource.get("origin_type")
            self.add_resource(resource_id, resource_name, origin_type=resource_origin)
            for k, v in resource_properties.items():
                if isinstance(v, dict):
                    v = json.dumps(v).replace('"', '\\"')
                self.add_property_value(resource_name, k, v)

            if resource.get("deployment_properties"):
                self.update_resource_deployment_properties(resource_name, resource.get("deployment_properties"))

    def _submit(self):
        """Submits the service in SDC and distributes the model"""
        oc = Client()

        DISTRIBUTION_STEPS = oc.config.sdc.SERVICE_DISTRIBUTION or []

        self.oc.sdc.service.checkin_service(**self.attributes, user_remarks="checking in")

        if (
            not DISTRIBUTION_STEPS
            or "request_service_certification" in DISTRIBUTION_STEPS
        ):
            self.oc.sdc.service.request_service_certification(
                **self.attributes, user_remarks="requesting certification"
            )

        if (
            not DISTRIBUTION_STEPS
            or "start_service_certification" in DISTRIBUTION_STEPS
        ):
            self.oc.sdc.service.start_service_certification(
                **self.attributes, user_remarks="certifying"
            )

        if (
            not DISTRIBUTION_STEPS
            or "finish_service_certification" in DISTRIBUTION_STEPS
        ):
            catalog_service = self.oc.sdc.service.finish_service_certification(
                **self.attributes, user_remarks="certified"
            )
            self.attributes["catalog_service_id"] = catalog_service.catalog_service_id

        if (
            not DISTRIBUTION_STEPS
            or "approve_service_certification" in DISTRIBUTION_STEPS
        ):
            self.oc.sdc.service.approve_service_certification(
                **self.attributes, user_remarks="approved"
            )
        headers = {"X-TransactionId": str(uuid.uuid4())}
        self.oc.sdc.service.distribute_sdc_service(**self.attributes, **headers)

        if self.wait_for_distribution:
            poll_distribution(self.service_name)

        self._refresh()

    def add_resource(
        self, catalog_resource_id, catalog_resource_name, origin_type="VF"
    ):
        """Attaches a resource to a Service in SDC

        :catalog_resource_id: ID of a resource in the SDC catalog
        :catalog_resource_name: name to give to the resource when attaching to service
        :origin_type: specifies the origin of the attached resource

        """
        milli_timestamp = int(time.time() * 1000)
        component_instances = self.tosca.get("componentInstances", [])
        if component_instances:
            for component in component_instances:
                if component.get("componentName") == catalog_resource_name:
                    self.oc.sdc.service.delete_resource_from_service(
                        catalog_service_id=self.catalog_service_id,
                        resource_instance_id=component.get("uniqueId")
                    )
                    break

        resource_instance = self.oc.sdc.service.add_resource_instance(
            **self.attributes,
            posX=random.randrange(150, 550),  # nosec
            posY=random.randrange(150, 450),  # nosec
            milli_timestamp=milli_timestamp,
            catalog_resource_id=catalog_resource_id,
            catalog_resource_name=catalog_resource_name,
            originType=origin_type,
        ).response_data

        response = {
            "id": resource_instance.get("uniqueId"),
            "tosca": resource_instance,
        }
        self.attributes[catalog_resource_name] = response

        self._refresh()

    def update_resource_deployment_properties(self, resource_name, deployment_properties):
        vnf_instance = {}
        for component_instance in self.tosca.get("componentInstances", []):
            if component_instance.get("componentName") == resource_name:
                vnf_instance = component_instance
                break

        if not vnf_instance:
            raise exceptions.ResourceNotFoundException(
                "Resource {} was not found on Service {}".format(
                    resource_name, self.service_name
                )
            )

        for deployment_artifact_name, deployment_artifact in vnf_instance.get("deploymentArtifacts").items():
            if deployment_artifact.get("artifactType") == "HEAT_ENV":
                deployment_artifact.update(deployment_properties)
                self.oc.sdc.service.update_module_deployment_properties(
                    catalog_service_id=self.catalog_service_id,
                    catalog_resource_instance_id=vnf_instance.get("uniqueId"),
                    module_id=deployment_artifact.get("uniqueId"),
                    payload_data=deployment_artifact,
                )

    def add_property_value(self, resource_name, property_name, input_value):
        """Updates an property value on a resource attached to a Service

        :resource_name: Name of a resource attached to a service
        :property_name: property name to update
        :input_value: value to update property with

        """
        resource = self.attributes.get(resource_name)
        if not resource:
            raise exceptions.ResourceNotFoundException(
                "Resource {} was not found on Service {}".format(
                    resource_name, self.service_name
                )
            )
        resource_id = resource["id"]

        instance_inputs = self.tosca.get("componentInstancesProperties", {}).get(
            resource_id, []
        )
        for prop in instance_inputs:
            if prop.get("name") == property_name:
                unique_id = prop.get("uniqueId")
                parent_unique_id = prop.get("parentUniqueId")
                owner_id = prop.get("ownerId")
                schemaType = prop.get("schemaType", "")
                property_type = prop.get("type")
                return self.oc.sdc.service.add_catalog_service_property(
                    **self.attributes,
                    unique_id=unique_id,
                    parent_unique_id=parent_unique_id,
                    owner_id=owner_id,
                    catalog_resource_instance_id=resource_id,
                    input_name=property_name,
                    input_value=input_value,
                    schema_type=schemaType,
                    property_type=property_type,
                )

        raise exceptions.PropertyNotFoundException(
            "Property {} was not found in VF Instance {}".format(
                property_name, resource_id
            )
        )

    def _refresh(self):
        self.tosca = self.oc.sdc.service.get_sdc_service(
            catalog_service_id=self.catalog_service_id
        ).response_data

    def _output(self):
        return self.tosca


def update_service(existing_service_id, service_input):
    oc = Client()

    kwargs = service_input

    existing_service = oc.sdc.service.get_sdc_service(
        catalog_service_id=existing_service_id
    ).response_data

    if existing_service.get("lifecycleState") != "NOT_CERTIFIED_CHECKOUT":
        service = oc.sdc.service.checkout_catalog_service(catalog_service_id=existing_service_id).response_data
    else:
        service = existing_service

    new_service_id = service.get("uniqueId")

    kwargs["catalog_service_id"] = new_service_id
    kwargs["tosca"] = oc.sdc.service.get_sdc_service(catalog_service_id=new_service_id).response_data

    return kwargs


def create_service(service_input):
    """Creates a service object in SDC

    :service_input: dictionary with values to input for service creation

    :return: dictionary of updated values for created service
    """
    oc = Client()

    category_name_lower = service_input.get("category_name").lower()
    category_name_icon = normalize_category_icon(service_input.get("category_name"))
    category_id = "serviceNewCategory.{}".format(category_name_lower)
    service_input["category_id"] = category_id
    service_input["category_name_lower"] = category_name_lower
    service_input["category_name_icon"] = category_name_icon

    service = oc.sdc.service.add_catalog_service(**service_input)

    service_input["catalog_service_id"] = service.catalog_service_id
    service_input["tosca"] = service.response_data

    return service_input


@utility
def get_service(service_name):
    """Queries SDC for the TOSCA model for a service"""
    oc = Client()

    return oc.sdc.service.get_sdc_service(
        catalog_service_id=get_service_id(service_name)
    ).response_data


@utility
def get_service_id(service_name):
    """Queries SDC for the uniqueId of a service model"""
    oc = Client()

    response = oc.sdc.service.get_services()
    results = response.response_data.get("services", [])
    update_time = -1
    catalog_service = {}
    for service in results:
        if service.get("name") == service_name and service.get("lastUpdateDate") > update_time:
            update_time = service.get("lastUpdateDate")
            catalog_service = service

    return catalog_service.get("uniqueId")


def get_service_uuid(service_name):
    return get_service(service_name).get("uuid")


def get_service_distribution(service_name):
    oc = Client()

    distribution_id = get_distribution_id(service_name)

    if distribution_id:
        return oc.sdc.service.get_service_distribution_details(
            distribution_id=distribution_id
        ).response_data

    return None


def get_distribution_id(service_name):
    oc = Client()

    distribution = oc.sdc.service.get_service_distribution(
        distribution_service_id=get_service_uuid(service_name)
    ).response_data
    if distribution:
        details = distribution.get("distributionStatusOfServiceList", [])
        for entry in details:
            return entry.get("distributionID")

    return None


@utility
def poll_distribution(service_name):
    """Polls a distributed service until distribution is complete"""
    oc = Client()

    poll_interval = oc.config.sdc.POLL_INTERVAL or 30
    x = 0
    while x < 30:
        distribution = get_service_distribution(service_name)
        if not distribution:
            raise exceptions.DistributionNotFound(
                "Could not determine distribution status for {}".format(service_name)
            )
        distribution_list = distribution.get("distributionStatusList")
        for component in distribution_list:
            status = component.get("status")
            component_name = component.get("omfComponentID")
            if status == "DISTRIBUTION_COMPLETE_ERROR":
                raise exceptions.DistributionFailure(
                    "Distribution failure for service {}, component details {}".format(
                        service_name, component
                    )
                )
            elif status == "COMPONENT_DONE_ERROR" and component_name == "aai-ml":
                raise exceptions.DistributionFailure(
                    "Distribution failure for service {}, component details {}".format(
                        service_name, component
                    )
                )
            elif status == "DISTRIBUTION_COMPLETE_OK":
                return "Distribution Successful"
        x += 1
        time.sleep(poll_interval)

    raise exceptions.DistributionTimeout(
        "Distribution polling timed out waiting for {}".format(service_name)
    )


@utility
def download_csar(service_name, output_file):
    oc = Client()

    service = get_service(service_name)
    artifact_id = service.get("toscaArtifacts", {}).get("assettoscacsar", {}).get("uniqueId")

    csar_data = oc.sdc.service.get_sdc_csar(
        catalog_service_id=service.get("uniqueId"),
        csar_artifact_id=artifact_id
    ).response_data

    data = base64.b64decode(csar_data.get("base64Contents"))

    output_file = f"{output_file}.csar" if not output_file.endswith(".csar") else output_file

    with open(output_file, "wb") as f:
        f.write(data)