Reduced content of MsoLogger
[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
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.MessageEnum;
43 import org.onap.so.logger.MsoLogger;
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
49 import com.fasterxml.jackson.databind.ObjectMapper;
50 import com.jayway.jsonpath.JsonPath;
51
52 @Component
53 public class ConfigurationScaleOut {
54
55         private static final Logger logger = LoggerFactory.getLogger(ConfigurationScaleOut.class);
56         @Autowired
57         private ExceptionBuilder exceptionUtil;
58         @Autowired
59         private ExtractPojosForBB extractPojosForBB;
60         @Autowired
61         private CatalogDbClient catalogDbClient;
62         @Autowired
63         private ApplicationControllerAction appCClient;
64         private static final String ACTION = "action";
65         private static final String MSO_REQUEST_ID = "msoRequestId";
66         private static final String VNF_ID = "vnfId";
67         private static final String VNF_NAME = "vnfName";
68         private static final String VFMODULE_ID = "vfModuleId";
69         private static final String CONTROLLER_TYPE = "controllerType";
70         private static final String PAYLOAD = "payload";
71         
72         public void setParamsForConfigurationScaleOut(BuildingBlockExecution execution) {
73                 
74                 GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
75                 
76                 try {
77                         List<Map<String, String>> jsonPathForCfgParams = gBBInput.getRequestContext().getConfigurationParameters();
78                         String key = null;
79                         String paramValue = null;
80                         ObjectMapper mapper = new ObjectMapper();
81                         String configScaleOutPayloadString = null;
82                         ControllerSelectionReference controllerSelectionReference;
83                         ConfigScaleOutPayload configPayload = new ConfigScaleOutPayload();
84                         GenericVnf vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
85                         String vnfId = vnf.getVnfId();
86                         String vnfName = vnf.getVnfName();      
87                         String vnfType = vnf.getVnfType();
88                         String actionCategory = Action.ConfigScaleOut.toString();
89                         controllerSelectionReference = catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, actionCategory);
90                         String controllerName = controllerSelectionReference.getControllerName();
91                         
92                         VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(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("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION.toString(),
157                                 "Caught exception in runAppcCommand in ConfigurationScaleOut", "BPMN",
158                                 MsoLogger.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 && !appcCode.equals("0")) {
165                         exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(appcCode), appcMessage);
166                 }
167         }
168 }