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.core.type.TypeReference;
63 import com.fasterxml.jackson.databind.DeserializationFeature;
64 import com.fasterxml.jackson.databind.JsonNode;
65 import com.fasterxml.jackson.databind.ObjectMapper;
66 import com.fasterxml.jackson.databind.SerializationFeature;
67 import java.io.IOException;
68 import java.util.ArrayList;
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());
154 .getAttributeKeySet()
155 .forEach(key -> log.debug(key + "=" + ctx.getAttribute(key)));
157 String flowSequence = getFlowSequence(inParams, ctx, localContext);
159 log.debug("Received Flow Sequence : " + flowSequence);
160 HashMap<Integer, Transaction> transactionMap = createTransactionMap(flowSequence, localContext);
161 executeAllTransaction(transactionMap, ctx);
162 log.info("Executed all the transaction successfully");
165 String getFlowSequence(Map<String, String> inParams, SvcLogicContext ctx, SvcLogicContext localContext) throws Exception {
166 String flowSequence = null;
167 if (localContext.getAttribute(SEQUENCE_TYPE) != null) {
168 if (localContext.getAttribute(GENERATION_NODE) != null) {
169 GraphExecutor transactionExecutor = new GraphExecutor();
170 Boolean generatorExists = transactionExecutor.hasGraph(
172 localContext.getAttribute(GENERATION_NODE),
177 if (generatorExists) {
178 flowSequence = transactionExecutor.executeGraph(
180 localContext.getAttribute(GENERATION_NODE),
182 .getProperty(FLOW_SEQUENCE);
184 throw new Exception("Can not find Custom defined Flow Generator for "
185 + localContext.getAttribute(GENERATION_NODE));
188 } else if ((localContext.getAttribute(SEQUENCE_TYPE)).equalsIgnoreCase(DESINGTIME)) {
190 localContext.setAttribute(VNFC_TYPE, ctx.getAttribute(VNFC_TYPE));
191 flowSequence = dbService.getDesignTimeFlowModel(localContext);
193 if (flowSequence == null) {
194 throw new Exception("Flow Sequence is not found User Designed VNF " + ctx.getAttribute(VNF_TYPE));
197 } else if ((localContext.getAttribute(SEQUENCE_TYPE)).equalsIgnoreCase(RUNTIME)) {
199 Transaction transaction = new Transaction();
200 String input = collectInputParams(ctx, transaction);
201 log.info("CollectInputParamsData-Input: " + input);
203 RestExecutor restExe = new RestExecutor();
204 Map<String, String> flowSeq = restExe.execute(transaction, localContext);
206 JSONObject sequence = new JSONObject(flowSeq.get("restResponse"));
207 if (sequence.has("output")) {
208 flowSequence = sequence.getJSONObject("output").toString();
210 log.info("MultistepSequenceGenerator-Output: " + flowSequence);
212 if (flowSequence == null) {
213 throw new Exception("Failed to get the Flow Sequece runtime for VNF type"
214 + ctx.getAttribute(VNF_TYPE));
217 } else if ((localContext.getAttribute(SEQUENCE_TYPE)).equalsIgnoreCase(EXTERNAL)) {
218 //String input = collectInputParams(localContext);
219 // flowSequnce = ""; //get it from the External interface calling the Rest End point - TBD
220 //if(flowSequnce == null)
222 throw new Exception("Flow Sequence not found for " + ctx.getAttribute(VNF_TYPE));
225 //No other type of model supported...
226 //in Future can get flowModel from other generators which will be included here
227 throw new Exception("No information found for sequence Owner Design-Time Vs Run-Time");
231 FlowGenerator flowGenerator = new FlowGenerator();
232 Transactions trans = flowGenerator.createSingleStepModel(inParams, ctx);
233 ObjectMapper mapper = new ObjectMapper();
234 flowSequence = mapper.writeValueAsString(trans);
235 log.debug("Single step Flow Sequence : " + flowSequence);
240 private void executeAllTransaction(HashMap<Integer, Transaction> transactionMap, SvcLogicContext ctx)
243 String fn = "FlowExecutorNode.executeAllTransaction ";
245 FlowExecutorInterface flowExecutor;
246 for (int key = 1; key <= transactionMap.size(); key++) {
247 log.debug(fn + "Starting transactions ID " + key + " :)=" + retry);
248 Transaction transaction = transactionMap.get(key);
249 if (!preProcessor(transactionMap, transaction)) {
250 log.info("Skipping Transaction ID " + transaction.getTransactionId());
253 if (transaction.getExecutionType() != null) {
254 switch (transaction.getExecutionType()) {
256 flowExecutor = new GraphExecutor();
259 flowExecutor = new NodeExecutor();
262 flowExecutor = new RestExecutor();
265 throw new Exception("No Executor found for transaction ID" + transaction.getTransactionId());
267 flowExecutor.execute(transaction, ctx);
268 ResponseAction responseAction = handleResponse(transaction);
270 if (responseAction.getWait() != null && Integer.parseInt(responseAction.getWait()) > 0) {
271 log.debug(fn + "Going to Sleep .... " + responseAction.getWait());
272 Thread.sleep(Integer.parseInt(responseAction.getWait()) * 1000L);
274 if (responseAction.isIntermediateMessage()) {
275 log.debug(fn + "Sending Intermediate Message back .... ");
276 sendIntermediateMessage();
278 if (responseAction.getRetry() != null && Integer.parseInt(responseAction.getRetry()) > retry) {
279 log.debug(fn + "Ooppss!!! We will retry again ....... ");
282 log.debug(fn + "key =" + key + "retry =" + retry);
284 if (responseAction.isIgnore()) {
285 log.debug(fn + "Ignoring this Error and moving ahead ....... ");
288 if (responseAction.isStop()) {
289 log.debug(fn + "Need to Stop ....... ");
292 if (responseAction.getJump() != null && Integer.parseInt(responseAction.getJump()) > 0) {
293 key = Integer.parseInt(responseAction.getJump());
296 log.debug(fn + "key =" + key + "retry =" + retry);
299 throw new Exception("Don't know how to execute transaction ID " + transaction.getTransactionId());
304 private void sendIntermediateMessage() {
305 // TODO Auto-generated method stub
308 private ResponseAction handleResponse(Transaction transaction) {
309 log.info("Handling Response for transaction Id " + transaction.getTransactionId());
310 DefaultResponseHandler defaultHandler = new DefaultResponseHandler();
311 return defaultHandler.handlerResponse(transaction);
314 private boolean preProcessor(HashMap<Integer, Transaction> transactionMap, Transaction transaction)
317 log.debug("Starting Preprocessing Logic ");
318 boolean runThisStep = false;
320 if (transaction.getPrecheck() != null
321 && transaction.getPrecheck().getPrecheckOptions() != null
322 && !transaction.getPrecheck().getPrecheckOptions().isEmpty()) {
324 List<PrecheckOption> precheckOptions = transaction.getPrecheck().getPrecheckOptions();
325 for (PrecheckOption precheck : precheckOptions) {
326 Transaction trans = transactionMap.get(precheck.getpTransactionID());
327 ObjectMapper mapper = new ObjectMapper();
328 log.info("Mapper= " + mapper.writeValueAsString(trans));
329 HashMap trmap = mapper.readValue(mapper.writeValueAsString(trans), HashMap.class);
330 runThisStep = trmap.get(precheck.getParamName()) != null
331 && ((String) trmap.get(precheck.getParamName()))
332 .equalsIgnoreCase(precheck.getParamValue());
334 if (("any").equalsIgnoreCase(transaction.getPrecheck().getPrecheckOperator()) && runThisStep) {
339 log.debug("No Pre check defined for transaction ID " + transaction.getTransactionId());
342 } catch (Exception e) {
343 log.error("Error occured when Preprocessing Logic ", e);
346 log.debug("Returing process current Transaction = " + runThisStep);
350 private HashMap<Integer, Transaction> createTransactionMap(String flowSequence, SvcLogicContext localContext)
353 ObjectMapper mapper = new ObjectMapper();
354 Transactions transactions = mapper.readValue(flowSequence, Transactions.class);
355 HashMap<Integer, Transaction> transMap = new HashMap<>();
356 for (Transaction transaction : transactions.getTransactions()) {
357 compileFlowDependencies(transaction, localContext);
358 //parse the Transactions Object and create records in process_flow_status table
359 //loadTransactionIntoStatus(transactions, ctx);
360 transMap.put(transaction.getTransactionId(), transaction);
365 private void compileFlowDependencies(Transaction transaction, SvcLogicContext localContext)
368 dbService.populateModuleAndRPC(transaction, localContext.getAttribute(VNF_TYPE));
369 ObjectMapper mapper = new ObjectMapper();
370 log.debug("Individual Transaction Details :" + transaction.toString());
372 if ((localContext.getAttribute(SEQUENCE_TYPE) == null)
373 || (localContext.getAttribute(SEQUENCE_TYPE) != null
374 && !localContext.getAttribute(SEQUENCE_TYPE)
375 .equalsIgnoreCase(DESINGTIME))) {
377 localContext.setAttribute("artifact-content", mapper.writeValueAsString(transaction));
378 dbService.loadSequenceIntoDB(localContext);
380 //get a field in transction class as transactionhandle interface and register the Handler here for each trnactions
383 private String collectInputParams(SvcLogicContext ctx, Transaction transaction) throws Exception {
385 String fn = "FlowExecuteNode.collectInputParams";
386 Properties prop = loadProperties();
387 log.info("Loaded Properties " + prop.toString());
389 String vnfId = ctx.getAttribute(VNF_ID);
390 String inputData = null;
391 log.debug(fn + "vnfId :" + vnfId);
393 if (StringUtils.isBlank(vnfId)) {
394 throw new Exception("VnfId is missing");
398 ActionIdentifier actionIdentifier = new ActionIdentifier();
399 log.debug("Enter ActionIdentifier");
400 if (StringUtils.isNotBlank(vnfId)) {
401 actionIdentifier.setVnfId(vnfId);
403 if (StringUtils.isNotBlank(ctx.getAttribute(VSERVER_ID))) {
404 actionIdentifier.setVserverId(ctx.getAttribute(VSERVER_ID));
406 if (StringUtils.isNotBlank(ctx.getAttribute(VNFC_NAME))) {
407 actionIdentifier.setVnfcName(ctx.getAttribute(VNFC_NAME));
409 log.info("ActionIdentifierData" + actionIdentifier.toString());
411 RequestInfo requestInfo = new RequestInfo();
412 log.info("Enter RequestInfo");
413 requestInfo.setAction(ctx.getAttribute(REQUEST_ACTION));
414 requestInfo.setActionLevel(ctx.getAttribute(ACTION_LEVEL));
415 requestInfo.setPayload(ctx.getAttribute(PAYLOAD));
416 requestInfo.setActionIdentifier(actionIdentifier);
417 log.debug("RequestInfo: " + requestInfo.toString());
419 InventoryInfo inventoryInfo = new InventoryInfoExtractor().getInventoryInfo(ctx, vnfId);
420 DependencyInfo dependencyInfo = getDependencyInfo(ctx);
421 Capabilities capabilities = getCapabilitiesData(ctx);
423 Input input = new Input();
424 log.info("Enter InputData");
425 input.setRequestInfo(requestInfo);
426 input.setInventoryInfo(inventoryInfo);
427 input.setDependencyInfo(dependencyInfo);
428 input.setCapabilities(capabilities);
429 log.info(fn + "Input parameters:" + input.toString());
431 ObjectMapper mapper = new ObjectMapper();
432 mapper.setSerializationInclusion(Include.NON_NULL);
433 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
434 inputData = mapper.writeValueAsString(input);
435 log.info("InputDataJson:" + inputData);
437 } catch (Exception e) {
438 log.error("Error occurred in " + fn, e);
441 String resourceUri = prop.getProperty(SEQ_GENERATOR_URL);
442 log.info(fn + "resourceUri= " + resourceUri);
444 EncryptionTool et = EncryptionTool.getInstance();
445 String pass = et.decrypt(prop.getProperty(SEQ_GENERATOR_PWD));
447 transaction.setPayload(inputData);
448 transaction.setExecutionRPC("POST");
449 transaction.setuId(prop.getProperty(SEQ_GENERATOR_UID));
450 transaction.setPswd(pass);
451 transaction.setExecutionEndPoint(resourceUri);
456 DependencyInfo getDependencyInfo(SvcLogicContext ctx) throws SvcLogicException, IOException {
458 String fn = "FlowExecutorNode.getDependencyInfo";
459 String dependencyData = dbService.getDependencyInfo(ctx);
460 log.info(fn + "dependencyDataInput:" + dependencyData);
462 DependencyInfo dependencyInfo = new DependencyInfo();
463 if (dependencyData == null) {
464 return dependencyInfo;
467 ObjectMapper mapper = new ObjectMapper();
468 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
469 mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
470 //JsonNode dependencyInfoData = mapper.readTree(dependencyData).get("dependencyInfo");
471 JsonNode vnfcData = mapper.readTree(dependencyData).get("vnfcs");
472 dependencyInfo.getVnfcs().addAll(mapper.readValue(vnfcData.toString(), new TypeReference<List<Vnfcs>>(){}));
474 log.info("Dependency Output:" + dependencyInfo.toString());
475 return dependencyInfo;
478 Capabilities getCapabilitiesData(SvcLogicContext ctx) throws SvcLogicException, IOException {
480 String fn = "FlowExecutorNode.getCapabilitiesData";
481 String capabilitiesData = dbService.getCapabilitiesData(ctx);
482 log.info(fn + "capabilitiesDataInput:" + capabilitiesData);
484 Capabilities capabilities = new Capabilities();
485 if (capabilitiesData == null) {
489 ObjectMapper mapper = new ObjectMapper();
490 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
491 mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
493 JsonNode capabilitiesNode = mapper.readTree(capabilitiesData);
494 log.info("capabilitiesNode:" + capabilitiesNode.toString());
496 capabilities.getVfModule().addAll(extractParameterList(mapper, capabilitiesNode, VF_MODULE));
497 capabilities.getVnfc().addAll(extractParameterList(mapper, capabilitiesNode, VNFC));
498 capabilities.getVnf().addAll(extractParameterList(mapper, capabilitiesNode, VNF));
499 capabilities.getVm().addAll(extractParameterList(mapper, capabilitiesNode, VM));
501 log.info("Capabilities Output:" + capabilities.toString());
506 private <T> List<T> extractParameterList(ObjectMapper mapper, JsonNode root, String parameter) throws IOException {
507 JsonNode parameterNode = root.get(parameter);
508 if (parameterNode == null) {
509 return new ArrayList<>();
511 return mapper.readValue(parameterNode.toString(), new TypeReference<List<T>>() {});
514 private Properties loadProperties() throws Exception {
515 String directory = envVariables.getenv(SDNC_CONFIG_DIR_VAR);
516 if (directory == null) {
517 throw new Exception("Cannot find Property file -" + SDNC_CONFIG_DIR_VAR);
519 String path = directory + APPC_FLOW_CONTROLLER;
520 return PropertiesLoader.load(path);