Merge "Update Logging"
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / flowspecific / tasks / CreateNetworkCollection.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 package org.onap.so.bpmn.infrastructure.flowspecific.tasks;
21
22 import org.onap.so.bpmn.common.BuildingBlockExecution;
23 import org.onap.so.bpmn.servicedecomposition.bbobjects.Collection;
24 import org.onap.so.bpmn.servicedecomposition.bbobjects.InstanceGroup;
25 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
26 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
27 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
28 import org.onap.so.client.exception.ExceptionBuilder;
29 import org.onap.so.client.orchestration.AAINetworkResources;
30 import org.onap.so.logger.MsoLogger;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Component;
33
34 @Component
35 public class CreateNetworkCollection {
36         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, CreateNetworkCollection.class);
37         @Autowired
38         private ExceptionBuilder exceptionUtil;
39         @Autowired
40         private ExtractPojosForBB extractPojosForBB;
41         @Autowired
42         private AAINetworkResources aaiNetworkResources;
43
44         private static final String UNDERSCORE = "_";
45         private static final String NETWORK_COLLECTION_NAME = "networkCollectionName";
46         
47         /**
48          * BPMN access method to build Network Collection Name
49          * @param execution
50          * @throws Exception
51          */
52         public void buildNetworkCollectionName(BuildingBlockExecution execution) throws Exception {
53                 try{
54                         ServiceInstance serviceInstance =  extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
55                         InstanceGroup instanceGroup =  serviceInstance.getCollection().getInstanceGroup();
56                         if(instanceGroup.getModelInfoInstanceGroup() != null) {
57                                 //Build collection name assembling SI name and IG function
58                                 if(serviceInstance.getServiceInstanceName() != null 
59                                                 && instanceGroup.getModelInfoInstanceGroup().getFunction() != null) {
60                                         String networkCollectionName = serviceInstance.getServiceInstanceName().concat(UNDERSCORE).concat(instanceGroup.getModelInfoInstanceGroup().getFunction());
61                                         //set networkCollectionName object on execution to be re-used within current BB
62                                         execution.setVariable(NETWORK_COLLECTION_NAME, networkCollectionName);
63                                 } else {
64                                         throw new IllegalArgumentException("Cannot generate collection name because either one or both fields are null: "
65                                                         + " Service Instance Name: " + serviceInstance.getServiceInstanceName() 
66                                                         + ", Instance Group Function: " + instanceGroup.getModelInfoInstanceGroup().getFunction());
67                                 }
68                         } else {
69                                 throw new IllegalArgumentException("Instance group model info is null");
70                         }
71                 } catch (Exception ex) {
72                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
73                 }
74         }
75         
76         /**
77          * BPMN access method to connect Network Collection
78          * @param execution
79          * @throws Exception
80          */
81         public void connectCollectionToInstanceGroup(BuildingBlockExecution execution) throws Exception {
82                 execution.setVariable("connectCollectionToInstanceGroupRollback", false);
83                 try{
84                         ServiceInstance serviceInstance =  extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
85                         Collection networkCollection =  serviceInstance.getCollection();
86                         aaiNetworkResources.connectNetworkCollectionInstanceGroupToNetworkCollection(networkCollection.getInstanceGroup(), networkCollection);
87                         execution.setVariable("connectCollectionToInstanceGroupRollback", true);
88                 } catch (Exception ex) {
89                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
90                 }
91         }
92         
93         /**
94          * BPMN access method to connect Network Collection
95          * @param execution
96          * @throws Exception
97          */
98         public void connectCollectionToServiceInstance(BuildingBlockExecution execution) throws Exception {
99                 execution.setVariable("connectCollectionToServiceInstanceRollback", false);
100                 try{
101                         ServiceInstance serviceInstance =  extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
102                         Collection networkCollection =  serviceInstance.getCollection();
103                         aaiNetworkResources.connectNetworkCollectionToServiceInstance(networkCollection, serviceInstance);
104                         execution.setVariable("connectCollectionToServiceInstanceRollback", true);
105                 } catch (Exception ex) {
106                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
107                 }
108         }
109 }