2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.bpmn.infrastructure.appc.tasks;
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;
48 public class AppcRunTasks {
49 private static final Logger logger = LoggerFactory.getLogger(AppcRunTasks.class);
51 private ExceptionBuilder exceptionUtil;
53 private ExtractPojosForBB extractPojosForBB;
55 private CatalogDbClient catalogDbClient;
57 private ApplicationControllerAction appCClient;
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("rollbackVnfStop", false);
72 execution.setVariable("rollbackVnfLock", false);
73 execution.setVariable("rollbackQuiesceTraffic", false);
76 public void runAppcCommand(BuildingBlockExecution execution, Action action) {
77 logger.trace("Start runAppcCommand ");
78 String appcCode = "1002";
79 String appcMessage = "";
81 GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
82 GenericVnf vnf = null;
84 vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
85 } catch (BBObjectNotFoundException e) {
86 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "No valid VNF exists");
89 String vnfName = null;
90 String vnfType = null;
91 String vnfHostIpAddress = null;
94 vnfId = vnf.getVnfId();
95 vnfName = vnf.getVnfName();
96 vnfType = vnf.getVnfType();
97 vnfHostIpAddress = vnf.getIpv4OamAddress();
99 String msoRequestId = gBBInput.getRequestContext().getMsoRequestId();
101 String aicIdentity = execution.getVariable("aicIdentity");
102 String vmIdList = execution.getVariable("vmIdList");
103 String vserverIdList = execution.getVariable("vserverIdList");
104 String identityUrl = execution.getVariable("identityUrl");
106 ControllerSelectionReference controllerSelectionReference = catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, action.toString());
107 String controllerType = controllerSelectionReference.getControllerName();
109 String vfModuleId = null;
110 VfModule vfModule = null;
112 vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
113 } catch (BBObjectNotFoundException e) {
115 if (vfModule != null) {
116 vfModuleId = vfModule.getVfModuleId();
119 HashMap<String, String> payloadInfo = buildPayloadInfo(vnfName, aicIdentity, vnfHostIpAddress, vmIdList, vserverIdList,
120 identityUrl, vfModuleId);
121 Optional<String> payload = null;
122 RequestParameters requestParameters = gBBInput.getRequestContext().getRequestParameters();
123 if(requestParameters != null){
124 String pay = requestParameters.getPayload();
126 payload = Optional.of(pay);
129 logger.debug("Running APP-C action: {}", action.toString());
130 logger.debug("VNFID: {}", vnfId);
131 appCClient.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
132 appcCode = appCClient.getErrorCode();
133 appcMessage = appCClient.getErrorMessage();
134 mapRollbackVariables(execution, action, appcCode);
136 catch (Exception e) {
137 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION.toString(),
138 "Caught exception in runAppcCommand", "BPMN",
139 MsoLogger.ErrorCode.UnknownError.getValue(), "APPC Error", e);
140 appcMessage = e.getMessage();
143 logger.error("Error Message: {}", appcMessage);
144 logger.error("ERROR CODE: {}", appcCode);
145 logger.trace("End of runAppCommand ");
146 if (appcCode != null && !appcCode.equals("0")) {
147 exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(appcCode), appcMessage);
151 protected void mapRollbackVariables(BuildingBlockExecution execution, Action action, String appcCode) {
152 if (appcCode.equals("0") && action != null) {
153 if (action.equals(Action.Lock)) {
154 execution.setVariable("rollbackVnfLock", true);
155 } else if (action.equals(Action.Unlock)) {
156 execution.setVariable("rollbackVnfLock", false);
157 } else if (action.equals(Action.Start)) {
158 execution.setVariable("rollbackVnfStop", false);
159 } else if (action.equals(Action.Stop)) {
160 execution.setVariable("rollbackVnfStop", true);
161 } else if (action.equals(Action.QuiesceTraffic)) {
162 execution.setVariable("rollbackQuiesceTraffic", true);
163 } else if (action.equals(Action.ResumeTraffic)) {
164 execution.setVariable("rollbackQuiesceTraffic", false);
169 private HashMap<String,String> buildPayloadInfo(String vnfName, String aicIdentity, String vnfHostIpAddress,
170 String vmIdList, String vserverIdList, String identityUrl, String vfModuleId) {
171 HashMap<String, String> payloadInfo = new HashMap<String, String>();
172 payloadInfo.put("vnfName", vnfName);
173 payloadInfo.put("aicIdentity", aicIdentity);
174 payloadInfo.put("vnfHostIpAddress", vnfHostIpAddress);
175 payloadInfo.put("vmIdList", vmIdList);
176 payloadInfo.put("vserverIdList", vserverIdList);
177 payloadInfo.put("identityUrl", identityUrl);
178 payloadInfo.put("vfModuleId",vfModuleId);