2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.listener.LCM.conv;
24 import com.fasterxml.jackson.annotation.JsonInclude;
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import com.fasterxml.jackson.databind.*;
27 import org.apache.commons.lang3.StringUtils;
28 import org.json.JSONObject;
29 import org.openecomp.appc.listener.LCM.model.DmaapMessage;
30 import org.openecomp.appc.listener.LCM.model.DmaapOutgoingMessage;
31 import org.openecomp.appc.listener.util.Mapper;
33 public class Converter {
35 public static DmaapOutgoingMessage convJsonNodeToDmaapOutgoingMessage(DmaapMessage event, JsonNode inObj) {
36 DmaapOutgoingMessage outObj = new DmaapOutgoingMessage();
37 outObj.setBody(inObj);
38 outObj.setRpcName(event.getRpcName());
39 outObj.setVersion(event.getVersion());
40 outObj.setType("response");
41 outObj.setCorrelationID(event.getCorrelationID());
45 public static String convDmaapOutgoingMessageToJsonString(DmaapMessage inObj) throws JsonProcessingException {
46 // return Mapper.toJsonString(inObj);
47 ObjectMapper objectMapper = new ObjectMapper();
48 ObjectWriter writer = objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL).configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY,true)
49 .writer().withFeatures(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
50 return writer.writeValueAsString(inObj);
54 public static DmaapOutgoingMessage buildDmaapOutgoingMessageWithUnexpectedError(DmaapMessage event,Exception inputException) {
55 DmaapOutgoingMessage dmaapOutgoingMessage = null;
56 String errMsg = StringUtils.isEmpty(inputException.getMessage())? inputException.toString() : inputException.getMessage();
57 JSONObject commonHeaderJsonObject = Mapper.toJsonObject(event.getBody().get("input").get("common-header"));
58 JSONObject jsonObjectOutput = new JSONObject().accumulate("common-header", commonHeaderJsonObject).accumulate("status", new JSONObject().accumulate("code",200).accumulate("value",errMsg));
59 dmaapOutgoingMessage = new DmaapOutgoingMessage();
60 dmaapOutgoingMessage.setRpcName(event.getRpcName());
61 dmaapOutgoingMessage.setCorrelationID(event.getCorrelationID());
62 dmaapOutgoingMessage.setType("error");
63 dmaapOutgoingMessage.setVersion(event.getVersion());
64 JSONObject jsonObjectBody = new JSONObject().accumulate("output",jsonObjectOutput);
65 JsonNode jsonNodeBody = Mapper.toJsonNodeFromJsonString(jsonObjectBody.toString());
66 dmaapOutgoingMessage.setBody(jsonNodeBody);
67 return dmaapOutgoingMessage;
70 public static String extractRequestIdWithSubId(JsonNode dmaapBody) {
71 //TODO: null pointer exception if dmaapBody is null, check if null or ensure is not null before calling
72 JsonNode commonHeaderJsonNode = dmaapBody.get("input").get("common-header");
73 String requestId = getValue(commonHeaderJsonNode,"request-id","");
74 String subRequestId = getValue(commonHeaderJsonNode,"sub-request-id","");
75 if(!StringUtils.isEmpty(subRequestId)){
76 requestId = requestId +"-"+subRequestId;
81 public static Integer extractStatusCode(JsonNode event) {
83 statusCode = event.get("output").get("status").get("code").asInt();
87 private static String getValue(JsonNode jsonNode,String name,String defaultValue){
91 JsonNode childJsonNode = jsonNode.get(name);
92 if(childJsonNode == null){
95 String value = childJsonNode.asText();