Change nexus values to properties
[appc.git] / app-c / appc / 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
34 public class Converter {
35
36
37     public static DmaapOutgoingMessage convJsonNodeToDmaapOutgoingMessage(JsonNode inObj, String rpcName) {
38         DmaapOutgoingMessage outObj = new DmaapOutgoingMessage();
39         outObj.setBody(inObj);
40         outObj.setRpcName(rpcName);
41         return outObj;
42     }
43
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);
50
51     }
52
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;
64     }
65
66     public static String extractRequestIdWithSubId(JsonNode dmaapBody) {
67         String requestId;
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;
75         }
76         return requestId;
77     }
78
79     public static Integer extractStatusCode(JsonNode event) {
80         Integer statusCode;
81         statusCode = event.get("output").get("status").get("code").asInt();
82         return statusCode;
83     }
84
85 }