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