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