5665302b2ab3222e9cf9c3f1c82a5acef5a6d145
[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  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.so.bpmn.infrastructure.flowspecific.tasks;
23
24 import org.onap.so.bpmn.common.BuildingBlockExecution;
25 import org.onap.so.bpmn.servicedecomposition.bbobjects.Collection;
26 import org.onap.so.bpmn.servicedecomposition.bbobjects.InstanceGroup;
27 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
28 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
29 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
30 import org.onap.so.client.exception.ExceptionBuilder;
31 import org.onap.so.client.orchestration.AAINetworkResources;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Component;
36
37 @Component
38 public class CreateNetworkCollection {
39         private static final Logger logger = LoggerFactory.getLogger(CreateNetworkCollection.class);
40         @Autowired
41         private ExceptionBuilder exceptionUtil;
42         @Autowired
43         private ExtractPojosForBB extractPojosForBB;
44         @Autowired
45         private AAINetworkResources aaiNetworkResources;
46
47         private static final String UNDERSCORE = "_";
48         private static final String NETWORK_COLLECTION_NAME = "networkCollectionName";
49         
50         /**
51          * BPMN access method to build Network Collection Name
52          * @param execution
53          * @throws Exception
54          */
55         public void buildNetworkCollectionName(BuildingBlockExecution execution) throws Exception {
56                 try{
57                         ServiceInstance serviceInstance =  extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
58                         InstanceGroup instanceGroup =  serviceInstance.getCollection().getInstanceGroup();
59                         if(instanceGroup.getModelInfoInstanceGroup() != null) {
60                                 //Build collection name assembling SI name and IG function
61                                 if(serviceInstance.getServiceInstanceName() != null 
62                                                 && instanceGroup.getModelInfoInstanceGroup().getFunction() != null) {
63                                         String networkCollectionName = serviceInstance.getServiceInstanceName().concat(UNDERSCORE).concat(instanceGroup.getModelInfoInstanceGroup().getFunction());
64                                         //set networkCollectionName object on execution to be re-used within current BB
65                                         execution.setVariable(NETWORK_COLLECTION_NAME, networkCollectionName);
66                                 } else {
67                                         throw new IllegalArgumentException("Cannot generate collection name because either one or both fields are null: "
68                                                         + " Service Instance Name: " + serviceInstance.getServiceInstanceName() 
69                                                         + ", Instance Group Function: " + instanceGroup.getModelInfoInstanceGroup().getFunction());
70                                 }
71                         } else {
72                                 throw new IllegalArgumentException("Instance group model info is null");
73                         }
74                 } catch (Exception ex) {
75                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
76                 }
77         }
78         
79         /**
80          * BPMN access method to connect Network Collection
81          * @param execution
82          * @throws Exception
83          */
84         public void connectCollectionToInstanceGroup(BuildingBlockExecution execution) throws Exception {
85                 execution.setVariable("connectCollectionToInstanceGroupRollback", false);
86                 try{
87                         ServiceInstance serviceInstance =  extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
88                         Collection networkCollection =  serviceInstance.getCollection();
89                         aaiNetworkResources.connectNetworkCollectionInstanceGroupToNetworkCollection(networkCollection.getInstanceGroup(), networkCollection);
90                         execution.setVariable("connectCollectionToInstanceGroupRollback", true);
91                 } catch (Exception ex) {
92                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
93                 }
94         }
95         
96         /**
97          * BPMN access method to connect Instance Group to Cloud Region
98          * @param execution
99          * @throws Exception
100          */
101         public void connectInstanceGroupToCloudRegion(BuildingBlockExecution execution) throws Exception {
102                 execution.setVariable("connectInstanceGroupToCloudRegionRollback", false);
103                 try{
104                         ServiceInstance serviceInstance =  extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
105                         Collection networkCollection =  serviceInstance.getCollection();
106                         aaiNetworkResources.connectInstanceGroupToCloudRegion(networkCollection.getInstanceGroup(), execution.getGeneralBuildingBlock().getCloudRegion());
107                         execution.setVariable("connectInstanceGroupToCloudRegionRollback", true);
108                 } catch (Exception ex) {
109                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
110                 }
111         }
112         
113         /**
114          * BPMN access method to connect Network Collection
115          * @param execution
116          * @throws Exception
117          */
118         public void connectCollectionToServiceInstance(BuildingBlockExecution execution) throws Exception {
119                 execution.setVariable("connectCollectionToServiceInstanceRollback", false);
120                 try{
121                         ServiceInstance serviceInstance =  extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
122                         Collection networkCollection =  serviceInstance.getCollection();
123                         aaiNetworkResources.connectNetworkCollectionToServiceInstance(networkCollection, serviceInstance);
124                         execution.setVariable("connectCollectionToServiceInstanceRollback", true);
125                 } catch (Exception ex) {
126                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
127                 }
128         }
129 }