Merge of new rebased code
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / main / java / org / openecomp / appc / listener / LCM / conv / Converter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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  */
21
22 package org.openecomp.appc.listener.LCM.conv;
23
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;
32
33 public class Converter {
34    
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());
42         return outObj;
43     }
44
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);
51
52     }
53
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;
68     }
69
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;
77         }
78         return requestId;
79     }
80
81     public static Integer extractStatusCode(JsonNode event) {
82         Integer statusCode;
83         statusCode = event.get("output").get("status").get("code").asInt();
84         return statusCode;
85     }
86
87     private static String getValue(JsonNode jsonNode,String name,String defaultValue){
88         if(jsonNode == null){
89             return defaultValue;
90         }
91         JsonNode childJsonNode = jsonNode.get(name);
92         if(childJsonNode == null){
93             return defaultValue;
94         }
95         String value = childJsonNode.asText();
96         if(value == null){
97             return defaultValue;
98         }
99         return value;
100     }
101
102 }