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