2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Nokia. 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=========================================================
20 package org.onap.appc.flow.controller.node;
22 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.DESINGTIME;
23 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.EXTERNAL;
24 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.FLOW_SEQUENCE;
25 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.GENERATION_NODE;
26 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.RUNTIME;
27 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.SEQUENCE_TYPE;
28 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNFC_TYPE;
29 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNF_TYPE;
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
33 import com.fasterxml.jackson.databind.ObjectMapper;
35 import org.json.JSONObject;
36 import org.onap.appc.flow.controller.data.Transaction;
37 import org.onap.appc.flow.controller.data.Transactions;
38 import org.onap.appc.flow.controller.dbervices.FlowControlDBService;
39 import org.onap.appc.flow.controller.executorImpl.GraphExecutor;
40 import org.onap.appc.flow.controller.executorImpl.RestExecutor;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
44 * Helper class for FlowControlNode
46 class FlowSequenceGenerator {
48 private static final EELFLogger log = EELFManager.getInstance().getLogger(FlowSequenceGenerator.class);
50 static final String MODULE = "APPC_COMMOM";
52 private final FlowControlDBService dbService;
53 private final FlowGenerator flowGenerator;
54 private final GraphExecutor transactionExecutor;
55 private final RestExecutor restExecutor;
56 private final InputParamsCollector inputParamsCollector;
58 FlowSequenceGenerator() {
59 this.dbService = FlowControlDBService.initialise();
60 this.flowGenerator = new FlowGenerator();
61 this.transactionExecutor = new GraphExecutor();
62 this.restExecutor = new RestExecutor();
63 this.inputParamsCollector = new InputParamsCollector();
67 * Constructor for tests, prefer to use default one
69 FlowSequenceGenerator(FlowControlDBService dbService, FlowGenerator flowGenerator,
70 GraphExecutor graphExecutor, RestExecutor restExecutor, EnvVariables envVariables) {
71 this.dbService = dbService;
72 this.flowGenerator = flowGenerator;
73 this.transactionExecutor = graphExecutor;
74 this.restExecutor = restExecutor;
75 this.inputParamsCollector = new InputParamsCollector(envVariables, dbService);
78 String getFlowSequence(Map<String, String> inParams, SvcLogicContext ctx, SvcLogicContext localContext)
82 if (localContext.getAttribute(SEQUENCE_TYPE) == null) {
83 Transactions trans = flowGenerator.createSingleStepModel(inParams, ctx);
84 ObjectMapper mapper = new ObjectMapper();
85 flowSequence = mapper.writeValueAsString(trans);
86 log.debug("Single step Flow Sequence : " + flowSequence);
91 if (localContext.getAttribute(GENERATION_NODE) != null) {
92 flowSequence = generationNode(localContext.getAttribute(GENERATION_NODE));
94 flowSequence = getFlowSequenceFromType(ctx, localContext, localContext.getAttribute(SEQUENCE_TYPE));
99 private String generationNode(String generationNode) throws Exception {
100 if (!transactionExecutor.hasGraph(MODULE, generationNode, null, "sync")) {
101 throw new Exception("Can not find Custom defined Flow Generator for " + generationNode);
103 return transactionExecutor
104 .executeGraph(MODULE, generationNode, null, "sync", null)
105 .getProperty(FLOW_SEQUENCE);
108 private String getFlowSequenceFromType(SvcLogicContext ctx, SvcLogicContext localContext,
109 String sequenceType) throws Exception {
111 String vnfType = ctx.getAttribute(VNF_TYPE);
112 if (sequenceType.equalsIgnoreCase(DESINGTIME)) {
114 localContext.setAttribute(VNFC_TYPE, ctx.getAttribute(VNFC_TYPE));
115 flowSequence = dbService.getDesignTimeFlowModel(localContext);
117 if (flowSequence == null) {
118 throw new Exception("Flow Sequence is not found User Designed VNF " + vnfType);
120 } else if (sequenceType.equalsIgnoreCase(RUNTIME)) {
122 Transaction transaction = inputParamsCollector.collectInputParams(ctx);
123 log.info("CollectInputParamsData-Input: " + transaction.getPayload());
125 Map<String, String> flowSeq = restExecutor.execute(transaction, localContext);
127 JSONObject output = new JSONObject(flowSeq.get("restResponse")).optJSONObject("output");
128 if (output == null) {
129 throw new Exception("Failed to get the Flow Sequence runtime for VNF type " + vnfType);
131 flowSequence = output.toString();
132 log.info("MultistepSequenceGenerator-Output: " + flowSequence);
134 } else if (sequenceType.equalsIgnoreCase(EXTERNAL)) {
135 //String input = collectInputParams(localContext);
136 // flowSequnce = ""; //get it from the External interface calling the Rest End point - TBD
137 //if(flowSequnce == null)
139 throw new Exception("Flow Sequence not found for " + vnfType);
142 //No other type of model supported...
143 //in Future can get flowModel from other generators which will be included here
144 throw new Exception("No information found for sequence Owner Design-Time Vs Run-Time");