Add junit coverage to RequestInfoBuilder class
[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 java.util.Properties;
35 import org.apache.commons.lang3.StringUtils;
36 import org.onap.appc.flow.controller.data.Transaction;
37 import org.onap.appc.flow.controller.executorImpl.RestExecutor;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
41
42 public class RestServiceNode implements SvcLogicJavaPlugin {
43
44     private static final EELFLogger log = EELFManager.getInstance().getLogger(RestServiceNode.class);
45     public static final String APPC_CONFIG_DIR_VAR = "APPC_CONFIG_DIR";
46
47     static final String REST_RESPONSE = "restResponse";
48
49     private final TransactionHandler transactionHandler;
50     private final RestExecutor restExecutor;
51     private final ResourceUriExtractor resourceUriExtractor;
52     private final EnvVariables envVariables;
53
54     public RestServiceNode() {
55         this.transactionHandler = new TransactionHandler();
56         this.restExecutor = new RestExecutor();
57         this.resourceUriExtractor = new ResourceUriExtractor();
58         this.envVariables = new EnvVariables();
59     }
60
61     /**
62      * Constructor for tests, prefer to use no arg constructor
63      */
64     RestServiceNode(TransactionHandler transactionHandler, RestExecutor restExecutor, ResourceUriExtractor uriExtractor,
65             EnvVariables envVariables) {
66         this.transactionHandler = transactionHandler;
67         this.restExecutor = restExecutor;
68         this.resourceUriExtractor = uriExtractor;
69         this.envVariables = envVariables;
70     }
71
72     public void sendRequest(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
73         String fn = "RestServiceNode.sendRequest";
74         log.info("Received processParamKeys call with params : " + inParams);
75         String responsePrefix = inParams.get(INPUT_PARAM_RESPONSE_PREFIX);
76         try {
77             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
78             // Remove below for Block
79             for (String key : ctx.getAttributeKeySet()) {
80                 log.info(fn + "Getting Key = " + key + "and Value = " + ctx.getAttribute(key));
81             }
82
83             Properties prop = loadProperties();
84             log.info("Loaded Properties " + prop.toString());
85
86             String resourceUri = resourceUriExtractor.extractResourceUri(ctx, prop);
87
88             log.info("Rest Constructed URL : " + resourceUri);
89
90             Transaction transaction = transactionHandler.buildTransaction(ctx, prop, resourceUri);
91             Map<String, String> output = restExecutor.execute(transaction, ctx);
92
93             String json = output.get(REST_RESPONSE);
94             log.info("Received response from Interface " + json);
95
96             JsonNode validatedJson = JsonValidator.validate(json);
97
98             if (validatedJson != null) {
99                 log.info("state is " + validatedJson.findValue("state"));
100                 ctx.setAttribute(responsePrefix + OUTPUT_STATUS_MESSAGE, output.get(REST_RESPONSE));
101             }
102
103             ctx.setAttribute(responsePrefix + OUTPUT_PARAM_STATUS, OUTPUT_STATUS_SUCCESS);
104
105         } catch (Exception e) {
106             ctx.setAttribute(responsePrefix + OUTPUT_PARAM_STATUS, OUTPUT_STATUS_FAILURE);
107             ctx.setAttribute(responsePrefix + OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
108             log.error("Error Message : " + e.getMessage(), e);
109             throw new SvcLogicException(e.getMessage());
110         }
111     }
112
113     private Properties loadProperties() throws Exception {
114         String directory = envVariables.getenv(APPC_CONFIG_DIR_VAR);
115         if (directory == null) {
116             throw new Exception("Cannot find Property file: " + APPC_CONFIG_DIR_VAR);
117         }
118         String path = directory + APPC_SOUTHBOUND;
119         return PropertiesLoader.load(path);
120     }
121 }