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.INPUT_PARAM_RESPONSE_PREFIX;
24 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_PARAM_ERROR_MESSAGE;
25 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_PARAM_STATUS;
26 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_STATUS_FAILURE;
27 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_STATUS_MESSAGE;
28 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_STATUS_SUCCESS;
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31 import com.fasterxml.jackson.databind.JsonNode;
33 import org.apache.commons.lang3.StringUtils;
34 import org.onap.appc.flow.controller.data.Transaction;
35 import org.onap.appc.flow.controller.executorImpl.RestExecutor;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
40 public class RestServiceNode implements SvcLogicJavaPlugin {
42 private static final EELFLogger log = EELFManager.getInstance().getLogger(RestServiceNode.class);
44 static final String REST_RESPONSE = "restResponse";
46 private final TransactionHandler transactionHandler;
47 private final RestExecutor restExecutor;
48 private final ResourceUriExtractor resourceUriExtractor;
49 private final EnvVariables envVariables;
51 public RestServiceNode() {
52 this.transactionHandler = new TransactionHandler();
53 this.restExecutor = new RestExecutor();
54 this.resourceUriExtractor = new ResourceUriExtractor();
55 this.envVariables = new EnvVariables();
59 * Constructor for tests, prefer to use no arg constructor
61 RestServiceNode(TransactionHandler transactionHandler, RestExecutor restExecutor, ResourceUriExtractor uriExtractor,
62 EnvVariables envVariables) {
63 this.transactionHandler = transactionHandler;
64 this.restExecutor = restExecutor;
65 this.resourceUriExtractor = uriExtractor;
66 this.envVariables = envVariables;
69 public void sendRequest(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
70 String fn = "RestServiceNode.sendRequest";
71 log.info("Received processParamKeys call with params : " + inParams);
72 String responsePrefix = inParams.get(INPUT_PARAM_RESPONSE_PREFIX);
74 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
75 // Remove below for Block
76 for (String key : ctx.getAttributeKeySet()) {
77 log.info(fn + "Getting Key = " + key + "and Value = " + ctx.getAttribute(key));
80 String resourceUri = resourceUriExtractor.extractResourceUri(ctx);
82 log.info("Rest Constructed URL : " + resourceUri);
84 Transaction transaction = transactionHandler.buildTransaction(ctx, resourceUri);
85 Map<String, String> output = restExecutor.execute(transaction, ctx);
87 String json = output.get(REST_RESPONSE);
88 log.info("Received response from Interface " + json);
90 JsonNode validatedJson = JsonValidator.validate(json);
92 if (validatedJson != null) {
93 log.info("state is " + validatedJson.findValue("state"));
94 ctx.setAttribute(responsePrefix + OUTPUT_STATUS_MESSAGE, output.get(REST_RESPONSE));
97 ctx.setAttribute(responsePrefix + OUTPUT_PARAM_STATUS, OUTPUT_STATUS_SUCCESS);
99 } catch (Exception e) {
100 ctx.setAttribute(responsePrefix + OUTPUT_PARAM_STATUS, OUTPUT_STATUS_FAILURE);
101 ctx.setAttribute(responsePrefix + OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
102 log.error("Error Message : " + e.getMessage(), e);
103 throw new SvcLogicException(e.getMessage());