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;
39 public class Converter {
44 public static DmaapOutgoingMessage convertJsonNodeToDmaapOutgoingMessage(DmaapMessage event, JsonNode inObj){
46 if (event == null || inObj == null) {
47 throw new IllegalArgumentException("One of given arguments is null");
50 DmaapOutgoingMessage outObj = new DmaapOutgoingMessage();
51 outObj.setBody(inObj);
52 outObj.setRpcName(event.getRpcName());
53 outObj.setVersion(event.getVersion());
54 outObj.setType("response");
55 outObj.setCorrelationID(event.getCorrelationID());
59 public static String convertDmaapOutgoingMessageToJsonString(DmaapMessage inObj) throws JsonProcessingException {
62 throw new IllegalArgumentException("Input message is null");
65 ObjectMapper objectMapper = new ObjectMapper();
66 ObjectWriter writer = objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
67 .configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
68 .writer().withFeatures(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
69 return writer.writeValueAsString(inObj);
73 public static DmaapOutgoingMessage buildDmaapOutgoingMessageWithUnexpectedError(DmaapMessage event,
74 Exception inputException) {
76 if (event == null || inputException == null) {
77 throw new IllegalArgumentException("One of given arguments is null");
80 DmaapOutgoingMessage dmaapOutgoingMessage;
82 StringUtils.isEmpty(inputException.getMessage()) ? inputException.toString() : inputException.getMessage();
83 JSONObject commonHeaderJsonObject = Mapper.toJsonObject(event.getBody().get("input").get("common-header"));
84 JSONObject jsonObjectOutput = new JSONObject().accumulate("common-header", commonHeaderJsonObject)
85 .accumulate("status", new JSONObject().accumulate("code", 200).accumulate("value", errMsg));
86 dmaapOutgoingMessage = new DmaapOutgoingMessage();
87 dmaapOutgoingMessage.setRpcName(event.getRpcName());
88 dmaapOutgoingMessage.setCorrelationID(event.getCorrelationID());
89 dmaapOutgoingMessage.setType("error");
90 dmaapOutgoingMessage.setVersion(event.getVersion());
91 JSONObject jsonObjectBody = new JSONObject().accumulate("output", jsonObjectOutput);
92 JsonNode jsonNodeBody = Mapper.toJsonNodeFromJsonString(jsonObjectBody.toString());
93 dmaapOutgoingMessage.setBody(jsonNodeBody);
94 return dmaapOutgoingMessage;
97 public static String extractRequestIdWithSubId(JsonNode dmaapBody) {
99 if (dmaapBody == null) {
100 throw new IllegalArgumentException("Dmaap body is null");
103 JsonNode commonHeaderJsonNode = dmaapBody.get("input").get("common-header");
104 String requestId = getValue(commonHeaderJsonNode, "request-id", "");
105 String subRequestId = getValue(commonHeaderJsonNode, "sub-request-id", "");
106 if (!StringUtils.isEmpty(subRequestId)) {
107 requestId = requestId + "-" + subRequestId;
112 public static Integer extractStatusCode(JsonNode event) {
115 throw new IllegalArgumentException("Input event is null");
119 statusCode = event.get("output").get("status").get("code").asInt();
123 private static String getValue(JsonNode jsonNode, String name, String defaultValue) {
124 if (jsonNode == null) {
127 JsonNode childJsonNode = jsonNode.get(name);
128 if (childJsonNode == null) {
131 String value = childJsonNode.asText();