Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / bpmn / servicedecomposition / tasks / BBInputSetupUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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=========================================================
21  */
22
23 package org.onap.so.bpmn.servicedecomposition.tasks;
24
25 import java.io.IOException;
26 import java.util.List;
27 import java.util.Map;
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.so.bpmn.common.InjectionHelper;
43 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
44 import org.onap.so.bpmn.servicedecomposition.tasks.exceptions.MultipleObjectsFoundException;
45 import org.onap.so.bpmn.servicedecomposition.tasks.exceptions.NoServiceInstanceFoundException;
46 import org.onap.so.client.aai.AAIObjectPlurals;
47 import org.onap.so.client.aai.AAIObjectType;
48 import org.onap.so.client.aai.entities.AAIResultWrapper;
49 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
50 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
51 import org.onap.so.client.graphinventory.entities.uri.Depth;
52 import org.onap.so.db.catalog.beans.CollectionNetworkResourceCustomization;
53 import org.onap.so.db.catalog.beans.CollectionResourceInstanceGroupCustomization;
54 import org.onap.so.db.catalog.beans.CvnfcConfigurationCustomization;
55 import org.onap.so.db.catalog.beans.NetworkCollectionResourceCustomization;
56 import org.onap.so.db.catalog.beans.Service;
57 import org.onap.so.db.catalog.beans.VfModuleCustomization;
58 import org.onap.so.db.catalog.beans.VnfcInstanceGroupCustomization;
59 import org.onap.so.db.catalog.client.CatalogDbClient;
60 import org.onap.so.db.request.beans.InfraActiveRequests;
61 import org.onap.so.db.request.client.RequestsDbClient;
62 import org.onap.so.serviceinstancebeans.CloudConfiguration;
63 import org.onap.so.serviceinstancebeans.RequestDetails;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
66 import org.springframework.beans.factory.annotation.Autowired;
67 import org.springframework.stereotype.Component;
68 import com.fasterxml.jackson.databind.DeserializationFeature;
69 import com.fasterxml.jackson.databind.ObjectMapper;
70 import com.fasterxml.jackson.databind.SerializationFeature;
71
72 @Component("BBInputSetupUtils")
73 public class BBInputSetupUtils {
74
75     private static final Logger logger = LoggerFactory.getLogger(BBInputSetupUtils.class);
76     private ObjectMapper objectMapper = new ObjectMapper();
77     private static final String REQUEST_ERROR = "Could not find request.";
78
79     @Autowired
80     protected CatalogDbClient catalogDbClient;
81
82     @Autowired
83     protected RequestsDbClient requestsDbClient;
84
85     @Autowired
86     protected InjectionHelper injectionHelper;
87
88     public void updateInfraActiveRequestVnfId(InfraActiveRequests request, String vnfId) {
89         if (request != null) {
90             request.setVnfId(vnfId);
91             this.requestsDbClient.updateInfraActiveRequests(request);
92         } else {
93             logger.debug(REQUEST_ERROR);
94         }
95     }
96
97     public void updateInfraActiveRequestVfModuleId(InfraActiveRequests request, String vfModuleId) {
98         if (request != null) {
99             request.setVfModuleId(vfModuleId);
100             this.requestsDbClient.updateInfraActiveRequests(request);
101         } else {
102             logger.debug(REQUEST_ERROR);
103         }
104     }
105
106     public void updateInfraActiveRequestVolumeGroupId(InfraActiveRequests request, String volumeGroupId) {
107         if (request != null) {
108             request.setVolumeGroupId(volumeGroupId);
109             this.requestsDbClient.updateInfraActiveRequests(request);
110         } else {
111             logger.debug(REQUEST_ERROR);
112         }
113     }
114
115     public void updateInfraActiveRequestNetworkId(InfraActiveRequests request, String networkId) {
116         if (request != null) {
117             request.setNetworkId(networkId);
118             this.requestsDbClient.updateInfraActiveRequests(request);
119         } else {
120             logger.debug(REQUEST_ERROR);
121         }
122     }
123
124     public Service getCatalogServiceByModelUUID(String modelUUID) {
125         return catalogDbClient.getServiceByID(modelUUID);
126     }
127
128     public Service getCatalogServiceByModelVersionAndModelInvariantUUID(String modelVersion,
129             String modelInvariantUUID) {
130         return catalogDbClient.getServiceByModelVersionAndModelInvariantUUID(modelVersion, modelInvariantUUID);
131     }
132
133     public CollectionNetworkResourceCustomization getCatalogCollectionNetworkResourceCustByID(String key) {
134         return catalogDbClient.getCollectionNetworkResourceCustomizationByID(key);
135     }
136
137     public NetworkCollectionResourceCustomization getCatalogNetworkCollectionResourceCustByID(
138             String collectionCustomizationId) {
139         return catalogDbClient.getNetworkCollectionResourceCustomizationByID(collectionCustomizationId);
140     }
141
142     public VfModuleCustomization getVfModuleCustomizationByModelCuztomizationUUID(String modelCustomizationUUID) {
143         return catalogDbClient.getVfModuleCustomizationByModelCuztomizationUUID(modelCustomizationUUID);
144     }
145
146     public CvnfcConfigurationCustomization getCvnfcConfigurationCustomization(String serviceModelUUID,
147             String vnfCustomizationUuid, String vfModuleCustomizationUuid, String cvnfcCustomizationUuid) {
148         return catalogDbClient.getCvnfcCustomization(serviceModelUUID, vnfCustomizationUuid, vfModuleCustomizationUuid,
149                 cvnfcCustomizationUuid);
150     }
151
152     public List<VnfcInstanceGroupCustomization> getVnfcInstanceGroups(String modelCustomizationUUID) {
153         return catalogDbClient.getVnfcInstanceGroupsByVnfResourceCust(modelCustomizationUUID);
154     }
155
156     public Map<String, String> getURIKeysFromServiceInstance(String serviceInstanceId) {
157         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
158         return uri.getURIKeys();
159     }
160
161     protected RequestDetails getRequestDetails(String requestId) throws IOException {
162         if (requestId != null && !requestId.isEmpty()) {
163             InfraActiveRequests activeRequest = this.getInfraActiveRequest(requestId);
164             String requestBody = activeRequest.getRequestBody().replaceAll("\\\\", "");
165             objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
166             objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
167             return objectMapper.readValue(requestBody, RequestDetails.class);
168         } else {
169             return null;
170         }
171     }
172
173     protected InfraActiveRequests getInfraActiveRequest(String requestId) {
174         if (requestId != null && !requestId.isEmpty()) {
175             return requestsDbClient.getInfraActiveRequestbyRequestId(requestId);
176         } else {
177             return null;
178         }
179     }
180
181     protected CloudRegion getCloudRegion(CloudConfiguration cloudConfiguration) {
182         if (cloudConfiguration != null) {
183             String cloudRegionId = cloudConfiguration.getLcpCloudRegionId();
184             String cloudOwner = cloudConfiguration.getCloudOwner();
185             if (cloudRegionId != null && cloudOwner != null && !cloudRegionId.isEmpty() && !cloudOwner.isEmpty()) {
186                 return injectionHelper.getAaiClient().get(CloudRegion.class, AAIUriFactory
187                         .createResourceUri(AAIObjectType.CLOUD_REGION, cloudOwner, cloudRegionId).depth(Depth.TWO))
188                         .orElse(null);
189
190             } else {
191                 return null;
192             }
193         } else {
194             return null;
195         }
196     }
197
198     protected InstanceGroup getAAIInstanceGroup(String instanceGroupId) {
199         return injectionHelper.getAaiClient().get(InstanceGroup.class,
200                 AAIUriFactory.createResourceUri(AAIObjectType.INSTANCE_GROUP, instanceGroupId)).orElse(null);
201     }
202
203     public org.onap.aai.domain.yang.Customer getAAICustomer(String globalSubscriberId) {
204         return injectionHelper.getAaiClient().get(org.onap.aai.domain.yang.Customer.class,
205                 AAIUriFactory.createResourceUri(AAIObjectType.CUSTOMER, globalSubscriberId)).orElse(null);
206     }
207
208     public ServiceSubscription getAAIServiceSubscription(String globalSubscriberId, String subscriptionServiceType) {
209
210         if (globalSubscriberId == null || globalSubscriberId.equals("") || subscriptionServiceType == null
211                 || subscriptionServiceType.equals("")) {
212             return null;
213         } else {
214             return injectionHelper.getAaiClient().get(ServiceSubscription.class, AAIUriFactory
215                     .createResourceUri(AAIObjectType.SERVICE_SUBSCRIPTION, globalSubscriberId, subscriptionServiceType))
216                     .orElse(null);
217         }
218
219     }
220
221     public ServiceInstance getAAIServiceInstanceById(String serviceInstanceId) {
222         return injectionHelper.getAaiClient()
223                 .get(ServiceInstance.class, AAIUriFactory
224                         .createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId).depth(Depth.TWO))
225                 .orElse(null);
226     }
227
228     protected ServiceInstance getAAIServiceInstanceByIdAndCustomer(String globalCustomerId, String serviceType,
229             String serviceInstanceId) {
230         return injectionHelper.getAaiClient().get(ServiceInstance.class, AAIUriFactory
231                 .createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalCustomerId, serviceType, serviceInstanceId)
232                 .depth(Depth.TWO)).orElse(null);
233     }
234
235     protected org.onap.aai.domain.yang.ServiceInstances getAAIServiceInstancesByName(String serviceInstanceName,
236             Customer customer) {
237
238         return injectionHelper.getAaiClient()
239                 .get(org.onap.aai.domain.yang.ServiceInstances.class,
240                         AAIUriFactory
241                                 .createResourceUri(AAIObjectPlurals.SERVICE_INSTANCE, customer.getGlobalCustomerId(),
242                                         customer.getServiceSubscription().getServiceType())
243                                 .queryParam("service-instance-name", serviceInstanceName).depth(Depth.TWO))
244                 .orElseGet(() -> {
245                     logger.debug("No Service Instance matched by name");
246                     return null;
247                 });
248     }
249
250     public org.onap.aai.domain.yang.ServiceInstance getAAIServiceInstanceByName(String serviceInstanceName,
251             Customer customer) throws Exception {
252         org.onap.aai.domain.yang.ServiceInstance aaiServiceInstance = null;
253         org.onap.aai.domain.yang.ServiceInstances aaiServiceInstances = null;
254         aaiServiceInstances = getAAIServiceInstancesByName(serviceInstanceName, customer);
255
256         if (aaiServiceInstances == null) {
257             return null;
258         } else if (aaiServiceInstances.getServiceInstance().size() > 1) {
259             throw new Exception("Multiple Service Instances Returned");
260         } else {
261             aaiServiceInstance = aaiServiceInstances.getServiceInstance().get(0);
262         }
263         return aaiServiceInstance;
264     }
265
266     protected ServiceInstances getAAIServiceInstancesByName(String globalCustomerId, String serviceType,
267             String serviceInstanceName) {
268
269         return injectionHelper.getAaiClient()
270                 .get(ServiceInstances.class,
271                         AAIUriFactory
272                                 .createResourceUri(AAIObjectPlurals.SERVICE_INSTANCE, globalCustomerId, serviceType)
273                                 .queryParam("service-instance-name", serviceInstanceName).depth(Depth.TWO))
274                 .orElseGet(() -> {
275                     logger.debug("No Service Instance matched by name");
276                     return null;
277                 });
278     }
279
280     public Optional<ServiceInstance> getAAIServiceInstanceByName(String globalCustomerId, String serviceType,
281             String serviceInstanceName) throws Exception {
282         ServiceInstance aaiServiceInstance = null;
283         ServiceInstances aaiServiceInstances = null;
284         aaiServiceInstances = getAAIServiceInstancesByName(globalCustomerId, serviceType, serviceInstanceName);
285
286         if (aaiServiceInstances == null) {
287             return Optional.empty();
288         } else if (aaiServiceInstances.getServiceInstance().size() > 1) {
289             throw new Exception("Multiple Service Instances Returned");
290         } else {
291             aaiServiceInstance = aaiServiceInstances.getServiceInstance().get(0);
292         }
293         return Optional.of(aaiServiceInstance);
294     }
295
296     public org.onap.so.db.catalog.beans.InstanceGroup getCatalogInstanceGroup(String modelUUID) {
297         return this.catalogDbClient.getInstanceGroupByModelUUID(modelUUID);
298     }
299
300     public List<CollectionResourceInstanceGroupCustomization> getCollectionResourceInstanceGroupCustomization(
301             String modelCustomizationUUID) {
302         return this.catalogDbClient
303                 .getCollectionResourceInstanceGroupCustomizationByModelCustUUID(modelCustomizationUUID);
304     }
305
306     public AAIResultWrapper getAAIResourceDepthOne(AAIResourceUri aaiResourceUri) {
307         AAIResourceUri clonedUri = aaiResourceUri.clone();
308         return this.injectionHelper.getAaiClient().get(clonedUri.depth(Depth.ONE));
309     }
310
311     public AAIResultWrapper getAAIResourceDepthTwo(AAIResourceUri aaiResourceUri) {
312         AAIResourceUri clonedUri = aaiResourceUri.clone();
313         return this.injectionHelper.getAaiClient().get(clonedUri.depth(Depth.TWO));
314     }
315
316     public Configuration getAAIConfiguration(String configurationId) {
317         return this.injectionHelper.getAaiClient()
318                 .get(Configuration.class,
319                         AAIUriFactory.createResourceUri(AAIObjectType.CONFIGURATION, configurationId).depth(Depth.ONE))
320                 .orElseGet(() -> {
321                     logger.debug("No Configuration matched by id");
322                     return null;
323                 });
324     }
325
326     public GenericVnf getAAIGenericVnf(String vnfId) {
327
328         return this.injectionHelper.getAaiClient()
329                 .get(GenericVnf.class,
330                         AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).depth(Depth.ONE))
331                 .orElseGet(() -> {
332                     logger.debug("No Generic Vnf matched by id");
333                     return null;
334                 });
335     }
336
337     public VolumeGroup getAAIVolumeGroup(String cloudOwnerId, String cloudRegionId, String volumeGroupId) {
338         return this.injectionHelper.getAaiClient()
339                 .get(VolumeGroup.class, AAIUriFactory
340                         .createResourceUri(AAIObjectType.VOLUME_GROUP, cloudOwnerId, cloudRegionId, volumeGroupId)
341                         .depth(Depth.ONE))
342                 .orElseGet(() -> {
343                     logger.debug("No Generic Vnf matched by id");
344                     return null;
345                 });
346     }
347
348     public VfModule getAAIVfModule(String vnfId, String vfModuleId) {
349         return this.injectionHelper.getAaiClient()
350                 .get(VfModule.class,
351                         AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId).depth(Depth.ONE))
352                 .orElseGet(() -> {
353                     logger.debug("No Generic Vnf matched by id");
354                     return null;
355                 });
356     }
357
358     public L3Network getAAIL3Network(String networkId) {
359
360         return this.injectionHelper.getAaiClient()
361                 .get(L3Network.class,
362                         AAIUriFactory.createResourceUri(AAIObjectType.L3_NETWORK, networkId).depth(Depth.ONE))
363                 .orElseGet(() -> {
364                     logger.debug("No Generic Vnf matched by id");
365                     return null;
366                 });
367
368     }
369
370     public Optional<ServiceInstance> getRelatedServiceInstanceFromInstanceGroup(String instanceGroupId)
371             throws Exception {
372         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.INSTANCE_GROUP, instanceGroupId);
373         uri.relatedTo(AAIObjectPlurals.SERVICE_INSTANCE);
374         Optional<ServiceInstances> serviceInstances = injectionHelper.getAaiClient().get(ServiceInstances.class, uri);
375         ServiceInstance serviceInstance = null;
376         if (!serviceInstances.isPresent()) {
377             logger.debug("No ServiceInstances were found");
378             return Optional.empty();
379         } else {
380             if (serviceInstances.get().getServiceInstance().isEmpty()) {
381                 throw new NoServiceInstanceFoundException("No ServiceInstances Returned");
382             } else if (serviceInstances.get().getServiceInstance().size() > 1) {
383                 throw new MultipleObjectsFoundException("Multiple ServiceInstances Returned");
384             } else {
385                 serviceInstance = serviceInstances.get().getServiceInstance().get(0);
386             }
387             return Optional.of(serviceInstance);
388         }
389     }
390
391     public Optional<L3Network> getRelatedNetworkByNameFromServiceInstance(String serviceInstanceId, String networkName)
392             throws Exception {
393         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
394         uri.relatedTo(AAIObjectPlurals.L3_NETWORK).queryParam("network-name", networkName);
395         Optional<L3Networks> networks = injectionHelper.getAaiClient().get(L3Networks.class, uri);
396         L3Network network = null;
397         if (!networks.isPresent()) {
398             logger.debug("No Networks matched by name");
399             return Optional.empty();
400         } else {
401             if (networks.get().getL3Network().size() > 1) {
402                 throw new Exception("Multiple Networks Returned");
403             } else {
404                 network = networks.get().getL3Network().get(0);
405             }
406             return Optional.of(network);
407         }
408     }
409
410     public Optional<GenericVnf> getRelatedVnfByNameFromServiceInstance(String serviceInstanceId, String vnfName)
411             throws Exception {
412         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
413         uri.relatedTo(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName);
414         Optional<GenericVnfs> vnfs = injectionHelper.getAaiClient().get(GenericVnfs.class, uri);
415         GenericVnf vnf = null;
416         if (!vnfs.isPresent()) {
417             logger.debug("No Vnfs matched by name");
418             return Optional.empty();
419         } else {
420             if (vnfs.get().getGenericVnf().size() > 1) {
421                 throw new Exception("Multiple Vnfs Returned");
422             } else {
423                 vnf = vnfs.get().getGenericVnf().get(0);
424             }
425             return Optional.of(vnf);
426         }
427     }
428
429     public Optional<VolumeGroup> getRelatedVolumeGroupByNameFromVnf(String vnfId, String volumeGroupName)
430             throws Exception {
431         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId);
432         uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-name", volumeGroupName);
433         Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
434         VolumeGroup volumeGroup = null;
435         if (!volumeGroups.isPresent()) {
436             logger.debug("No VolumeGroups matched by name");
437             return Optional.empty();
438         } else {
439             if (volumeGroups.get().getVolumeGroup().size() > 1) {
440                 throw new Exception("Multiple VolumeGroups Returned");
441             } else {
442                 volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
443             }
444             return Optional.of(volumeGroup);
445         }
446     }
447
448     public Optional<VolumeGroup> getRelatedVolumeGroupByNameFromVfModule(String vnfId, String vfModuleId,
449             String volumeGroupName) throws Exception {
450         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId);
451         uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-name", volumeGroupName);
452         Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
453         VolumeGroup volumeGroup = null;
454         if (!volumeGroups.isPresent()) {
455             logger.debug("No VolumeGroups matched by name");
456             return Optional.empty();
457         } else {
458             if (volumeGroups.get().getVolumeGroup().size() > 1) {
459                 throw new Exception("Multiple VolumeGroups Returned");
460             } else {
461                 volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
462             }
463             return Optional.of(volumeGroup);
464         }
465     }
466 }