enahance Sonar coverage rate
[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.HashMap;
25 import java.util.Map;
26 import java.util.Map.Entry;
27
28 import org.apache.commons.lang3.StringUtils;
29 import org.onap.appc.flow.controller.utils.FlowControllerConstants;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
33
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36 import com.fasterxml.jackson.core.JsonProcessingException;
37 import com.fasterxml.jackson.core.type.TypeReference;
38 import com.fasterxml.jackson.databind.JsonNode;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40
41 public class JsonParsingNode implements SvcLogicJavaPlugin{
42
43
44     private static final  EELFLogger log = EELFManager.getInstance().getLogger(JsonParsingNode.class);
45     private static final String SDNC_CONFIG_DIR_VAR = "SDNC_CONFIG_DIR";
46
47     public void parse(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
48         String fn = "RestServiceNode.sendRequest";
49         log.info("Received processParamKeys call with params : " + inParams);
50         String responsePrefix = inParams.get(FlowControllerConstants.INPUT_PARAM_RESPONSE_PRIFIX);
51         try
52         {
53             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";        
54             //Remove below for Block
55             if(isValidJSON(inParams.get("data")) !=null){
56                 JsonNode jnode = isValidJSON(inParams.get("data"));
57                 ObjectMapper mapper = new ObjectMapper();
58                 Map<String, Object> map = new HashMap<String, Object>();
59                 map = mapper.readValue(jnode.toString(), new TypeReference<Map<String, String>>(){});                
60                 for (Entry<String, Object> entry : map.entrySet())
61                 {
62                     ctx.setAttribute(responsePrefix + entry.getKey(),(String) entry.getValue());
63                 }
64             
65             }
66             ctx.setAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_STATUS, FlowControllerConstants.OUTPUT_STATUS_SUCCESS);
67             
68         } catch (Exception e) {
69             ctx.setAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_STATUS, FlowControllerConstants.OUTPUT_STATUS_FAILURE);
70             ctx.setAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
71             e.printStackTrace();
72             log.error("Error Message : "  + e.getMessage());
73             throw new SvcLogicException(e.getMessage());
74         }
75     }
76
77     public JsonNode isValidJSON(String json) throws IOException {
78         JsonNode output = null;     
79         log.info("Received response from Interface " + json);
80         if(json ==null  || json.isEmpty())
81             return null;
82         try{ 
83             ObjectMapper objectMapper = new ObjectMapper();
84             output = objectMapper.readTree(json);
85         } catch(JsonProcessingException e){
86             log.warn("Response received from interface is not a valid JSON block" + json);
87             return null;
88         }    
89         
90         return output;
91     }
92 }