58dd5a1df6cbcf846eb4aab63cfdee3819681152
[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                                                 try{
99                                                         configScaleOutParam = JsonPath.parse(sdncVfModuleQueryResponse).read(paramValue);
100                                                 }catch(ClassCastException e){
101                                                         configScaleOutParam = null;
102                                                         msoLogger.warnSimple("Incorrect JSON path. Path points to object rather than value causing: ", e);
103                                                 }
104                                                 paramsMap.put(key, configScaleOutParam);
105                                         }
106                                 }
107                         }
108                         requestParameters.setVfModuleId(vfModule.getVfModuleId());
109                         requestParameters.setVnfHostIpAddress(vnf.getIpv4OamAddress());
110                         configPayload.setConfigurationParameters(paramsMap);
111                         configPayload.setRequestParameters(requestParameters);
112                         configScaleOutPayloadString = mapper.writeValueAsString(configPayload);
113                         
114                         execution.setVariable(ACTION, actionCategory);
115                         execution.setVariable(MSO_REQUEST_ID, gBBInput.getRequestContext().getMsoRequestId());
116                         execution.setVariable(VNF_ID, vnfId);
117                         execution.setVariable(VNF_NAME, vnfName);
118                         execution.setVariable(VFMODULE_ID, vfModule.getVfModuleId());
119                         execution.setVariable(CONTROLLER_TYPE, controllerName);
120                         execution.setVariable(PAYLOAD, configScaleOutPayloadString);
121                 } catch (Exception ex) {
122                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
123                 }
124         }
125         
126         public void callAppcClient(BuildingBlockExecution execution) {
127                 try{
128                         Action commandAction = Action.valueOf(execution.getVariable(ACTION));
129                         String msoRequestId = execution.getVariable(MSO_REQUEST_ID);
130                         String vnfId = execution.getVariable(VNF_ID);
131                         Optional<String> payloadString = null;
132                         if(execution.getVariable(PAYLOAD) != null){
133                                 String pay = execution.getVariable(PAYLOAD);
134                                 payloadString =  Optional.of(pay);
135                         }
136                         String controllerType = execution.getVariable(CONTROLLER_TYPE);
137                         HashMap<String, String> payloadInfo = new HashMap<>();
138                         payloadInfo.put(VNF_NAME, execution.getVariable(VNF_NAME));
139                         payloadInfo.put(VFMODULE_ID,execution.getVariable(VFMODULE_ID));
140                         //PayloadInfo contains extra information that adds on to payload before making request to appc
141                         appCClient.runAppCCommand(commandAction, msoRequestId, vnfId, payloadString, payloadInfo, controllerType);
142                 }catch(Exception ex){
143                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
144                 }
145         }
146 }