4b85538428aae84fa96b63c182093a30a990f5b5
[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  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.servicedecomposition.tasks;
22
23 import java.io.IOException;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Optional;
27
28 import org.onap.aai.domain.yang.CloudRegion;
29 import org.onap.aai.domain.yang.Configuration;
30 import org.onap.aai.domain.yang.GenericVnf;
31 import org.onap.aai.domain.yang.GenericVnfs;
32 import org.onap.aai.domain.yang.InstanceGroup;
33 import org.onap.aai.domain.yang.L3Network;
34 import org.onap.aai.domain.yang.L3Networks;
35 import org.onap.aai.domain.yang.ServiceInstance;
36 import org.onap.aai.domain.yang.ServiceInstances;
37 import org.onap.aai.domain.yang.ServiceSubscription;
38 import org.onap.aai.domain.yang.VfModule;
39 import org.onap.aai.domain.yang.VolumeGroup;
40 import org.onap.aai.domain.yang.VolumeGroups;
41 import org.onap.so.bpmn.common.InjectionHelper;
42 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
43 import org.onap.so.client.aai.AAIObjectPlurals;
44 import org.onap.so.client.aai.AAIObjectType;
45 import org.onap.so.client.aai.entities.AAIResultWrapper;
46 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
47 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
48 import org.onap.so.client.db.request.RequestsDbClient;
49 import org.onap.so.client.graphinventory.entities.uri.Depth;
50 import org.onap.so.db.catalog.beans.CollectionNetworkResourceCustomization;
51 import org.onap.so.db.catalog.beans.CollectionResourceInstanceGroupCustomization;
52 import org.onap.so.db.catalog.beans.NetworkCollectionResourceCustomization;
53 import org.onap.so.db.catalog.beans.Service;
54 import org.onap.so.db.catalog.beans.VnfcInstanceGroupCustomization;
55 import org.onap.so.db.catalog.client.CatalogDbClient;
56 import org.onap.so.db.request.beans.InfraActiveRequests;
57 import org.onap.so.logger.MsoLogger;
58 import org.onap.so.serviceinstancebeans.CloudConfiguration;
59 import org.onap.so.serviceinstancebeans.RequestDetails;
60 import org.springframework.beans.factory.annotation.Autowired;
61 import org.springframework.stereotype.Component;
62
63 import com.fasterxml.jackson.databind.DeserializationFeature;
64 import com.fasterxml.jackson.databind.ObjectMapper;
65 import com.fasterxml.jackson.databind.SerializationFeature;
66
67 @Component("BBInputSetupUtils")
68 public class BBInputSetupUtils {
69
70         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, BBInputSetupUtils.class);
71         private ObjectMapper objectMapper = new ObjectMapper();
72         private static final String REQUEST_ERROR = "Could not find request.";
73
74         @Autowired
75         protected CatalogDbClient catalogDbClient;
76
77         @Autowired
78         protected RequestsDbClient requestsDbClient;
79
80         @Autowired
81         protected InjectionHelper injectionHelper;
82
83         public void updateInfraActiveRequestVnfId(InfraActiveRequests request, String vnfId) {
84                 if(request != null) {
85                         request.setVnfId(vnfId);
86                         this.requestsDbClient.updateInfraActiveRequests(request);
87                 } else {
88                         msoLogger.debug(REQUEST_ERROR);
89                 }
90         }
91
92         public void updateInfraActiveRequestVfModuleId(InfraActiveRequests request, String vfModuleId) {
93                 if(request != null) {
94                         request.setVfModuleId(vfModuleId);
95                         this.requestsDbClient.updateInfraActiveRequests(request);
96                 } else {
97                         msoLogger.debug(REQUEST_ERROR);
98                 }
99         }
100
101         public void updateInfraActiveRequestVolumeGroupId(InfraActiveRequests request, String volumeGroupId) {
102                 if(request != null) {
103                         request.setVolumeGroupId(volumeGroupId);
104                         this.requestsDbClient.updateInfraActiveRequests(request);
105                 } else {
106                         msoLogger.debug(REQUEST_ERROR);
107                 }
108         }
109
110         public void updateInfraActiveRequestNetworkId(InfraActiveRequests request, String networkId) {
111                 if(request != null) {
112                         request.setNetworkId(networkId);
113                         this.requestsDbClient.updateInfraActiveRequests(request);
114                 } else {
115                         msoLogger.debug(REQUEST_ERROR);
116                 }
117         }
118         
119         public Service getCatalogServiceByModelUUID(String modelUUID) {
120                 return catalogDbClient.getServiceByID(modelUUID);
121         }
122         
123         public Service getCatalogServiceByModelVersionAndModelInvariantUUID(String modelVersion, String modelInvariantUUID) {
124                 return catalogDbClient.getServiceByModelVersionAndModelInvariantUUID(modelVersion, modelInvariantUUID);
125         }
126
127         public CollectionNetworkResourceCustomization getCatalogCollectionNetworkResourceCustByID(String key) {
128                 return catalogDbClient.getCollectionNetworkResourceCustomizationByID(key);
129         }
130
131         public NetworkCollectionResourceCustomization getCatalogNetworkCollectionResourceCustByID(
132                         String collectionCustomizationId) {
133                 return catalogDbClient.getNetworkCollectionResourceCustomizationByID(collectionCustomizationId);
134         }
135
136         public List<VnfcInstanceGroupCustomization> getVnfcInstanceGroups(String modelCustomizationUUID) {
137                 return catalogDbClient.getVnfcInstanceGroupsByVnfResourceCust(modelCustomizationUUID);
138         }
139
140         public Map<String, String> getURIKeysFromServiceInstance(String serviceInstanceId) {
141                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
142                 return uri.getURIKeys();
143         }
144
145         protected RequestDetails getRequestDetails(String requestId) throws IOException {
146                 if (requestId != null && !requestId.isEmpty()) {
147                         InfraActiveRequests activeRequest = this.getInfraActiveRequest(requestId);
148                         String requestBody = activeRequest.getRequestBody().replaceAll("\\\\", "");
149                         objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
150                         objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
151                         return objectMapper.readValue(requestBody, RequestDetails.class);
152                 } else {
153                         return null;
154                 }
155         }
156
157         protected InfraActiveRequests getInfraActiveRequest(String requestId) {
158                 if (requestId != null && !requestId.isEmpty()) {
159                         return requestsDbClient.getInfraActiveRequestbyRequestId(requestId);
160                 } else {
161                         return null;
162                 }
163         }
164
165         protected CloudRegion getCloudRegion(CloudConfiguration cloudConfiguration, String cloudOwner) {
166                 if (cloudConfiguration != null) {
167                         String cloudRegionId = cloudConfiguration.getLcpCloudRegionId();
168                         if (cloudRegionId != null && !cloudRegionId.isEmpty()) {
169                                 return injectionHelper.getAaiClient().get(CloudRegion.class,
170                                                 AAIUriFactory.createResourceUri(AAIObjectType.CLOUD_REGION, cloudOwner,
171                                                                 cloudConfiguration.getLcpCloudRegionId())).orElse(null);
172                         
173                         } else {
174                                 return null;
175                         }
176                 } else {
177                         return null;
178                 }
179         }
180
181         protected InstanceGroup getAAIInstanceGroup(String instanceGroupId) {
182                 return injectionHelper.getAaiClient().get(InstanceGroup.class,
183                         AAIUriFactory.createResourceUri(AAIObjectType.INSTANCE_GROUP, instanceGroupId))
184                         .orElse(null);
185         }
186
187         public org.onap.aai.domain.yang.Customer getAAICustomer(String globalSubscriberId) {
188                 return injectionHelper.getAaiClient().get(org.onap.aai.domain.yang.Customer.class,
189                         AAIUriFactory.createResourceUri(AAIObjectType.CUSTOMER, globalSubscriberId))
190                         .orElse(null);
191         }
192
193         public ServiceSubscription getAAIServiceSubscription(String globalSubscriberId, String subscriptionServiceType) {
194         
195                 if(globalSubscriberId == null || globalSubscriberId.equals("") || subscriptionServiceType == null || subscriptionServiceType.equals("")) {
196                         return null;
197                 } else {
198                         return injectionHelper.getAaiClient().get(ServiceSubscription.class, AAIUriFactory.createResourceUri(
199                                         AAIObjectType.SERVICE_SUBSCRIPTION, globalSubscriberId, subscriptionServiceType))
200                                         .orElse(null);
201                 }
202                 
203         }
204
205         public ServiceInstance getAAIServiceInstanceById(String serviceInstanceId) {
206                 return injectionHelper.getAaiClient().get(ServiceInstance.class,
207                                 AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId).depth(Depth.TWO))
208                                 .orElse(null);
209         }
210
211         protected ServiceInstance getAAIServiceInstanceByIdAndCustomer(String globalCustomerId, String serviceType,
212                         String serviceInstanceId) {
213                 return injectionHelper.getAaiClient().get(ServiceInstance.class, AAIUriFactory.createResourceUri(
214                                 AAIObjectType.SERVICE_INSTANCE, globalCustomerId, serviceType, serviceInstanceId).depth(Depth.TWO))
215                                 .orElse(null);
216         }
217
218         protected org.onap.aai.domain.yang.ServiceInstances getAAIServiceInstancesByName(String serviceInstanceName,
219                         Customer customer) {
220                 
221                 return injectionHelper.getAaiClient().get(org.onap.aai.domain.yang.ServiceInstances.class,
222                                 AAIUriFactory
223                                                 .createResourceUri(AAIObjectPlurals.SERVICE_INSTANCE, customer.getGlobalCustomerId(),
224                                                                 customer.getServiceSubscription().getServiceType())
225                                                 .queryParam("service-instance-name", serviceInstanceName).depth(Depth.TWO))
226                                 .orElseGet(() -> {
227                                         msoLogger.debug("No Service Instance matched by name");
228                                         return null;
229                                 });
230         }
231
232         public org.onap.aai.domain.yang.ServiceInstance getAAIServiceInstanceByName(String serviceInstanceName,
233                         Customer customer) throws Exception {
234                 org.onap.aai.domain.yang.ServiceInstance aaiServiceInstance = null;
235                 org.onap.aai.domain.yang.ServiceInstances aaiServiceInstances = null;
236                 aaiServiceInstances = getAAIServiceInstancesByName(serviceInstanceName, customer);
237
238                 if (aaiServiceInstances == null) {
239                         return null;
240                 } else if (aaiServiceInstances.getServiceInstance().size() > 1) {
241                         throw new Exception("Multiple Service Instances Returned");
242                 } else {
243                         aaiServiceInstance = aaiServiceInstances.getServiceInstance().get(0);
244                 }
245                 return aaiServiceInstance;
246         }
247
248         protected ServiceInstances getAAIServiceInstancesByName(String globalCustomerId, String serviceType,
249                         String serviceInstanceName) {
250                 
251                 return injectionHelper.getAaiClient().get(ServiceInstances.class,
252                                 AAIUriFactory.createResourceUri(AAIObjectPlurals.SERVICE_INSTANCE, globalCustomerId, serviceType)
253                                                 .queryParam("service-instance-name", serviceInstanceName).depth(Depth.TWO))
254                                 .orElseGet(() -> {
255                                         msoLogger.debug("No Service Instance matched by name");
256                                         return null;
257                                 });
258         }
259
260         public Optional<ServiceInstance> getAAIServiceInstanceByName(String globalCustomerId, String serviceType,
261                         String serviceInstanceName) throws Exception {
262                 ServiceInstance aaiServiceInstance = null;
263                 ServiceInstances aaiServiceInstances = null;
264                 aaiServiceInstances = getAAIServiceInstancesByName(globalCustomerId, serviceType, serviceInstanceName);
265
266                 if (aaiServiceInstances == null) {
267                         return Optional.empty();
268                 } else if (aaiServiceInstances.getServiceInstance().size() > 1) {
269                         throw new Exception("Multiple Service Instances Returned");
270                 } else {
271                         aaiServiceInstance = aaiServiceInstances.getServiceInstance().get(0);
272                 }
273                 return Optional.of(aaiServiceInstance);
274         }
275
276         public org.onap.so.db.catalog.beans.InstanceGroup getCatalogInstanceGroup(String modelUUID) {
277                 return this.catalogDbClient.getInstanceGroupByModelUUID(modelUUID);
278         }
279
280         public List<CollectionResourceInstanceGroupCustomization> getCollectionResourceInstanceGroupCustomization(
281                         String modelCustomizationUUID) {
282                 return this.catalogDbClient
283                                 .getCollectionResourceInstanceGroupCustomizationByModelCustUUID(modelCustomizationUUID);
284         }
285
286         public AAIResultWrapper getAAIResourceDepthOne(AAIResourceUri aaiResourceUri) {
287                 AAIResourceUri clonedUri = aaiResourceUri.clone();
288                 return this.injectionHelper.getAaiClient().get(clonedUri.depth(Depth.ONE));
289         }
290         
291         public AAIResultWrapper getAAIResourceDepthTwo(AAIResourceUri aaiResourceUri) {
292                 AAIResourceUri clonedUri = aaiResourceUri.clone();
293                 return this.injectionHelper.getAaiClient().get(clonedUri.depth(Depth.TWO));
294         }
295
296         public Configuration getAAIConfiguration(String configurationId) {
297                 return this.injectionHelper.getAaiClient().get(Configuration.class,
298                                 AAIUriFactory.createResourceUri(AAIObjectType.CONFIGURATION, configurationId).depth(Depth.ONE))
299                                 .orElseGet(() -> {
300                                         msoLogger.debug("No Configuration matched by id");
301                                         return null;
302                                 });
303         }
304         
305         public GenericVnf getAAIGenericVnf(String vnfId) {
306                 
307                 return this.injectionHelper.getAaiClient().get(GenericVnf.class,
308                                 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).depth(Depth.ONE))
309                                 .orElseGet(() -> {
310                                         msoLogger.debug("No Generic Vnf matched by id");
311                                         return null;
312                                 });
313         }
314         
315         public VolumeGroup getAAIVolumeGroup(String cloudOwnerId, String cloudRegionId, String volumeGroupId) {
316                 return this.injectionHelper.getAaiClient().get(VolumeGroup.class,
317                                 AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP, cloudOwnerId, cloudRegionId, volumeGroupId).depth(Depth.ONE))
318                                 .orElseGet(() -> {
319                                         msoLogger.debug("No Generic Vnf matched by id");
320                                         return null;
321                                 });
322         }
323
324         public VfModule getAAIVfModule(String vnfId, String vfModuleId) {
325                 return this.injectionHelper.getAaiClient().get(VfModule.class,
326                                 AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId).depth(Depth.ONE))
327                                 .orElseGet(() -> {
328                                         msoLogger.debug("No Generic Vnf matched by id");
329                                         return null;
330                                 });
331         }
332         
333         public L3Network getAAIL3Network(String networkId) {
334
335                 return this.injectionHelper.getAaiClient().get(L3Network.class,
336                                 AAIUriFactory.createResourceUri(AAIObjectType.L3_NETWORK, networkId).depth(Depth.ONE))
337                                 .orElseGet(() -> {
338                                         msoLogger.debug("No Generic Vnf matched by id");
339                                         return null;
340                                 });
341                 
342         }
343         
344         public Optional<L3Network> getRelatedNetworkByNameFromServiceInstance(String serviceInstanceId, String networkName) throws Exception{
345                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
346                 uri.relatedTo(AAIObjectPlurals.L3_NETWORK).queryParam("network-name", networkName);
347                 Optional<L3Networks> networks = injectionHelper.getAaiClient().get(L3Networks.class, uri);
348                 L3Network network = null;
349                 if (!networks.isPresent()) {
350                         msoLogger.debug("No Networks matched by name");
351                         return Optional.empty();
352                 } else {
353                         if (networks.get().getL3Network().size() > 1) {
354                                 throw new Exception("Multiple Networks Returned");
355                         } else {
356                                 network = networks.get().getL3Network().get(0);
357                         }
358                         return Optional.of(network);
359                 }
360         }
361
362         public Optional<GenericVnf> getRelatedVnfByNameFromServiceInstance(String serviceInstanceId, String vnfName) throws Exception {
363                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
364                 uri.relatedTo(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName);
365                 Optional<GenericVnfs> vnfs = injectionHelper.getAaiClient().get(GenericVnfs.class, uri);
366                 GenericVnf vnf = null;
367                 if (!vnfs.isPresent()) {
368                         msoLogger.debug("No Vnfs matched by name");
369                         return Optional.empty();
370                 } else {
371                          if (vnfs.get().getGenericVnf().size() > 1) {
372                                 throw new Exception("Multiple Vnfs Returned");
373                         } else {
374                                 vnf = vnfs.get().getGenericVnf().get(0);
375                         }
376                         return Optional.of(vnf);
377                 }
378         }
379
380         public Optional<VolumeGroup> getRelatedVolumeGroupByNameFromVnf(String vnfId, String volumeGroupName) throws Exception{
381                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId);
382                 uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-name", volumeGroupName);
383                 Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
384                 VolumeGroup volumeGroup = null;
385                 if (!volumeGroups.isPresent()) {
386                         msoLogger.debug("No VolumeGroups matched by name");
387                         return Optional.empty();
388                 } else {
389                         if (volumeGroups.get().getVolumeGroup().size() > 1) {
390                                 throw new Exception("Multiple VolumeGroups Returned");
391                         } else {
392                                 volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
393                         }
394                         return Optional.of(volumeGroup);
395                 }
396         }
397
398         public Optional<VolumeGroup> getRelatedVolumeGroupByNameFromVfModule(String vnfId, String vfModuleId, String volumeGroupName) throws Exception {
399                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId);
400                 uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-name", volumeGroupName);
401                 Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
402                 VolumeGroup volumeGroup = null;
403                 if (!volumeGroups.isPresent()) {
404                         msoLogger.debug("No VolumeGroups matched by name");
405                         return Optional.empty();
406                 } else {
407                         if (volumeGroups.get().getVolumeGroup().size() > 1) {
408                                 throw new Exception("Multiple VolumeGroups Returned");
409                         } else {
410                                 volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
411                         }
412                         return Optional.of(volumeGroup);
413                 }
414         }
415 }