2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20 * ============LICENSE_END=========================================================
23 package org.onap.appc.flow.controller.node;
25 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.ACTION_LEVEL;
26 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.APPC_FLOW_CONTROLLER;
27 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.DESINGTIME;
28 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.EXTERNAL;
29 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.FLOW_SEQUENCE;
30 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.GENERATION_NODE;
31 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.GRAPH;
32 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_PARAM_RESPONSE_PREFIX;
33 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.NODE;
34 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_PARAM_ERROR_MESSAGE;
35 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_PARAM_STATUS;
36 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_STATUS_FAILURE;
37 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_STATUS_MESSAGE;
38 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_STATUS_SUCCESS;
39 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.PAYLOAD;
40 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.REQUEST_ACTION;
41 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.REQUEST_ID;
42 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.RESPONSE_PREFIX;
43 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.REST;
44 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.RUNTIME;
45 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.SEQUENCE_TYPE;
46 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.SEQ_GENERATOR_PWD;
47 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.SEQ_GENERATOR_UID;
48 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.SEQ_GENERATOR_URL;
49 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VF_MODULE;
50 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VM;
51 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNF;
52 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNFC;
53 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNFC_NAME;
54 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNFC_TYPE;
55 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNF_ID;
56 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNF_TYPE;
57 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VSERVER_ID;
59 import com.att.eelf.configuration.EELFLogger;
60 import com.att.eelf.configuration.EELFManager;
61 import com.fasterxml.jackson.annotation.JsonInclude.Include;
62 import com.fasterxml.jackson.databind.DeserializationFeature;
63 import com.fasterxml.jackson.databind.JsonNode;
64 import com.fasterxml.jackson.databind.ObjectMapper;
65 import com.fasterxml.jackson.databind.SerializationFeature;
66 import java.io.IOException;
67 import java.util.ArrayList;
68 import java.util.Arrays;
69 import java.util.HashMap;
70 import java.util.List;
72 import java.util.Properties;
73 import org.apache.commons.lang3.StringUtils;
74 import org.json.JSONObject;
75 import org.onap.appc.flow.controller.ResponseHandlerImpl.DefaultResponseHandler;
76 import org.onap.appc.flow.controller.data.PrecheckOption;
77 import org.onap.appc.flow.controller.data.ResponseAction;
78 import org.onap.appc.flow.controller.data.Transaction;
79 import org.onap.appc.flow.controller.data.Transactions;
80 import org.onap.appc.flow.controller.dbervices.FlowControlDBService;
81 import org.onap.appc.flow.controller.executorImpl.GraphExecutor;
82 import org.onap.appc.flow.controller.executorImpl.NodeExecutor;
83 import org.onap.appc.flow.controller.executorImpl.RestExecutor;
84 import org.onap.appc.flow.controller.interfaceData.ActionIdentifier;
85 import org.onap.appc.flow.controller.interfaceData.Capabilities;
86 import org.onap.appc.flow.controller.interfaceData.DependencyInfo;
87 import org.onap.appc.flow.controller.interfaceData.Input;
88 import org.onap.appc.flow.controller.interfaceData.InventoryInfo;
89 import org.onap.appc.flow.controller.interfaceData.RequestInfo;
90 import org.onap.appc.flow.controller.interfaceData.Vnfcs;
91 import org.onap.appc.flow.controller.interfaces.FlowExecutorInterface;
92 import org.onap.appc.flow.controller.utils.EncryptionTool;
93 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
94 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
95 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
97 public class FlowControlNode implements SvcLogicJavaPlugin {
99 private static final EELFLogger log = EELFManager.getInstance().getLogger(FlowControlNode.class);
100 private static final String SDNC_CONFIG_DIR_VAR = "SDNC_CONFIG_DIR";
102 private final EnvVariables envVariables;
103 private final FlowControlDBService dbService;
105 public FlowControlNode() {
106 this.envVariables = new EnvVariables();
107 this.dbService = FlowControlDBService.initialise();
110 FlowControlNode(EnvVariables envVariables, FlowControlDBService dbService) {
111 this.envVariables = envVariables;
112 this.dbService = dbService;
115 public void processFlow(Map<String, String> inParams, SvcLogicContext ctx)
116 throws SvcLogicException {
117 log.debug("Received processParamKeys call with params : " + inParams);
118 String responsePrefix = inParams.get(INPUT_PARAM_RESPONSE_PREFIX);
120 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
121 SvcLogicContext localContext = new SvcLogicContext();
123 localContext.setAttribute(REQUEST_ID, ctx.getAttribute(REQUEST_ID));
124 localContext.setAttribute(VNF_TYPE, ctx.getAttribute(VNF_TYPE));
125 localContext.setAttribute(REQUEST_ACTION, ctx.getAttribute(REQUEST_ACTION));
126 localContext.setAttribute(ACTION_LEVEL, ctx.getAttribute(ACTION_LEVEL));
127 localContext.setAttribute(RESPONSE_PREFIX, responsePrefix);
128 ctx.setAttribute(RESPONSE_PREFIX, responsePrefix);
130 dbService.getFlowReferenceData(ctx, inParams, localContext);
132 for (String key : localContext.getAttributeKeySet()) {
133 log.debug("processFlow " + key + "=" + ctx.getAttribute(key));
135 processFlowSequence(inParams, ctx, localContext);
136 if (!ctx.getAttribute(responsePrefix + OUTPUT_PARAM_STATUS).equals(OUTPUT_STATUS_SUCCESS)) {
137 throw new SvcLogicException(ctx.getAttribute(responsePrefix + OUTPUT_STATUS_MESSAGE));
139 } catch (Exception e) {
140 ctx.setAttribute(responsePrefix + OUTPUT_PARAM_STATUS, OUTPUT_STATUS_FAILURE);
141 ctx.setAttribute(responsePrefix + OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
142 log.error("Error occurred in processFlow ", e);
143 throw new SvcLogicException(e.getMessage());
147 private void processFlowSequence(Map<String, String> inParams, SvcLogicContext ctx, SvcLogicContext localContext)
150 String fn = "FlowExecutorNode.processflowSequence";
151 log.debug(fn + "Received model for flow : " + localContext.toString());
153 String flowSequence = null;
154 for (String key : localContext.getAttributeKeySet()) {
155 log.debug(key + "=" + ctx.getAttribute(key));
157 if (localContext.getAttribute(SEQUENCE_TYPE) != null) {
158 if (localContext.getAttribute(GENERATION_NODE) != null) {
159 GraphExecutor transactionExecutor = new GraphExecutor();
160 Boolean generatorExists = transactionExecutor.hasGraph(
162 localContext.getAttribute(GENERATION_NODE),
167 if (generatorExists) {
168 flowSequence = transactionExecutor.executeGraph(
170 localContext.getAttribute(GENERATION_NODE),
172 .getProperty(FLOW_SEQUENCE);
174 throw new Exception("Can not find Custom defined Flow Generator for "
175 + localContext.getAttribute(GENERATION_NODE));
178 } else if ((localContext.getAttribute(SEQUENCE_TYPE)).equalsIgnoreCase(DESINGTIME)) {
180 localContext.setAttribute(VNFC_TYPE, ctx.getAttribute(VNFC_TYPE));
181 flowSequence = dbService.getDesignTimeFlowModel(localContext);
183 if (flowSequence == null) {
184 throw new Exception("Flow Sequence is not found User Designed VNF " + ctx.getAttribute(VNF_TYPE));
187 } else if ((localContext.getAttribute(SEQUENCE_TYPE)).equalsIgnoreCase(RUNTIME)) {
189 Transaction transaction = new Transaction();
190 String input = collectInputParams(ctx, transaction);
191 log.info("CollectInputParamsData-Input: " + input);
193 RestExecutor restExe = new RestExecutor();
194 Map<String, String> flowSeq = restExe.execute(transaction, localContext);
196 JSONObject sequence = new JSONObject(flowSeq.get("restResponse"));
197 if (sequence.has("output")) {
198 flowSequence = sequence.getJSONObject("output").toString();
200 log.info("MultistepSequenceGenerator-Output: " + flowSequence);
202 if (flowSequence == null) {
203 throw new Exception("Failed to get the Flow Sequece runtime for VNF type"
204 + ctx.getAttribute(VNF_TYPE));
207 } else if ((localContext.getAttribute(SEQUENCE_TYPE)).equalsIgnoreCase(EXTERNAL)) {
208 //String input = collectInputParams(localContext);
209 // flowSequnce = ""; //get it from the External interface calling the Rest End point - TBD
210 //if(flowSequnce == null)
212 throw new Exception("Flow Sequence not found for " + ctx.getAttribute(VNF_TYPE));
215 //No other type of model supported...
216 //in Future can get flowModel from other generators which will be included here
217 throw new Exception("No information found for sequence Owner Design-Time Vs Run-Time");
221 FlowGenerator flowGenerator = new FlowGenerator();
222 Transactions trans = flowGenerator.createSingleStepModel(inParams, ctx);
223 ObjectMapper mapper = new ObjectMapper();
224 flowSequence = mapper.writeValueAsString(trans);
225 log.debug("Single step Flow Sequence : " + flowSequence);
228 log.debug("Received Flow Sequence : " + flowSequence);
229 HashMap<Integer, Transaction> transactionMap = createTransactionMap(flowSequence, localContext);
230 executeAllTransaction(transactionMap, ctx);
231 log.info("Executed all the transaction successfully");
234 private void executeAllTransaction(HashMap<Integer, Transaction> transactionMap, SvcLogicContext ctx)
237 String fn = "FlowExecutorNode.executeAllTransaction ";
239 FlowExecutorInterface flowExecutor;
240 for (int key = 1; key <= transactionMap.size(); key++) {
241 log.debug(fn + "Starting transactions ID " + key + " :)=" + retry);
242 Transaction transaction = transactionMap.get(key);
243 if (!preProcessor(transactionMap, transaction)) {
244 log.info("Skipping Transaction ID " + transaction.getTransactionId());
247 if (transaction.getExecutionType() != null) {
248 switch (transaction.getExecutionType()) {
250 flowExecutor = new GraphExecutor();
253 flowExecutor = new NodeExecutor();
256 flowExecutor = new RestExecutor();
259 throw new Exception("No Executor found for transaction ID" + transaction.getTransactionId());
261 flowExecutor.execute(transaction, ctx);
262 ResponseAction responseAction = handleResponse(transaction);
264 if (responseAction.getWait() != null && Integer.parseInt(responseAction.getWait()) > 0) {
265 log.debug(fn + "Going to Sleep .... " + responseAction.getWait());
266 Thread.sleep(Integer.parseInt(responseAction.getWait()) * 1000L);
268 if (responseAction.isIntermediateMessage()) {
269 log.debug(fn + "Sending Intermediate Message back .... ");
270 sendIntermediateMessage();
272 if (responseAction.getRetry() != null && Integer.parseInt(responseAction.getRetry()) > retry) {
273 log.debug(fn + "Ooppss!!! We will retry again ....... ");
276 log.debug(fn + "key =" + key + "retry =" + retry);
278 if (responseAction.isIgnore()) {
279 log.debug(fn + "Ignoring this Error and moving ahead ....... ");
282 if (responseAction.isStop()) {
283 log.debug(fn + "Need to Stop ....... ");
286 if (responseAction.getJump() != null && Integer.parseInt(responseAction.getJump()) > 0) {
287 key = Integer.parseInt(responseAction.getJump());
290 log.debug(fn + "key =" + key + "retry =" + retry);
293 throw new Exception("Don't know how to execute transaction ID " + transaction.getTransactionId());
298 private void sendIntermediateMessage() {
299 // TODO Auto-generated method stub
302 private ResponseAction handleResponse(Transaction transaction) {
303 log.info("Handling Response for transaction Id " + transaction.getTransactionId());
304 DefaultResponseHandler defaultHandler = new DefaultResponseHandler();
305 return defaultHandler.handlerResponse(transaction);
308 private boolean preProcessor(HashMap<Integer, Transaction> transactionMap, Transaction transaction)
311 log.debug("Starting Preprocessing Logic ");
312 boolean runThisStep = false;
314 if (transaction.getPrecheck() != null
315 && transaction.getPrecheck().getPrecheckOptions() != null
316 && !transaction.getPrecheck().getPrecheckOptions().isEmpty()) {
318 List<PrecheckOption> precheckOptions = transaction.getPrecheck().getPrecheckOptions();
319 for (PrecheckOption precheck : precheckOptions) {
320 Transaction trans = transactionMap.get(precheck.getpTransactionID());
321 ObjectMapper mapper = new ObjectMapper();
322 log.info("Mapper= " + mapper.writeValueAsString(trans));
323 HashMap trmap = mapper.readValue(mapper.writeValueAsString(trans), HashMap.class);
324 runThisStep = trmap.get(precheck.getParamName()) != null
325 && ((String) trmap.get(precheck.getParamName()))
326 .equalsIgnoreCase(precheck.getParamValue());
328 if (("any").equalsIgnoreCase(transaction.getPrecheck().getPrecheckOperator()) && runThisStep) {
333 log.debug("No Pre check defined for transaction ID " + transaction.getTransactionId());
336 } catch (Exception e) {
337 log.error("Error occured when Preprocessing Logic ", e);
340 log.debug("Returing process current Transaction = " + runThisStep);
344 private HashMap<Integer, Transaction> createTransactionMap(String flowSequence, SvcLogicContext localContext)
347 ObjectMapper mapper = new ObjectMapper();
348 Transactions transactions = mapper.readValue(flowSequence, Transactions.class);
349 HashMap<Integer, Transaction> transMap = new HashMap<>();
350 for (Transaction transaction : transactions.getTransactions()) {
351 compileFlowDependencies(transaction, localContext);
352 //parse the Transactions Object and create records in process_flow_status table
353 //loadTransactionIntoStatus(transactions, ctx);
354 transMap.put(transaction.getTransactionId(), transaction);
359 private void compileFlowDependencies(Transaction transaction, SvcLogicContext localContext)
362 dbService.populateModuleAndRPC(transaction, localContext.getAttribute(VNF_TYPE));
363 ObjectMapper mapper = new ObjectMapper();
364 log.debug("Individual Transaction Details :" + transaction.toString());
366 if ((localContext.getAttribute(SEQUENCE_TYPE) == null)
367 || (localContext.getAttribute(SEQUENCE_TYPE) != null
368 && !localContext.getAttribute(SEQUENCE_TYPE)
369 .equalsIgnoreCase(DESINGTIME))) {
371 localContext.setAttribute("artifact-content", mapper.writeValueAsString(transaction));
372 dbService.loadSequenceIntoDB(localContext);
374 //get a field in transction class as transactionhandle interface and register the Handler here for each trnactions
377 private String collectInputParams(SvcLogicContext ctx, Transaction transaction) throws Exception {
379 String fn = "FlowExecuteNode.collectInputParams";
380 Properties prop = loadProperties();
381 log.info("Loaded Properties " + prop.toString());
383 String vnfId = ctx.getAttribute(VNF_ID);
384 String inputData = null;
385 log.debug(fn + "vnfId :" + vnfId);
387 if (StringUtils.isBlank(vnfId)) {
388 throw new Exception("VnfId is missing");
392 ActionIdentifier actionIdentifier = new ActionIdentifier();
393 log.debug("Enter ActionIdentifier");
394 if (StringUtils.isNotBlank(vnfId)) {
395 actionIdentifier.setVnfId(vnfId);
397 if (StringUtils.isNotBlank(ctx.getAttribute(VSERVER_ID))) {
398 actionIdentifier.setVserverId(ctx.getAttribute(VSERVER_ID));
400 if (StringUtils.isNotBlank(ctx.getAttribute(VNFC_NAME))) {
401 actionIdentifier.setVnfcName(ctx.getAttribute(VNFC_NAME));
403 log.info("ActionIdentifierData" + actionIdentifier.toString());
405 RequestInfo requestInfo = new RequestInfo();
406 log.info("Enter RequestInfo");
407 requestInfo.setAction(ctx.getAttribute(REQUEST_ACTION));
408 requestInfo.setActionLevel(ctx.getAttribute(ACTION_LEVEL));
409 requestInfo.setPayload(ctx.getAttribute(PAYLOAD));
410 requestInfo.setActionIdentifier(actionIdentifier);
411 log.debug("RequestInfo: " + requestInfo.toString());
413 InventoryInfo inventoryInfo = new InventoryInfoExtractor().getInventoryInfo(ctx, vnfId);
414 DependencyInfo dependencyInfo = getDependencyInfo(ctx);
415 Capabilities capabilities = getCapabilitiesData(ctx);
417 Input input = new Input();
418 log.info("Enter InputData");
419 input.setRequestInfo(requestInfo);
420 input.setInventoryInfo(inventoryInfo);
421 input.setDependencyInfo(dependencyInfo);
422 input.setCapabilities(capabilities);
423 log.info(fn + "Input parameters:" + input.toString());
425 ObjectMapper mapper = new ObjectMapper();
426 mapper.setSerializationInclusion(Include.NON_NULL);
427 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
428 inputData = mapper.writeValueAsString(input);
429 log.info("InputDataJson:" + inputData);
431 } catch (Exception e) {
432 log.error("Error occurred in " + fn, e);
435 String resourceUri = prop.getProperty(SEQ_GENERATOR_URL);
436 log.info(fn + "resourceUri= " + resourceUri);
438 EncryptionTool et = EncryptionTool.getInstance();
439 String pass = et.decrypt(prop.getProperty(SEQ_GENERATOR_PWD));
441 transaction.setPayload(inputData);
442 transaction.setExecutionRPC("POST");
443 transaction.setuId(prop.getProperty(SEQ_GENERATOR_UID));
444 transaction.setPswd(pass);
445 transaction.setExecutionEndPoint(resourceUri);
450 private DependencyInfo getDependencyInfo(SvcLogicContext ctx) throws Exception {
452 String fn = "FlowExecutorNode.getDependencyInfo";
453 DependencyInfo dependencyInfo = new DependencyInfo();
454 String dependencyData = dbService.getDependencyInfo(ctx);
455 log.info(fn + "dependencyDataInput:" + dependencyData);
457 if (dependencyData != null) {
458 ObjectMapper mapper = new ObjectMapper();
459 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
460 mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
461 //JsonNode dependencyInfoData = mapper.readTree(dependencyData).get("dependencyInfo");
462 JsonNode vnfcData = mapper.readTree(dependencyData).get("vnfcs");
463 List<Vnfcs> vnfclist = Arrays.asList(mapper.readValue(vnfcData.toString(), Vnfcs[].class));
464 dependencyInfo.getVnfcs().addAll(vnfclist);
466 log.info("Dependency Output:" + dependencyInfo.toString());
468 return dependencyInfo;
471 private Capabilities getCapabilitiesData(SvcLogicContext ctx) throws Exception {
473 String fn = "FlowExecutorNode.getCapabilitiesData";
474 Capabilities capabilities = new Capabilities();
475 String capabilitiesData = dbService.getCapabilitiesData(ctx);
476 log.info(fn + "capabilitiesDataInput:" + capabilitiesData);
478 if (capabilitiesData != null) {
479 ObjectMapper mapper = new ObjectMapper();
480 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
481 mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
482 JsonNode capabilitiesNode = mapper.readValue(capabilitiesData, JsonNode.class);
483 log.info("capabilitiesNode:" + capabilitiesNode.toString());
485 JsonNode vnfs = capabilitiesNode.findValue(VNF);
486 List<String> vnfsList = new ArrayList<>();
488 for (int i = 0; i < vnfs.size(); i++) {
489 String vnf = vnfs.get(i).asText();
494 JsonNode vfModules = capabilitiesNode.findValue(VF_MODULE);
495 List<String> vfModulesList = new ArrayList<>();
496 if (vfModules != null) {
497 for (int i = 0; i < vfModules.size(); i++) {
498 String vfModule = vfModules.get(i).asText();
499 vfModulesList.add(vfModule);
503 JsonNode vnfcs = capabilitiesNode.findValue(VNFC);
504 List<String> vnfcsList = new ArrayList<>();
506 for (int i = 0; i < vnfcs.size(); i++) {
507 String vnfc1 = vnfcs.get(i).asText();
508 vnfcsList.add(vnfc1);
512 JsonNode vms = capabilitiesNode.findValue(VM);
513 List<String> vmList = new ArrayList<>();
515 for (int i = 0; i < vms.size(); i++) {
516 String vm1 = vms.get(i).asText();
521 capabilities.getVnfc().addAll(vnfcsList);
522 capabilities.getVnf().addAll(vnfsList);
523 capabilities.getVfModule().addAll(vfModulesList);
524 capabilities.getVm().addAll(vmList);
526 log.info("Capabilities Output:" + capabilities.toString());
531 private Properties loadProperties() throws Exception {
532 String directory = envVariables.getenv(SDNC_CONFIG_DIR_VAR);
533 if (directory == null) {
534 throw new Exception("Cannot find Property file -" + SDNC_CONFIG_DIR_VAR);
536 String path = directory + APPC_FLOW_CONTROLLER;
537 return PropertiesLoader.load(path);