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