Moving all files to root directory
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / main / java / org / openecomp / appc / listener / util / Mapper.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.util;
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import com.fasterxml.jackson.databind.JsonNode;
29 import org.json.JSONObject;
30 import com.att.eelf.configuration.EELFLogger;
31 import com.att.eelf.configuration.EELFManager;
32
33 import com.fasterxml.jackson.databind.ObjectMapper;
34
35 public class Mapper {
36
37     private static final EELFLogger LOG = EELFManager.getInstance().getLogger(Mapper.class);
38
39     private static ObjectMapper mapper = new ObjectMapper();
40
41     /**
42      * @return The object mapper that we are using.
43      */
44     public static ObjectMapper getMapper() {
45         return mapper;
46     }
47
48     /**
49      * Convert a String to a DcaeMessage
50      * 
51      * @param data
52      *            The json string to try and parse
53      * @return A DcaeMessage from the json string or null if it could not
54      */
55     public static <T> T mapOne(String data, Class<T> cls) {
56         try {
57             return mapper.readValue(data, cls);
58         } catch (Exception e) {
59             LOG.warn(String.format("Could not map [ %s ] to %s", data, cls.getName()), e);
60             return null;
61         }
62     }
63
64     public static <T> List<T> mapList(List<String> data, Class<T> cls) {
65         List<T> out = new ArrayList<T>();
66         for (String s : data) {
67             T tmp = Mapper.mapOne(s, cls);
68             if (tmp != null) {
69                 out.add(tmp);
70             }
71         }
72         return out;
73     }
74
75     /**
76      * Convenience method to try and convert objects to json String
77      * 
78      * @param obj
79      *            The object to try and convert
80      * @return A json string representing the object or null if it could not be converted
81      */
82     public static String toJsonString(Object obj) {
83         String jsonStr;
84         try {
85             if (obj instanceof JSONObject) {
86                 jsonStr = obj.toString();
87             }else {
88                 jsonStr = mapper.writeValueAsString(obj);
89             }
90             return jsonStr;
91         } catch (Exception e) {
92             LOG.warn(String.format("Could not map %s to JSONObject.", obj), e);
93             return null;
94         }
95     }
96
97     public static JSONObject toJsonObject(Object obj) {
98         String jsonStr;
99         try {
100             if (obj.getClass().equals(String.class)) {
101                 jsonStr = (String) obj;
102             } else {
103                 jsonStr = mapper.writeValueAsString(obj);
104             }
105             return new JSONObject(jsonStr);
106         } catch (Exception e) {
107             LOG.warn(String.format("Could not map %s to JSONObject.", obj), e);
108             return null;
109         }
110     }
111     public static JsonNode toJsonNodeFromJsonString(String jsonStr) {
112         JsonNode jsonNode = null;
113         if(jsonStr != null) {
114             try {
115                 jsonNode = mapper.readTree(jsonStr);
116             } catch (IOException e) {
117                 LOG.warn(String.format("Could not map %s to jsonNode.", jsonStr), e);
118             }
119         }
120         return jsonNode;
121     }
122     public static JsonNode toJsonNode(Object obj) {
123         JsonNode jsonNode = null;
124         String jsonStr = toJsonString(obj);
125         if(jsonStr != null) {
126             try {
127                 jsonNode = mapper.readTree(jsonStr);
128             } catch (IOException e) {
129                 LOG.warn(String.format("Could not map %s to JSONObject.", obj), e);
130             }
131         }
132         return jsonNode;
133     }
134 }