Add junit coverage to RequestInfoBuilder class
[appc.git] / appc-config / appc-flow-controller / provider / src / main / java / org / onap / appc / flow / controller / node / FlowSequenceGenerator.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.appc.flow.controller.node;
21
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;
30
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34 import java.util.Map;
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;
42
43 /**
44  * Helper class for FlowControlNode
45  */
46 class FlowSequenceGenerator {
47
48   private static final EELFLogger log = EELFManager.getInstance().getLogger(FlowSequenceGenerator.class);
49
50   static final String MODULE = "APPC_COMMOM";
51
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;
57
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();
64   }
65
66   /**
67    * Constructor for tests, prefer to use default one
68    */
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);
76   }
77
78   String getFlowSequence(Map<String, String> inParams, SvcLogicContext ctx, SvcLogicContext localContext)
79       throws Exception {
80
81     String flowSequence;
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);
87
88       return flowSequence;
89     }
90
91     if (localContext.getAttribute(GENERATION_NODE) != null) {
92       flowSequence = generationNode(localContext.getAttribute(GENERATION_NODE));
93     } else {
94       flowSequence = getFlowSequenceFromType(ctx, localContext, localContext.getAttribute(SEQUENCE_TYPE));
95     }
96     return flowSequence;
97   }
98
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);
102     }
103     return transactionExecutor
104         .executeGraph(MODULE, generationNode, null, "sync", null)
105         .getProperty(FLOW_SEQUENCE);
106   }
107
108   private String getFlowSequenceFromType(SvcLogicContext ctx, SvcLogicContext localContext,
109       String sequenceType) throws Exception {
110     String flowSequence;
111     String vnfType = ctx.getAttribute(VNF_TYPE);
112     if (sequenceType.equalsIgnoreCase(DESINGTIME)) {
113
114       localContext.setAttribute(VNFC_TYPE, ctx.getAttribute(VNFC_TYPE));
115       flowSequence = dbService.getDesignTimeFlowModel(localContext);
116
117       if (flowSequence == null) {
118         throw new Exception("Flow Sequence is not found User Designed VNF " + vnfType);
119       }
120     } else if (sequenceType.equalsIgnoreCase(RUNTIME)) {
121
122       Transaction transaction = inputParamsCollector.collectInputParams(ctx);
123       log.info("CollectInputParamsData-Input: " + transaction.getPayload());
124
125       Map<String, String> flowSeq = restExecutor.execute(transaction, localContext);
126
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);
130       }
131       flowSequence = output.toString();
132       log.info("MultistepSequenceGenerator-Output: " + flowSequence);
133
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)
138
139       throw new Exception("Flow Sequence not found for " + vnfType);
140
141     } else {
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");
145     }
146     return flowSequence;
147   }
148
149 }