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.
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20 * ============LICENSE_END=========================================================
23 package org.onap.appc.flow.controller.node;
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
27 import com.fasterxml.jackson.core.JsonProcessingException;
28 import com.fasterxml.jackson.databind.JsonNode;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import org.apache.commons.lang3.StringUtils;
31 import org.onap.appc.flow.controller.data.Transaction;
32 import org.onap.appc.flow.controller.executorImpl.RestExecutor;
33 import org.onap.appc.flow.controller.utils.FlowControllerConstants;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
38 import java.io.FileInputStream;
39 import java.io.IOException;
40 import java.io.InputStream;
42 import java.util.Properties;
44 public class RestServiceNode implements SvcLogicJavaPlugin{
46 private static final EELFLogger log = EELFManager.getInstance().getLogger(RestServiceNode.class);
47 private static final String SDNC_CONFIG_DIR_VAR = "SDNC_CONFIG_DIR";
49 public void sendRequest(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
50 String fn = "RestServiceNode.sendRequest";
51 log.info("Received processParamKeys call with params : " + inParams);
52 String responsePrefix = inParams.get(FlowControllerConstants.INPUT_PARAM_RESPONSE_PRIFIX);
54 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
55 //Remove below for Block
56 for (Object key : ctx.getAttributeKeySet()) {
57 String parmName = (String) key;
58 String parmValue = ctx.getAttribute(parmName);
59 log.info(fn + "Getting Key = " + parmName + "and Value = " + parmValue);
63 ctx.setAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_STATUS, FlowControllerConstants.OUTPUT_STATUS_SUCCESS);
65 } catch (Exception e) {
66 ctx.setAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_STATUS, FlowControllerConstants.OUTPUT_STATUS_FAILURE);
67 ctx.setAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
68 log.error("Error Message : " + e.getMessage(), e);
69 throw new SvcLogicException(e.getMessage());
73 private void send(SvcLogicContext ctx, Map<String, String> inParams) throws Exception {
75 Properties prop = loadProperties();
76 log.info("Loaded Properties " + prop.toString());
77 String responsePrefix = inParams.get(FlowControllerConstants.INPUT_PARAM_RESPONSE_PRIFIX);
78 RestExecutor restRequestExecutor = new RestExecutor();
79 String resourceUri = "";
80 if(ctx.getAttribute(FlowControllerConstants.INPUT_URL) != null && !(ctx.getAttribute(FlowControllerConstants.INPUT_URL).isEmpty()))
81 resourceUri = ctx.getAttribute(FlowControllerConstants.INPUT_URL);
83 resourceUri = resourceUri.concat(FlowControllerConstants.HTTP);
84 log.info("resourceUri= " + resourceUri );
85 resourceUri = resourceUri.concat(ctx.getAttribute(FlowControllerConstants.INPUT_HOST_IP_ADDRESS));
86 resourceUri = resourceUri.concat(":");
87 resourceUri = resourceUri.concat(ctx.getAttribute(FlowControllerConstants.INPUT_PORT_NUMBER));
89 if(ctx.getAttribute(FlowControllerConstants.INPUT_CONTEXT) != null && !ctx.getAttribute(FlowControllerConstants.INPUT_CONTEXT).isEmpty()){
90 resourceUri = resourceUri.concat("/").concat(ctx.getAttribute(FlowControllerConstants.INPUT_CONTEXT));
91 log.info("resourceUri= " + resourceUri );
93 else if(prop.getProperty(ctx.getAttribute(FlowControllerConstants.INPUT_REQUEST_ACTION).concat(".context")) != null ){
94 log.info("resourceUri = " + resourceUri );
95 resourceUri = resourceUri.concat("/").concat(prop.getProperty(ctx.getAttribute(FlowControllerConstants.INPUT_REQUEST_ACTION).concat(".context")));
97 throw new Exception("Could Not found the context for operation " + ctx.getAttribute(FlowControllerConstants.INPUT_REQUEST_ACTION));
100 if(ctx.getAttribute(FlowControllerConstants.INPUT_SUB_CONTEXT) != null && !ctx.getAttribute(FlowControllerConstants.INPUT_SUB_CONTEXT).isEmpty()){
101 resourceUri = resourceUri.concat("/").concat(ctx.getAttribute(FlowControllerConstants.INPUT_SUB_CONTEXT));
102 log.info("resourceUri" + resourceUri );
104 else if(prop.getProperty(ctx.getAttribute(FlowControllerConstants.INPUT_REQUEST_ACTION).concat(".sub-context")) != null ){
105 resourceUri = resourceUri.concat("/").concat(prop.getProperty(ctx.getAttribute(FlowControllerConstants.INPUT_REQUEST_ACTION).concat(".sub-context")));
106 log.info("resourceUri" + resourceUri );
110 log.info("Rest Constructed URL : " + resourceUri);
111 Transaction transaction = new Transaction();
113 transaction.setExecutionEndPoint(resourceUri);
114 transaction.setExecutionRPC(ctx.getAttribute(FlowControllerConstants.INPUT_REQUEST_ACTION_TYPE));
115 transaction.setAction(FlowControllerConstants.INPUT_REQUEST_ACTION);
116 if(ctx.getAttribute(FlowControllerConstants.INPUT_REQUEST_ACTION_TYPE) == null || ctx.getAttribute(FlowControllerConstants.INPUT_REQUEST_ACTION_TYPE).isEmpty())
117 throw new Exception("Dont know REST operation for Action " + transaction.getExecutionRPC());
118 if(ctx.getAttribute(FlowControllerConstants.INPUT_REQUEST_ACTION) == null || ctx.getAttribute(FlowControllerConstants.INPUT_REQUEST_ACTION).isEmpty())
119 throw new Exception("Dont know request-action " + transaction.getAction());
121 //This code need to get changed to get the UserID and pass from a common place.
122 if(transaction.getuId() == null)
123 transaction.setuId(prop.getProperty(ctx.getAttribute(FlowControllerConstants.INPUT_REQUEST_ACTION).concat(".default-rest-user")));
124 if(transaction.getPswd() == null)
125 transaction.setPswd(prop.getProperty(ctx.getAttribute(FlowControllerConstants.INPUT_REQUEST_ACTION).concat(".default-rest-pass")));
127 Map<String, String> output = restRequestExecutor.execute(transaction, ctx);
129 if(isValidJSON(output.get("restResponse")) != null) {
130 ctx.setAttribute(responsePrefix + "." + FlowControllerConstants.OUTPUT_STATUS_MESSAGE , output.get("restResponse"));
131 // JsonNode restResponse = isValidJSON(output.get("restResponse"));
132 // for (String key : inParams.keySet()) {
133 // if(key !=null && key.startsWith("output-")){
134 // log.info("Found Key = " + key);
135 // log.info("Found Key in Params " + inParams.get(key) + ".");
136 // JsonNode setValue = restResponse.findValue(inParams.get(key));
137 // log.info("Found value = " + setValue);
138 // if(setValue !=null && setValue.textValue() !=null && !setValue.textValue().isEmpty())
139 // ctx.setAttribute(responsePrefix + "." + key, setValue.textValue());
141 // ctx.setAttribute(responsePrefix + "." + key, null);
145 log.info("Response from Rest :" );
147 } catch(Exception e) {
148 log.error("Error Message: " + e.getMessage(), e);
153 private Properties loadProperties() throws Exception {
154 Properties props = new Properties();
155 String propDir = System.getenv(SDNC_CONFIG_DIR_VAR);
157 throw new Exception("Cannot find Property file -" + SDNC_CONFIG_DIR_VAR);
158 String propFile = propDir + FlowControllerConstants.APPC_FLOW_CONTROLLER;
159 try (InputStream propStream = new FileInputStream(propFile))
161 props.load(propStream);
165 throw new Exception("Could not load properties file " + propFile, e);
170 private JsonNode isValidJSON(String json) throws IOException {
172 log.info("Received response from Interface " + json);
173 if(json == null || json.isEmpty())
176 ObjectMapper objectMapper = new ObjectMapper();
177 output = objectMapper.readTree(json);
178 } catch(JsonProcessingException e) {
179 log.warn("Response received from interface is not a valid JSON block" + json, e);
182 log.info("state is " + output.findValue("state"));