aboutsummaryrefslogtreecommitdiffstats
path: root/onap-client/onap_client/so/service_instance.py
blob: 3675a4e99c727801619d8ee3f9938c41cb544a1d (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
# -*- 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============================================

import uuid

from onap_client.lib import generate_dummy_string
from onap_client.resource import Resource
from onap_client.client.clients import get_client as Client
from onap_client.exceptions import (
    SORequestStatusUnavailable,
    SORequestFailed,
    SORequestTimeout,
    TenantNotFound,
    ServiceInstanceNotFound,
)
from onap_client import sdc
from onap_client.util import utility
from time import sleep


class ServiceInstance(Resource):
    resource_name = "SERVICE_INSTANCE"
    spec = {
        "service_instance_name": {
            "type": str,
            "required": False,
            "default": generate_dummy_string("SI_"),
        },
        "requestor_id": {"type": str, "required": False, "default": "cs0008"},
        "model_name": {"type": str, "required": True},
        "model_version": {"type": str, "required": False, "default": "1.0"},
        "tenant_name": {"type": str, "required": True},
        "cloud_owner": {"type": str, "required": True},
        "cloud_region": {"type": str, "required": True},
        "api_type": {"type": str, "required": False, "default": "GR_API"},
        "service_type": {"type": str, "required": True},
        "customer_name": {"type": str, "required": True},
        "project_name": {"type": str, "required": True},
        "owning_entity_name": {"type": str, "required": True},
    }

    def _create(self, instance_input):
        tenant_id = get_tenant_id(
            instance_input.get("cloud_region"),
            instance_input.get("cloud_owner"),
            instance_input.get("tenant_name")
        )
        instance_input["tenant_id"] = tenant_id
        instance_input["customer_id"] = instance_input.get("customer_name")

        service_model = self.oc.sdc.service.get_sdc_service(
            catalog_service_id=sdc.service.get_service_id(
                instance_input.get("model_name")
            )
        ).response_data

        instance_input["model_invariant_id"] = service_model["invariantUUID"]
        instance_input["model_version_id"] = service_model["uniqueId"]

        category_parameters = self.oc.vid.maintenance.get_category_parameters().response_data
        for entity in category_parameters.get("categoryParameters", {}).get("owningEntity", []):
            if entity.get("name") == instance_input.get("owning_entity_name"):
                instance_input["owning_entity_id"] = entity.get("id")
                break

        return create_service_instance(instance_input)


@utility
def get_service_instance(instance_name):
    """Queries SDNC for a list of all service instances and returns
    The service instance that matches <instance name>"""
    oc = Client()

    service_instances = oc.sdnc.config.get_service_instances().response_data
    for si in service_instances.get("services", {}).get("service", []):
        if si.get("service-data", {}).get("service-request-input", {}).get("service-instance-name") == instance_name:
            return si

    raise ServiceInstanceNotFound("Service Instance {} was not found".format(instance_name))


def get_tenant_id(cloud_region, cloud_owner, tenant_name):
    oc = Client()

    tenants = oc.aai.cloud_infrastructure.get_cloud_region_tenants(
        cloud_owner=cloud_owner,
        cloud_region=cloud_region
    ).response_data

    for tenant in tenants.get("tenant"):
        if tenant.get("tenant-name") == tenant_name:
            return tenant.get("tenant-id")

    raise TenantNotFound("Tenant {} was not found in AAI".format(tenant_name))


def create_service_instance(instance_input):
    oc = Client()

    headers = {"X-TransactionId": str(uuid.uuid4())}
    service_instance = oc.so.service_instantiation.create_service_instance(
        **instance_input, **headers
    )

    request_id = service_instance.response_data.get("requestReferences", {}).get(
        "requestId"
    )

    instance_input["request_info"] = poll_request(request_id)

    return instance_input


@utility
def poll_request(request_id):
    """Poll an SO request until completion"""
    oc = Client()

    poll_interval = oc.config.so.POLL_INTERVAL or 30
    request = None
    x = 0
    while x < 30:
        request = oc.so.service_instantiation.get_request_status(
            request_id=request_id
        ).response_data
        status = request.get("request", {}).get("requestStatus", {}).get("requestState")
        if not status:
            raise SORequestStatusUnavailable(
                "Could not determine request for {}".format(request_id)
            )
        if status == "FAILED":
            failure_message = (
                request.get("request", {}).get("requestStatus", {}).get("statusMessage")
            )
            raise SORequestFailed(
                "Request {} failed with message {}".format(request_id, failure_message)
            )
        elif status == "COMPLETE":
            return request

        x += 1

        sleep(poll_interval)

    raise SORequestTimeout("Request {} timed out polling for status".format(request_id))


@utility
def delete_service_instance(service_instance_name, api_type="GR_API"):
    """Delete a Service Instance from SO"""
    oc = Client()

    si = get_service_instance(service_instance_name)
    si_id = si.get("service-instance-id")
    invariant_id = si.get("service-data").get("service-information").get("onap-model-information").get("model-invariant-uuid")
    version = si.get("service-data").get("service-information").get("onap-model-information").get("model-version")

    return oc.so.service_instantiation.delete_service_instance(
        service_invariant_id=invariant_id,
        service_name=service_instance_name,
        service_version=version,
        service_instance_id=si_id,
        api_type=api_type,
    ).response_data