Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / flowspecific / tasks / ConfigurationScaleOut.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 package org.onap.so.bpmn.infrastructure.flowspecific.tasks;
23
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Optional;
28 import org.onap.appc.client.lcm.model.Action;
29 import org.onap.so.bpmn.appc.payload.beans.ConfigScaleOutPayload;
30 import org.onap.so.bpmn.appc.payload.beans.RequestParametersConfigScaleOut;
31 import org.onap.so.bpmn.common.BuildingBlockExecution;
32 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
33 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
34 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
35 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
36 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
37 import org.onap.so.client.appc.ApplicationControllerAction;
38 import org.onap.so.client.exception.ExceptionBuilder;
39 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
40 import org.onap.so.db.catalog.client.CatalogDbClient;
41 import org.onap.so.logger.ErrorCode;
42 import org.onap.so.logger.MessageEnum;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.stereotype.Component;
47 import com.fasterxml.jackson.databind.ObjectMapper;
48 import com.jayway.jsonpath.JsonPath;
49
50 @Component
51 public class ConfigurationScaleOut {
52
53     private static final Logger logger = LoggerFactory.getLogger(ConfigurationScaleOut.class);
54     @Autowired
55     private ExceptionBuilder exceptionUtil;
56     @Autowired
57     private ExtractPojosForBB extractPojosForBB;
58     @Autowired
59     private CatalogDbClient catalogDbClient;
60     @Autowired
61     private ApplicationControllerAction appCClient;
62     private static final String ACTION = "action";
63     private static final String MSO_REQUEST_ID = "msoRequestId";
64     private static final String VNF_ID = "vnfId";
65     private static final String VNF_NAME = "vnfName";
66     private static final String VFMODULE_ID = "vfModuleId";
67     private static final String CONTROLLER_TYPE = "controllerType";
68     private static final String PAYLOAD = "payload";
69
70     public void setParamsForConfigurationScaleOut(BuildingBlockExecution execution) {
71
72         GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
73
74         try {
75             List<Map<String, String>> jsonPathForCfgParams = gBBInput.getRequestContext().getConfigurationParameters();
76             String key = null;
77             String paramValue = null;
78             ObjectMapper mapper = new ObjectMapper();
79             String configScaleOutPayloadString = null;
80             ControllerSelectionReference controllerSelectionReference;
81             ConfigScaleOutPayload configPayload = new ConfigScaleOutPayload();
82             GenericVnf vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
83             String vnfId = vnf.getVnfId();
84             String vnfName = vnf.getVnfName();
85             String vnfType = vnf.getVnfType();
86             String actionCategory = Action.ConfigScaleOut.toString();
87             controllerSelectionReference =
88                     catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, actionCategory);
89             String controllerName = controllerSelectionReference.getControllerName();
90
91             VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
92             String sdncVfModuleQueryResponse = execution.getVariable("SDNCQueryResponse_" + vfModule.getVfModuleId());
93
94             Map<String, String> paramsMap = new HashMap<>();
95             RequestParametersConfigScaleOut requestParameters = new RequestParametersConfigScaleOut();
96             String configScaleOutParam = null;
97             if (jsonPathForCfgParams != null) {
98                 for (Map<String, String> param : jsonPathForCfgParams) {
99                     for (Map.Entry<String, String> entry : param.entrySet()) {
100                         key = entry.getKey();
101                         paramValue = entry.getValue();
102                         try {
103                             configScaleOutParam = JsonPath.parse(sdncVfModuleQueryResponse).read(paramValue);
104                         } catch (ClassCastException e) {
105                             configScaleOutParam = null;
106                             logger.warn("Incorrect JSON path. Path points to object rather than value causing: ", e);
107                         }
108                         paramsMap.put(key, configScaleOutParam);
109                     }
110                 }
111             }
112             requestParameters.setVfModuleId(vfModule.getVfModuleId());
113             requestParameters.setVnfHostIpAddress(vnf.getIpv4OamAddress());
114             configPayload.setConfigurationParameters(paramsMap);
115             configPayload.setRequestParameters(requestParameters);
116             configScaleOutPayloadString = mapper.writeValueAsString(configPayload);
117
118             execution.setVariable(ACTION, actionCategory);
119             execution.setVariable(MSO_REQUEST_ID, gBBInput.getRequestContext().getMsoRequestId());
120             execution.setVariable(VNF_ID, vnfId);
121             execution.setVariable(VNF_NAME, vnfName);
122             execution.setVariable(VFMODULE_ID, vfModule.getVfModuleId());
123             execution.setVariable(CONTROLLER_TYPE, controllerName);
124             execution.setVariable(PAYLOAD, configScaleOutPayloadString);
125         } catch (Exception ex) {
126             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
127         }
128     }
129
130     public void callAppcClient(BuildingBlockExecution execution) {
131         logger.trace("Start runAppcCommand ");
132         String appcCode = "1002";
133         String appcMessage = "";
134         try {
135             Action commandAction = Action.valueOf(execution.getVariable(ACTION));
136             String msoRequestId = execution.getVariable(MSO_REQUEST_ID);
137             String vnfId = execution.getVariable(VNF_ID);
138             Optional<String> payloadString = null;
139             if (execution.getVariable(PAYLOAD) != null) {
140                 String pay = execution.getVariable(PAYLOAD);
141                 payloadString = Optional.of(pay);
142             }
143             String controllerType = execution.getVariable(CONTROLLER_TYPE);
144             HashMap<String, String> payloadInfo = new HashMap<>();
145             payloadInfo.put(VNF_NAME, execution.getVariable(VNF_NAME));
146             payloadInfo.put(VFMODULE_ID, execution.getVariable(VFMODULE_ID));
147             logger.debug("Running APP-C action: {}", commandAction.toString());
148             logger.debug("VNFID: {}", vnfId);
149             // PayloadInfo contains extra information that adds on to payload before making request to appc
150             appCClient.runAppCCommand(commandAction, msoRequestId, vnfId, payloadString, payloadInfo, controllerType);
151             appcCode = appCClient.getErrorCode();
152             appcMessage = appCClient.getErrorMessage();
153
154         } catch (Exception e) {
155             logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION.toString(),
156                     "Caught exception in runAppcCommand in ConfigurationScaleOut", "BPMN",
157                     ErrorCode.UnknownError.getValue(), "APPC Error", e);
158             appcMessage = e.getMessage();
159         }
160         logger.error("Error Message: " + appcMessage);
161         logger.error("ERROR CODE: " + appcCode);
162         logger.trace("End of runAppCommand ");
163         if (appcCode != null && !appcCode.equals("0")) {
164             exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(appcCode), appcMessage);
165         }
166     }
167 }