e534142471835e517cae62deed52b5e90042fffe
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / aai / tasks / AAICreateTasks.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.infrastructure.aai.tasks;
22
23 import org.camunda.bpm.engine.delegate.BpmnError;
24 import org.onap.so.bpmn.common.BuildingBlockExecution;
25 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
26 import org.onap.so.bpmn.servicedecomposition.bbobjects.Collection;
27 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
28 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
29 import org.onap.so.bpmn.servicedecomposition.bbobjects.InstanceGroup;
30 import org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network;
31 import org.onap.so.bpmn.servicedecomposition.bbobjects.LineOfBusiness;
32 import org.onap.so.bpmn.servicedecomposition.bbobjects.OwningEntity;
33 import org.onap.so.bpmn.servicedecomposition.bbobjects.Platform;
34 import org.onap.so.bpmn.servicedecomposition.bbobjects.Project;
35 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
36 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
37 import org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup;
38 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
39 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
40 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
41 import org.onap.so.client.exception.BBObjectNotFoundException;
42 import org.onap.so.client.exception.ExceptionBuilder;
43 import org.onap.so.client.orchestration.AAINetworkResources;
44 import org.onap.so.client.orchestration.AAIServiceInstanceResources;
45 import org.onap.so.client.orchestration.AAIVfModuleResources;
46 import org.onap.so.client.orchestration.AAIVnfResources;
47 import org.onap.so.client.orchestration.AAIVolumeGroupResources;
48 import org.onap.so.client.orchestration.AAIVpnBindingResources;
49 import org.onap.so.db.catalog.beans.OrchestrationStatus;
50 import org.onap.so.logger.MessageEnum;
51 import org.onap.so.logger.MsoLogger;
52 import org.springframework.beans.factory.annotation.Autowired;
53 import org.springframework.stereotype.Component;
54
55 @Component
56 public class AAICreateTasks {
57
58         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, AAICreateTasks.class);
59         private static final String networkTypeProvider = "PROVIDER";
60         private static String NETWORK_COLLECTION_NAME = "networkCollectionName";
61         @Autowired
62         private AAIServiceInstanceResources aaiSIResources;
63         @Autowired
64         private AAIVnfResources aaiVnfResources;
65         @Autowired
66         private ExceptionBuilder exceptionUtil;
67         @Autowired
68         private ExtractPojosForBB extractPojosForBB;
69         @Autowired
70         private AAIVolumeGroupResources aaiVolumeGroupResources;
71         @Autowired
72         private AAIVfModuleResources aaiVfModuleResources;
73         @Autowired
74         private AAINetworkResources aaiNetworkResources;
75         @Autowired
76         private AAIVpnBindingResources aaiVpnBindingResources;
77
78         public void createServiceInstance(BuildingBlockExecution execution) {
79                 try {
80                         ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
81                         Customer customer = execution.getGeneralBuildingBlock().getCustomer();
82                         aaiSIResources.createServiceInstance(serviceInstance, customer);
83                 } catch (Exception ex) {
84                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
85                 }
86         }
87
88     public void createServiceSubscription(BuildingBlockExecution execution){
89         try{
90             ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution,
91                     ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
92             Customer customer = execution.getGeneralBuildingBlock().getCustomer();
93             if (null == customer) {
94                 String errorMessage = "Exception in creating ServiceSubscription. Customer not present for ServiceInstanceID: "
95                         + serviceInstance.getServiceInstanceId();
96                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, errorMessage, "BPMN", MsoLogger.getServiceName(),
97                         MsoLogger.ErrorCode.UnknownError, errorMessage);
98                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, errorMessage);
99             }
100             aaiSIResources.createServiceSubscription(customer);
101         } catch (BpmnError ex) {
102             throw ex;
103         }
104         catch (Exception ex) {
105             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
106         }
107     }
108
109         public void createProject(BuildingBlockExecution execution) {
110                 try {
111                         ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
112                         Project project = serviceInstance.getProject();
113                         if(project != null) {
114                                 if (project.getProjectName() == null || "".equals(project.getProjectName())) {
115                                         msoLogger.info("ProjectName is null in input. Skipping create project...");
116                                 } else {
117                                         aaiSIResources.createProjectandConnectServiceInstance(project, serviceInstance);
118                                 }
119                         }
120                 } catch (Exception ex) {
121                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
122                 }
123         }
124
125         public void createOwningEntity(BuildingBlockExecution execution) {
126                 try {
127                         ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
128                         OwningEntity owningEntity = serviceInstance.getOwningEntity();
129                         String owningEntityId = owningEntity.getOwningEntityId();
130                         String owningEntityName = owningEntity.getOwningEntityName();
131                         if (owningEntityId == null || "".equals(owningEntityId)) {
132                                 String msg = "Exception in AAICreateOwningEntity. OwningEntityId is null.";
133                                 execution.setVariable("ErrorCreateOEAAI", msg);
134                                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
135                         } else {
136                                 if (aaiSIResources.existsOwningEntity(owningEntity)) {
137                                         aaiSIResources.connectOwningEntityandServiceInstance(owningEntity, serviceInstance);
138                                 } else {
139                                         if (owningEntityName == null || "".equals(owningEntityName)) {
140                                                 String msg = "Exception in AAICreateOwningEntity. Can't create an owningEntity with no owningEntityName.";
141                                                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, msg, "BPMN", MsoLogger.getServiceName(),
142                                                                 MsoLogger.ErrorCode.UnknownError, msg);
143                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
144                                         } else {
145                                                 if(aaiSIResources.existsOwningEntityName(owningEntityName)){
146                                                         String msg = "Exception in AAICreateOwningEntity. Can't create OwningEntity as name already exists in AAI associated with a different owning-entity-id (name must be unique)";
147                                                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, msg, "BPMN", MsoLogger.getServiceName(),
148                                                                         MsoLogger.ErrorCode.UnknownError, msg);
149                                                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
150                                                 }else{
151                                                         aaiSIResources.createOwningEntityandConnectServiceInstance(owningEntity, serviceInstance);
152                                                 }
153                                         }
154                                 }
155                         }
156                 } catch (Exception ex) {
157                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
158                 }
159         }
160
161         public void createVnf(BuildingBlockExecution execution) {
162                 try {
163                         GenericVnf vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
164                         ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
165                         execution.setVariable("callHoming", Boolean.TRUE.equals(vnf.isCallHoming()));
166                         aaiVnfResources.createVnfandConnectServiceInstance(vnf, serviceInstance);
167                 } catch (Exception ex) {
168                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
169                 }
170         }
171         
172         public void createPlatform(BuildingBlockExecution execution) {
173                 try {
174                         GenericVnf vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
175                         Platform platform = vnf.getPlatform();
176                         if(platform != null) {
177                                 if (platform.getPlatformName() == null || "".equals(platform.getPlatformName())) {
178                                         msoLogger.debug("PlatformName is null in input. Skipping create platform...");
179                                 } else {
180                                         aaiVnfResources.createPlatformandConnectVnf(platform,vnf);
181                                 }
182                         }
183                 } catch (Exception ex) {
184                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
185                 }
186
187         }
188         
189         public void createLineOfBusiness(BuildingBlockExecution execution) {
190                 try {
191                         GenericVnf vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
192                         LineOfBusiness lineOfBusiness = vnf.getLineOfBusiness();
193                         if(lineOfBusiness != null) {
194                                 if (lineOfBusiness.getLineOfBusinessName() == null || "".equals(lineOfBusiness.getLineOfBusinessName())) {
195                                         msoLogger.info("lineOfBusiness is null in input. Skipping create lineOfBusiness...");
196                                 } else {
197                                         aaiVnfResources.createLineOfBusinessandConnectVnf(lineOfBusiness,vnf);
198                                 }
199                         }
200                 } catch (Exception ex) {
201                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
202                 }
203         }
204
205         public void createVolumeGroup(BuildingBlockExecution execution) {
206                 try {
207                         GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
208                         
209                         GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
210                         VolumeGroup volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID, execution.getLookupMap().get(ResourceKey.VOLUME_GROUP_ID));
211                         CloudRegion cloudRegion = gBBInput.getCloudRegion();
212                         aaiVolumeGroupResources.createVolumeGroup(volumeGroup, cloudRegion);
213                         aaiVolumeGroupResources.connectVolumeGroupToVnf(genericVnf, volumeGroup, cloudRegion);
214                         aaiVolumeGroupResources.connectVolumeGroupToTenant(volumeGroup,cloudRegion);
215                 } catch (Exception ex) {
216                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
217                 }
218         }
219         
220         public void createVfModule(BuildingBlockExecution execution) {
221                 try {
222                         GenericVnf vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
223                         VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
224                         aaiVfModuleResources.createVfModule(vfModule, vnf);
225                 } catch (Exception ex) {                        
226                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
227                 }       
228         }
229         
230         /**
231          * BPMN access method to establish relationships in AAI
232          * @param execution
233          * @throws Exception
234          */
235         public void connectVfModuleToVolumeGroup(BuildingBlockExecution execution) {
236                 try {
237                         GenericVnf vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
238                         VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
239                         VolumeGroup volumeGroup = null;
240                         try{
241                                 volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID, execution.getLookupMap().get(ResourceKey.VOLUME_GROUP_ID));
242                         } catch (BBObjectNotFoundException e){
243                                 msoLogger.info("VolumeGroup not found. Skipping Connect between VfModule and VolumeGroup");
244                         }
245                         if (volumeGroup != null) {
246                                 aaiVfModuleResources.connectVfModuleToVolumeGroup(vnf, vfModule, volumeGroup, execution.getGeneralBuildingBlock().getCloudRegion());
247                         }
248                 } catch (Exception ex) {
249                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
250                 }       
251         }
252         
253         /**
254          * BPMN access method to execute Create L3Network operation (PUT )in AAI
255          * @param execution
256          * @throws Exception
257          */
258         public void createNetwork(BuildingBlockExecution execution) {
259                 try {
260                         ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
261                         L3Network l3network =  extractPojosForBB.extractByKey(execution, ResourceKey.NETWORK_ID, execution.getLookupMap().get(ResourceKey.NETWORK_ID));
262                         //set default to false. ToBe updated by SDNC
263                         l3network.setIsBoundToVpn(false);
264                         //define is bound to vpn flag as true if NEUTRON_NETWORK_TYPE is PROVIDER
265                         if (l3network.getModelInfoNetwork().getNeutronNetworkType().equalsIgnoreCase(networkTypeProvider))
266                                 l3network.setIsBoundToVpn(true);
267                         //put network shell in AAI
268                         aaiNetworkResources.createNetworkConnectToServiceInstance(l3network, serviceInstance);
269                 } catch (Exception ex) {
270                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
271                 }       
272         }
273         public void createCustomer(BuildingBlockExecution execution) throws Exception {
274                 try {
275                         Customer customer = execution.getGeneralBuildingBlock().getCustomer();
276                         
277                         aaiVpnBindingResources.createCustomer(customer);
278                 } catch(Exception ex) {
279                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
280                 }
281         }
282         
283         /**
284          * BPMN access method to execute NetworkCollection operation (PUT) in AAI
285          * @param execution
286          * @throws Exception
287          */
288         public void createNetworkCollection(BuildingBlockExecution execution) {
289                 try {
290                         ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
291                         Collection networkCollection =  serviceInstance.getCollection();
292                         //pass name generated for NetworkCollection/NetworkCollectionInstanceGroup in previous step of the BB flow
293                         //put shell in AAI
294                         networkCollection.setName(execution.getVariable(NETWORK_COLLECTION_NAME));
295                         aaiNetworkResources.createNetworkCollection(networkCollection);
296                 } catch (Exception ex) {
297                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
298                 }       
299         }
300         
301         /**
302          * BPMN access method to execute NetworkCollectionInstanceGroup operation (PUT) in AAI
303          * @param execution
304          * @throws Exception
305          */
306         public void createNetworkCollectionInstanceGroup(BuildingBlockExecution execution) {
307                 try {
308                         ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
309                         InstanceGroup instanceGroup =  serviceInstance.getCollection().getInstanceGroup();
310                         //set name generated for NetworkCollection/NetworkCollectionInstanceGroup in previous step of the BB flow
311                         instanceGroup.setInstanceGroupName(execution.getVariable(NETWORK_COLLECTION_NAME));
312                         //put shell in AAI
313                         aaiNetworkResources.createNetworkInstanceGroup(instanceGroup);
314                 } catch (Exception ex) {
315                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
316                 }       
317         }
318         
319         
320         /**
321          * BPMN access method to establish relationships in AAI
322          * @param execution
323          * @throws Exception
324          */
325         public void connectNetworkToTenant(BuildingBlockExecution execution) {
326                 try {
327                         L3Network l3network =  extractPojosForBB.extractByKey(execution, ResourceKey.NETWORK_ID, execution.getLookupMap().get(ResourceKey.NETWORK_ID));
328                         aaiNetworkResources.connectNetworkToTenant(l3network, execution.getGeneralBuildingBlock().getCloudRegion());
329                 } catch (Exception ex) {
330                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
331                 }       
332         }
333         
334         /**
335          * BPMN access method to establish relationships in AAI
336          * @param execution
337          * @throws Exception
338          */
339         public void connectNetworkToCloudRegion(BuildingBlockExecution execution) {
340                 try {
341                         L3Network l3network =  extractPojosForBB.extractByKey(execution, ResourceKey.NETWORK_ID, execution.getLookupMap().get(ResourceKey.NETWORK_ID));
342                         aaiNetworkResources.connectNetworkToCloudRegion(l3network, execution.getGeneralBuildingBlock().getCloudRegion());
343                 } catch (Exception ex) {
344                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
345                 }       
346         }
347         
348         /**
349          * BPMN access method to establish relationships in AAI
350          * @param execution
351          * @throws Exception
352          */
353         public void connectNetworkToNetworkCollectionServiceInstance(BuildingBlockExecution execution) {
354                 try {
355                         ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
356                         L3Network l3network =  extractPojosForBB.extractByKey(execution, ResourceKey.NETWORK_ID, execution.getLookupMap().get(ResourceKey.NETWORK_ID));
357                         aaiNetworkResources.connectNetworkToNetworkCollectionServiceInstance(l3network, serviceInstance);
358                 } catch (Exception ex) {
359                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
360                 }       
361         }
362         
363         /**
364          * BPMN access method to establish relationships in AAI
365          * @param execution
366          * @throws Exception
367          */
368         public void connectNetworkToNetworkCollectionInstanceGroup(BuildingBlockExecution execution) {
369                 try {
370                         ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
371                         L3Network l3network =  extractPojosForBB.extractByKey(execution, ResourceKey.NETWORK_ID, execution.getLookupMap().get(ResourceKey.NETWORK_ID));
372                         //connect network only if Instance Group / Collection objects exist
373                         if (serviceInstance.getCollection() != null && serviceInstance.getCollection().getInstanceGroup() != null)
374                                 aaiNetworkResources.connectNetworkToNetworkCollectionInstanceGroup(l3network, serviceInstance.getCollection().getInstanceGroup());
375                 } catch (Exception ex) {
376                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
377                 }       
378         }
379 }