Changed to unmaintained
[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  * Copyright (C) 2018 AT&T intellectual property. All rights reserved.
7  * =============================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.appc.flow.controller.node;
22
23 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.ACTION_LEVEL;
24 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.APPC_FLOW_CONTROLLER;
25 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.PAYLOAD;
26 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.REQUEST_ACTION;
27 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.SEQ_GENERATOR_PWD;
28 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.SEQ_GENERATOR_UID;
29 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.SEQ_GENERATOR_URL;
30 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNFC_NAME;
31 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNF_ID;
32 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VSERVER_ID;
33
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36 import com.fasterxml.jackson.annotation.JsonInclude.Include;
37 import com.fasterxml.jackson.core.type.TypeReference;
38 import com.fasterxml.jackson.databind.DeserializationFeature;
39 import com.fasterxml.jackson.databind.JsonNode;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41 import com.fasterxml.jackson.databind.SerializationFeature;
42 import java.io.IOException;
43 import java.util.List;
44 import java.util.Properties;
45 import java.util.function.Consumer;
46 import org.apache.commons.lang3.StringUtils;
47 import org.onap.appc.flow.controller.data.Transaction;
48 import org.onap.appc.flow.controller.dbervices.FlowControlDBService;
49 import org.onap.appc.flow.controller.interfaceData.ActionIdentifier;
50 import org.onap.appc.flow.controller.interfaceData.Capabilities;
51 import org.onap.appc.flow.controller.interfaceData.DependencyInfo;
52 import org.onap.appc.flow.controller.interfaceData.Input;
53 import org.onap.appc.flow.controller.interfaceData.InventoryInfo;
54 import org.onap.appc.flow.controller.interfaceData.RequestInfo;
55 import org.onap.appc.flow.controller.interfaceData.Vnfcs;
56 import org.onap.appc.flow.controller.utils.EncryptionTool;
57 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
58 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
59
60 class InputParamsCollector {
61
62   private static final EELFLogger log = EELFManager.getInstance().getLogger(InputParamsCollector.class);
63
64   private final EnvVariables envVariables;
65   private final FlowControlDBService dbService;
66
67   static final String SDNC_CONFIG_DIR_VAR = "SDNC_CONFIG_DIR";
68
69   InputParamsCollector() {
70     this.envVariables = new EnvVariables();
71     this.dbService = FlowControlDBService.initialise();
72   }
73
74   InputParamsCollector(EnvVariables envVariables, FlowControlDBService dbService) {
75     this.envVariables = envVariables;
76     this.dbService = dbService;
77   }
78
79   Transaction collectInputParams(SvcLogicContext ctx) throws Exception {
80
81     String fn = "FlowExecuteNode.collectInputParams";
82     Properties prop = loadProperties();
83     log.info("Loaded Properties " + prop.toString());
84
85     String vnfId = ctx.getAttribute(VNF_ID);
86     log.debug(fn + "vnfId :" + vnfId);
87
88     if (StringUtils.isBlank(vnfId)) {
89       throw new Exception("VnfId is missing");
90     }
91
92     String resourceUri = prop.getProperty(SEQ_GENERATOR_URL);
93     log.info(fn + "resourceUri= " + resourceUri);
94
95     String pass = EncryptionTool.getInstance().decrypt(prop.getProperty(SEQ_GENERATOR_PWD));
96
97     Transaction transaction = new Transaction();
98     transaction.setPayload(getInputData(ctx, fn, vnfId));
99     transaction.setExecutionRPC("POST");
100     transaction.setuId(prop.getProperty(SEQ_GENERATOR_UID));
101     transaction.setPswd(pass);
102     transaction.setExecutionEndPoint(resourceUri);
103
104     return transaction;
105   }
106
107   private String getInputData(SvcLogicContext ctx, String fn, String vnfId) throws IOException, SvcLogicException {
108     ActionIdentifier actionIdentifier = new ActionIdentifier();
109     log.debug("Enter ActionIdentifier");
110
111     applyIfNotBlank(vnfId, actionIdentifier::setVnfId);
112     applyIfNotBlank(ctx.getAttribute(VSERVER_ID), actionIdentifier::setVserverId);
113     applyIfNotBlank(ctx.getAttribute(VNFC_NAME), actionIdentifier::setVnfcName);
114
115     log.info("ActionIdentifierData" + actionIdentifier.toString());
116
117     log.info("Enter RequestInfo");
118     RequestInfo requestInfo = getRequestInfo(ctx, actionIdentifier);
119     log.debug("RequestInfo: " + requestInfo.toString());
120
121     InventoryInfo inventoryInfo = new InventoryInfoExtractor().getInventoryInfo(ctx, vnfId);
122     Capabilities capabilities = new CapabilitiesDataExtractor(dbService).getCapabilitiesData(ctx);
123     ctx.setAttribute("artifact-content", null);
124
125     log.info("Enter InputData");
126     Input input = getInput(requestInfo, inventoryInfo, null, capabilities);
127     log.info(fn + "Input parameters:" + input.toString());
128
129     ObjectMapper mapper = new ObjectMapper();
130     mapper.setSerializationInclusion(Include.NON_NULL);
131     mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
132     String inputData = mapper.writeValueAsString(input);
133     log.info("InputDataJson:" + inputData);
134
135     return inputData;
136   }
137
138   private Input getInput(RequestInfo requestInfo, InventoryInfo inventoryInfo,
139       DependencyInfo dependencyInfo, Capabilities capabilities) {
140     Input input = new Input();
141     input.setRequestInfo(requestInfo);
142     input.setInventoryInfo(inventoryInfo);
143     input.setDependencyInfo(dependencyInfo);
144     input.setCapabilities(capabilities);
145     return input;
146   }
147
148   private RequestInfo getRequestInfo(SvcLogicContext ctx, ActionIdentifier actionIdentifier) {
149     RequestInfo requestInfo = new RequestInfo();
150     requestInfo.setAction(ctx.getAttribute(REQUEST_ACTION));
151     requestInfo.setActionLevel(ctx.getAttribute(ACTION_LEVEL));
152     requestInfo.setPayload(ctx.getAttribute(PAYLOAD));
153     requestInfo.setActionIdentifier(actionIdentifier);
154     return requestInfo;
155   }
156
157   DependencyInfo getDependencyInfo(SvcLogicContext ctx) throws SvcLogicException, IOException {
158
159     String fn = "FlowExecutorNode.getDependencyInfo";
160     String dependencyData = dbService.getDependencyInfo(ctx);
161     log.info(fn + "dependencyDataInput:" + dependencyData);
162
163     DependencyInfo dependencyInfo = new DependencyInfo();
164     if (dependencyData == null) {
165       return dependencyInfo;
166     }
167
168     ObjectMapper mapper = new ObjectMapper();
169     mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
170     mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
171     //JsonNode dependencyInfoData = mapper.readTree(dependencyData).get("dependencyInfo");
172     JsonNode vnfcData = mapper.readTree(dependencyData).get("vnfcs");
173     dependencyInfo.getVnfcs().addAll(mapper.readValue(vnfcData.toString(), new TypeReference<List<Vnfcs>>(){}));
174
175     log.info("Dependency Output:" + dependencyInfo.toString());
176     return dependencyInfo;
177   }
178
179   private Properties loadProperties() throws Exception {
180     String directory = envVariables.getenv(SDNC_CONFIG_DIR_VAR);
181     if (directory == null) {
182       throw new Exception("Cannot find Property file -" + SDNC_CONFIG_DIR_VAR);
183     }
184     String path = directory + APPC_FLOW_CONTROLLER;
185     return PropertiesLoader.load(path);
186   }
187
188   private void applyIfNotBlank(String parameter, Consumer<String> consumer) {
189     if (StringUtils.isNotBlank(parameter)) {
190       consumer.accept(parameter);
191     }
192   }
193
194 }