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 com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
27 import com.fasterxml.jackson.core.JsonProcessingException;
28 import com.fasterxml.jackson.core.type.TypeReference;
29 import com.fasterxml.jackson.databind.JsonNode;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import java.io.IOException;
33 import java.util.Map.Entry;
34 import org.apache.commons.lang3.StringUtils;
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;
40 public class JsonParsingNode implements SvcLogicJavaPlugin {
42 private static final EELFLogger log = EELFManager.getInstance().getLogger(JsonParsingNode.class);
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 + ".") : "";
50 //Remove below for Block
51 JsonNode jnode = isValidJson(inParams.get("data"));
53 Map<String, Object> map
54 = new ObjectMapper().readValue(jnode.toString(), new TypeReference<Map<String, String>>(){});
55 for (Entry<String, Object> entry : map.entrySet()) {
56 ctx.setAttribute(responsePrefix + entry.getKey(),(String) entry.getValue());
59 ctx.setAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_STATUS,
60 FlowControllerConstants.OUTPUT_STATUS_SUCCESS);
62 } catch (Exception e) {
63 ctx.setAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_STATUS,
64 FlowControllerConstants.OUTPUT_STATUS_FAILURE);
65 ctx.setAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_ERROR_MESSAGE,
67 log.error(fn + " Error Message : " + e.getMessage(), e);
68 throw new SvcLogicException(e.getMessage());
72 private JsonNode isValidJson(String json) throws IOException {
74 log.info("Received response from Interface " + json);
75 if (json == null || json.isEmpty()) {
79 ObjectMapper objectMapper = new ObjectMapper();
80 output = objectMapper.readTree(json);
81 } catch (JsonProcessingException e) {
82 log.warn("Response received from interface is not a valid JSON block" + json, e);