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