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=========================================================
22 package org.onap.appc.flow.controller.node;
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import com.fasterxml.jackson.annotation.JsonInclude.Include;
27 import com.fasterxml.jackson.databind.DeserializationFeature;
28 import com.fasterxml.jackson.databind.JsonNode;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.fasterxml.jackson.databind.SerializationFeature;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.HashMap;
37 import java.util.List;
39 import java.util.Properties;
40 import org.apache.commons.lang3.StringUtils;
41 import org.json.JSONObject;
42 import org.onap.appc.flow.controller.ResponseHandlerImpl.DefaultResponseHandler;
43 import org.onap.appc.flow.controller.data.PrecheckOption;
44 import org.onap.appc.flow.controller.data.ResponseAction;
45 import org.onap.appc.flow.controller.data.Transaction;
46 import org.onap.appc.flow.controller.data.Transactions;
47 import org.onap.appc.flow.controller.dbervices.FlowControlDBService;
48 import org.onap.appc.flow.controller.executorImpl.GraphExecutor;
49 import org.onap.appc.flow.controller.executorImpl.NodeExecutor;
50 import org.onap.appc.flow.controller.executorImpl.RestExecutor;
51 import org.onap.appc.flow.controller.interfaceData.*;
52 import org.onap.appc.flow.controller.interfaces.FlowExecutorInterface;
53 import org.onap.appc.flow.controller.utils.EncryptionTool;
54 import org.onap.appc.flow.controller.utils.FlowControllerConstants;
55 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
56 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
57 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
59 public class FlowControlNode implements SvcLogicJavaPlugin{
61 private static final EELFLogger log = EELFManager.getInstance().getLogger(FlowControlNode.class);
62 private static final String SDNC_CONFIG_DIR_VAR = "SDNC_CONFIG_DIR";
64 public void processFlow(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
65 log.debug("Received processParamKeys call with params : " + inParams);
66 String responsePrefix = inParams.get(FlowControllerConstants.INPUT_PARAM_RESPONSE_PRIFIX);
68 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
69 FlowControlDBService dbservice = FlowControlDBService.initialise();
70 SvcLogicContext localContext = new SvcLogicContext();
71 localContext.setAttribute(FlowControllerConstants.REQUEST_ID, ctx.getAttribute(FlowControllerConstants.REQUEST_ID));
72 localContext.setAttribute(FlowControllerConstants.VNF_TYPE, ctx.getAttribute(FlowControllerConstants.VNF_TYPE));
73 localContext.setAttribute(FlowControllerConstants.REQUEST_ACTION, ctx.getAttribute(FlowControllerConstants.REQUEST_ACTION));
74 localContext.setAttribute(FlowControllerConstants.ACTION_LEVEL, ctx.getAttribute(FlowControllerConstants.ACTION_LEVEL));
75 localContext.setAttribute(FlowControllerConstants.RESPONSE_PREFIX, responsePrefix);
76 ctx.setAttribute(FlowControllerConstants.RESPONSE_PREFIX, responsePrefix);
77 dbservice.getFlowReferenceData(ctx, inParams, localContext);
79 for (Object key : localContext.getAttributeKeySet()) {
80 String parmName = (String) key;
81 String parmValue = ctx.getAttribute(parmName);
82 log.debug("processFlow " + parmName + "=" + parmValue);
85 processFlowSequence(inParams, ctx, localContext);
86 if(!ctx.getAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_STATUS).equals(FlowControllerConstants.OUTPUT_STATUS_SUCCESS))
87 throw new SvcLogicException(ctx.getAttribute(responsePrefix + FlowControllerConstants.OUTPUT_STATUS_MESSAGE));
89 } catch (Exception e) {
90 ctx.setAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_STATUS, FlowControllerConstants.OUTPUT_STATUS_FAILURE);
91 ctx.setAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
92 log.error("Error occured in processFlow ", e);
93 throw new SvcLogicException(e.getMessage());
97 private void processFlowSequence(Map<String, String> inParams, SvcLogicContext ctx, SvcLogicContext localContext) throws Exception
99 String fn = "FlowExecutorNode.processflowSequence";
100 log.debug(fn + "Received model for flow : " + localContext.toString());
101 FlowControlDBService dbservice = FlowControlDBService.initialise();
102 String flowSequnce = null;
103 for (Object key : localContext.getAttributeKeySet()) {
104 String parmName = (String) key;
105 String parmValue = ctx.getAttribute(parmName);
106 log.debug(parmName + "=" + parmValue);
109 if(localContext.getAttribute(FlowControllerConstants.SEQUENCE_TYPE) !=null){
111 if(localContext.getAttribute(FlowControllerConstants.GENERATION_NODE) != null){
112 GraphExecutor transactionExecutor = new GraphExecutor();
113 Boolean generatorExists = transactionExecutor.hasGraph("APPC_COMMOM", localContext.getAttribute(FlowControllerConstants.GENERATION_NODE), null, "sync");
115 flowSequnce = transactionExecutor.executeGraph("APPC_COMMOM", localContext.getAttribute(FlowControllerConstants.GENERATION_NODE),
116 null, "sync", null).getProperty(FlowControllerConstants.FLOW_SEQUENCE);
119 throw new Exception("Can not find Custom defined Flow Generator for " + localContext.getAttribute(FlowControllerConstants.GENERATION_NODE));
121 else if((localContext.getAttribute(FlowControllerConstants.SEQUENCE_TYPE)).equalsIgnoreCase(FlowControllerConstants.DESINGTIME)){
122 localContext.setAttribute(FlowControllerConstants.VNFC_TYPE, ctx.getAttribute(FlowControllerConstants.VNFC_TYPE));
123 flowSequnce = dbservice.getDesignTimeFlowModel(localContext);
124 if(flowSequnce == null)
125 throw new Exception("Flow Sequence is not found User Desinged VNF " + ctx.getAttribute(FlowControllerConstants.VNF_TYPE));
127 else if((localContext.getAttribute(FlowControllerConstants.SEQUENCE_TYPE)).equalsIgnoreCase(FlowControllerConstants.RUNTIME)){
129 Transaction transaction = new Transaction();
130 String input = collectInputParams(ctx,transaction);
131 log.info("CollectInputParamsData-Input: " + input );
133 RestExecutor restExe = new RestExecutor();
134 Map<String,String>flowSeq= restExe.execute(transaction, localContext);
136 JSONObject sequence = new JSONObject(flowSeq.get("restResponse"));
137 if(sequence.has("output"))
138 flowSequnce = sequence.getJSONObject("output").toString();
139 log.info("MultistepSequenceGenerator-Output: "+flowSequnce);
141 if(flowSequnce == null)
142 throw new Exception("Failed to get the Flow Sequece runtime for VNF type" + ctx.getAttribute(FlowControllerConstants.VNF_TYPE));
145 else if((localContext.getAttribute(FlowControllerConstants.SEQUENCE_TYPE)).equalsIgnoreCase(FlowControllerConstants.EXTERNAL)){
146 //String input = collectInputParams(localContext);
147 // flowSequnce = ""; //get it from the External interface calling the Rest End point - TBD
148 //if(flowSequnce == null)
149 throw new Exception("Flow Sequence not found for " + ctx.getAttribute(FlowControllerConstants.VNF_TYPE));
153 //No other type of model supported...in Future can get flowModel from other generators which will be included here
154 throw new Exception("No information found for sequence Owner Design-Time Vs Run-Time" );
158 FlowGenerator flowGenerator = new FlowGenerator();
159 Transactions trans = flowGenerator.createSingleStepModel(inParams,ctx);
160 ObjectMapper mapper = new ObjectMapper();
161 flowSequnce = mapper.writeValueAsString(trans);
162 log.debug("Single step Flow Sequence : " + flowSequnce);
164 log.debug("Received Flow Sequence : " + flowSequnce);
166 HashMap<Integer, Transaction> transactionMap = createTransactionMap(flowSequnce, localContext);
167 exeuteAllTransaction(transactionMap, ctx);
168 log.info("Executed all the transacstion successfully");
171 private void exeuteAllTransaction(HashMap<Integer, Transaction> transactionMap, SvcLogicContext ctx) throws Exception {
173 String fn = "FlowExecutorNode.exeuteAllTransaction ";
175 FlowExecutorInterface flowExecutor;
176 for (int key = 1; key <= transactionMap.size() ; key++)
178 log.debug(fn + "Starting transactions ID " + key + " :)=" + retry);
179 Transaction transaction = transactionMap.get(key);
180 if(!preProcessor(transactionMap, transaction)){
181 log.info("Skipping Transaction ID " + transaction.getTransactionId());
184 if(transaction.getExecutionType() != null){
185 switch (transaction.getExecutionType()){
186 case FlowControllerConstants.GRAPH :
187 flowExecutor = new GraphExecutor();
189 case FlowControllerConstants.NODE :
190 flowExecutor = new NodeExecutor();
192 case FlowControllerConstants.REST :
193 flowExecutor = new RestExecutor();
196 throw new Exception("No Executor found for transaction ID" + transaction.getTransactionId());
198 flowExecutor.execute(transaction, ctx);
199 ResponseAction responseAction= handleResponse(transaction);
201 if(responseAction.getWait() != null && Integer.parseInt(responseAction.getWait()) > 0){
202 log.debug(fn + "Going to Sleep .... " + responseAction.getWait());
203 Thread.sleep(Integer.parseInt(responseAction.getWait())*1000L);
206 if(responseAction.isIntermediateMessage()){
207 log.debug(fn + "Sending Intermediate Message back .... ");
208 sendIntermediateMessage();
210 if(responseAction.getRetry() != null && Integer.parseInt(responseAction.getRetry()) > retry ){
211 log.debug(fn + "Ooppss!!! We will retry again ....... ");
214 log.debug(fn + "key =" + key + "retry =" + retry);
217 if(responseAction.isIgnore()){
218 log.debug(fn + "Ignoring this Error and moving ahead ....... ");
221 if(responseAction.isStop()){
222 log.debug(fn + "Need to Stop ....... ");
225 if(responseAction.getJump() != null && Integer.parseInt(responseAction.getJump()) > 0 ){
226 key = Integer.parseInt(responseAction.getJump());
229 log.debug(fn + "key =" + key + "retry =" + retry);
232 throw new Exception("Don't know how to execute transaction ID " + transaction.getTransactionId());
237 private void sendIntermediateMessage() {
238 // TODO Auto-generated method stub
241 private ResponseAction handleResponse(Transaction transaction) {
242 log.info("Handling Response for transaction Id " + transaction.getTransactionId());
243 DefaultResponseHandler defaultHandler = new DefaultResponseHandler();
244 return defaultHandler.handlerResponse(transaction);
247 private boolean preProcessor(HashMap<Integer, Transaction> transactionMap, Transaction transaction) throws IOException {
248 log.debug("Starting Preprocessing Logic ");
249 boolean runthisStep = false;
251 if(transaction.getPrecheck() != null && transaction.getPrecheck().getPrecheckOptions() != null
252 && !transaction.getPrecheck().getPrecheckOptions().isEmpty()){
253 List<PrecheckOption> precheckOptions = transaction.getPrecheck().getPrecheckOptions();
254 for(PrecheckOption precheck : precheckOptions){
255 Transaction trans = transactionMap.get(precheck.getpTransactionID());
256 ObjectMapper mapper = new ObjectMapper();
257 log.info("Mapper= " + mapper.writeValueAsString(trans));
258 HashMap trmap = mapper.readValue(mapper.writeValueAsString(trans), HashMap.class);
259 if(trmap.get(precheck.getParamName()) != null &&
260 ((String) trmap.get(precheck.getParamName())).equalsIgnoreCase(precheck.getParamValue()))
265 if(("any").equalsIgnoreCase(transaction.getPrecheck().getPrecheckOperator()) && runthisStep)
269 log.debug("No Pre check defined for transaction ID " + transaction.getTransactionId());
275 log.error("Error occured when Preprocessing Logic ", e);
278 log.debug("Returing process current Transaction = " + runthisStep);
283 private HashMap<Integer, Transaction> createTransactionMap(String flowSequnce, SvcLogicContext localContext) throws Exception {
284 ObjectMapper mapper = new ObjectMapper();
285 Transactions transactions = mapper.readValue(flowSequnce,Transactions.class);
286 HashMap<Integer, Transaction> transMap = new HashMap<>();
287 for(Transaction transaction : transactions.getTransactions()){
288 compileFlowDependencies(transaction, localContext);
289 //loadTransactionIntoStatus(transactions, ctx); //parse the Transactions Object and create records in process_flow_status table
290 transMap.put(transaction.getTransactionId(), transaction);
295 private void compileFlowDependencies(Transaction transaction, SvcLogicContext localContext) throws Exception {
296 FlowControlDBService dbservice = FlowControlDBService.initialise();
297 dbservice.populateModuleAndRPC(transaction, localContext.getAttribute(FlowControllerConstants.VNF_TYPE));
298 ObjectMapper mapper = new ObjectMapper();
299 log.debug("Indivisual Transaction Details :" + transaction.toString());
300 if((localContext.getAttribute(FlowControllerConstants.SEQUENCE_TYPE) == null) ||
301 (localContext.getAttribute(FlowControllerConstants.SEQUENCE_TYPE) != null &&
302 ! localContext.getAttribute(FlowControllerConstants.SEQUENCE_TYPE).equalsIgnoreCase(FlowControllerConstants.DESINGTIME))){
303 localContext.setAttribute("artifact-content", mapper.writeValueAsString(transaction));
304 dbservice.loadSequenceIntoDB(localContext);
306 //get a field in transction class as transactionhandle interface and register the Handler here for each trnactions
309 private String collectInputParams(SvcLogicContext ctx,Transaction transaction) throws Exception {
311 String fn = "FlowExecuteNode.collectInputParams";
312 Properties prop = loadProperties();
313 log.info("Loaded Properties " + prop.toString());
315 String vnfId = ctx.getAttribute(FlowControllerConstants.VNF_ID);
316 String inputData = null;
317 log.debug(fn + "vnfId :" + vnfId);
319 if (StringUtils.isBlank(vnfId)) {
320 throw new Exception("VnfId is missing");
325 ActionIdentifier actionIdentifier = new ActionIdentifier();
326 log.debug("Enter ActionIdentifier");
327 if (StringUtils.isNotBlank(vnfId)) {
328 actionIdentifier.setVnfId(vnfId);
330 if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.VSERVER_ID))) {
331 actionIdentifier.setVserverId(ctx.getAttribute(FlowControllerConstants.VSERVER_ID));
333 if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.VNFC_NAME))) {
334 actionIdentifier.setVnfcName(ctx.getAttribute(FlowControllerConstants.VNFC_NAME));
336 log.info("ActionIdentifierData" + actionIdentifier.toString());
338 RequestInfo requestInfo = new RequestInfo();
339 log.info("Enter RequestInfo");
340 requestInfo.setAction(ctx.getAttribute(FlowControllerConstants.REQUEST_ACTION));
341 requestInfo.setActionLevel(ctx.getAttribute(FlowControllerConstants.ACTION_LEVEL));
342 requestInfo.setPayload(ctx.getAttribute(FlowControllerConstants.PAYLOAD));
343 requestInfo.setActionIdentifier(actionIdentifier);
344 log.debug("RequestInfo: " + requestInfo.toString());
346 InventoryInfo inventoryInfo = getInventoryInfo(ctx, vnfId);
347 DependencyInfo dependencyInfo = getDependencyInfo(ctx);
348 Capabilities capabilites = getCapabilitesData(ctx);
350 Input input = new Input();
351 log.info("Enter InputData");
352 input.setRequestInfo(requestInfo);
353 input.setInventoryInfo(inventoryInfo);
354 input.setDependencyInfo(dependencyInfo);
355 input.setCapabilities(capabilites);
356 log.info(fn + "Input parameters:" + input.toString());
358 ObjectMapper mapper = new ObjectMapper();
359 mapper.setSerializationInclusion(Include.NON_NULL);
360 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
361 inputData = mapper.writeValueAsString(input);
362 log.info("InputDataJson:" + inputData);
364 } catch (Exception e) {
365 log.error("Error occured in " + fn, e);
368 String resourceUri = prop.getProperty(FlowControllerConstants.SEQ_GENERATOR_URL);
369 log.info(fn + "resourceUri= " + resourceUri);
371 EncryptionTool et = EncryptionTool.getInstance();
372 String pass = et.decrypt(prop.getProperty(FlowControllerConstants.SEQ_GENERATOR_PWD));
374 transaction.setPayload(inputData);
375 transaction.setExecutionRPC("POST");
376 transaction.setuId(prop.getProperty(FlowControllerConstants.SEQ_GENERATOR_UID));
377 transaction.setPswd(pass);
378 transaction.setExecutionEndPoint(resourceUri);
383 private DependencyInfo getDependencyInfo(SvcLogicContext ctx) throws Exception {
385 String fn = "FlowExecutorNode.getDependencyInfo";
386 DependencyInfo dependencyInfo = new DependencyInfo();
387 FlowControlDBService dbservice = FlowControlDBService.initialise();
388 String dependencyData = dbservice.getDependencyInfo(ctx);
389 log.info(fn + "dependencyDataInput:" + dependencyData);
391 if (dependencyData != null) {
392 ObjectMapper mapper = new ObjectMapper();
393 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
394 mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
395 //JsonNode dependencyInfoData = mapper.readTree(dependencyData).get("dependencyInfo");
396 JsonNode vnfcData = mapper.readTree(dependencyData).get("vnfcs");
397 List<Vnfcs> vnfclist = Arrays.asList(mapper.readValue(vnfcData.toString(), Vnfcs[].class));
398 dependencyInfo.getVnfcs().addAll(vnfclist);
400 log.info("Dependency Output:"+ dependencyInfo.toString());
402 return dependencyInfo;
405 private Capabilities getCapabilitesData(SvcLogicContext ctx) throws Exception {
407 String fn = "FlowExecutorNode.getCapabilitesData";
408 Capabilities capabilities = new Capabilities();
409 FlowControlDBService dbservice = FlowControlDBService.initialise();
410 String capabilitiesData = dbservice.getCapabilitiesData(ctx);
411 log.info(fn + "capabilitiesDataInput:" + capabilitiesData);
413 if (capabilitiesData != null) {
414 ObjectMapper mapper = new ObjectMapper();
415 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
416 mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
417 JsonNode capabilitiesNode = mapper.readValue(capabilitiesData,JsonNode.class);
418 log.info("capabilitiesNode:" + capabilitiesNode.toString());
420 JsonNode vnfs = capabilitiesNode.findValue(FlowControllerConstants.VNF);
421 List<String> vnfsList = new ArrayList<>();
423 for (int i = 0; i < vnfs.size(); i++) {
425 String vnf = vnfs.get(i).asText();
430 JsonNode vfModules = capabilitiesNode.findValue(FlowControllerConstants.VF_MODULE);
431 List<String> vfModulesList = new ArrayList<>();
432 if (vfModules != null) {
433 for (int i = 0; i < vfModules.size(); i++) {
435 String vfModule = vfModules.get(i).asText();
436 vfModulesList.add(vfModule);
440 JsonNode vnfcs = capabilitiesNode.findValue(FlowControllerConstants.VNFC);
441 List<String> vnfcsList = new ArrayList<>();
443 for (int i = 0; i < vnfcs.size(); i++) {
445 String vnfc1 = vnfcs.get(i).asText();
446 vnfcsList.add(vnfc1);
450 JsonNode vms = capabilitiesNode.findValue(FlowControllerConstants.VM);
452 List<String> vmList = new ArrayList<>();
454 for (int i = 0; i < vms.size(); i++) {
456 String vm1 = vms.get(i).asText();
461 capabilities.getVnfc().addAll(vnfcsList);
462 capabilities.getVnf().addAll(vnfsList);
463 capabilities.getVfModule().addAll(vfModulesList);
464 capabilities.getVm().addAll(vmList);
466 log.info("Capabilities Output:"+ capabilities.toString());
471 private InventoryInfo getInventoryInfo(SvcLogicContext ctx, String vnfId) throws Exception{
472 String fn = "FlowExecutorNode.getInventoryInfo";
474 VnfInfo vnfInfo = new VnfInfo();
475 vnfInfo.setVnfId(vnfId);
476 vnfInfo.setVnfName(ctx.getAttribute("tmp.vnfInfo.vnf.vnf-name"));
477 vnfInfo.setVnfType(ctx.getAttribute("tmp.vnfInfo.vnf.vnf-type"));
479 String vmcount = ctx.getAttribute("tmp.vnfInfo.vm-count");
480 if(StringUtils.isNotBlank(vmcount)){
481 int vmCount = Integer.parseInt(vmcount);
482 log.info(fn +"vmcount:"+ vmCount);
485 Vnfcslist vnfc = new Vnfcslist();
486 for (int i = 0; i < vmCount; i++) {
488 vm.setVserverId(ctx.getAttribute("tmp.vnfInfo.vm[" + i + "].vserver-id"));
489 String vnfccount = ctx.getAttribute("tmp.vnfInfo.vm[" + i + "].vnfc-count");
490 int vnfcCount = Integer.parseInt(vnfccount);
493 vnfc.setVnfcName(ctx.getAttribute("tmp.vnfInfo.vm[" + i + "].vnfc-name"));
494 vnfc.setVnfcType(ctx.getAttribute("tmp.vnfInfo.vm[" + i + "].vnfc-type"));
497 vnfInfo.getVm().add(vm);
500 InventoryInfo inventoryInfo = new InventoryInfo();
501 inventoryInfo.setVnfInfo(vnfInfo);
502 log.info(fn + "Inventory Output:" +inventoryInfo.toString());
504 return inventoryInfo;
507 private static Properties loadProperties() throws Exception {
508 Properties props = new Properties();
509 String propDir = System.getenv(SDNC_CONFIG_DIR_VAR);
511 throw new Exception("Cannot find Property file -" + SDNC_CONFIG_DIR_VAR);
512 String propFile = propDir + FlowControllerConstants.APPC_FLOW_CONTROLLER;
513 try (InputStream propStream = new FileInputStream(propFile))
515 props.load(propStream);
519 throw new Exception("Could not load properties file " + propFile, e);