Improve coverage flow/controller/node #2
[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.core.JsonProcessingException;
36 import com.fasterxml.jackson.databind.JsonNode;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38 import java.io.IOException;
39 import java.util.Map;
40 import java.util.Properties;
41 import org.apache.commons.lang3.StringUtils;
42 import org.onap.appc.flow.controller.data.Transaction;
43 import org.onap.appc.flow.controller.executorImpl.RestExecutor;
44 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
45 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
46 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
47
48 public class RestServiceNode implements SvcLogicJavaPlugin {
49
50   private static final EELFLogger log = EELFManager.getInstance().getLogger(RestServiceNode.class);
51   private static final String SDNC_CONFIG_DIR_VAR = "SDNC_CONFIG_DIR";
52
53   public void sendRequest(Map<String, String> inParams, SvcLogicContext ctx)
54       throws SvcLogicException {
55     String fn = "RestServiceNode.sendRequest";
56     log.info("Received processParamKeys call with params : " + inParams);
57     String responsePrefix = inParams.get(INPUT_PARAM_RESPONSE_PREFIX);
58     try {
59       responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
60       //Remove below for Block
61       for (Object key : ctx.getAttributeKeySet()) {
62         String parmName = (String) key;
63         String parmValue = ctx.getAttribute(parmName);
64         log.info(fn + "Getting Key = " + parmName + "and Value = " + parmValue);
65       }
66
67       send(ctx, inParams);
68       ctx.setAttribute(responsePrefix + OUTPUT_PARAM_STATUS, OUTPUT_STATUS_SUCCESS);
69
70     } catch (Exception e) {
71       ctx.setAttribute(responsePrefix + OUTPUT_PARAM_STATUS, OUTPUT_STATUS_FAILURE);
72       ctx.setAttribute(responsePrefix + OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
73       log.error("Error Message : " + e.getMessage(), e);
74       throw new SvcLogicException(e.getMessage());
75     }
76   }
77
78   private void send(SvcLogicContext ctx, Map<String, String> inParams) throws Exception {
79     try {
80       Properties prop = loadProperties();
81       log.info("Loaded Properties " + prop.toString());
82       String responsePrefix = inParams.get(INPUT_PARAM_RESPONSE_PREFIX);
83       String resourceUri = ResourceUriExtractor.extractResourceUri(ctx, prop);
84       log.info("Rest Constructed URL : " + resourceUri);
85
86       Transaction transaction = TransactionHandler.buildTransaction(ctx, prop, resourceUri);
87
88       RestExecutor restRequestExecutor = new RestExecutor();
89       Map<String, String> output = restRequestExecutor.execute(transaction, ctx);
90
91       if (isValidJson(output.get("restResponse")) != null) {
92         ctx.setAttribute(responsePrefix + "." + OUTPUT_STATUS_MESSAGE,
93             output.get("restResponse"));
94 //                JsonNode restResponse = isValidJson(output.get("restResponse"));
95 //                for (String key : inParams.keySet()) {
96 //                    if(key !=null &&  key.startsWith("output-")){
97 //                            log.info("Found Key = " + key);
98 //                            log.info("Found Key in Params " + inParams.get(key) + ".");
99 //                            JsonNode setValue =  restResponse.findValue(inParams.get(key));                            
100 //                             log.info("Found value = " + setValue);
101 //                             if(setValue !=null && setValue.textValue() !=null && !setValue.textValue().isEmpty())
102 //                                 ctx.setAttribute(responsePrefix + "." + key, setValue.textValue());
103 //                             else
104 //                                 ctx.setAttribute(responsePrefix + "." + key, null);
105 //                    }
106 //                }                
107       }
108       log.info("Response from Rest :");
109
110     } catch (Exception e) {
111       log.error("Error Message: " + e.getMessage(), e);
112       throw e;
113     }
114   }
115
116   private Properties loadProperties() throws Exception {
117     String directory = System.getenv(SDNC_CONFIG_DIR_VAR);
118     if (directory == null) {
119       throw new Exception("Cannot find Property file: " + SDNC_CONFIG_DIR_VAR);
120     }
121     String path = directory + APPC_FLOW_CONTROLLER;
122     return PropertiesLoader.load(path);
123   }
124
125   private JsonNode isValidJson(String json) throws IOException {
126     JsonNode output;
127     log.info("Received response from Interface " + json);
128     if (json == null || json.isEmpty()) {
129       return null;
130     }
131     try {
132       ObjectMapper objectMapper = new ObjectMapper();
133       output = objectMapper.readTree(json);
134     } catch (JsonProcessingException e) {
135       log.warn("Response received from interface is not a valid JSON block" + json, e);
136       return null;
137     }
138     log.info("state is " + output.findValue("state"));
139     return output;
140   }
141 }