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.*;
30 import org.apache.commons.lang3.StringUtils;
31 import org.json.JSONObject;
32 import org.onap.appc.listener.LCM.model.DmaapMessage;
33 import org.onap.appc.listener.LCM.model.DmaapOutgoingMessage;
34 import org.onap.appc.listener.util.Mapper;
36 public class Converter {
38 public static DmaapOutgoingMessage convJsonNodeToDmaapOutgoingMessage(DmaapMessage event, JsonNode inObj) {
39 DmaapOutgoingMessage outObj = new DmaapOutgoingMessage();
40 outObj.setBody(inObj);
41 outObj.setRpcName(event.getRpcName());
42 outObj.setVersion(event.getVersion());
43 outObj.setType("response");
44 outObj.setCorrelationID(event.getCorrelationID());
48 public static String convDmaapOutgoingMessageToJsonString(DmaapMessage inObj) throws JsonProcessingException {
49 // return Mapper.toJsonString(inObj);
50 ObjectMapper objectMapper = new ObjectMapper();
51 ObjectWriter writer = objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL).configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY,true)
52 .writer().withFeatures(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
53 return writer.writeValueAsString(inObj);
57 public static DmaapOutgoingMessage buildDmaapOutgoingMessageWithUnexpectedError(DmaapMessage event,Exception inputException) {
58 DmaapOutgoingMessage dmaapOutgoingMessage = null;
59 String errMsg = StringUtils.isEmpty(inputException.getMessage())? inputException.toString() : inputException.getMessage();
60 JSONObject commonHeaderJsonObject = Mapper.toJsonObject(event.getBody().get("input").get("common-header"));
61 JSONObject jsonObjectOutput = new JSONObject().accumulate("common-header", commonHeaderJsonObject).accumulate("status", new JSONObject().accumulate("code",200).accumulate("value",errMsg));
62 dmaapOutgoingMessage = new DmaapOutgoingMessage();
63 dmaapOutgoingMessage.setRpcName(event.getRpcName());
64 dmaapOutgoingMessage.setCorrelationID(event.getCorrelationID());
65 dmaapOutgoingMessage.setType("error");
66 dmaapOutgoingMessage.setVersion(event.getVersion());
67 JSONObject jsonObjectBody = new JSONObject().accumulate("output",jsonObjectOutput);
68 JsonNode jsonNodeBody = Mapper.toJsonNodeFromJsonString(jsonObjectBody.toString());
69 dmaapOutgoingMessage.setBody(jsonNodeBody);
70 return dmaapOutgoingMessage;
73 public static String extractRequestIdWithSubId(JsonNode dmaapBody) {
74 //TODO: null pointer exception if dmaapBody is null, check if null or ensure is not null before calling
75 JsonNode commonHeaderJsonNode = dmaapBody.get("input").get("common-header");
76 String requestId = getValue(commonHeaderJsonNode,"request-id","");
77 String subRequestId = getValue(commonHeaderJsonNode,"sub-request-id","");
78 if(!StringUtils.isEmpty(subRequestId)){
79 requestId = requestId +"-"+subRequestId;
84 public static Integer extractStatusCode(JsonNode event) {
86 statusCode = event.get("output").get("status").get("code").asInt();
90 private static String getValue(JsonNode jsonNode,String name,String defaultValue){
94 JsonNode childJsonNode = jsonNode.get(name);
95 if(childJsonNode == null){
98 String value = childJsonNode.asText();