2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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.
18 * * ============LICENSE_END=========================================================
21 package org.onap.appc.flow.controller.node;
23 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.APPC_SOUTHBOUND;
24 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_PARAM_RESPONSE_PREFIX;
25 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_PARAM_ERROR_MESSAGE;
26 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_PARAM_STATUS;
27 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_STATUS_FAILURE;
28 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_STATUS_MESSAGE;
29 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_STATUS_SUCCESS;
30 import com.att.eelf.configuration.EELFLogger;
31 import com.att.eelf.configuration.EELFManager;
32 import com.fasterxml.jackson.databind.JsonNode;
34 import org.apache.commons.lang3.StringUtils;
35 import org.onap.appc.flow.controller.data.Transaction;
36 import org.onap.appc.flow.controller.executorImpl.RestExecutor;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
41 public class RestServiceNode implements SvcLogicJavaPlugin {
43 private static final EELFLogger log = EELFManager.getInstance().getLogger(RestServiceNode.class);
45 static final String REST_RESPONSE = "restResponse";
47 private final TransactionHandler transactionHandler;
48 private final RestExecutor restExecutor;
49 private final ResourceUriExtractor resourceUriExtractor;
50 private final EnvVariables envVariables;
52 public RestServiceNode() {
53 this.transactionHandler = new TransactionHandler();
54 this.restExecutor = new RestExecutor();
55 this.resourceUriExtractor = new ResourceUriExtractor();
56 this.envVariables = new EnvVariables();
60 * Constructor for tests, prefer to use no arg constructor
62 RestServiceNode(TransactionHandler transactionHandler, RestExecutor restExecutor, ResourceUriExtractor uriExtractor,
63 EnvVariables envVariables) {
64 this.transactionHandler = transactionHandler;
65 this.restExecutor = restExecutor;
66 this.resourceUriExtractor = uriExtractor;
67 this.envVariables = envVariables;
70 public void sendRequest(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
71 String fn = "RestServiceNode.sendRequest";
72 log.info("Received processParamKeys call with params : " + inParams);
73 String responsePrefix = inParams.get(INPUT_PARAM_RESPONSE_PREFIX);
75 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
76 // Remove below for Block
77 for (String key : ctx.getAttributeKeySet()) {
78 log.info(fn + "Getting Key = " + key + "and Value = " + ctx.getAttribute(key));
81 String resourceUri = resourceUriExtractor.extractResourceUri(ctx);
83 log.info("Rest Constructed URL : " + resourceUri);
85 Transaction transaction = transactionHandler.buildTransaction(ctx, resourceUri);
86 Map<String, String> output = restExecutor.execute(transaction, ctx);
88 String json = output.get(REST_RESPONSE);
89 log.info("Received response from Interface " + json);
91 JsonNode validatedJson = JsonValidator.validate(json);
93 if (validatedJson != null) {
94 log.info("state is " + validatedJson.findValue("state"));
95 ctx.setAttribute(responsePrefix + OUTPUT_STATUS_MESSAGE, output.get(REST_RESPONSE));
98 ctx.setAttribute(responsePrefix + OUTPUT_PARAM_STATUS, OUTPUT_STATUS_SUCCESS);
100 } catch (Exception e) {
101 ctx.setAttribute(responsePrefix + OUTPUT_PARAM_STATUS, OUTPUT_STATUS_FAILURE);
102 ctx.setAttribute(responsePrefix + OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
103 log.error("Error Message : " + e.getMessage(), e);
104 throw new SvcLogicException(e.getMessage());