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