2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.listener.LCM.conv;
27 import com.fasterxml.jackson.annotation.JsonInclude;
28 import com.fasterxml.jackson.core.JsonProcessingException;
29 import com.fasterxml.jackson.databind.JsonNode;
30 import com.fasterxml.jackson.databind.MapperFeature;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.fasterxml.jackson.databind.ObjectWriter;
33 import com.fasterxml.jackson.databind.SerializationFeature;
34 import org.apache.commons.lang3.StringUtils;
35 import org.json.JSONObject;
36 import org.onap.appc.listener.LCM.model.DmaapMessage;
37 import org.onap.appc.listener.LCM.model.DmaapOutgoingMessage;
38 import org.onap.appc.listener.util.Mapper;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
41 public class Converter {
46 public static DmaapOutgoingMessage convertJsonNodeToDmaapOutgoingMessage(DmaapMessage event, JsonNode inObj){
48 if (event == null || inObj == null) {
49 throw new IllegalArgumentException("One of given arguments is null");
52 DmaapOutgoingMessage outObj = new DmaapOutgoingMessage();
53 outObj.setBody(inObj);
54 outObj.setRpcName(event.getRpcName());
55 outObj.setVersion(event.getVersion());
56 outObj.setType("response");
57 outObj.setCorrelationID(event.getCorrelationID());
61 public static String convertDmaapOutgoingMessageToJsonString(DmaapMessage inObj) throws JsonProcessingException {
64 throw new IllegalArgumentException("Input message is null");
67 ObjectMapper objectMapper = new ObjectMapper();
68 ObjectWriter writer = objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
69 .configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
70 .writer().withFeatures(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
71 return writer.writeValueAsString(inObj);
75 public static DmaapOutgoingMessage buildDmaapOutgoingMessageWithUnexpectedError(DmaapMessage event,
76 Exception inputException) {
78 if (event == null || inputException == null) {
79 throw new IllegalArgumentException("One of given arguments is null");
82 DmaapOutgoingMessage dmaapOutgoingMessage;
84 StringUtils.isEmpty(inputException.getMessage()) ? inputException.toString() : inputException.getMessage();
85 JSONObject commonHeaderJsonObject = Mapper.toJsonObject(event.getBody().get("input").get("common-header"));
86 JSONObject jsonObjectOutput = new JSONObject().accumulate("common-header", commonHeaderJsonObject)
87 .accumulate("status", new JSONObject().accumulate("code", 200).accumulate("value", errMsg));
88 dmaapOutgoingMessage = new DmaapOutgoingMessage();
89 dmaapOutgoingMessage.setRpcName(event.getRpcName());
90 dmaapOutgoingMessage.setCorrelationID(event.getCorrelationID());
91 dmaapOutgoingMessage.setType("error");
92 dmaapOutgoingMessage.setVersion(event.getVersion());
93 JSONObject jsonObjectBody = new JSONObject().accumulate("output", jsonObjectOutput);
94 JsonNode jsonNodeBody = Mapper.toJsonNodeFromJsonString(jsonObjectBody.toString());
95 dmaapOutgoingMessage.setBody(jsonNodeBody);
96 return dmaapOutgoingMessage;
99 public static String extractRequestIdWithSubId(JsonNode dmaapBody) {
101 if (dmaapBody == null) {
102 throw new IllegalArgumentException("Dmaap body is null");
105 JsonNode commonHeaderJsonNode = dmaapBody.get("input").get("common-header");
106 String requestId = getValue(commonHeaderJsonNode, "request-id", "");
107 String subRequestId = getValue(commonHeaderJsonNode, "sub-request-id", "");
108 if (!StringUtils.isEmpty(subRequestId)) {
109 requestId = requestId + "-" + subRequestId;
114 public static Integer extractStatusCode(JsonNode event) {
117 throw new IllegalArgumentException("Input event is null");
121 statusCode = event.get("output").get("status").get("code").asInt();
125 private static String getValue(JsonNode jsonNode, String name, String defaultValue) {
126 if (jsonNode == null) {
129 JsonNode childJsonNode = jsonNode.get(name);
130 if (childJsonNode == null) {
133 String value = childJsonNode.asText();