75c66345a011d482f675dd78ab4848c7db6b03db
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / appc / tasks / AppcRunTasks.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
23 package org.onap.so.bpmn.infrastructure.appc.tasks;
24
25 import java.util.HashMap;
26 import java.util.Optional;
27 import org.onap.appc.client.lcm.model.Action;
28 import org.onap.so.bpmn.common.BuildingBlockExecution;
29 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
30 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
31 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
32 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
33 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestParameters;
34 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
35 import org.onap.so.client.appc.ApplicationControllerAction;
36 import org.onap.so.client.exception.BBObjectNotFoundException;
37 import org.onap.so.client.exception.ExceptionBuilder;
38 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
39 import org.onap.so.db.catalog.client.CatalogDbClient;
40 import org.onap.so.logger.ErrorCode;
41 import org.onap.so.logger.MessageEnum;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.stereotype.Component;
46
47 @Component
48 public class AppcRunTasks {
49         private static final Logger logger = LoggerFactory.getLogger(AppcRunTasks.class);
50         @Autowired
51         private ExceptionBuilder exceptionUtil;
52         @Autowired
53         private ExtractPojosForBB extractPojosForBB;
54         @Autowired
55         private CatalogDbClient catalogDbClient;
56         @Autowired
57         private ApplicationControllerAction appCClient;
58         
59         public void preProcessActivity(BuildingBlockExecution execution) {
60                 execution.setVariable("actionSnapshot", Action.Snapshot);
61                 execution.setVariable("actionLock", Action.Lock);
62                 execution.setVariable("actionUnlock", Action.Unlock);
63                 execution.setVariable("actionUpgradePreCheck", Action.UpgradePreCheck);
64                 execution.setVariable("actionUpgradePostCheck", Action.UpgradePostCheck);
65                 execution.setVariable("actionQuiesceTraffic", Action.QuiesceTraffic);
66                 execution.setVariable("actionUpgradeBackup", Action.UpgradeBackup);
67                 execution.setVariable("actionUpgradeSoftware", Action.UpgradeSoftware);
68                 execution.setVariable("actionResumeTraffic", Action.ResumeTraffic);             
69                 execution.setVariable("actionStop", Action.Stop);
70                 execution.setVariable("actionStart", Action.Start);
71                 execution.setVariable("actionHealthCheck", Action.HealthCheck);
72                 execution.setVariable("actionDistributeTraffic", Action.DistributeTraffic);
73                 execution.setVariable("actionDistributeTrafficCheck", Action.DistributeTrafficCheck);
74                 execution.setVariable("rollbackVnfStop", false);
75                 execution.setVariable("rollbackVnfLock", false);
76                 execution.setVariable("rollbackQuiesceTraffic", false);
77         }
78         
79         public void runAppcCommand(BuildingBlockExecution execution, Action action) {
80                 logger.trace("Start runAppcCommand ");
81                 String appcCode = "1002";
82                 String appcMessage = "";
83                 try {
84                         GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
85                         GenericVnf vnf = null;
86                         try {
87                                 vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
88                         } catch (BBObjectNotFoundException e) {
89                                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "No valid VNF exists");
90                         }
91         String vnfId = null;
92         String vnfName = null;
93         String vnfType = null;
94         String vnfHostIpAddress = null;
95
96         if (vnf != null) {
97             vnfId = vnf.getVnfId();
98             vnfName = vnf.getVnfName();
99             vnfType = vnf.getVnfType();
100             vnfHostIpAddress = vnf.getIpv4OamAddress();
101         }
102                         String msoRequestId = gBBInput.getRequestContext().getMsoRequestId();
103
104                         String aicIdentity = execution.getVariable("aicIdentity");
105                         String vmIdList = execution.getVariable("vmIdList");
106                         String vserverIdList = execution.getVariable("vserverIdList");
107                         String identityUrl =  execution.getVariable("identityUrl");
108                         
109                         ControllerSelectionReference controllerSelectionReference = catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, action.toString());
110                         String controllerType = controllerSelectionReference.getControllerName();
111                         
112                         String vfModuleId = null;
113                         VfModule vfModule = null;
114                         try {
115                                 vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
116                         } catch (BBObjectNotFoundException e) {
117                         }
118                         if (vfModule != null) {
119                                 vfModuleId = vfModule.getVfModuleId();
120                         }
121                         
122                         HashMap<String, String> payloadInfo = buildPayloadInfo(vnfName, aicIdentity, vnfHostIpAddress, vmIdList, vserverIdList,
123                                         identityUrl, vfModuleId);
124                         Optional<String> payload = null;
125                         RequestParameters requestParameters = gBBInput.getRequestContext().getRequestParameters();
126                         if(requestParameters != null){
127                                 String pay = requestParameters.getPayload();
128                                 if (pay != null) {
129                                         payload =  Optional.of(pay);
130                                 }
131                         }
132                         logger.debug("Running APP-C action: {}", action.toString());
133                         logger.debug("VNFID: {}", vnfId);
134                         appCClient.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
135                         appcCode = appCClient.getErrorCode();
136                         appcMessage = appCClient.getErrorMessage();
137                         mapRollbackVariables(execution, action, appcCode);
138                 }
139                 catch (Exception e) {
140                         logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION.toString(),
141                                 "Caught exception in runAppcCommand", "BPMN",
142                                 ErrorCode.UnknownError.getValue(), "APPC Error", e);
143                         appcMessage = e.getMessage();
144                 }               
145                 
146                 logger.error("Error Message: {}", appcMessage);
147                 logger.error("ERROR CODE: {}", appcCode);
148                 logger.trace("End of runAppCommand ");
149                 if (appcCode != null && !appcCode.equals("0")) {
150                         exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(appcCode), appcMessage);
151                 }
152         }
153         
154         protected void mapRollbackVariables(BuildingBlockExecution execution, Action action, String appcCode) {
155                 if (appcCode.equals("0") && action != null) {
156                         if (action.equals(Action.Lock)) {
157                                 execution.setVariable("rollbackVnfLock", true);
158                         } else if (action.equals(Action.Unlock)) {
159                                 execution.setVariable("rollbackVnfLock", false);
160                         } else if (action.equals(Action.Start)) {
161                                 execution.setVariable("rollbackVnfStop", false);
162                         } else if (action.equals(Action.Stop)) {
163                                 execution.setVariable("rollbackVnfStop", true);
164                         } else if (action.equals(Action.QuiesceTraffic)) {
165                                 execution.setVariable("rollbackQuiesceTraffic", true);
166                         } else if (action.equals(Action.ResumeTraffic)) {
167                                 execution.setVariable("rollbackQuiesceTraffic", false);
168                         }
169                 }
170         }
171         
172         private HashMap<String,String> buildPayloadInfo(String vnfName, String aicIdentity, String vnfHostIpAddress, 
173                         String vmIdList, String vserverIdList, String identityUrl, String vfModuleId) {
174                 HashMap<String, String> payloadInfo = new HashMap<String, String>();
175                 payloadInfo.put("vnfName", vnfName);
176                 payloadInfo.put("aicIdentity", aicIdentity);
177                 payloadInfo.put("vnfHostIpAddress", vnfHostIpAddress);
178                 payloadInfo.put("vmIdList", vmIdList);
179                 payloadInfo.put("vserverIdList", vserverIdList);
180                 payloadInfo.put("identityUrl", identityUrl);
181                 payloadInfo.put("vfModuleId",vfModuleId);
182                 return payloadInfo;
183         }
184 }