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
|
# -*- 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 Client as SOClient
from onap_client.exceptions import (
ServiceInstanceNotFound,
VNFComponentNotFound,
ModuleModelNameNotFound,
NoArtifactFoundInModel,
VNFInstanceNotFound,
)
from onap_client import sdc
from onap_client import so
from onap_client.util import utility
oc = SOClient()
so_client = oc.so
sdc_client = oc.sdc
sdnc_client = oc.sdnc
class VNFInstance(Resource):
resource_name = "VNF_INSTANCE"
spec = {
"vnf_instance_name": {
"type": str,
"required": False,
"default": generate_dummy_string("VNF_"),
},
"service_instance_name": {"type": str, "required": True},
"requestor_id": {"type": str, "required": False, "default": "cs0008"},
"model_name": {"type": str, "required": True},
"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"},
"platform": {"type": str, "required": True},
"line_of_business": {"type": str, "required": True},
}
def _create(self, instance_input):
tenant_id = so.service_instance.get_tenant_id(
instance_input.get("cloud_region"),
instance_input.get("cloud_owner"),
instance_input.get("tenant_name")
)
instance_input["tenant_id"] = tenant_id
service_instance = get_service_instance(
instance_input.get("service_instance_name")
)
if not service_instance:
raise ServiceInstanceNotFound(
"No service instance found for {}".format(
instance_input.get("service_instance_name")
)
)
service_instance_id = service_instance.get("service-instance-id")
model_information = (
service_instance.get("service-data")
.get("service-information")
.get("onap-model-information")
)
service_invariant_id = model_information["model-invariant-uuid"]
service_model_id = model_information["model-uuid"]
service_model_version = model_information["model-version"]
service_model_name = model_information["model-name"]
vnf_component = get_vnf_model_component(
service_model_name, instance_input.get("model_name")
)
if not vnf_component:
raise VNFComponentNotFound(
"No component found for {}".format(instance_input.get("model_name"))
)
vnf_model_customization_id = vnf_component["customizationUUID"]
vnf_model_version_id = vnf_component["actualComponentUid"]
vnf_model_version = vnf_component["componentVersion"]
vnf_model = sdc_client.vnf.get_catalog_resource(
catalog_resource_id=vnf_model_version_id,
).response_data
vnf_model_invariant_id = vnf_model["invariantUUID"]
instance_input["model_invariant_id"] = vnf_model_invariant_id
instance_input["model_version_id"] = vnf_model_version_id
instance_input["model_customization_id"] = vnf_model_customization_id
instance_input["model_version"] = vnf_model_version
instance_input["service_model_name"] = service_model_name
instance_input["service_model_invariant_id"] = service_invariant_id
instance_input["service_model_version"] = service_model_version
instance_input["service_model_version_id"] = service_model_id
instance_input["service_instance_id"] = service_instance_id
return create_vnf_instance(instance_input)
def get_vnf_model_component(service_model_name, vnf_model_name):
service_model = sdc_client.service.get_sdc_service(
catalog_service_id=sdc.service.get_service_id(service_model_name)
).response_data
for component in service_model.get("componentInstances", []):
if component["componentName"] == vnf_model_name:
return component
return None
def get_service_instance(service_instance_name):
service_instances = sdnc_client.config.get_service_instances().response_data
for si in service_instances.get("services", {}).get("service", []):
si_name = (
si.get("service-data", {})
.get("service-request-input", {})
.get("service-instance-name")
)
if si_name == service_instance_name:
return si
return None
def get_module_model(vnf_model, heat_template_name):
artifact_uuid = None
deployment_artifacts = vnf_model.get("deploymentArtifacts", {})
for artifact_name, artifact_data in deployment_artifacts.items():
if artifact_data.get("artifactName") == heat_template_name:
artifact_uuid = artifact_data.get("artifactUUID")
if not artifact_uuid:
raise NoArtifactFoundInModel(
"Heat Template {} was not found in service model".format(heat_template_name)
)
group_instances = vnf_model.get("groupInstances", [])
for instance in group_instances:
if artifact_uuid in instance.get("artifactsUuid", []):
# return instance.get("groupName")
return instance
raise ModuleModelNameNotFound(
"Module Model Name for {} was not found in service model".format(
heat_template_name
)
)
def get_vnf_instance(service_instance, vnf_instance_name):
for vnf_instance in (
service_instance.get("service-data", {}).get("vnfs", {}).get("vnf", [])
):
vi_name = (
vnf_instance.get("vnf-data", {}).get("vnf-information", {}).get("vnf-name")
)
if vi_name == vnf_instance_name:
return vnf_instance
return None
def create_vnf_instance(instance_input):
headers = {"X-TransactionId": str(uuid.uuid4())}
vnf_instance = so_client.service_instantiation.create_vnf_instance(
**instance_input, **headers
)
request_id = vnf_instance.response_data.get("requestReferences", {}).get(
"requestId"
)
instance_input["request_info"] = so.service_instance.poll_request(request_id)
return instance_input
@utility
def delete_vnf_instance(service_instance_name, vnf_instance_name, api_type="GR_API"):
"""Delete a VNF Instance from SO"""
si = so.service_instance.get_service_instance(service_instance_name)
si_id = si.get("service-instance-id")
for vnfi in si.get("service-data", {}).get("vnfs", {}).get("vnf", []):
vnfi_id = vnfi.get("vnf-id")
if vnfi.get("vnf-data", {}).get("vnf-request-input", {}).get("vnf-name") == vnf_instance_name:
invariant_id = vnfi.get("vnf-data").get("vnf-information").get("onap-model-information").get("model-invariant-uuid")
vnf_version = vnfi.get("vnf-data").get("vnf-information").get("onap-model-information").get("model-version")
tenant_id = vnfi.get("vnf-data").get("vnf-request-input").get("tenant")
cloud_owner = vnfi.get("vnf-data").get("vnf-request-input").get("cloud-owner")
cloud_region = vnfi.get("vnf-data").get("vnf-request-input").get("aic-cloud-region")
return so_client.service_instantiation.delete_vnf_instance(
vnf_invariant_id=invariant_id,
vnf_version=vnf_version,
vnf_name=vnf_instance_name,
cloud_region=cloud_region,
cloud_owner=cloud_owner,
tenant_id=tenant_id,
vnf_instance_id=vnfi_id,
service_instance_id=si_id,
api_type=api_type,
).response_data
raise VNFInstanceNotFound("VNF Instance was not found: {} {}".format(service_instance_name, vnf_instance_name))
|