36eab8f981772b6b6eae8cc271469292dbf79f46
[so.git] /
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
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      * 
53      * @param execution
54      * @throws Exception
55      */
56     public void buildNetworkCollectionName(BuildingBlockExecution execution) throws Exception {
57         try {
58             ServiceInstance serviceInstance =
59                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
60             InstanceGroup instanceGroup = serviceInstance.getCollection().getInstanceGroup();
61             if (instanceGroup.getModelInfoInstanceGroup() != null) {
62                 // Build collection name assembling SI name and IG function
63                 if (serviceInstance.getServiceInstanceName() != null
64                         && instanceGroup.getModelInfoInstanceGroup().getFunction() != null) {
65                     String networkCollectionName = serviceInstance.getServiceInstanceName().concat(UNDERSCORE)
66                             .concat(instanceGroup.getModelInfoInstanceGroup().getFunction());
67                     // set networkCollectionName object on execution to be re-used within current BB
68                     execution.setVariable(NETWORK_COLLECTION_NAME, networkCollectionName);
69                 } else {
70                     throw new IllegalArgumentException(
71                             "Cannot generate collection name because either one or both fields are null: "
72                                     + " Service Instance Name: " + serviceInstance.getServiceInstanceName()
73                                     + ", Instance Group Function: "
74                                     + instanceGroup.getModelInfoInstanceGroup().getFunction());
75                 }
76             } else {
77                 throw new IllegalArgumentException("Instance group model info is null");
78             }
79         } catch (Exception ex) {
80             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
81         }
82     }
83
84     /**
85      * BPMN access method to connect Network Collection
86      * 
87      * @param execution
88      * @throws Exception
89      */
90     public void connectCollectionToInstanceGroup(BuildingBlockExecution execution) throws Exception {
91         execution.setVariable("connectCollectionToInstanceGroupRollback", false);
92         try {
93             ServiceInstance serviceInstance =
94                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
95             Collection networkCollection = serviceInstance.getCollection();
96             aaiNetworkResources.connectNetworkCollectionInstanceGroupToNetworkCollection(
97                     networkCollection.getInstanceGroup(), networkCollection);
98             execution.setVariable("connectCollectionToInstanceGroupRollback", true);
99         } catch (Exception ex) {
100             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
101         }
102     }
103
104     /**
105      * BPMN access method to connect Instance Group to Cloud Region
106      * 
107      * @param execution
108      * @throws Exception
109      */
110     public void connectInstanceGroupToCloudRegion(BuildingBlockExecution execution) throws Exception {
111         execution.setVariable("connectInstanceGroupToCloudRegionRollback", false);
112         try {
113             ServiceInstance serviceInstance =
114                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
115             Collection networkCollection = serviceInstance.getCollection();
116             aaiNetworkResources.connectInstanceGroupToCloudRegion(networkCollection.getInstanceGroup(),
117                     execution.getGeneralBuildingBlock().getCloudRegion());
118             execution.setVariable("connectInstanceGroupToCloudRegionRollback", true);
119         } catch (Exception ex) {
120             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
121         }
122     }
123
124     /**
125      * BPMN access method to connect Network Collection
126      * 
127      * @param execution
128      * @throws Exception
129      */
130     public void connectCollectionToServiceInstance(BuildingBlockExecution execution) throws Exception {
131         execution.setVariable("connectCollectionToServiceInstanceRollback", false);
132         try {
133             ServiceInstance serviceInstance =
134                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
135             Collection networkCollection = serviceInstance.getCollection();
136             aaiNetworkResources.connectNetworkCollectionToServiceInstance(networkCollection, serviceInstance);
137             execution.setVariable("connectCollectionToServiceInstanceRollback", true);
138         } catch (Exception ex) {
139             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
140         }
141     }
142 }