fix catalogdb cvfnc customization
[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.CvnfcConfigurationCustomization;
57 import org.onap.so.db.catalog.beans.NetworkCollectionResourceCustomization;
58 import org.onap.so.db.catalog.beans.Service;
59 import org.onap.so.db.catalog.beans.VfModuleCustomization;
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 CvnfcConfigurationCustomization getCvnfcConfigurationCustomization(String serviceModelUUID, String vnfCustomizationUuid,
149                         String vfModuleCustomizationUuid, String cvnfcCustomizationUuid){
150                 return catalogDbClient.getCvnfcCustomization(serviceModelUUID, vnfCustomizationUuid,vfModuleCustomizationUuid, cvnfcCustomizationUuid);
151         }
152         
153         public List<VnfcInstanceGroupCustomization> getVnfcInstanceGroups(String modelCustomizationUUID) {
154                 return catalogDbClient.getVnfcInstanceGroupsByVnfResourceCust(modelCustomizationUUID);
155         }
156
157         public Map<String, String> getURIKeysFromServiceInstance(String serviceInstanceId) {
158                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
159                 return uri.getURIKeys();
160         }
161
162         protected RequestDetails getRequestDetails(String requestId) throws IOException {
163                 if (requestId != null && !requestId.isEmpty()) {
164                         InfraActiveRequests activeRequest = this.getInfraActiveRequest(requestId);
165                         String requestBody = activeRequest.getRequestBody().replaceAll("\\\\", "");
166                         objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
167                         objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
168                         return objectMapper.readValue(requestBody, RequestDetails.class);
169                 } else {
170                         return null;
171                 }
172         }
173
174         protected InfraActiveRequests getInfraActiveRequest(String requestId) {
175                 if (requestId != null && !requestId.isEmpty()) {
176                         return requestsDbClient.getInfraActiveRequestbyRequestId(requestId);
177                 } else {
178                         return null;
179                 }
180         }
181
182         protected CloudRegion getCloudRegion(CloudConfiguration cloudConfiguration) {
183                 if (cloudConfiguration != null) {
184                         String cloudRegionId = cloudConfiguration.getLcpCloudRegionId();
185                         String cloudOwner = cloudConfiguration.getCloudOwner();
186                         if (cloudRegionId != null && cloudOwner != null && !cloudRegionId.isEmpty() && !cloudOwner.isEmpty()) {
187                                 return injectionHelper.getAaiClient().get(CloudRegion.class,
188                                                 AAIUriFactory.createResourceUri(AAIObjectType.CLOUD_REGION, cloudOwner,
189                                                                 cloudRegionId).depth(Depth.TWO)).orElse(null);
190                         
191                         } else {
192                                 return null;
193                         }
194                 } else {
195                         return null;
196                 }
197         }
198
199         protected InstanceGroup getAAIInstanceGroup(String instanceGroupId) {
200                 return injectionHelper.getAaiClient().get(InstanceGroup.class,
201                         AAIUriFactory.createResourceUri(AAIObjectType.INSTANCE_GROUP, instanceGroupId))
202                         .orElse(null);
203         }
204
205         public org.onap.aai.domain.yang.Customer getAAICustomer(String globalSubscriberId) {
206                 return injectionHelper.getAaiClient().get(org.onap.aai.domain.yang.Customer.class,
207                         AAIUriFactory.createResourceUri(AAIObjectType.CUSTOMER, globalSubscriberId))
208                         .orElse(null);
209         }
210
211         public ServiceSubscription getAAIServiceSubscription(String globalSubscriberId, String subscriptionServiceType) {
212         
213                 if(globalSubscriberId == null || globalSubscriberId.equals("") || subscriptionServiceType == null || subscriptionServiceType.equals("")) {
214                         return null;
215                 } else {
216                         return injectionHelper.getAaiClient().get(ServiceSubscription.class, AAIUriFactory.createResourceUri(
217                                         AAIObjectType.SERVICE_SUBSCRIPTION, globalSubscriberId, subscriptionServiceType))
218                                         .orElse(null);
219                 }
220                 
221         }
222
223         public ServiceInstance getAAIServiceInstanceById(String serviceInstanceId) {
224                 return injectionHelper.getAaiClient().get(ServiceInstance.class,
225                                 AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId).depth(Depth.TWO))
226                                 .orElse(null);
227         }
228
229         protected ServiceInstance getAAIServiceInstanceByIdAndCustomer(String globalCustomerId, String serviceType,
230                         String serviceInstanceId) {
231                 return injectionHelper.getAaiClient().get(ServiceInstance.class, AAIUriFactory.createResourceUri(
232                                 AAIObjectType.SERVICE_INSTANCE, globalCustomerId, serviceType, serviceInstanceId).depth(Depth.TWO))
233                                 .orElse(null);
234         }
235
236         protected org.onap.aai.domain.yang.ServiceInstances getAAIServiceInstancesByName(String serviceInstanceName,
237                         Customer customer) {
238                 
239                 return injectionHelper.getAaiClient().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().get(ServiceInstances.class,
270                                 AAIUriFactory.createResourceUri(AAIObjectPlurals.SERVICE_INSTANCE, globalCustomerId, serviceType)
271                                                 .queryParam("service-instance-name", serviceInstanceName).depth(Depth.TWO))
272                                 .orElseGet(() -> {
273                                         logger.debug("No Service Instance matched by name");
274                                         return null;
275                                 });
276         }
277
278         public Optional<ServiceInstance> getAAIServiceInstanceByName(String globalCustomerId, String serviceType,
279                         String serviceInstanceName) throws Exception {
280                 ServiceInstance aaiServiceInstance = null;
281                 ServiceInstances aaiServiceInstances = null;
282                 aaiServiceInstances = getAAIServiceInstancesByName(globalCustomerId, serviceType, serviceInstanceName);
283
284                 if (aaiServiceInstances == null) {
285                         return Optional.empty();
286                 } else if (aaiServiceInstances.getServiceInstance().size() > 1) {
287                         throw new Exception("Multiple Service Instances Returned");
288                 } else {
289                         aaiServiceInstance = aaiServiceInstances.getServiceInstance().get(0);
290                 }
291                 return Optional.of(aaiServiceInstance);
292         }
293
294         public org.onap.so.db.catalog.beans.InstanceGroup getCatalogInstanceGroup(String modelUUID) {
295                 return this.catalogDbClient.getInstanceGroupByModelUUID(modelUUID);
296         }
297
298         public List<CollectionResourceInstanceGroupCustomization> getCollectionResourceInstanceGroupCustomization(
299                         String modelCustomizationUUID) {
300                 return this.catalogDbClient
301                                 .getCollectionResourceInstanceGroupCustomizationByModelCustUUID(modelCustomizationUUID);
302         }
303
304         public AAIResultWrapper getAAIResourceDepthOne(AAIResourceUri aaiResourceUri) {
305                 AAIResourceUri clonedUri = aaiResourceUri.clone();
306                 return this.injectionHelper.getAaiClient().get(clonedUri.depth(Depth.ONE));
307         }
308         
309         public AAIResultWrapper getAAIResourceDepthTwo(AAIResourceUri aaiResourceUri) {
310                 AAIResourceUri clonedUri = aaiResourceUri.clone();
311                 return this.injectionHelper.getAaiClient().get(clonedUri.depth(Depth.TWO));
312         }
313
314         public Configuration getAAIConfiguration(String configurationId) {
315                 return this.injectionHelper.getAaiClient().get(Configuration.class,
316                                 AAIUriFactory.createResourceUri(AAIObjectType.CONFIGURATION, configurationId).depth(Depth.ONE))
317                                 .orElseGet(() -> {
318                                         logger.debug("No Configuration matched by id");
319                                         return null;
320                                 });
321         }
322         
323         public GenericVnf getAAIGenericVnf(String vnfId) {
324                 
325                 return this.injectionHelper.getAaiClient().get(GenericVnf.class,
326                                 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).depth(Depth.ONE))
327                                 .orElseGet(() -> {
328                                         logger.debug("No Generic Vnf matched by id");
329                                         return null;
330                                 });
331         }
332         
333         public VolumeGroup getAAIVolumeGroup(String cloudOwnerId, String cloudRegionId, String volumeGroupId) {
334                 return this.injectionHelper.getAaiClient().get(VolumeGroup.class,
335                                 AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP, cloudOwnerId, cloudRegionId, volumeGroupId).depth(Depth.ONE))
336                                 .orElseGet(() -> {
337                                         logger.debug("No Generic Vnf matched by id");
338                                         return null;
339                                 });
340         }
341
342         public VfModule getAAIVfModule(String vnfId, String vfModuleId) {
343                 return this.injectionHelper.getAaiClient().get(VfModule.class,
344                                 AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId).depth(Depth.ONE))
345                                 .orElseGet(() -> {
346                                         logger.debug("No Generic Vnf matched by id");
347                                         return null;
348                                 });
349         }
350         
351         public L3Network getAAIL3Network(String networkId) {
352
353                 return this.injectionHelper.getAaiClient().get(L3Network.class,
354                                 AAIUriFactory.createResourceUri(AAIObjectType.L3_NETWORK, networkId).depth(Depth.ONE))
355                                 .orElseGet(() -> {
356                                         logger.debug("No Generic Vnf matched by id");
357                                         return null;
358                                 });
359                 
360         }
361         
362         public Optional<ServiceInstance> getRelatedServiceInstanceFromInstanceGroup(String instanceGroupId) throws Exception {
363                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.INSTANCE_GROUP, instanceGroupId);
364                 uri.relatedTo(AAIObjectPlurals.SERVICE_INSTANCE);
365                 Optional<ServiceInstances> serviceInstances = injectionHelper.getAaiClient().get(ServiceInstances.class, uri);
366                 ServiceInstance serviceInstance = null;
367                 if (!serviceInstances.isPresent()) {
368                         logger.debug("No ServiceInstances were found");
369                         return Optional.empty();
370                 } else {
371                         if (serviceInstances.get().getServiceInstance().isEmpty()) {
372                                 throw new NoServiceInstanceFoundException("No ServiceInstances Returned");
373                         } else if (serviceInstances.get().getServiceInstance().size() > 1) {
374                                 throw new MultipleObjectsFoundException("Multiple ServiceInstances Returned");
375                         } else {
376                                 serviceInstance = serviceInstances.get().getServiceInstance().get(0);
377                         }
378                         return Optional.of(serviceInstance);
379                 }
380         }
381         
382         public Optional<L3Network> getRelatedNetworkByNameFromServiceInstance(String serviceInstanceId, String networkName) throws Exception{
383                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
384                 uri.relatedTo(AAIObjectPlurals.L3_NETWORK).queryParam("network-name", networkName);
385                 Optional<L3Networks> networks = injectionHelper.getAaiClient().get(L3Networks.class, uri);
386                 L3Network network = null;
387                 if (!networks.isPresent()) {
388                         logger.debug("No Networks matched by name");
389                         return Optional.empty();
390                 } else {
391                         if (networks.get().getL3Network().size() > 1) {
392                                 throw new Exception("Multiple Networks Returned");
393                         } else {
394                                 network = networks.get().getL3Network().get(0);
395                         }
396                         return Optional.of(network);
397                 }
398         }
399
400         public Optional<GenericVnf> getRelatedVnfByNameFromServiceInstance(String serviceInstanceId, String vnfName) throws Exception {
401                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
402                 uri.relatedTo(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName);
403                 Optional<GenericVnfs> vnfs = injectionHelper.getAaiClient().get(GenericVnfs.class, uri);
404                 GenericVnf vnf = null;
405                 if (!vnfs.isPresent()) {
406                         logger.debug("No Vnfs matched by name");
407                         return Optional.empty();
408                 } else {
409                          if (vnfs.get().getGenericVnf().size() > 1) {
410                                 throw new Exception("Multiple Vnfs Returned");
411                         } else {
412                                 vnf = vnfs.get().getGenericVnf().get(0);
413                         }
414                         return Optional.of(vnf);
415                 }
416         }
417
418         public Optional<VolumeGroup> getRelatedVolumeGroupByNameFromVnf(String vnfId, String volumeGroupName) throws Exception{
419                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId);
420                 uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-name", volumeGroupName);
421                 Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
422                 VolumeGroup volumeGroup = null;
423                 if (!volumeGroups.isPresent()) {
424                         logger.debug("No VolumeGroups matched by name");
425                         return Optional.empty();
426                 } else {
427                         if (volumeGroups.get().getVolumeGroup().size() > 1) {
428                                 throw new Exception("Multiple VolumeGroups Returned");
429                         } else {
430                                 volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
431                         }
432                         return Optional.of(volumeGroup);
433                 }
434         }
435
436         public Optional<VolumeGroup> getRelatedVolumeGroupByNameFromVfModule(String vnfId, String vfModuleId, String volumeGroupName) throws Exception {
437                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId);
438                 uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-name", volumeGroupName);
439                 Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
440                 VolumeGroup volumeGroup = null;
441                 if (!volumeGroups.isPresent()) {
442                         logger.debug("No VolumeGroups matched by name");
443                         return Optional.empty();
444                 } else {
445                         if (volumeGroups.get().getVolumeGroup().size() > 1) {
446                                 throw new Exception("Multiple VolumeGroups Returned");
447                         } else {
448                                 volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
449                         }
450                         return Optional.of(volumeGroup);
451                 }
452         }
453 }