2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.listener.LCM.conv;
26 import com.fasterxml.jackson.annotation.JsonInclude;
27 import com.fasterxml.jackson.core.JsonProcessingException;
28 import com.fasterxml.jackson.databind.JsonNode;
29 import com.fasterxml.jackson.databind.MapperFeature;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import com.fasterxml.jackson.databind.ObjectWriter;
32 import com.fasterxml.jackson.databind.SerializationFeature;
33 import org.apache.commons.lang3.StringUtils;
34 import org.json.JSONObject;
35 import org.onap.appc.listener.LCM.model.DmaapMessage;
36 import org.onap.appc.listener.LCM.model.DmaapOutgoingMessage;
37 import org.onap.appc.listener.util.Mapper;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
40 public class Converter {
45 public static DmaapOutgoingMessage convertJsonNodeToDmaapOutgoingMessage(DmaapMessage event, JsonNode inObj){
47 if (event == null || inObj == null) {
48 throw new IllegalArgumentException("One of given arguments is null");
51 DmaapOutgoingMessage outObj = new DmaapOutgoingMessage();
52 outObj.setBody(inObj);
53 outObj.setRpcName(event.getRpcName());
54 outObj.setVersion(event.getVersion());
55 outObj.setType("response");
56 outObj.setCorrelationID(event.getCorrelationID());
60 public static String convertDmaapOutgoingMessageToJsonString(DmaapMessage inObj) throws JsonProcessingException {
63 throw new IllegalArgumentException("Input message is null");
66 ObjectMapper objectMapper = new ObjectMapper();
67 ObjectWriter writer = objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
68 .configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
69 .writer().withFeatures(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
70 return writer.writeValueAsString(inObj);
74 public static DmaapOutgoingMessage buildDmaapOutgoingMessageWithUnexpectedError(DmaapMessage event,
75 Exception inputException) {
77 if (event == null || inputException == null) {
78 throw new IllegalArgumentException("One of given arguments is null");
81 DmaapOutgoingMessage dmaapOutgoingMessage;
83 StringUtils.isEmpty(inputException.getMessage()) ? inputException.toString() : inputException.getMessage();
84 JSONObject commonHeaderJsonObject = Mapper.toJsonObject(event.getBody().get("input").get("common-header"));
85 JSONObject jsonObjectOutput = new JSONObject().accumulate("common-header", commonHeaderJsonObject)
86 .accumulate("status", new JSONObject().accumulate("code", 200).accumulate("value", errMsg));
87 dmaapOutgoingMessage = new DmaapOutgoingMessage();
88 dmaapOutgoingMessage.setRpcName(event.getRpcName());
89 dmaapOutgoingMessage.setCorrelationID(event.getCorrelationID());
90 dmaapOutgoingMessage.setType("error");
91 dmaapOutgoingMessage.setVersion(event.getVersion());
92 JSONObject jsonObjectBody = new JSONObject().accumulate("output", jsonObjectOutput);
93 JsonNode jsonNodeBody = Mapper.toJsonNodeFromJsonString(jsonObjectBody.toString());
94 dmaapOutgoingMessage.setBody(jsonNodeBody);
95 return dmaapOutgoingMessage;
98 public static String extractRequestIdWithSubId(JsonNode dmaapBody) {
100 if (dmaapBody == null) {
101 throw new IllegalArgumentException("Dmaap body is null");
104 JsonNode commonHeaderJsonNode = dmaapBody.get("input").get("common-header");
105 String requestId = getValue(commonHeaderJsonNode, "request-id", "");
106 String subRequestId = getValue(commonHeaderJsonNode, "sub-request-id", "");
107 if (!StringUtils.isEmpty(subRequestId)) {
108 requestId = requestId + "-" + subRequestId;
113 public static Integer extractStatusCode(JsonNode event) {
116 throw new IllegalArgumentException("Input event is null");
120 statusCode = event.get("output").get("status").get("code").asInt();
124 private static String getValue(JsonNode jsonNode, String name, String defaultValue) {
125 if (jsonNode == null) {
128 JsonNode childJsonNode = jsonNode.get(name);
129 if (childJsonNode == null) {
132 String value = childJsonNode.asText();