Removed MsoLogger class
[so.git] / adapters / mso-vfc-adapter / src / main / java / org / onap / so / adapters / vfc / util / JsonUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2018 IBM.
8  * Modifications Copyright (c) 2019 Samsung
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.so.adapters.vfc.util;
25
26 import java.io.IOException;
27
28 import org.onap.so.adapters.vfc.constant.HttpCode;
29 import org.onap.so.adapters.vfc.exceptions.ApplicationException;
30 import org.onap.so.logger.ErrorCode;
31 import org.onap.so.logger.MessageEnum;
32
33 import com.fasterxml.jackson.annotation.JsonInclude.Include;
34 import com.fasterxml.jackson.core.type.TypeReference;
35 import com.fasterxml.jackson.databind.DeserializationFeature;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * Interface for json analyzing.<br/>
42  * <p>
43  * </p>
44  * 
45  * @author
46  * @version ONAP Amsterdam Release 2017-9-6
47  */
48 public class JsonUtil {
49
50   /**
51    * Log service
52    */
53   private static final Logger logger = LoggerFactory.getLogger(JsonUtil.class);
54
55   /**
56    * Mapper.
57    */
58   private static final ObjectMapper MAPPER = new ObjectMapper();
59   private static final String UNMARSHAL_FAIL_MSG="fail to unMarshal json";
60   static {
61     MAPPER.setConfig(MAPPER.getDeserializationConfig().without(
62         DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
63     MAPPER.setSerializationInclusion(Include.NON_NULL);
64   }
65
66   /**
67    * Constructor<br/>
68    * <p>
69    * </p>
70    * 
71    * @since ONAP Amsterdam Release 2017-9-6
72    */
73   private JsonUtil() {
74
75   }
76
77   /**
78    * Parse the string in form of json.<br/>
79    * 
80    * @param jsonstr json string.
81    * @param type that convert json string to
82    * @return model object
83    * @since ONAP Amsterdam Release 2017-9-6
84    */
85   public static <T> T unMarshal(String jsonstr, Class<T> type) throws ApplicationException {
86     try {
87       return MAPPER.readValue(jsonstr, type);
88     } catch (IOException e) {
89       logger.error("{} {} {}", MessageEnum.RA_NS_EXC.toString(), ErrorCode.BusinessProcesssError.getValue(),
90           UNMARSHAL_FAIL_MSG, e);
91       throw new ApplicationException(HttpCode.BAD_REQUEST, UNMARSHAL_FAIL_MSG);
92     }
93   }
94
95   /**
96    * Parse the string in form of json.<br/>
97    * 
98    * @param jsonstr json string.
99    * @param type that convert json string to
100    * @return model object
101    * @since ONAP Amsterdam Release 2017-9-6
102    */
103   public static <T> T unMarshal(String jsonstr, TypeReference<T> type) throws ApplicationException {
104     try {
105       return MAPPER.readValue(jsonstr, type);
106     } catch (IOException e) {
107       logger.error("{} {} {}", MessageEnum.RA_NS_EXC.toString(), ErrorCode.BusinessProcesssError.getValue(),
108           UNMARSHAL_FAIL_MSG, e);
109       throw new ApplicationException(HttpCode.BAD_REQUEST, UNMARSHAL_FAIL_MSG);
110     }
111   }
112
113   /**
114    * Convert object to json string.<br/>
115    * 
116    * @param srcObj data object
117    * @return json string
118    * @since ONAP Amsterdam Release 2017-9-6
119    */
120   public static String marshal(Object srcObj) throws ApplicationException {
121     try {
122       return MAPPER.writeValueAsString(srcObj);
123     } catch (IOException e) {
124       logger.error("{} {} {}", MessageEnum.RA_NS_EXC.toString(), ErrorCode.BusinessProcesssError.getValue(),
125           "fail to marshal json", e);
126       throw new ApplicationException(HttpCode.BAD_REQUEST, "srcObj marshal failed!");
127     }
128   }
129
130   /**
131    * Get mapper.<br/>
132    * 
133    * @return mapper
134    * @since ONAP Amsterdam Release 2017-9-6
135    */
136   public static ObjectMapper getMapper() {
137     return MAPPER;
138   }
139 }