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