d13c5db8716ec78f830e5025ab290b60c651813e
[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.MsoLogger;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Component;
43
44 import com.fasterxml.jackson.databind.ObjectMapper;
45 import com.jayway.jsonpath.JsonPath;
46
47 @Component
48 public class ConfigurationScaleOut {
49
50         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, ConfigurationScaleOut.class);
51         @Autowired
52         private ExceptionBuilder exceptionUtil;
53         @Autowired
54         private ExtractPojosForBB extractPojosForBB;
55         @Autowired
56         private CatalogDbClient catalogDbClient;
57         @Autowired
58         private ApplicationControllerAction appCClient;
59         private static final String ACTION = "action";
60         private static final String MSO_REQUEST_ID = "msoRequestId";
61         private static final String VNF_ID = "vnfId";
62         private static final String VNF_NAME = "vnfName";
63         private static final String VFMODULE_ID = "vfModuleId";
64         private static final String CONTROLLER_TYPE = "controllerType";
65         private static final String PAYLOAD = "payload";
66         
67         public void setParamsForConfigurationScaleOut(BuildingBlockExecution execution) {
68                 
69                 GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
70                 
71                 try {
72                         List<Map<String, String>> jsonPathForCfgParams = gBBInput.getRequestContext().getConfigurationParameters();
73                         String key = null;
74                         String paramValue = null;
75                         ObjectMapper mapper = new ObjectMapper();
76                         String configScaleOutPayloadString = null;
77                         ControllerSelectionReference controllerSelectionReference;
78                         ConfigScaleOutPayload configPayload = new ConfigScaleOutPayload();
79                         GenericVnf vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
80                         String vnfId = vnf.getVnfId();
81                         String vnfName = vnf.getVnfName();      
82                         String vnfType = vnf.getVnfType();
83                         String actionCategory = Action.ConfigScaleOut.toString();
84                         controllerSelectionReference = catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, actionCategory);
85                         String controllerName = controllerSelectionReference.getControllerName();
86                         
87                         VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
88                         String sdncVfModuleQueryResponse = execution.getVariable("SDNCQueryResponse_" + vfModule.getVfModuleId());
89                         
90                         Map<String, String> paramsMap = new HashMap<>();
91                         RequestParametersConfigScaleOut requestParameters = new RequestParametersConfigScaleOut();
92                         String configScaleOutParam = null;
93                         if (jsonPathForCfgParams != null) {
94                                 for (Map<String, String> param : jsonPathForCfgParams) {
95                                         for (Map.Entry<String,String> entry : param.entrySet()) {
96                                                 key = entry.getKey();
97                                                 paramValue = entry.getValue();
98                                                 configScaleOutParam = JsonPath.parse(sdncVfModuleQueryResponse).read(paramValue);
99                                                 if(configScaleOutParam != null){
100                                                         paramsMap.put(key, configScaleOutParam);
101                                                 }
102                                         }
103                                 }
104                         }
105                         requestParameters.setVfModuleId(vfModule.getVfModuleId());
106                         requestParameters.setVnfHostIpAddress(vnf.getIpv4OamAddress());
107                         configPayload.setConfigurationParameters(paramsMap);
108                         configPayload.setRequestParameters(requestParameters);
109                         configScaleOutPayloadString = mapper.writeValueAsString(configPayload);
110                         configScaleOutPayloadString = configScaleOutPayloadString.replaceAll("\"", "\\\\\"");
111                         
112                         execution.setVariable(ACTION, actionCategory);
113                         execution.setVariable(MSO_REQUEST_ID, gBBInput.getRequestContext().getMsoRequestId());
114                         execution.setVariable(VNF_ID, vnfId);
115                         execution.setVariable(VNF_NAME, vnfName);
116                         execution.setVariable(VFMODULE_ID, vfModule.getVfModuleId());
117                         execution.setVariable(CONTROLLER_TYPE, controllerName);
118                         execution.setVariable(PAYLOAD, configScaleOutPayloadString);
119                 } catch (Exception ex) {
120                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
121                 }
122         }
123         
124         public void callAppcClient(BuildingBlockExecution execution) {
125                 try{
126                         Action commandAction = Action.valueOf(execution.getVariable(ACTION));
127                         String msoRequestId = execution.getVariable(MSO_REQUEST_ID);
128                         String vnfId = execution.getVariable(VNF_ID);
129                         Optional<String> payloadString = null;
130                         if(execution.getVariable(PAYLOAD) != null){
131                                 String pay = execution.getVariable(PAYLOAD);
132                                 payloadString =  Optional.of(pay);
133                         }
134                         String controllerType = execution.getVariable(CONTROLLER_TYPE);
135                         HashMap<String, String> payloadInfo = new HashMap<>();
136                         payloadInfo.put(VNF_NAME, execution.getVariable(VNF_NAME));
137                         payloadInfo.put(VFMODULE_ID,execution.getVariable(VFMODULE_ID));
138                         //PayloadInfo contains extra information that adds on to payload before making request to appc
139                         appCClient.runAppCCommand(commandAction, msoRequestId, vnfId, payloadString, payloadInfo, controllerType);
140                 }catch(Exception ex){
141                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
142                 }
143         }
144 }