Dynamic Cloud Owner Support
[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) {
167                 if (cloudConfiguration != null) {
168                         String cloudRegionId = cloudConfiguration.getLcpCloudRegionId();
169                         String cloudOwner = cloudConfiguration.getCloudOwner();
170                         if (cloudRegionId != null && cloudOwner != null && !cloudRegionId.isEmpty() && !cloudOwner.isEmpty()) {
171                                 return injectionHelper.getAaiClient().get(CloudRegion.class,
172                                                 AAIUriFactory.createResourceUri(AAIObjectType.CLOUD_REGION, cloudOwner,
173                                                                 cloudRegionId)).orElse(null);
174                         
175                         } else {
176                                 return null;
177                         }
178                 } else {
179                         return null;
180                 }
181         }
182
183         protected InstanceGroup getAAIInstanceGroup(String instanceGroupId) {
184                 return injectionHelper.getAaiClient().get(InstanceGroup.class,
185                         AAIUriFactory.createResourceUri(AAIObjectType.INSTANCE_GROUP, instanceGroupId))
186                         .orElse(null);
187         }
188
189         public org.onap.aai.domain.yang.Customer getAAICustomer(String globalSubscriberId) {
190                 return injectionHelper.getAaiClient().get(org.onap.aai.domain.yang.Customer.class,
191                         AAIUriFactory.createResourceUri(AAIObjectType.CUSTOMER, globalSubscriberId))
192                         .orElse(null);
193         }
194
195         public ServiceSubscription getAAIServiceSubscription(String globalSubscriberId, String subscriptionServiceType) {
196         
197                 if(globalSubscriberId == null || globalSubscriberId.equals("") || subscriptionServiceType == null || subscriptionServiceType.equals("")) {
198                         return null;
199                 } else {
200                         return injectionHelper.getAaiClient().get(ServiceSubscription.class, AAIUriFactory.createResourceUri(
201                                         AAIObjectType.SERVICE_SUBSCRIPTION, globalSubscriberId, subscriptionServiceType))
202                                         .orElse(null);
203                 }
204                 
205         }
206
207         public ServiceInstance getAAIServiceInstanceById(String serviceInstanceId) {
208                 return injectionHelper.getAaiClient().get(ServiceInstance.class,
209                                 AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId).depth(Depth.TWO))
210                                 .orElse(null);
211         }
212
213         protected ServiceInstance getAAIServiceInstanceByIdAndCustomer(String globalCustomerId, String serviceType,
214                         String serviceInstanceId) {
215                 return injectionHelper.getAaiClient().get(ServiceInstance.class, AAIUriFactory.createResourceUri(
216                                 AAIObjectType.SERVICE_INSTANCE, globalCustomerId, serviceType, serviceInstanceId).depth(Depth.TWO))
217                                 .orElse(null);
218         }
219
220         protected org.onap.aai.domain.yang.ServiceInstances getAAIServiceInstancesByName(String serviceInstanceName,
221                         Customer customer) {
222                 
223                 return injectionHelper.getAaiClient().get(org.onap.aai.domain.yang.ServiceInstances.class,
224                                 AAIUriFactory
225                                                 .createResourceUri(AAIObjectPlurals.SERVICE_INSTANCE, customer.getGlobalCustomerId(),
226                                                                 customer.getServiceSubscription().getServiceType())
227                                                 .queryParam("service-instance-name", serviceInstanceName).depth(Depth.TWO))
228                                 .orElseGet(() -> {
229                                         msoLogger.debug("No Service Instance matched by name");
230                                         return null;
231                                 });
232         }
233
234         public org.onap.aai.domain.yang.ServiceInstance getAAIServiceInstanceByName(String serviceInstanceName,
235                         Customer customer) throws Exception {
236                 org.onap.aai.domain.yang.ServiceInstance aaiServiceInstance = null;
237                 org.onap.aai.domain.yang.ServiceInstances aaiServiceInstances = null;
238                 aaiServiceInstances = getAAIServiceInstancesByName(serviceInstanceName, customer);
239
240                 if (aaiServiceInstances == null) {
241                         return null;
242                 } else if (aaiServiceInstances.getServiceInstance().size() > 1) {
243                         throw new Exception("Multiple Service Instances Returned");
244                 } else {
245                         aaiServiceInstance = aaiServiceInstances.getServiceInstance().get(0);
246                 }
247                 return aaiServiceInstance;
248         }
249
250         protected ServiceInstances getAAIServiceInstancesByName(String globalCustomerId, String serviceType,
251                         String serviceInstanceName) {
252                 
253                 return injectionHelper.getAaiClient().get(ServiceInstances.class,
254                                 AAIUriFactory.createResourceUri(AAIObjectPlurals.SERVICE_INSTANCE, globalCustomerId, serviceType)
255                                                 .queryParam("service-instance-name", serviceInstanceName).depth(Depth.TWO))
256                                 .orElseGet(() -> {
257                                         msoLogger.debug("No Service Instance matched by name");
258                                         return null;
259                                 });
260         }
261
262         public Optional<ServiceInstance> getAAIServiceInstanceByName(String globalCustomerId, String serviceType,
263                         String serviceInstanceName) throws Exception {
264                 ServiceInstance aaiServiceInstance = null;
265                 ServiceInstances aaiServiceInstances = null;
266                 aaiServiceInstances = getAAIServiceInstancesByName(globalCustomerId, serviceType, serviceInstanceName);
267
268                 if (aaiServiceInstances == null) {
269                         return Optional.empty();
270                 } else if (aaiServiceInstances.getServiceInstance().size() > 1) {
271                         throw new Exception("Multiple Service Instances Returned");
272                 } else {
273                         aaiServiceInstance = aaiServiceInstances.getServiceInstance().get(0);
274                 }
275                 return Optional.of(aaiServiceInstance);
276         }
277
278         public org.onap.so.db.catalog.beans.InstanceGroup getCatalogInstanceGroup(String modelUUID) {
279                 return this.catalogDbClient.getInstanceGroupByModelUUID(modelUUID);
280         }
281
282         public List<CollectionResourceInstanceGroupCustomization> getCollectionResourceInstanceGroupCustomization(
283                         String modelCustomizationUUID) {
284                 return this.catalogDbClient
285                                 .getCollectionResourceInstanceGroupCustomizationByModelCustUUID(modelCustomizationUUID);
286         }
287
288         public AAIResultWrapper getAAIResourceDepthOne(AAIResourceUri aaiResourceUri) {
289                 AAIResourceUri clonedUri = aaiResourceUri.clone();
290                 return this.injectionHelper.getAaiClient().get(clonedUri.depth(Depth.ONE));
291         }
292         
293         public AAIResultWrapper getAAIResourceDepthTwo(AAIResourceUri aaiResourceUri) {
294                 AAIResourceUri clonedUri = aaiResourceUri.clone();
295                 return this.injectionHelper.getAaiClient().get(clonedUri.depth(Depth.TWO));
296         }
297
298         public Configuration getAAIConfiguration(String configurationId) {
299                 return this.injectionHelper.getAaiClient().get(Configuration.class,
300                                 AAIUriFactory.createResourceUri(AAIObjectType.CONFIGURATION, configurationId).depth(Depth.ONE))
301                                 .orElseGet(() -> {
302                                         msoLogger.debug("No Configuration matched by id");
303                                         return null;
304                                 });
305         }
306         
307         public GenericVnf getAAIGenericVnf(String vnfId) {
308                 
309                 return this.injectionHelper.getAaiClient().get(GenericVnf.class,
310                                 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).depth(Depth.ONE))
311                                 .orElseGet(() -> {
312                                         msoLogger.debug("No Generic Vnf matched by id");
313                                         return null;
314                                 });
315         }
316         
317         public VolumeGroup getAAIVolumeGroup(String cloudOwnerId, String cloudRegionId, String volumeGroupId) {
318                 return this.injectionHelper.getAaiClient().get(VolumeGroup.class,
319                                 AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP, cloudOwnerId, cloudRegionId, volumeGroupId).depth(Depth.ONE))
320                                 .orElseGet(() -> {
321                                         msoLogger.debug("No Generic Vnf matched by id");
322                                         return null;
323                                 });
324         }
325
326         public VfModule getAAIVfModule(String vnfId, String vfModuleId) {
327                 return this.injectionHelper.getAaiClient().get(VfModule.class,
328                                 AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId).depth(Depth.ONE))
329                                 .orElseGet(() -> {
330                                         msoLogger.debug("No Generic Vnf matched by id");
331                                         return null;
332                                 });
333         }
334         
335         public L3Network getAAIL3Network(String networkId) {
336
337                 return this.injectionHelper.getAaiClient().get(L3Network.class,
338                                 AAIUriFactory.createResourceUri(AAIObjectType.L3_NETWORK, networkId).depth(Depth.ONE))
339                                 .orElseGet(() -> {
340                                         msoLogger.debug("No Generic Vnf matched by id");
341                                         return null;
342                                 });
343                 
344         }
345         
346         public Optional<L3Network> getRelatedNetworkByNameFromServiceInstance(String serviceInstanceId, String networkName) throws Exception{
347                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
348                 uri.relatedTo(AAIObjectPlurals.L3_NETWORK).queryParam("network-name", networkName);
349                 Optional<L3Networks> networks = injectionHelper.getAaiClient().get(L3Networks.class, uri);
350                 L3Network network = null;
351                 if (!networks.isPresent()) {
352                         msoLogger.debug("No Networks matched by name");
353                         return Optional.empty();
354                 } else {
355                         if (networks.get().getL3Network().size() > 1) {
356                                 throw new Exception("Multiple Networks Returned");
357                         } else {
358                                 network = networks.get().getL3Network().get(0);
359                         }
360                         return Optional.of(network);
361                 }
362         }
363
364         public Optional<GenericVnf> getRelatedVnfByNameFromServiceInstance(String serviceInstanceId, String vnfName) throws Exception {
365                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
366                 uri.relatedTo(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName);
367                 Optional<GenericVnfs> vnfs = injectionHelper.getAaiClient().get(GenericVnfs.class, uri);
368                 GenericVnf vnf = null;
369                 if (!vnfs.isPresent()) {
370                         msoLogger.debug("No Vnfs matched by name");
371                         return Optional.empty();
372                 } else {
373                          if (vnfs.get().getGenericVnf().size() > 1) {
374                                 throw new Exception("Multiple Vnfs Returned");
375                         } else {
376                                 vnf = vnfs.get().getGenericVnf().get(0);
377                         }
378                         return Optional.of(vnf);
379                 }
380         }
381
382         public Optional<VolumeGroup> getRelatedVolumeGroupByNameFromVnf(String vnfId, String volumeGroupName) throws Exception{
383                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId);
384                 uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-name", volumeGroupName);
385                 Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
386                 VolumeGroup volumeGroup = null;
387                 if (!volumeGroups.isPresent()) {
388                         msoLogger.debug("No VolumeGroups matched by name");
389                         return Optional.empty();
390                 } else {
391                         if (volumeGroups.get().getVolumeGroup().size() > 1) {
392                                 throw new Exception("Multiple VolumeGroups Returned");
393                         } else {
394                                 volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
395                         }
396                         return Optional.of(volumeGroup);
397                 }
398         }
399
400         public Optional<VolumeGroup> getRelatedVolumeGroupByNameFromVfModule(String vnfId, String vfModuleId, String volumeGroupName) throws Exception {
401                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId);
402                 uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-name", volumeGroupName);
403                 Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
404                 VolumeGroup volumeGroup = null;
405                 if (!volumeGroups.isPresent()) {
406                         msoLogger.debug("No VolumeGroups matched by name");
407                         return Optional.empty();
408                 } else {
409                         if (volumeGroups.get().getVolumeGroup().size() > 1) {
410                                 throw new Exception("Multiple VolumeGroups Returned");
411                         } else {
412                                 volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
413                         }
414                         return Optional.of(volumeGroup);
415                 }
416         }
417 }