4b4b3eca231d2e28806e2075c0eccd515829fab4
[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.MessageEnum;
41 import org.onap.so.logger.MsoLogger;
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("rollbackVnfStop", false);
73                 execution.setVariable("rollbackVnfLock", false);
74                 execution.setVariable("rollbackQuiesceTraffic", false);
75         }
76         
77         public void runAppcCommand(BuildingBlockExecution execution, Action action) {
78                 logger.trace("Start runAppcCommand ");
79                 String appcCode = "1002";
80                 String appcMessage = "";
81                 try {
82                         GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
83                         GenericVnf vnf = null;
84                         try {
85                                 vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
86                         } catch (BBObjectNotFoundException e) {
87                                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "No valid VNF exists");
88                         }
89         String vnfId = null;
90         String vnfName = null;
91         String vnfType = null;
92         String vnfHostIpAddress = null;
93
94         if (vnf != null) {
95             vnfId = vnf.getVnfId();
96             vnfName = vnf.getVnfName();
97             vnfType = vnf.getVnfType();
98             vnfHostIpAddress = vnf.getIpv4OamAddress();
99         }
100                         String msoRequestId = gBBInput.getRequestContext().getMsoRequestId();
101
102                         String aicIdentity = execution.getVariable("aicIdentity");
103                         String vmIdList = execution.getVariable("vmIdList");
104                         String vserverIdList = execution.getVariable("vserverIdList");
105                         String identityUrl =  execution.getVariable("identityUrl");
106                         
107                         ControllerSelectionReference controllerSelectionReference = catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, action.toString());
108                         String controllerType = controllerSelectionReference.getControllerName();
109                         
110                         String vfModuleId = null;
111                         VfModule vfModule = null;
112                         try {
113                                 vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
114                         } catch (BBObjectNotFoundException e) {
115                         }
116                         if (vfModule != null) {
117                                 vfModuleId = vfModule.getVfModuleId();
118                         }
119                         
120                         HashMap<String, String> payloadInfo = buildPayloadInfo(vnfName, aicIdentity, vnfHostIpAddress, vmIdList, vserverIdList,
121                                         identityUrl, vfModuleId);
122                         Optional<String> payload = null;
123                         RequestParameters requestParameters = gBBInput.getRequestContext().getRequestParameters();
124                         if(requestParameters != null){
125                                 String pay = requestParameters.getPayload();
126                                 if (pay != null) {
127                                         payload =  Optional.of(pay);
128                                 }
129                         }
130                         logger.debug("Running APP-C action: {}", action.toString());
131                         logger.debug("VNFID: {}", vnfId);
132                         appCClient.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
133                         appcCode = appCClient.getErrorCode();
134                         appcMessage = appCClient.getErrorMessage();
135                         mapRollbackVariables(execution, action, appcCode);
136                 }
137                 catch (Exception e) {
138                         logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION.toString(),
139                                 "Caught exception in runAppcCommand", "BPMN",
140                                 MsoLogger.ErrorCode.UnknownError.getValue(), "APPC Error", e);
141                         appcMessage = e.getMessage();
142                 }               
143                 
144                 logger.error("Error Message: {}", appcMessage);
145                 logger.error("ERROR CODE: {}", appcCode);
146                 logger.trace("End of runAppCommand ");
147                 if (appcCode != null && !appcCode.equals("0")) {
148                         exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(appcCode), appcMessage);
149                 }
150         }
151         
152         protected void mapRollbackVariables(BuildingBlockExecution execution, Action action, String appcCode) {
153                 if (appcCode.equals("0") && action != null) {
154                         if (action.equals(Action.Lock)) {
155                                 execution.setVariable("rollbackVnfLock", true);
156                         } else if (action.equals(Action.Unlock)) {
157                                 execution.setVariable("rollbackVnfLock", false);
158                         } else if (action.equals(Action.Start)) {
159                                 execution.setVariable("rollbackVnfStop", false);
160                         } else if (action.equals(Action.Stop)) {
161                                 execution.setVariable("rollbackVnfStop", true);
162                         } else if (action.equals(Action.QuiesceTraffic)) {
163                                 execution.setVariable("rollbackQuiesceTraffic", true);
164                         } else if (action.equals(Action.ResumeTraffic)) {
165                                 execution.setVariable("rollbackQuiesceTraffic", false);
166                         }
167                 }
168         }
169         
170         private HashMap<String,String> buildPayloadInfo(String vnfName, String aicIdentity, String vnfHostIpAddress, 
171                         String vmIdList, String vserverIdList, String identityUrl, String vfModuleId) {
172                 HashMap<String, String> payloadInfo = new HashMap<String, String>();
173                 payloadInfo.put("vnfName", vnfName);
174                 payloadInfo.put("aicIdentity", aicIdentity);
175                 payloadInfo.put("vnfHostIpAddress", vnfHostIpAddress);
176                 payloadInfo.put("vmIdList", vmIdList);
177                 payloadInfo.put("vserverIdList", vserverIdList);
178                 payloadInfo.put("identityUrl", identityUrl);
179                 payloadInfo.put("vfModuleId",vfModuleId);
180                 return payloadInfo;
181         }
182 }