Sonar fixes in RestServiceNode Part2
[appc.git] / appc-config / appc-flow-controller / provider / src / main / java / org / onap / appc / flow / controller / node / JsonParsingNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APP-C
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.appc.flow.controller.node;
22
23 import java.io.IOException;
24 import java.util.Map;
25 import java.util.Map.Entry;
26
27 import org.apache.commons.lang3.StringUtils;
28 import org.onap.appc.flow.controller.utils.FlowControllerConstants;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
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.core.type.TypeReference;
37 import com.fasterxml.jackson.databind.JsonNode;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39
40 public class JsonParsingNode implements SvcLogicJavaPlugin{
41
42     private static final  EELFLogger log = EELFManager.getInstance().getLogger(JsonParsingNode.class);
43
44     public void parse(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
45         String fn = "RestServiceNode.sendRequest";
46         log.info("Received processParamKeys call with params : " + inParams);
47         String responsePrefix = inParams.get(FlowControllerConstants.INPUT_PARAM_RESPONSE_PRIFIX);
48         responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
49         try {
50             //Remove below for Block
51             JsonNode jnode = isValidJSON(inParams.get("data"));
52             if(jnode != null) {
53                 Map<String, Object> map = new ObjectMapper().readValue(jnode.toString(), new TypeReference<Map<String, String>>(){});
54                 for (Entry<String, Object> entry : map.entrySet())
55                 {
56                     ctx.setAttribute(responsePrefix + entry.getKey(),(String) entry.getValue());
57                 }
58             }
59             ctx.setAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_STATUS, FlowControllerConstants.OUTPUT_STATUS_SUCCESS);
60             
61         } catch (Exception e) {
62             ctx.setAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_STATUS, FlowControllerConstants.OUTPUT_STATUS_FAILURE);
63             ctx.setAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
64             log.error(fn + " Error Message : " + e.getMessage(), e);
65             throw new SvcLogicException(e.getMessage());
66         }
67     }
68
69     private JsonNode isValidJSON(String json) throws IOException {
70         JsonNode output;
71         log.info("Received response from Interface " + json);
72         if(json == null  || json.isEmpty())
73             return null;
74         try {
75             ObjectMapper objectMapper = new ObjectMapper();
76             output = objectMapper.readTree(json);
77         } catch(JsonProcessingException e) {
78             log.warn("Response received from interface is not a valid JSON block" + json, e);
79             return null;
80         }
81         return output;
82     }
83 }