2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.bpmn.servicedecomposition.tasks;
25 import java.io.IOException;
26 import java.util.List;
28 import java.util.Optional;
29 import org.onap.aai.domain.yang.CloudRegion;
30 import org.onap.aai.domain.yang.Configuration;
31 import org.onap.aai.domain.yang.GenericVnf;
32 import org.onap.aai.domain.yang.GenericVnfs;
33 import org.onap.aai.domain.yang.InstanceGroup;
34 import org.onap.aai.domain.yang.L3Network;
35 import org.onap.aai.domain.yang.L3Networks;
36 import org.onap.aai.domain.yang.ServiceInstance;
37 import org.onap.aai.domain.yang.ServiceInstances;
38 import org.onap.aai.domain.yang.ServiceSubscription;
39 import org.onap.aai.domain.yang.VfModule;
40 import org.onap.aai.domain.yang.VolumeGroup;
41 import org.onap.aai.domain.yang.VolumeGroups;
42 import org.onap.aai.domain.yang.VpnBinding;
43 import org.onap.so.bpmn.common.InjectionHelper;
44 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
45 import org.onap.so.bpmn.servicedecomposition.tasks.exceptions.MultipleObjectsFoundException;
46 import org.onap.so.bpmn.servicedecomposition.tasks.exceptions.NoServiceInstanceFoundException;
47 import org.onap.so.client.aai.AAIObjectPlurals;
48 import org.onap.so.client.aai.AAIObjectType;
49 import org.onap.so.client.aai.entities.AAIResultWrapper;
50 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
51 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
52 import org.onap.so.client.graphinventory.entities.uri.Depth;
53 import org.onap.so.db.catalog.beans.CollectionNetworkResourceCustomization;
54 import org.onap.so.db.catalog.beans.CollectionResourceInstanceGroupCustomization;
55 import org.onap.so.db.catalog.beans.CvnfcConfigurationCustomization;
56 import org.onap.so.db.catalog.beans.NetworkCollectionResourceCustomization;
57 import org.onap.so.db.catalog.beans.Service;
58 import org.onap.so.db.catalog.beans.VfModuleCustomization;
59 import org.onap.so.db.catalog.beans.VnfcInstanceGroupCustomization;
60 import org.onap.so.db.catalog.client.CatalogDbClient;
61 import org.onap.so.db.request.beans.InfraActiveRequests;
62 import org.onap.so.db.request.client.RequestsDbClient;
63 import org.onap.so.serviceinstancebeans.CloudConfiguration;
64 import org.onap.so.serviceinstancebeans.ModelType;
65 import org.onap.so.serviceinstancebeans.RelatedInstance;
66 import org.onap.so.serviceinstancebeans.RelatedInstanceList;
67 import org.onap.so.serviceinstancebeans.RequestDetails;
68 import org.slf4j.Logger;
69 import org.slf4j.LoggerFactory;
70 import org.springframework.beans.factory.annotation.Autowired;
71 import org.springframework.stereotype.Component;
72 import com.fasterxml.jackson.databind.DeserializationFeature;
73 import com.fasterxml.jackson.databind.ObjectMapper;
74 import com.fasterxml.jackson.databind.SerializationFeature;
76 @Component("BBInputSetupUtils")
77 public class BBInputSetupUtils {
79 private static final Logger logger = LoggerFactory.getLogger(BBInputSetupUtils.class);
80 private ObjectMapper objectMapper = new ObjectMapper();
81 private static final String REQUEST_ERROR = "Could not find request.";
84 protected CatalogDbClient catalogDbClient;
87 protected RequestsDbClient requestsDbClient;
90 protected InjectionHelper injectionHelper;
92 public RelatedInstance getRelatedInstanceByType(RequestDetails requestDetails, ModelType modelType) {
93 if (requestDetails.getRelatedInstanceList() != null) {
94 for (RelatedInstanceList relatedInstanceList : requestDetails.getRelatedInstanceList()) {
95 RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
96 if (relatedInstance != null && relatedInstance.getModelInfo() != null
97 && relatedInstance.getModelInfo().getModelType() != null
98 && relatedInstance.getModelInfo().getModelType().equals(modelType)) {
99 return relatedInstance;
106 public void updateInfraActiveRequestVnfId(InfraActiveRequests request, String vnfId) {
107 if (request != null) {
108 request.setVnfId(vnfId);
109 this.requestsDbClient.updateInfraActiveRequests(request);
111 logger.debug(REQUEST_ERROR);
115 public void updateInfraActiveRequestVfModuleId(InfraActiveRequests request, String vfModuleId) {
116 if (request != null) {
117 request.setVfModuleId(vfModuleId);
118 this.requestsDbClient.updateInfraActiveRequests(request);
120 logger.debug(REQUEST_ERROR);
124 public void updateInfraActiveRequestVolumeGroupId(InfraActiveRequests request, String volumeGroupId) {
125 if (request != null) {
126 request.setVolumeGroupId(volumeGroupId);
127 this.requestsDbClient.updateInfraActiveRequests(request);
129 logger.debug(REQUEST_ERROR);
133 public void updateInfraActiveRequestNetworkId(InfraActiveRequests request, String networkId) {
134 if (request != null) {
135 request.setNetworkId(networkId);
136 this.requestsDbClient.updateInfraActiveRequests(request);
138 logger.debug(REQUEST_ERROR);
142 public Service getCatalogServiceByModelUUID(String modelUUID) {
143 return catalogDbClient.getServiceByID(modelUUID);
146 public Service getCatalogServiceByModelVersionAndModelInvariantUUID(String modelVersion,
147 String modelInvariantUUID) {
148 return catalogDbClient.getServiceByModelVersionAndModelInvariantUUID(modelVersion, modelInvariantUUID);
151 public CollectionNetworkResourceCustomization getCatalogCollectionNetworkResourceCustByID(String key) {
152 return catalogDbClient.getCollectionNetworkResourceCustomizationByID(key);
155 public NetworkCollectionResourceCustomization getCatalogNetworkCollectionResourceCustByID(
156 String collectionCustomizationId) {
157 return catalogDbClient.getNetworkCollectionResourceCustomizationByID(collectionCustomizationId);
160 public VfModuleCustomization getVfModuleCustomizationByModelCuztomizationUUID(String modelCustomizationUUID) {
161 return catalogDbClient.getVfModuleCustomizationByModelCuztomizationUUID(modelCustomizationUUID);
164 public CvnfcConfigurationCustomization getCvnfcConfigurationCustomization(String serviceModelUUID,
165 String vnfCustomizationUuid, String vfModuleCustomizationUuid, String cvnfcCustomizationUuid) {
166 return catalogDbClient.getCvnfcCustomization(serviceModelUUID, vnfCustomizationUuid, vfModuleCustomizationUuid,
167 cvnfcCustomizationUuid);
170 public List<VnfcInstanceGroupCustomization> getVnfcInstanceGroups(String modelCustomizationUUID) {
171 return catalogDbClient.getVnfcInstanceGroupsByVnfResourceCust(modelCustomizationUUID);
174 public Map<String, String> getURIKeysFromServiceInstance(String serviceInstanceId) {
175 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
176 return uri.getURIKeys();
179 protected RequestDetails getRequestDetails(String requestId) throws IOException {
180 if (requestId != null && !requestId.isEmpty()) {
181 InfraActiveRequests activeRequest = this.getInfraActiveRequest(requestId);
182 String requestBody = activeRequest.getRequestBody().replaceAll("\\\\", "");
183 objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
184 objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
185 return objectMapper.readValue(requestBody, RequestDetails.class);
191 protected InfraActiveRequests getInfraActiveRequest(String requestId) {
192 if (requestId != null && !requestId.isEmpty()) {
193 return requestsDbClient.getInfraActiveRequestbyRequestId(requestId);
199 protected CloudRegion getCloudRegion(CloudConfiguration cloudConfiguration) {
200 if (cloudConfiguration != null) {
201 String cloudRegionId = cloudConfiguration.getLcpCloudRegionId();
202 String cloudOwner = cloudConfiguration.getCloudOwner();
203 if (cloudRegionId != null && cloudOwner != null && !cloudRegionId.isEmpty() && !cloudOwner.isEmpty()) {
204 return injectionHelper.getAaiClient().get(CloudRegion.class, AAIUriFactory
205 .createResourceUri(AAIObjectType.CLOUD_REGION, cloudOwner, cloudRegionId).depth(Depth.TWO))
216 protected InstanceGroup getAAIInstanceGroup(String instanceGroupId) {
217 return injectionHelper.getAaiClient().get(InstanceGroup.class,
218 AAIUriFactory.createResourceUri(AAIObjectType.INSTANCE_GROUP, instanceGroupId)).orElse(null);
221 public org.onap.aai.domain.yang.Customer getAAICustomer(String globalSubscriberId) {
222 return injectionHelper.getAaiClient().get(org.onap.aai.domain.yang.Customer.class,
223 AAIUriFactory.createResourceUri(AAIObjectType.CUSTOMER, globalSubscriberId)).orElse(null);
226 public ServiceSubscription getAAIServiceSubscription(String globalSubscriberId, String subscriptionServiceType) {
228 if (globalSubscriberId == null || globalSubscriberId.equals("") || subscriptionServiceType == null
229 || subscriptionServiceType.equals("")) {
232 return injectionHelper.getAaiClient().get(ServiceSubscription.class, AAIUriFactory
233 .createResourceUri(AAIObjectType.SERVICE_SUBSCRIPTION, globalSubscriberId, subscriptionServiceType))
239 public ServiceInstance getAAIServiceInstanceById(String serviceInstanceId) {
240 return injectionHelper.getAaiClient()
241 .get(ServiceInstance.class, AAIUriFactory
242 .createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId).depth(Depth.TWO))
246 protected ServiceInstance getAAIServiceInstanceByIdAndCustomer(String globalCustomerId, String serviceType,
247 String serviceInstanceId) {
248 return injectionHelper.getAaiClient().get(ServiceInstance.class, AAIUriFactory
249 .createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalCustomerId, serviceType, serviceInstanceId)
250 .depth(Depth.TWO)).orElse(null);
253 protected org.onap.aai.domain.yang.ServiceInstances getAAIServiceInstancesByName(String serviceInstanceName,
256 return injectionHelper.getAaiClient()
257 .get(org.onap.aai.domain.yang.ServiceInstances.class,
259 .createResourceUri(AAIObjectPlurals.SERVICE_INSTANCE, customer.getGlobalCustomerId(),
260 customer.getServiceSubscription().getServiceType())
261 .queryParam("service-instance-name", serviceInstanceName).depth(Depth.TWO))
263 logger.debug("No Service Instance matched by name");
268 public org.onap.aai.domain.yang.ServiceInstance getAAIServiceInstanceByName(String serviceInstanceName,
269 Customer customer) throws Exception {
270 org.onap.aai.domain.yang.ServiceInstance aaiServiceInstance = null;
271 org.onap.aai.domain.yang.ServiceInstances aaiServiceInstances = null;
272 aaiServiceInstances = getAAIServiceInstancesByName(serviceInstanceName, customer);
274 if (aaiServiceInstances == null) {
276 } else if (aaiServiceInstances.getServiceInstance().size() > 1) {
277 throw new Exception("Multiple Service Instances Returned");
279 aaiServiceInstance = aaiServiceInstances.getServiceInstance().get(0);
281 return aaiServiceInstance;
284 protected ServiceInstances getAAIServiceInstancesByName(String globalCustomerId, String serviceType,
285 String serviceInstanceName) {
287 return injectionHelper.getAaiClient()
288 .get(ServiceInstances.class,
290 .createResourceUri(AAIObjectPlurals.SERVICE_INSTANCE, globalCustomerId, serviceType)
291 .queryParam("service-instance-name", serviceInstanceName).depth(Depth.TWO))
293 logger.debug("No Service Instance matched by name");
298 public Optional<ServiceInstance> getAAIServiceInstanceByName(String globalCustomerId, String serviceType,
299 String serviceInstanceName) throws Exception {
300 ServiceInstance aaiServiceInstance = null;
301 ServiceInstances aaiServiceInstances = null;
302 aaiServiceInstances = getAAIServiceInstancesByName(globalCustomerId, serviceType, serviceInstanceName);
304 if (aaiServiceInstances == null) {
305 return Optional.empty();
306 } else if (aaiServiceInstances.getServiceInstance().size() > 1) {
307 throw new Exception("Multiple Service Instances Returned");
309 aaiServiceInstance = aaiServiceInstances.getServiceInstance().get(0);
311 return Optional.of(aaiServiceInstance);
314 public org.onap.so.db.catalog.beans.InstanceGroup getCatalogInstanceGroup(String modelUUID) {
315 return this.catalogDbClient.getInstanceGroupByModelUUID(modelUUID);
318 public List<CollectionResourceInstanceGroupCustomization> getCollectionResourceInstanceGroupCustomization(
319 String modelCustomizationUUID) {
320 return this.catalogDbClient
321 .getCollectionResourceInstanceGroupCustomizationByModelCustUUID(modelCustomizationUUID);
324 public AAIResultWrapper getAAIResourceDepthOne(AAIResourceUri aaiResourceUri) {
325 AAIResourceUri clonedUri = aaiResourceUri.clone();
326 return this.injectionHelper.getAaiClient().get(clonedUri.depth(Depth.ONE));
329 public AAIResultWrapper getAAIResourceDepthTwo(AAIResourceUri aaiResourceUri) {
330 AAIResourceUri clonedUri = aaiResourceUri.clone();
331 return this.injectionHelper.getAaiClient().get(clonedUri.depth(Depth.TWO));
334 public Configuration getAAIConfiguration(String configurationId) {
335 return this.injectionHelper.getAaiClient()
336 .get(Configuration.class,
337 AAIUriFactory.createResourceUri(AAIObjectType.CONFIGURATION, configurationId).depth(Depth.ONE))
339 logger.debug("No Configuration matched by id");
344 public GenericVnf getAAIGenericVnf(String vnfId) {
346 return this.injectionHelper.getAaiClient()
347 .get(GenericVnf.class,
348 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).depth(Depth.ONE))
350 logger.debug("No Generic Vnf matched by id");
355 public VpnBinding getAAIVpnBinding(String vpnBindingId) {
357 return this.injectionHelper.getAaiClient()
358 .get(VpnBinding.class,
359 AAIUriFactory.createResourceUri(AAIObjectType.VPN_BINDING, vpnBindingId).depth(Depth.ONE))
361 logger.debug("No VpnBinding matched by id");
366 public VolumeGroup getAAIVolumeGroup(String cloudOwnerId, String cloudRegionId, String volumeGroupId) {
367 return this.injectionHelper.getAaiClient()
368 .get(VolumeGroup.class, AAIUriFactory
369 .createResourceUri(AAIObjectType.VOLUME_GROUP, cloudOwnerId, cloudRegionId, volumeGroupId)
372 logger.debug("No Generic Vnf matched by id");
377 public VfModule getAAIVfModule(String vnfId, String vfModuleId) {
378 return this.injectionHelper.getAaiClient()
380 AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId).depth(Depth.ONE))
382 logger.debug("No Generic Vnf matched by id");
387 public L3Network getAAIL3Network(String networkId) {
389 return this.injectionHelper.getAaiClient()
390 .get(L3Network.class,
391 AAIUriFactory.createResourceUri(AAIObjectType.L3_NETWORK, networkId).depth(Depth.ONE))
393 logger.debug("No Generic Vnf matched by id");
399 public Optional<ServiceInstance> getRelatedServiceInstanceFromInstanceGroup(String instanceGroupId)
401 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.INSTANCE_GROUP, instanceGroupId);
402 uri.relatedTo(AAIObjectPlurals.SERVICE_INSTANCE);
403 Optional<ServiceInstances> serviceInstances = injectionHelper.getAaiClient().get(ServiceInstances.class, uri);
404 ServiceInstance serviceInstance = null;
405 if (!serviceInstances.isPresent()) {
406 logger.debug("No ServiceInstances were found");
407 return Optional.empty();
409 if (serviceInstances.get().getServiceInstance().isEmpty()) {
410 throw new NoServiceInstanceFoundException("No ServiceInstances Returned");
411 } else if (serviceInstances.get().getServiceInstance().size() > 1) {
412 throw new MultipleObjectsFoundException("Multiple ServiceInstances Returned");
414 serviceInstance = serviceInstances.get().getServiceInstance().get(0);
416 return Optional.of(serviceInstance);
420 public Optional<L3Network> getRelatedNetworkByNameFromServiceInstance(String serviceInstanceId, String networkName)
422 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
423 uri.relatedTo(AAIObjectPlurals.L3_NETWORK).queryParam("network-name", networkName);
424 Optional<L3Networks> networks = injectionHelper.getAaiClient().get(L3Networks.class, uri);
425 L3Network network = null;
426 if (!networks.isPresent()) {
427 logger.debug("No Networks matched by name");
428 return Optional.empty();
430 if (networks.get().getL3Network().size() > 1) {
431 throw new Exception("Multiple Networks Returned");
433 network = networks.get().getL3Network().get(0);
435 return Optional.of(network);
439 public Optional<GenericVnf> getRelatedVnfByNameFromServiceInstance(String serviceInstanceId, String vnfName)
441 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
442 uri.relatedTo(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName);
443 Optional<GenericVnfs> vnfs = injectionHelper.getAaiClient().get(GenericVnfs.class, uri);
444 GenericVnf vnf = null;
445 if (!vnfs.isPresent()) {
446 logger.debug("No Vnfs matched by name");
447 return Optional.empty();
449 if (vnfs.get().getGenericVnf().size() > 1) {
450 throw new Exception("Multiple Vnfs Returned");
452 vnf = vnfs.get().getGenericVnf().get(0);
454 return Optional.of(vnf);
458 public Optional<VolumeGroup> getRelatedVolumeGroupByNameFromVnf(String vnfId, String volumeGroupName)
460 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId);
461 uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-name", volumeGroupName);
462 Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
463 VolumeGroup volumeGroup = null;
464 if (!volumeGroups.isPresent()) {
465 logger.debug("No VolumeGroups matched by name");
466 return Optional.empty();
468 if (volumeGroups.get().getVolumeGroup().size() > 1) {
469 throw new Exception("Multiple VolumeGroups Returned");
471 volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
473 return Optional.of(volumeGroup);
477 public Optional<VolumeGroup> getRelatedVolumeGroupByNameFromVfModule(String vnfId, String vfModuleId,
478 String volumeGroupName) throws Exception {
479 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId);
480 uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-name", volumeGroupName);
481 Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
482 VolumeGroup volumeGroup = null;
483 if (!volumeGroups.isPresent()) {
484 logger.debug("No VolumeGroups matched by name");
485 return Optional.empty();
487 if (volumeGroups.get().getVolumeGroup().size() > 1) {
488 throw new Exception("Multiple VolumeGroups Returned");
490 volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
492 return Optional.of(volumeGroup);
496 public Optional<org.onap.aai.domain.yang.VpnBinding> getAICVpnBindingFromNetwork(
497 org.onap.aai.domain.yang.L3Network aaiLocalNetwork) {
498 AAIResultWrapper networkWrapper = new AAIResultWrapper(aaiLocalNetwork);
499 if (networkWrapper.getRelationships().isPresent()
500 && !networkWrapper.getRelationships().get().getRelatedUris(AAIObjectType.VPN_BINDING).isEmpty()) {
501 return getAAIResourceDepthOne(
502 networkWrapper.getRelationships().get().getRelatedUris(AAIObjectType.VPN_BINDING).get(0))
503 .asBean(org.onap.aai.domain.yang.VpnBinding.class);
505 return Optional.empty();