615b7279dce4ea9efd4c6ad342a020e01d34fa43
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.bpmn.infrastructure.flowspecific.tasks;
21
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Optional;
26
27 import org.onap.appc.client.lcm.model.Action;
28 import org.onap.so.bpmn.appc.payload.beans.ConfigScaleOutPayload;
29 import org.onap.so.bpmn.appc.payload.beans.RequestParametersConfigScaleOut;
30 import org.onap.so.bpmn.common.BuildingBlockExecution;
31 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
32 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
33 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
34 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
35 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
36 import org.onap.so.client.appc.ApplicationControllerAction;
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.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Component;
44
45 import com.fasterxml.jackson.databind.ObjectMapper;
46 import com.jayway.jsonpath.JsonPath;
47
48 @Component
49 public class ConfigurationScaleOut {
50
51         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, ConfigurationScaleOut.class);
52         @Autowired
53         private ExceptionBuilder exceptionUtil;
54         @Autowired
55         private ExtractPojosForBB extractPojosForBB;
56         @Autowired
57         private CatalogDbClient catalogDbClient;
58         @Autowired
59         private ApplicationControllerAction appCClient;
60         private static final String ACTION = "action";
61         private static final String MSO_REQUEST_ID = "msoRequestId";
62         private static final String VNF_ID = "vnfId";
63         private static final String VNF_NAME = "vnfName";
64         private static final String VFMODULE_ID = "vfModuleId";
65         private static final String CONTROLLER_TYPE = "controllerType";
66         private static final String PAYLOAD = "payload";
67         
68         public void setParamsForConfigurationScaleOut(BuildingBlockExecution execution) {
69                 
70                 GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
71                 
72                 try {
73                         List<Map<String, String>> jsonPathForCfgParams = gBBInput.getRequestContext().getConfigurationParameters();
74                         String key = null;
75                         String paramValue = null;
76                         ObjectMapper mapper = new ObjectMapper();
77                         String configScaleOutPayloadString = null;
78                         ControllerSelectionReference controllerSelectionReference;
79                         ConfigScaleOutPayload configPayload = new ConfigScaleOutPayload();
80                         GenericVnf vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
81                         String vnfId = vnf.getVnfId();
82                         String vnfName = vnf.getVnfName();      
83                         String vnfType = vnf.getVnfType();
84                         String actionCategory = Action.ConfigScaleOut.toString();
85                         controllerSelectionReference = catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, actionCategory);
86                         String controllerName = controllerSelectionReference.getControllerName();
87                         
88                         VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
89                         String sdncVfModuleQueryResponse = execution.getVariable("SDNCQueryResponse_" + vfModule.getVfModuleId());
90                         
91                         Map<String, String> paramsMap = new HashMap<>();
92                         RequestParametersConfigScaleOut requestParameters = new RequestParametersConfigScaleOut();
93                         String configScaleOutParam = null;
94                         if (jsonPathForCfgParams != null) {
95                                 for (Map<String, String> param : jsonPathForCfgParams) {
96                                         for (Map.Entry<String,String> entry : param.entrySet()) {
97                                                 key = entry.getKey();
98                                                 paramValue = entry.getValue();
99                                                 try{
100                                                         configScaleOutParam = JsonPath.parse(sdncVfModuleQueryResponse).read(paramValue);
101                                                 }catch(ClassCastException e){
102                                                         configScaleOutParam = null;
103                                                         msoLogger.warnSimple("Incorrect JSON path. Path points to object rather than value causing: ", e);
104                                                 }
105                                                 paramsMap.put(key, configScaleOutParam);
106                                         }
107                                 }
108                         }
109                         requestParameters.setVfModuleId(vfModule.getVfModuleId());
110                         requestParameters.setVnfHostIpAddress(vnf.getIpv4OamAddress());
111                         configPayload.setConfigurationParameters(paramsMap);
112                         configPayload.setRequestParameters(requestParameters);
113                         configScaleOutPayloadString = mapper.writeValueAsString(configPayload);
114                         
115                         execution.setVariable(ACTION, actionCategory);
116                         execution.setVariable(MSO_REQUEST_ID, gBBInput.getRequestContext().getMsoRequestId());
117                         execution.setVariable(VNF_ID, vnfId);
118                         execution.setVariable(VNF_NAME, vnfName);
119                         execution.setVariable(VFMODULE_ID, vfModule.getVfModuleId());
120                         execution.setVariable(CONTROLLER_TYPE, controllerName);
121                         execution.setVariable(PAYLOAD, configScaleOutPayloadString);
122                 } catch (Exception ex) {
123                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
124                 }
125         }
126         
127         public void callAppcClient(BuildingBlockExecution execution) {
128                 msoLogger.trace("Start runAppcCommand ");
129                 String appcCode = "1002";
130                 String appcMessage = "";
131                 try{
132                         Action commandAction = Action.valueOf(execution.getVariable(ACTION));
133                         String msoRequestId = execution.getVariable(MSO_REQUEST_ID);
134                         String vnfId = execution.getVariable(VNF_ID);
135                         Optional<String> payloadString = null;
136                         if(execution.getVariable(PAYLOAD) != null){
137                                 String pay = execution.getVariable(PAYLOAD);
138                                 payloadString =  Optional.of(pay);
139                         }
140                         String controllerType = execution.getVariable(CONTROLLER_TYPE);
141                         HashMap<String, String> payloadInfo = new HashMap<>();
142                         payloadInfo.put(VNF_NAME, execution.getVariable(VNF_NAME));
143                         payloadInfo.put(VFMODULE_ID,execution.getVariable(VFMODULE_ID));
144                         msoLogger.debug("Running APP-C action: " + commandAction.toString());
145                         msoLogger.debug("VNFID: " + vnfId);     
146                         //PayloadInfo contains extra information that adds on to payload before making request to appc
147                         appCClient.runAppCCommand(commandAction, msoRequestId, vnfId, payloadString, payloadInfo, controllerType);
148                         appcCode = appCClient.getErrorCode();
149                         appcMessage = appCClient.getErrorMessage();
150                 
151                 } catch (Exception e) {
152                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "Caught exception in runAppcCommand in ConfigurationScaleOut", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "APPC Error", e);
153                         appcMessage = e.getMessage();
154                 }
155                 msoLogger.error("Error Message: " + appcMessage);
156                 msoLogger.error("ERROR CODE: " + appcCode);
157                 msoLogger.trace("End of runAppCommand ");
158                 if (appcCode != null && !appcCode.equals("0")) {
159                         exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(appcCode), appcMessage);
160                 }
161         }
162 }