2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20 * ============LICENSE_END=========================================================
23 package org.onap.appc.flow.controller.node;
25 import java.io.IOException;
27 import java.util.Map.Entry;
29 import org.apache.commons.lang3.StringUtils;
30 import org.onap.appc.flow.controller.utils.FlowControllerConstants;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37 import com.fasterxml.jackson.core.JsonProcessingException;
38 import com.fasterxml.jackson.core.type.TypeReference;
39 import com.fasterxml.jackson.databind.JsonNode;
40 import com.fasterxml.jackson.databind.ObjectMapper;
42 public class JsonParsingNode implements SvcLogicJavaPlugin{
44 private static final EELFLogger log = EELFManager.getInstance().getLogger(JsonParsingNode.class);
46 public void parse(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
47 String fn = "RestServiceNode.sendRequest";
48 log.info("Received processParamKeys call with params : " + inParams);
49 String responsePrefix = inParams.get(FlowControllerConstants.INPUT_PARAM_RESPONSE_PRIFIX);
50 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
52 //Remove below for Block
53 JsonNode jnode = isValidJSON(inParams.get("data"));
55 Map<String, Object> map = new ObjectMapper().readValue(jnode.toString(), new TypeReference<Map<String, String>>(){});
56 for (Entry<String, Object> entry : map.entrySet())
58 ctx.setAttribute(responsePrefix + entry.getKey(),(String) entry.getValue());
61 ctx.setAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_STATUS, FlowControllerConstants.OUTPUT_STATUS_SUCCESS);
63 } catch (Exception e) {
64 ctx.setAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_STATUS, FlowControllerConstants.OUTPUT_STATUS_FAILURE);
65 ctx.setAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
66 log.error(fn + " Error Message : " + e.getMessage(), e);
67 throw new SvcLogicException(e.getMessage());
71 private JsonNode isValidJSON(String json) throws IOException {
73 log.info("Received response from Interface " + json);
74 if(json == null || json.isEmpty())
77 ObjectMapper objectMapper = new ObjectMapper();
78 output = objectMapper.readTree(json);
79 } catch(JsonProcessingException e) {
80 log.warn("Response received from interface is not a valid JSON block" + json, e);