Changed to unmaintained
[appc.git] / appc-config / appc-flow-controller / provider / src / main / java / org / onap / appc / flow / controller / node / RestServiceNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.appc.flow.controller.node;
22
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;
32 import java.util.Map;
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;
39
40 public class RestServiceNode implements SvcLogicJavaPlugin {
41
42     private static final EELFLogger log = EELFManager.getInstance().getLogger(RestServiceNode.class);
43
44     static final String REST_RESPONSE = "restResponse";
45
46     private final TransactionHandler transactionHandler;
47     private final RestExecutor restExecutor;
48     private final ResourceUriExtractor resourceUriExtractor;
49     private final EnvVariables envVariables;
50
51     public RestServiceNode() {
52         this.transactionHandler = new TransactionHandler();
53         this.restExecutor = new RestExecutor();
54         this.resourceUriExtractor = new ResourceUriExtractor();
55         this.envVariables = new EnvVariables();
56     }
57
58     /**
59      * Constructor for tests, prefer to use no arg constructor
60      */
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;
67     }
68
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);
73         try {
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));
78             }
79
80             String resourceUri = resourceUriExtractor.extractResourceUri(ctx);
81
82             log.info("Rest Constructed URL : " + resourceUri);
83
84             Transaction transaction = transactionHandler.buildTransaction(ctx, resourceUri);
85             Map<String, String> output = restExecutor.execute(transaction, ctx);
86
87             String json = output.get(REST_RESPONSE);
88             log.info("Received response from Interface " + json);
89
90             JsonNode validatedJson = JsonValidator.validate(json);
91
92             if (validatedJson != null) {
93                 log.info("state is " + validatedJson.findValue("state"));
94                 ctx.setAttribute(responsePrefix + OUTPUT_STATUS_MESSAGE, output.get(REST_RESPONSE));
95             }
96
97             ctx.setAttribute(responsePrefix + OUTPUT_PARAM_STATUS, OUTPUT_STATUS_SUCCESS);
98
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());
104         }
105     }
106
107
108 }