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 static org.onap.appc.flow.controller.utils.FlowControllerConstants.APPC_FLOW_CONTROLLER;
26 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_PARAM_RESPONSE_PREFIX;
27 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_PARAM_ERROR_MESSAGE;
28 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_PARAM_STATUS;
29 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_STATUS_FAILURE;
30 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_STATUS_MESSAGE;
31 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_STATUS_SUCCESS;
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
35 import com.fasterxml.jackson.databind.JsonNode;
37 import java.util.Properties;
38 import org.apache.commons.lang3.StringUtils;
39 import org.onap.appc.flow.controller.data.Transaction;
40 import org.onap.appc.flow.controller.executorImpl.RestExecutor;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
45 public class RestServiceNode implements SvcLogicJavaPlugin {
47 private static final EELFLogger log = EELFManager.getInstance().getLogger(RestServiceNode.class);
48 private static final String SDNC_CONFIG_DIR_VAR = "SDNC_CONFIG_DIR";
50 static final String REST_RESPONSE = "restResponse";
52 private final TransactionHandler transactionHandler;
53 private final RestExecutor restExecutor;
54 private final ResourceUriExtractor resourceUriExtractor;
55 private final EnvVariables envVariables;
57 public RestServiceNode() {
58 this.transactionHandler = new TransactionHandler();
59 this.restExecutor = new RestExecutor();
60 this.resourceUriExtractor = new ResourceUriExtractor();
61 this.envVariables = new EnvVariables();
65 * Constructor for tests, prefer to use no arg constructor
67 RestServiceNode(TransactionHandler transactionHandler, RestExecutor restExecutor,
68 ResourceUriExtractor uriExtractor, EnvVariables envVariables) {
69 this.transactionHandler = transactionHandler;
70 this.restExecutor = restExecutor;
71 this.resourceUriExtractor = uriExtractor;
72 this.envVariables = envVariables;
75 public void sendRequest(Map<String, String> inParams, SvcLogicContext ctx)
76 throws SvcLogicException {
77 String fn = "RestServiceNode.sendRequest";
78 log.info("Received processParamKeys call with params : " + inParams);
79 String responsePrefix = inParams.get(INPUT_PARAM_RESPONSE_PREFIX);
81 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
82 //Remove below for Block
83 for (String key : ctx.getAttributeKeySet()) {
84 log.info(fn + "Getting Key = " + key + "and Value = " + ctx.getAttribute(key));
87 Properties prop = loadProperties();
88 log.info("Loaded Properties " + prop.toString());
90 String resourceUri = resourceUriExtractor.extractResourceUri(ctx, prop);
92 log.info("Rest Constructed URL : " + resourceUri);
94 Transaction transaction = transactionHandler.buildTransaction(ctx, prop, resourceUri);
95 Map<String, String> output = restExecutor.execute(transaction, ctx);
97 String json = output.get(REST_RESPONSE);
98 log.info("Received response from Interface " + json);
100 JsonNode validatedJson = JsonValidator.validate(json);
102 if (validatedJson != null) {
103 log.info("state is " + validatedJson.findValue("state"));
104 ctx.setAttribute(responsePrefix + OUTPUT_STATUS_MESSAGE, output.get(REST_RESPONSE));
107 ctx.setAttribute(responsePrefix + OUTPUT_PARAM_STATUS, OUTPUT_STATUS_SUCCESS);
109 } catch (Exception e) {
110 ctx.setAttribute(responsePrefix + OUTPUT_PARAM_STATUS, OUTPUT_STATUS_FAILURE);
111 ctx.setAttribute(responsePrefix + OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
112 log.error("Error Message : " + e.getMessage(), e);
113 throw new SvcLogicException(e.getMessage());
117 private Properties loadProperties() throws Exception {
118 String directory = envVariables.getenv(SDNC_CONFIG_DIR_VAR);
119 if (directory == null) {
120 throw new Exception("Cannot find Property file: " + SDNC_CONFIG_DIR_VAR);
122 String path = directory + APPC_FLOW_CONTROLLER;
123 return PropertiesLoader.load(path);