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