Changes needed for MultiStep Actions
[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.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;
33 import java.util.Map;
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;
40
41 public class RestServiceNode implements SvcLogicJavaPlugin {
42
43     private static final EELFLogger log = EELFManager.getInstance().getLogger(RestServiceNode.class);
44
45     static final String REST_RESPONSE = "restResponse";
46
47     private final TransactionHandler transactionHandler;
48     private final RestExecutor restExecutor;
49     private final ResourceUriExtractor resourceUriExtractor;
50     private final EnvVariables envVariables;
51
52     public RestServiceNode() {
53         this.transactionHandler = new TransactionHandler();
54         this.restExecutor = new RestExecutor();
55         this.resourceUriExtractor = new ResourceUriExtractor();
56         this.envVariables = new EnvVariables();
57     }
58
59     /**
60      * Constructor for tests, prefer to use no arg constructor
61      */
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;
68     }
69
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);
74         try {
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));
79             }
80
81             String resourceUri = resourceUriExtractor.extractResourceUri(ctx);
82
83             log.info("Rest Constructed URL : " + resourceUri);
84
85             Transaction transaction = transactionHandler.buildTransaction(ctx, resourceUri);
86             Map<String, String> output = restExecutor.execute(transaction, ctx);
87
88             String json = output.get(REST_RESPONSE);
89             log.info("Received response from Interface " + json);
90
91             JsonNode validatedJson = JsonValidator.validate(json);
92
93             if (validatedJson != null) {
94                 log.info("state is " + validatedJson.findValue("state"));
95                 ctx.setAttribute(responsePrefix + OUTPUT_STATUS_MESSAGE, output.get(REST_RESPONSE));
96             }
97
98             ctx.setAttribute(responsePrefix + OUTPUT_PARAM_STATUS, OUTPUT_STATUS_SUCCESS);
99
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());
105         }
106     }
107
108
109 }