Changed to unmaintained
[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 : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  *
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.flow.controller.node;
23
24 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_PARAM_RESPONSE_PREFIX;
25 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_PARAM_ERROR_MESSAGE;
26 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_PARAM_STATUS;
27 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_STATUS_FAILURE;
28 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_STATUS_SUCCESS;
29
30 import com.att.eelf.configuration.EELFLogger;
31 import com.att.eelf.configuration.EELFManager;
32 import com.fasterxml.jackson.core.type.TypeReference;
33 import com.fasterxml.jackson.databind.JsonNode;
34 import com.fasterxml.jackson.databind.ObjectMapper;
35 import java.io.IOException;
36 import java.util.Map;
37 import java.util.Map.Entry;
38 import org.apache.commons.lang3.StringUtils;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
42
43 public class JsonParsingNode implements SvcLogicJavaPlugin {
44
45   private static final EELFLogger log = EELFManager.getInstance().getLogger(JsonParsingNode.class);
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(INPUT_PARAM_RESPONSE_PREFIX);
51     responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
52     try {
53       String json = inParams.get("data");
54       log.info("Received response from Interface " + json);
55       JsonNode node = JsonValidator.validate(json);
56
57       if (node != null) {
58         Map<String, String> map = convertToMap(node);
59         for (Entry<String, String> entry : map.entrySet()) {
60           ctx.setAttribute(responsePrefix + entry.getKey(), entry.getValue());
61         }
62       }
63       ctx.setAttribute(responsePrefix + OUTPUT_PARAM_STATUS, OUTPUT_STATUS_SUCCESS);
64
65     } catch (Exception e) {
66       ctx.setAttribute(responsePrefix + OUTPUT_PARAM_STATUS, OUTPUT_STATUS_FAILURE);
67       ctx.setAttribute(responsePrefix + OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
68       log.error(fn + " Error Message : " + e.getMessage(), e);
69       throw new SvcLogicException(e.getMessage());
70     }
71   }
72
73   private Map<String, String> convertToMap(JsonNode node) throws IOException {
74     return new ObjectMapper().readValue(node.toString(), new TypeReference<Map<String, String>>() {
75     });
76   }
77
78 }