Containerization feature of SO
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / flowspecific / tasks / AssignNetwork.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.L3Network;
24 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
25 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
26 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
27 import org.onap.so.client.exception.ExceptionBuilder;
28 import org.onap.so.db.catalog.beans.OrchestrationStatus;
29 import org.onap.so.logger.MsoLogger;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.stereotype.Component;
32
33 @Component
34 public class AssignNetwork {
35
36         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL,
37                         AssignNetwork.class);
38         @Autowired
39         private ExceptionBuilder exceptionUtil;
40         @Autowired
41         private ExtractPojosForBB extractPojosForBB;
42
43         /**
44          * Check if there are collection(s)
45          * @param execution
46          * @return boolean
47          */
48         public boolean hasCollection(BuildingBlockExecution execution) {
49                 boolean hasCollection = false;
50                 try {
51                         ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID,
52                                         execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
53                         if (serviceInstance.getCollection() != null){
54                                 hasCollection = true;
55                                 msoLogger.debug("there are collections to create");
56                         }
57                 } catch (Exception ex) {
58                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
59                 }
60                 return hasCollection;
61         }
62         
63         /**
64          * Check if network was found by name
65          * @param execution
66          * @return
67          */
68         public boolean networkFoundByName(BuildingBlockExecution execution) {
69                 boolean networkFound = false;
70                 try {
71                         L3Network l3network = extractPojosForBB.extractByKey(execution, ResourceKey.NETWORK_ID,
72                                         execution.getLookupMap().get(ResourceKey.NETWORK_ID));
73
74                         if (!OrchestrationStatus.PRECREATED.equals(l3network.getOrchestrationStatus())){
75                                 networkFound = true;
76                                 msoLogger.debug("network found in NOT PRECREATED status");
77                         }
78                 } catch (Exception ex) {
79                         // return false if no network present
80                 }
81                 return networkFound;
82         }
83         
84         /**
85          * BPMN access method. Return flag if BPMN flow should skip AAI interaction
86          * @param execution
87          * @return TRUE if network collection was NOT present OR network WAS found by name
88          */
89         public boolean skipNetworkCreationInAAI(BuildingBlockExecution execution) {
90
91                 return !hasCollection(execution) || networkFoundByName(execution);
92         }
93 }