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("actionHealthCheck", Action.HealthCheck);
72 execution.setVariable("rollbackVnfStop", false);
73 execution.setVariable("rollbackVnfLock", false);
74 execution.setVariable("rollbackQuiesceTraffic", false);
77 public void runAppcCommand(BuildingBlockExecution execution, Action action) {
78 logger.trace("Start runAppcCommand ");
79 String appcCode = "1002";
80 String appcMessage = "";
82 GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
83 GenericVnf vnf = null;
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");
90 String vnfName = null;
91 String vnfType = null;
92 String vnfHostIpAddress = null;
95 vnfId = vnf.getVnfId();
96 vnfName = vnf.getVnfName();
97 vnfType = vnf.getVnfType();
98 vnfHostIpAddress = vnf.getIpv4OamAddress();
100 String msoRequestId = gBBInput.getRequestContext().getMsoRequestId();
102 String aicIdentity = execution.getVariable("aicIdentity");
103 String vmIdList = execution.getVariable("vmIdList");
104 String vserverIdList = execution.getVariable("vserverIdList");
105 String identityUrl = execution.getVariable("identityUrl");
107 ControllerSelectionReference controllerSelectionReference = catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, action.toString());
108 String controllerType = controllerSelectionReference.getControllerName();
110 String vfModuleId = null;
111 VfModule vfModule = null;
113 vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
114 } catch (BBObjectNotFoundException e) {
116 if (vfModule != null) {
117 vfModuleId = vfModule.getVfModuleId();
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();
127 payload = Optional.of(pay);
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);
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();
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);
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);
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);