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