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.
18 * ============LICENSE_END=========================================================
21 package org.onap.appc.flow.controller.node;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.nio.charset.Charset;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.HashMap;
30 import java.util.List;
32 import java.util.Properties;
34 import org.apache.commons.io.IOUtils;
35 import org.apache.commons.lang3.StringUtils;
36 import org.onap.appc.flow.controller.ResponseHandlerImpl.DefaultResponseHandler;
37 import org.onap.appc.flow.controller.data.PrecheckOption;
38 import org.onap.appc.flow.controller.data.ResponseAction;
39 import org.onap.appc.flow.controller.data.Transaction;
40 import org.onap.appc.flow.controller.data.Transactions;
41 import org.onap.appc.flow.controller.dbervices.FlowControlDBService;
42 import org.onap.appc.flow.controller.executorImpl.GraphExecutor;
43 import org.onap.appc.flow.controller.executorImpl.NodeExecutor;
44 import org.onap.appc.flow.controller.executorImpl.RestExecutor;
45 import org.onap.appc.flow.controller.interfaceData.ActionIdentifier;
46 import org.onap.appc.flow.controller.interfaceData.Capabilities;
47 import org.onap.appc.flow.controller.interfaceData.DependencyInfo;
48 import org.onap.appc.flow.controller.interfaceData.Input;
49 import org.onap.appc.flow.controller.interfaceData.InventoryInfo;
50 import org.onap.appc.flow.controller.interfaceData.RequestInfo;
51 import org.onap.appc.flow.controller.interfaceData.Vm;
52 import org.onap.appc.flow.controller.interfaceData.VnfInfo;
53 import org.onap.appc.flow.controller.interfaceData.Vnfcs;
54 import org.onap.appc.flow.controller.interfaceData.Vnfcslist;
55 import org.onap.appc.flow.controller.interfaces.FlowExecutorInterface;
56 import org.onap.appc.flow.controller.utils.FlowControllerConstants;
57 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
58 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
59 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
61 import com.att.eelf.configuration.EELFLogger;
62 import com.att.eelf.configuration.EELFManager;
63 import com.fasterxml.jackson.annotation.JsonInclude.Include;
64 import com.fasterxml.jackson.core.JsonParseException;
65 import com.fasterxml.jackson.databind.DeserializationFeature;
66 import com.fasterxml.jackson.databind.JsonMappingException;
67 import com.fasterxml.jackson.databind.JsonNode;
68 import com.fasterxml.jackson.databind.ObjectMapper;
69 import com.fasterxml.jackson.databind.SerializationFeature;
71 public class FlowControlNode implements SvcLogicJavaPlugin{
74 private static final EELFLogger log = EELFManager.getInstance().getLogger(FlowControlNode.class);
75 private static final String SDNC_CONFIG_DIR_VAR = "SDNC_CONFIG_DIR";
77 public void processFlow(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
78 log.debug("Received processParamKeys call with params : " + inParams);
79 String responsePrefix = inParams.get(FlowControllerConstants.INPUT_PARAM_RESPONSE_PRIFIX);
82 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
83 FlowControlDBService dbservice = FlowControlDBService.initialise();
84 SvcLogicContext localContext = new SvcLogicContext();
85 localContext.setAttribute(FlowControllerConstants.REQUEST_ID, ctx.getAttribute(FlowControllerConstants.REQUEST_ID));
86 localContext.setAttribute(FlowControllerConstants.VNF_TYPE, ctx.getAttribute(FlowControllerConstants.VNF_TYPE));
87 localContext.setAttribute(FlowControllerConstants.REQUEST_ACTION, ctx.getAttribute(FlowControllerConstants.REQUEST_ACTION));
88 localContext.setAttribute(FlowControllerConstants.ACTION_LEVEL, ctx.getAttribute(FlowControllerConstants.ACTION_LEVEL));
89 localContext.setAttribute(FlowControllerConstants.RESPONSE_PREFIX, responsePrefix);
90 ctx.setAttribute(FlowControllerConstants.RESPONSE_PREFIX, responsePrefix);
91 dbservice.getFlowReferenceData(ctx, inParams, localContext);
93 for (Object key : localContext.getAttributeKeySet()) {
94 String parmName = (String) key;
95 String parmValue = ctx.getAttribute(parmName);
96 log.debug("processFlow " + parmName + "=" + parmValue);
99 processFlowSequence(inParams, ctx, localContext);
100 if(!ctx.getAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_STATUS).equals(FlowControllerConstants.OUTPUT_STATUS_SUCCESS))
101 throw new SvcLogicException(ctx.getAttribute(responsePrefix + FlowControllerConstants.OUTPUT_STATUS_MESSAGE));
103 } catch (Exception e) {
104 ctx.setAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_STATUS, FlowControllerConstants.OUTPUT_STATUS_FAILURE);
105 ctx.setAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
107 throw new SvcLogicException(e.getMessage());
111 private void processFlowSequence( Map<String, String> inParams, SvcLogicContext ctx, SvcLogicContext localContext) throws Exception
113 String fn = "FlowExecutorNode.processflowSequence";
114 log.debug(fn + "Received model for flow : " + localContext.toString());
115 FlowControlDBService dbservice = FlowControlDBService.initialise();
116 String flowSequnce =null;
117 for (Object key : localContext.getAttributeKeySet()) {
118 String parmName = (String) key;
119 String parmValue = ctx.getAttribute(parmName);
120 log.debug(parmName + "=" + parmValue);
123 if(localContext != null && localContext.getAttribute(FlowControllerConstants.SEQUENCE_TYPE) !=null){
125 if(localContext.getAttribute(FlowControllerConstants.GENERATION_NODE) != null){
126 GraphExecutor transactionExecutor = new GraphExecutor();
127 Boolean generatorExists = transactionExecutor.hasGraph("APPC_COMMOM", localContext.getAttribute(FlowControllerConstants.GENERATION_NODE), null, "sync");
129 flowSequnce = transactionExecutor.executeGraph("APPC_COMMOM", localContext.getAttribute(FlowControllerConstants.GENERATION_NODE),
130 null, "sync", null).getProperty(FlowControllerConstants.FLOW_SEQUENCE);
133 throw new Exception("Can not find Custom defined Flow Generator for " + localContext.getAttribute(FlowControllerConstants.GENERATION_NODE));
135 else if(((String) localContext.getAttribute(FlowControllerConstants.SEQUENCE_TYPE)).equalsIgnoreCase(FlowControllerConstants.DESINGTIME)){
136 localContext.setAttribute(FlowControllerConstants.VNFC_TYPE, ctx.getAttribute(FlowControllerConstants.VNFC_TYPE));
137 flowSequnce = dbservice.getDesignTimeFlowModel(localContext);
138 if(flowSequnce == null)
139 throw new Exception("Flow Sequence is not found User Desinged VNF " + ctx.getAttribute(FlowControllerConstants.VNF_TYPE));
141 else if(((String) localContext.getAttribute(FlowControllerConstants.SEQUENCE_TYPE)).equalsIgnoreCase(FlowControllerConstants.RUNTIME)){
143 Transaction transaction = new Transaction();
144 String input = collectInputParams(ctx,transaction);
145 log.info("collectInputParamsData" + input );
147 RestExecutor restExe = new RestExecutor();
148 HashMap<String,String>flowSeq= restExe.execute(transaction, localContext);
149 flowSequnce=flowSeq.get("restResponse");
151 if(flowSequnce == null)
152 throw new Exception("Failed to get the Flow Sequece runtime for VNF type" + ctx.getAttribute(FlowControllerConstants.VNF_TYPE));
155 else if(((String) localContext.getAttribute(FlowControllerConstants.SEQUENCE_TYPE)).equalsIgnoreCase(FlowControllerConstants.EXTERNAL)){
156 //String input = collectInputParams(localContext);
157 // flowSequnce = ""; //get it from the External interface calling the Rest End point - TBD
158 if(flowSequnce == null)
159 throw new Exception("Flow Sequence not found for " + ctx.getAttribute(FlowControllerConstants.VNF_TYPE));
163 //No other type of model supported...in Future can get flowModel from other generators which will be included here
164 throw new Exception("No information found for sequence Owner Design-Time Vs Run-Time" );
168 FlowGenerator flowGenerator = new FlowGenerator();
169 Transactions trans = flowGenerator.createSingleStepModel(inParams,ctx);
170 ObjectMapper mapper = new ObjectMapper();
171 flowSequnce = mapper.writeValueAsString(trans);
172 log.debug("Single step Flow Sequence : " + flowSequnce);
175 log.debug("Received Flow Sequence : " + flowSequnce);
177 HashMap<Integer, Transaction> transactionMap = createTransactionMap(flowSequnce, localContext);
178 exeuteAllTransaction(transactionMap, ctx);
179 log.info("Executed all the transacstion successfully");
183 private void exeuteAllTransaction(HashMap<Integer, Transaction> transactionMap, SvcLogicContext ctx) throws Exception {
185 String fn = "FlowExecutorNode.exeuteAllTransaction ";
187 FlowExecutorInterface flowExecutor = null;
188 for (int key = 1; key <= transactionMap.size() ; key++ )
190 log.debug(fn + "Starting transactions ID " + key + " :)=" + retry);
191 Transaction transaction = transactionMap.get(key);
192 if(!preProcessor(transactionMap, transaction)){
193 log.info("Skipping Transaction ID " + transaction.getTransactionId());
196 if(transaction.getExecutionType() != null){
197 switch (transaction.getExecutionType()){
198 case FlowControllerConstants.GRAPH :
199 flowExecutor = new GraphExecutor();
201 case FlowControllerConstants.NODE :
202 flowExecutor = new NodeExecutor();
204 case FlowControllerConstants.REST :
205 flowExecutor = new RestExecutor();
208 throw new Exception("No Executor found for transaction ID" + transaction.getTransactionId());
210 flowExecutor.execute(transaction, ctx);
211 ResponseAction responseAction= handleResponse(transaction);
213 if(responseAction.getWait() != null && Integer.parseInt(responseAction.getWait()) > 0){
214 log.debug(fn + "Going to Sleep .... " + responseAction.getWait());
215 Thread.sleep(Integer.parseInt(responseAction.getWait())*1000);
218 if(responseAction.isIntermediateMessage()){
219 log.debug(fn + "Sending Intermediate Message back .... ");
220 sendIntermediateMessage();
222 if(responseAction.getRetry() != null && Integer.parseInt(responseAction.getRetry()) > retry ){
223 log.debug(fn + "Ooppss!!! We will retry again ....... ");
226 log.debug(fn + "key =" + key + "retry =" + retry);
229 if(responseAction.isIgnore()){
230 log.debug(fn + "Ignoring this Error and moving ahead ....... ");
233 if(responseAction.isStop()){
234 log.debug(fn + "Need to Stop ....... ");
237 if(responseAction.getJump() != null && Integer.parseInt(responseAction.getJump()) > 0 ){
238 key = Integer.parseInt(responseAction.getJump());
241 log.debug(fn + "key =" + key + "retry =" + retry);
245 throw new Exception("Don't know how to execute transaction ID " + transaction.getTransactionId());
250 private void sendIntermediateMessage() {
251 // TODO Auto-generated method stub
255 private ResponseAction handleResponse(Transaction transaction) {
256 log.info("Handling Response for transaction Id " + transaction.getTransactionId());
257 DefaultResponseHandler defaultHandler = new DefaultResponseHandler();
258 return defaultHandler.handlerResponse(transaction);
261 private boolean preProcessor(HashMap<Integer, Transaction> transactionMap, Transaction transaction) throws IOException {
263 log.debug("Starting Preprocessing Logic ");
264 boolean runthisStep = false;
266 if(transaction.getPrecheck() != null && transaction.getPrecheck().getPrecheckOptions() != null
267 && !transaction.getPrecheck().getPrecheckOptions().isEmpty()){
268 List<PrecheckOption> precheckOptions = transaction.getPrecheck().getPrecheckOptions();
269 for(PrecheckOption precheck : precheckOptions){
270 Transaction trans = transactionMap.get(precheck.getpTransactionID());
271 ObjectMapper mapper = new ObjectMapper();
272 log.info("Mapper= " + mapper.writeValueAsString(trans));
273 HashMap<Object, Object> trmap = mapper.readValue(mapper.writeValueAsString(trans), HashMap.class);
274 if(trmap.get(precheck.getParamName()) != null &&
275 ((String) trmap.get(precheck.getParamName())).equalsIgnoreCase(precheck.getParamValue()))
280 if(transaction.getPrecheck().getPrecheckOperator() != null &&
281 transaction.getPrecheck().getPrecheckOperator().equalsIgnoreCase("any") && runthisStep)
287 log.debug("No Pre check defined for transaction ID " + transaction.getTransactionId());
297 log.debug("Returing process current Transaction = " + runthisStep);
302 private HashMap<Integer, Transaction> createTransactionMap(String flowSequnce, SvcLogicContext localContext) throws SvcLogicException, JsonParseException, JsonMappingException, IOException {
303 ObjectMapper mapper = new ObjectMapper();
304 Transactions transactions = mapper.readValue(flowSequnce,Transactions.class);
305 HashMap<Integer, Transaction> transMap = new HashMap<Integer, Transaction>();
306 for(Transaction transaction : transactions.getTransactions()){
307 compileFlowDependencies(transaction, localContext);
308 //loadTransactionIntoStatus(transactions, ctx); //parse the Transactions Object and create records in process_flow_status table
309 transMap.put(transaction.getTransactionId(), transaction);
314 private void compileFlowDependencies(Transaction transaction, SvcLogicContext localContext) throws SvcLogicException, JsonParseException, JsonMappingException, IOException {
316 String fn = "FlowExecutorNode.compileFlowDependencies";
317 FlowControlDBService dbservice = FlowControlDBService.initialise();
318 dbservice.populateModuleAndRPC(transaction, localContext.getAttribute(FlowControllerConstants.VNF_TYPE));
319 ObjectMapper mapper = new ObjectMapper();
320 log.debug("Indivisual Transaction Details :" + transaction.toString());
321 if((localContext.getAttribute(FlowControllerConstants.SEQUENCE_TYPE) == null) ||
322 ( localContext.getAttribute(FlowControllerConstants.SEQUENCE_TYPE) != null &&
323 ! localContext.getAttribute(FlowControllerConstants.SEQUENCE_TYPE).equalsIgnoreCase(FlowControllerConstants.DESINGTIME))){
324 localContext.setAttribute("artifact-content", mapper.writeValueAsString(transaction));
325 dbservice.loadSequenceIntoDB(localContext);
327 //get a field in transction class as transactionhandle interface and register the Handler here for each trnactions
330 private String collectInputParams(SvcLogicContext ctx, Transaction transaction) throws Exception {
332 String fn = "FlowExecuteNode.collectInputParams";
333 Properties prop = loadProperties();
334 log.info("Loaded Properties " + prop.toString());
336 String vnfId = ctx.getAttribute(FlowControllerConstants.VNF_ID);
337 log.debug(fn + "vnfId :" + vnfId);
339 if (StringUtils.isBlank(vnfId)) {
340 throw new Exception("VnfId is missing");
343 ActionIdentifier actionIdentifier = new ActionIdentifier();
344 actionIdentifier.setVnfId(vnfId);
345 actionIdentifier.setVserverId(ctx.getAttribute(FlowControllerConstants.VSERVER_ID));
346 actionIdentifier.setVnfcName(ctx.getAttribute(FlowControllerConstants.VNFC_NAME));
348 RequestInfo requestInfo = new RequestInfo();
349 requestInfo.setAction(ctx.getAttribute(FlowControllerConstants.ACTION));
350 requestInfo.setActionLevel(ctx.getAttribute(FlowControllerConstants.ACTION_LEVEL));
351 requestInfo.setPayload(ctx.getAttribute(FlowControllerConstants.PAYLOAD));
352 requestInfo.setActionIdentifier(actionIdentifier);
354 InventoryInfo inventoryInfo = getInventoryInfo(ctx,vnfId);
355 DependencyInfo dependencyInfo = getDependencyInfo(ctx);
356 Capabilities capabilites = getCapabilitesData(ctx);
358 Input input = new Input();
359 input.setRequestInfo(requestInfo);
360 input.setInventoryInfo(inventoryInfo);
361 input.setDependencyInfo(dependencyInfo);
362 input.setCapabilities(capabilites);
363 //input.setTunableParameters(null);
366 log.info(fn + "Input parameters:" + input.toString());
368 String inputData = null;
370 ObjectMapper mapper = new ObjectMapper();
371 mapper.setSerializationInclusion(Include.NON_NULL);
372 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE,true);
373 inputData = mapper.writeValueAsString(input);
374 log.info("InputDataJson:"+inputData);
376 } catch (Exception e) {
380 String resourceUri = prop.getProperty(FlowControllerConstants.SEQ_GENERATOR_URL);
381 log.info(fn + "resourceUri= " + resourceUri);
383 transaction.setPayload(inputData);
384 transaction.setExecutionRPC("POST");
385 transaction.setuId(prop.getProperty(FlowControllerConstants.SEQ_GENERATOR_UID));
386 transaction.setPswd(prop.getProperty(FlowControllerConstants.SEQ_GENERATOR_PWD));
387 transaction.setExecutionEndPoint(resourceUri);
393 private DependencyInfo getDependencyInfo(SvcLogicContext ctx) throws Exception {
395 String fn = "FlowExecutorNode.getDependencyInfo";
396 DependencyInfo dependencyInfo = new DependencyInfo();
397 FlowControlDBService dbservice = FlowControlDBService.initialise();
398 String dependencyData = dbservice.getDependencyInfo(ctx);
399 log.info(fn + "dependencyDataInput:" + dependencyData);
401 if (dependencyData != null) {
402 ObjectMapper mapper = new ObjectMapper();
403 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
404 mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
405 JsonNode dependencyInfoData = mapper.readTree(dependencyData).get("dependencyInfo");
406 JsonNode vnfcData = mapper.readTree(dependencyInfoData.toString()).get("vnfcs");
407 List<Vnfcs> vnfclist = Arrays.asList(mapper.readValue(vnfcData.toString(), Vnfcs[].class));
408 dependencyInfo.getVnfcs().addAll(vnfclist);
410 log.info("Dependency Output:"+ dependencyInfo.toString());
413 return dependencyInfo;
417 private Capabilities getCapabilitesData(SvcLogicContext ctx)throws Exception {
419 String fn = "FlowExecutorNode.getCapabilitesData";
420 Capabilities capabilities = new Capabilities();
421 FlowControlDBService dbservice = FlowControlDBService.initialise();
422 String capabilitiesData = dbservice.getCapabilitiesData(ctx);
423 log.info(fn + "capabilitiesDataInput:" + capabilitiesData);
425 if (capabilitiesData != null) {
426 ObjectMapper mapper = new ObjectMapper();
427 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
428 mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
429 JsonNode capabilitiesNode = mapper.readValue(capabilitiesData,JsonNode.class);
430 log.info("capabilitiesNode:" + capabilitiesNode.toString());
432 JsonNode vnfs = capabilitiesNode.findValue(FlowControllerConstants.VNF);
433 List<String> vnfsList = new ArrayList<String>();
435 for (int i = 0; i < vnfs.size(); i++) {
437 String vnf = vnfs.get(i).asText();
442 JsonNode vfModules = capabilitiesNode.findValue(FlowControllerConstants.VF_MODULE);
443 List<String> vfModulesList = new ArrayList<String>();
444 if (vfModules != null) {
445 for (int i = 0; i < vfModules.size(); i++) {
447 String vfModule = vfModules.get(i).asText();
448 vfModulesList.add(vfModule);
452 JsonNode vnfcs = capabilitiesNode.findValue(FlowControllerConstants.VNFC);
453 List<String> vnfcsList = new ArrayList<String>();
455 for (int i = 0; i < vnfcs.size(); i++) {
457 String vnfc1 = vnfcs.get(i).asText();
458 vnfcsList.add(vnfc1);
462 JsonNode vms = capabilitiesNode.findValue(FlowControllerConstants.VM);
464 List<String> vmList = new ArrayList<String>();
466 for (int i = 0; i < vms.size(); i++) {
468 String vm1 = vms.get(i).asText();
473 capabilities.getVnfc().addAll(vnfcsList);
474 capabilities.getVnf().addAll(vnfsList);
475 capabilities.getVfModule().addAll(vfModulesList);
476 capabilities.getVm().addAll(vmList);
478 log.info("Capabilities Output:"+ capabilities.toString());
486 private InventoryInfo getInventoryInfo(SvcLogicContext ctx, String vnfId) throws Exception{
488 String fn = "FlowExecutorNode.getInventoryInfo";
489 String vmcount = ctx.getAttribute("tmp.vnfInfo.vm-count");
490 int vmCount = Integer.parseInt(vmcount);
491 log.info(fn +"vmcount:"+ vmCount);
493 VnfInfo vnfInfo = new VnfInfo();
494 vnfInfo.setVnfId(vnfId);
495 vnfInfo.setVnfName(ctx.getAttribute("tmp.vnfInfo.vnf.vnf-name"));
496 vnfInfo.setVnfType(ctx.getAttribute("tmp.vnfInfo.vnf.vnf-type"));
499 Vnfcslist vnfc = new Vnfcslist();
503 for (int i = 0; i < vmCount; i++) {
505 vm.setVserverId(ctx.getAttribute("tmp.vnfInfo.vm[" + i + "].vserverId"));
506 String vnfccount = ctx.getAttribute("tmp.vnfInfo.vm[" + i + "].vnfc-count");
507 int vnfcCount = Integer.parseInt(vnfccount);
510 vnfc.setVnfcName(ctx.getAttribute("tmp.vnfInfo.vm[" + i + "].vnfc-name"));
511 vnfc.setVnfcType(ctx.getAttribute("tmp.vnfInfo.vm[" + i + "].vnfc-type"));
514 vnfInfo.getVm().add(vm);
518 InventoryInfo inventoryInfo = new InventoryInfo();
519 inventoryInfo.setVnfInfo(vnfInfo);
521 return inventoryInfo;
525 private String getFlowSequence() throws IOException {
527 String sequenceModel = IOUtils.toString(FlowControlNode.class.getClassLoader().getResourceAsStream("sequence.json"), Charset.defaultCharset());
533 private static Properties loadProperties() throws Exception {
534 Properties props = new Properties();
535 String propDir = System.getenv(SDNC_CONFIG_DIR_VAR);
537 throw new Exception("Cannot find Property file -" + SDNC_CONFIG_DIR_VAR);
538 String propFile = propDir + FlowControllerConstants.APPC_FLOW_CONTROLLER;
539 InputStream propStream = new FileInputStream(propFile);
542 props.load(propStream);
546 throw new Exception("Could not load properties file " + propFile, e);
556 log.warn("Could not close FileInputStream", e);