2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.listener.LCM.conv;
25 import com.fasterxml.jackson.annotation.JsonInclude;
26 import com.fasterxml.jackson.core.JsonProcessingException;
27 import com.fasterxml.jackson.databind.*;
28 import org.apache.commons.lang3.StringUtils;
29 import org.json.JSONObject;
30 import org.openecomp.appc.listener.LCM.model.DmaapMessage;
31 import org.openecomp.appc.listener.LCM.model.DmaapOutgoingMessage;
32 import org.openecomp.appc.listener.util.Mapper;
34 public class Converter {
36 public static DmaapOutgoingMessage convJsonNodeToDmaapOutgoingMessage(DmaapMessage event, JsonNode inObj) {
37 DmaapOutgoingMessage outObj = new DmaapOutgoingMessage();
38 outObj.setBody(inObj);
39 outObj.setRpcName(event.getRpcName());
40 outObj.setVersion(event.getVersion());
41 outObj.setType("response");
42 outObj.setCorrelationID(event.getCorrelationID());
46 public static String convDmaapOutgoingMessageToJsonString(DmaapMessage inObj) throws JsonProcessingException {
47 // return Mapper.toJsonString(inObj);
48 ObjectMapper objectMapper = new ObjectMapper();
49 ObjectWriter writer = objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL).configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY,true)
50 .writer().withFeatures(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
51 return writer.writeValueAsString(inObj);
55 public static DmaapOutgoingMessage buildDmaapOutgoingMessageWithUnexpectedError(DmaapMessage event,Exception inputException) {
56 DmaapOutgoingMessage dmaapOutgoingMessage = null;
57 String errMsg = StringUtils.isEmpty(inputException.getMessage())? inputException.toString() : inputException.getMessage();
58 JSONObject commonHeaderJsonObject = Mapper.toJsonObject(event.getBody().get("input").get("common-header"));
59 JSONObject jsonObjectOutput = new JSONObject().accumulate("common-header", commonHeaderJsonObject).accumulate("status", new JSONObject().accumulate("code",200).accumulate("value",errMsg));
60 dmaapOutgoingMessage = new DmaapOutgoingMessage();
61 dmaapOutgoingMessage.setRpcName(event.getRpcName());
62 dmaapOutgoingMessage.setCorrelationID(event.getCorrelationID());
63 dmaapOutgoingMessage.setType("error");
64 dmaapOutgoingMessage.setVersion(event.getVersion());
65 JSONObject jsonObjectBody = new JSONObject().accumulate("output",jsonObjectOutput);
66 JsonNode jsonNodeBody = Mapper.toJsonNodeFromJsonString(jsonObjectBody.toString());
67 dmaapOutgoingMessage.setBody(jsonNodeBody);
68 return dmaapOutgoingMessage;
71 public static String extractRequestIdWithSubId(JsonNode dmaapBody) {
72 //TODO: null pointer exception if dmaapBody is null, check if null or ensure is not null before calling
73 JsonNode commonHeaderJsonNode = dmaapBody.get("input").get("common-header");
74 String requestId = getValue(commonHeaderJsonNode,"request-id","");
75 String subRequestId = getValue(commonHeaderJsonNode,"sub-request-id","");
76 if(!StringUtils.isEmpty(subRequestId)){
77 requestId = requestId +"-"+subRequestId;
82 public static Integer extractStatusCode(JsonNode event) {
84 statusCode = event.get("output").get("status").get("code").asInt();
88 private static String getValue(JsonNode jsonNode,String name,String defaultValue){
92 JsonNode childJsonNode = jsonNode.get(name);
93 if(childJsonNode == null){
96 String value = childJsonNode.asText();