Add junit coverage to RequestInfoBuilder class
[appc.git] / appc-config / appc-flow-controller / provider / src / main / java / org / onap / appc / flow / controller / node / InputParamsCollector.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.ACTION_LEVEL;
23 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.APPC_FLOW_CONTROLLER;
24 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.PAYLOAD;
25 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.REQUEST_ACTION;
26 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.SEQ_GENERATOR_PWD;
27 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.SEQ_GENERATOR_UID;
28 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.SEQ_GENERATOR_URL;
29 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNFC_NAME;
30 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNF_ID;
31 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VSERVER_ID;
32
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
35 import com.fasterxml.jackson.annotation.JsonInclude.Include;
36 import com.fasterxml.jackson.core.type.TypeReference;
37 import com.fasterxml.jackson.databind.DeserializationFeature;
38 import com.fasterxml.jackson.databind.JsonNode;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40 import com.fasterxml.jackson.databind.SerializationFeature;
41 import java.io.IOException;
42 import java.util.List;
43 import java.util.Properties;
44 import java.util.function.Consumer;
45 import org.apache.commons.lang3.StringUtils;
46 import org.onap.appc.flow.controller.data.Transaction;
47 import org.onap.appc.flow.controller.dbervices.FlowControlDBService;
48 import org.onap.appc.flow.controller.interfaceData.ActionIdentifier;
49 import org.onap.appc.flow.controller.interfaceData.Capabilities;
50 import org.onap.appc.flow.controller.interfaceData.DependencyInfo;
51 import org.onap.appc.flow.controller.interfaceData.Input;
52 import org.onap.appc.flow.controller.interfaceData.InventoryInfo;
53 import org.onap.appc.flow.controller.interfaceData.RequestInfo;
54 import org.onap.appc.flow.controller.interfaceData.Vnfcs;
55 import org.onap.appc.flow.controller.utils.EncryptionTool;
56 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
57 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
58
59 class InputParamsCollector {
60
61   private static final EELFLogger log = EELFManager.getInstance().getLogger(InputParamsCollector.class);
62
63   private final EnvVariables envVariables;
64   private final FlowControlDBService dbService;
65
66   static final String SDNC_CONFIG_DIR_VAR = "SDNC_CONFIG_DIR";
67
68   InputParamsCollector() {
69     this.envVariables = new EnvVariables();
70     this.dbService = FlowControlDBService.initialise();
71   }
72
73   InputParamsCollector(EnvVariables envVariables, FlowControlDBService dbService) {
74     this.envVariables = envVariables;
75     this.dbService = dbService;
76   }
77
78   Transaction collectInputParams(SvcLogicContext ctx) throws Exception {
79
80     String fn = "FlowExecuteNode.collectInputParams";
81     Properties prop = loadProperties();
82     log.info("Loaded Properties " + prop.toString());
83
84     String vnfId = ctx.getAttribute(VNF_ID);
85     log.debug(fn + "vnfId :" + vnfId);
86
87     if (StringUtils.isBlank(vnfId)) {
88       throw new Exception("VnfId is missing");
89     }
90
91     String resourceUri = prop.getProperty(SEQ_GENERATOR_URL);
92     log.info(fn + "resourceUri= " + resourceUri);
93
94     String pass = EncryptionTool.getInstance().decrypt(prop.getProperty(SEQ_GENERATOR_PWD));
95
96     Transaction transaction = new Transaction();
97     transaction.setPayload(getInputData(ctx, fn, vnfId));
98     transaction.setExecutionRPC("POST");
99     transaction.setuId(prop.getProperty(SEQ_GENERATOR_UID));
100     transaction.setPswd(pass);
101     transaction.setExecutionEndPoint(resourceUri);
102
103     return transaction;
104   }
105
106   private String getInputData(SvcLogicContext ctx, String fn, String vnfId) throws IOException, SvcLogicException {
107     ActionIdentifier actionIdentifier = new ActionIdentifier();
108     log.debug("Enter ActionIdentifier");
109
110     applyIfNotBlank(vnfId, actionIdentifier::setVnfId);
111     applyIfNotBlank(ctx.getAttribute(VSERVER_ID), actionIdentifier::setVserverId);
112     applyIfNotBlank(ctx.getAttribute(VNFC_NAME), actionIdentifier::setVnfcName);
113
114     log.info("ActionIdentifierData" + actionIdentifier.toString());
115
116     log.info("Enter RequestInfo");
117     RequestInfo requestInfo = getRequestInfo(ctx, actionIdentifier);
118     log.debug("RequestInfo: " + requestInfo.toString());
119
120     InventoryInfo inventoryInfo = new InventoryInfoExtractor().getInventoryInfo(ctx, vnfId);
121     Capabilities capabilities = new CapabilitiesDataExtractor(dbService).getCapabilitiesData(ctx);
122     DependencyInfo dependencyInfo = getDependencyInfo(ctx);
123
124     log.info("Enter InputData");
125     Input input = getInput(requestInfo, inventoryInfo, dependencyInfo, capabilities);
126     log.info(fn + "Input parameters:" + input.toString());
127
128     ObjectMapper mapper = new ObjectMapper();
129     mapper.setSerializationInclusion(Include.NON_NULL);
130     mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
131     String inputData = mapper.writeValueAsString(input);
132     log.info("InputDataJson:" + inputData);
133
134     return inputData;
135   }
136
137   private Input getInput(RequestInfo requestInfo, InventoryInfo inventoryInfo,
138       DependencyInfo dependencyInfo, Capabilities capabilities) {
139     Input input = new Input();
140     input.setRequestInfo(requestInfo);
141     input.setInventoryInfo(inventoryInfo);
142     input.setDependencyInfo(dependencyInfo);
143     input.setCapabilities(capabilities);
144     return input;
145   }
146
147   private RequestInfo getRequestInfo(SvcLogicContext ctx, ActionIdentifier actionIdentifier) {
148     RequestInfo requestInfo = new RequestInfo();
149     requestInfo.setAction(ctx.getAttribute(REQUEST_ACTION));
150     requestInfo.setActionLevel(ctx.getAttribute(ACTION_LEVEL));
151     requestInfo.setPayload(ctx.getAttribute(PAYLOAD));
152     requestInfo.setActionIdentifier(actionIdentifier);
153     return requestInfo;
154   }
155
156   DependencyInfo getDependencyInfo(SvcLogicContext ctx) throws SvcLogicException, IOException {
157
158     String fn = "FlowExecutorNode.getDependencyInfo";
159     String dependencyData = dbService.getDependencyInfo(ctx);
160     log.info(fn + "dependencyDataInput:" + dependencyData);
161
162     DependencyInfo dependencyInfo = new DependencyInfo();
163     if (dependencyData == null) {
164       return dependencyInfo;
165     }
166
167     ObjectMapper mapper = new ObjectMapper();
168     mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
169     mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
170     //JsonNode dependencyInfoData = mapper.readTree(dependencyData).get("dependencyInfo");
171     JsonNode vnfcData = mapper.readTree(dependencyData).get("vnfcs");
172     dependencyInfo.getVnfcs().addAll(mapper.readValue(vnfcData.toString(), new TypeReference<List<Vnfcs>>(){}));
173
174     log.info("Dependency Output:" + dependencyInfo.toString());
175     return dependencyInfo;
176   }
177
178   private Properties loadProperties() throws Exception {
179     String directory = envVariables.getenv(SDNC_CONFIG_DIR_VAR);
180     if (directory == null) {
181       throw new Exception("Cannot find Property file -" + SDNC_CONFIG_DIR_VAR);
182     }
183     String path = directory + APPC_FLOW_CONTROLLER;
184     return PropertiesLoader.load(path);
185   }
186
187   private void applyIfNotBlank(String parameter, Consumer<String> consumer) {
188     if (StringUtils.isNotBlank(parameter)) {
189       consumer.accept(parameter);
190     }
191   }
192
193 }