e3f5a9700d10bb51a431724fe9a5e3ac93b1b2b6
[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  * APPC
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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.listener.LCM.conv;
24
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;
33
34 public class Converter {
35    
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());
43         return outObj;
44     }
45
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);
52
53     }
54
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;
69     }
70
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;
78         }
79         return requestId;
80     }
81
82     public static Integer extractStatusCode(JsonNode event) {
83         Integer statusCode;
84         statusCode = event.get("output").get("status").get("code").asInt();
85         return statusCode;
86     }
87
88     private static String getValue(JsonNode jsonNode,String name,String defaultValue){
89         if(jsonNode == null){
90             return defaultValue;
91         }
92         JsonNode childJsonNode = jsonNode.get(name);
93         if(childJsonNode == null){
94             return defaultValue;
95         }
96         String value = childJsonNode.asText();
97         if(value == null){
98             return defaultValue;
99         }
100         return value;
101     }
102
103 }