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;
 
  34 public class Converter {
 
  37     public static DmaapOutgoingMessage convJsonNodeToDmaapOutgoingMessage(JsonNode inObj, String rpcName) {
 
  38         DmaapOutgoingMessage outObj = new DmaapOutgoingMessage();
 
  39         outObj.setBody(inObj);
 
  40         outObj.setRpcName(rpcName);
 
  44     public static String convDmaapOutgoingMessageToJsonString(DmaapMessage inObj) throws JsonProcessingException {
 
  45 //        return Mapper.toJsonString(inObj);
 
  46         ObjectMapper objectMapper = new ObjectMapper();
 
  47         ObjectWriter writer = objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL).configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY,true)
 
  48                 .writer().withFeatures(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
 
  49         return writer.writeValueAsString(inObj);
 
  53     public static DmaapOutgoingMessage buildDmaapOutgoingMessageWithUnexpectedError(JsonNode dmaapInputBody, String rpcName,Exception inputException) {
 
  54         DmaapOutgoingMessage dmaapOutgoingMessage = null;
 
  55         String errMsg = StringUtils.isEmpty(inputException.getMessage())? inputException.toString() : inputException.getMessage();
 
  56         JSONObject commonHeaderJsonObject = Mapper.toJsonObject(dmaapInputBody.get("input").get("common-header"));
 
  57         JSONObject jsonObjectOutput = new JSONObject().accumulate("common-header", commonHeaderJsonObject).accumulate("status", new JSONObject().accumulate("code",200).accumulate("value",errMsg));
 
  58         dmaapOutgoingMessage = new DmaapOutgoingMessage();
 
  59         dmaapOutgoingMessage.setRpcName(rpcName);
 
  60         JSONObject jsonObjectBody = new JSONObject().accumulate("output",jsonObjectOutput);
 
  61         JsonNode jsonNodeBody = Mapper.toJsonNodeFromJsonString(jsonObjectBody.toString());
 
  62         dmaapOutgoingMessage.setBody(jsonNodeBody);
 
  63         return dmaapOutgoingMessage;
 
  66     public static String extractRequestIdWithSubId(JsonNode dmaapBody) {
 
  68         //TODO: null pointer exception if dmaapBody is null, check if null or ensure is not null before calling
 
  69         JsonNode commonHeaderJsonNode = dmaapBody.get("input").get("common-header");
 
  70         requestId = commonHeaderJsonNode.get("request-id").asText();
 
  71         requestId = requestId != null ? requestId : "";
 
  72         String subRequestId = commonHeaderJsonNode.get("sub-request-id").asText();
 
  73         if(!StringUtils.isEmpty(subRequestId)){
 
  74             requestId = requestId +"-"+subRequestId;
 
  79     public static Integer extractStatusCode(JsonNode event) {
 
  81         statusCode = event.get("output").get("status").get("code").asInt();