e0516ce9aff6fe6bd1bf7d9c7d123fd5d39e34ad
[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("rollbackVnfStop", false);
72                 execution.setVariable("rollbackVnfLock", false);
73                 execution.setVariable("rollbackQuiesceTraffic", false);
74         }
75         
76         public void runAppcCommand(BuildingBlockExecution execution, Action action) {
77                 logger.trace("Start runAppcCommand ");
78                 String appcCode = "1002";
79                 String appcMessage = "";
80                 try {
81                         GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
82                         GenericVnf vnf = null;
83                         try {
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");
87                         }
88         String vnfId = null;
89         String vnfName = null;
90         String vnfType = null;
91         String vnfHostIpAddress = null;
92
93         if (vnf != null) {
94             vnfId = vnf.getVnfId();
95             vnfName = vnf.getVnfName();
96             vnfType = vnf.getVnfType();
97             vnfHostIpAddress = vnf.getIpv4OamAddress();
98         }
99                         String msoRequestId = gBBInput.getRequestContext().getMsoRequestId();
100
101                         String aicIdentity = execution.getVariable("aicIdentity");
102                         String vmIdList = execution.getVariable("vmIdList");
103                         String vserverIdList = execution.getVariable("vserverIdList");
104                         String identityUrl =  execution.getVariable("identityUrl");
105                         
106                         ControllerSelectionReference controllerSelectionReference = catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, action.toString());
107                         String controllerType = controllerSelectionReference.getControllerName();
108                         
109                         String vfModuleId = null;
110                         VfModule vfModule = null;
111                         try {
112                                 vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
113                         } catch (BBObjectNotFoundException e) {
114                         }
115                         if (vfModule != null) {
116                                 vfModuleId = vfModule.getVfModuleId();
117                         }
118                         
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();
125                                 if (pay != null) {
126                                         payload =  Optional.of(pay);
127                                 }
128                         }
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);
135                 }
136                 catch (Exception e) {
137                         logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION.toString(),
138                                 "Caught exception in runAppcCommand", "BPMN", MsoLogger.getServiceName(),
139                                 MsoLogger.ErrorCode.UnknownError.getValue(), "APPC Error", e);
140                         appcMessage = e.getMessage();
141                 }               
142                 
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);
148                 }
149         }
150         
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);
165                         }
166                 }
167         }
168         
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);
179                 return payloadInfo;
180         }
181 }