Add logback to logstash test code
[vfc/nfvo/wfengine.git] / logging-sdk / src / main / java / org / openo / log / impl / Facitility.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openo.log.impl;
17
18 import java.io.IOException;
19 import java.io.StringWriter;
20 import java.io.Writer;
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26 import java.util.Map.Entry;
27
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.fasterxml.jackson.databind.ObjectMapper;
32
33 public class Facitility {
34   private static final Logger LOGGER = LoggerFactory.getLogger(Facitility.class.getName());
35
36   private Facitility() {
37
38   }
39
40   public static String oToJ(Object o) {
41     ObjectMapper om = new ObjectMapper();
42     Writer w = new StringWriter();
43     String json = null;
44     try {
45
46       om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"));
47       om.writeValue(w, o);
48       json = w.toString();
49       w.close();
50     } catch (IOException e) {
51       LOGGER.error("IOException", e);
52     }
53     return json;
54   }
55
56   public static Map<String, String> readJson2Map(String json) {
57     ObjectMapper objectMapper = new ObjectMapper();
58     try {
59       Map<String, String> maps = objectMapper.readValue(json, Map.class);
60       return maps;
61     } catch (Exception e) {
62       LOGGER.error("IOException", e);
63       return null;
64     }
65   }
66
67   public static String hashMapToJson(HashMap<String, String> map) {
68     String string = "{";
69     for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
70       Entry e = (Entry) it.next();
71       string += "\"" + e.getKey() + "\":";
72       string += "\"" + e.getValue() + "\",";
73     }
74     string = string.substring(0, string.lastIndexOf(","));
75     string += "}";
76     return string;
77   }
78
79   public static String dateFormat(Date date) {
80     SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
81     return time.format(date);
82   }
83
84   public static String checkRequiredParam(HashMap<String, Object> checkParamsMap) {
85     StringBuilder errMsg = new StringBuilder();
86     java.util.Iterator<String> hashMapIt = checkParamsMap.keySet().iterator();
87     int count = 0;
88     while (hashMapIt.hasNext()) {
89       String key = hashMapIt.next();
90       Object value = checkParamsMap.get(key);
91       if (value == null || "".equals(value)) {
92         errMsg.append(key);
93         count++;
94         if (count < checkParamsMap.size() - 1) {
95           errMsg.append(" and ");
96         }
97       }
98
99     }
100     if (count > 0) {
101       errMsg.append(" can't be null or \"\"!\n ");
102     }
103     return errMsg.toString();
104   }
105
106   public static String checkRequiredJsonParam(String jsonParam, String key) {
107     return "";
108 //    StringBuilder errMsg = new StringBuilder();
109 //    try {
110 //      ObjectMapper mapper = new ObjectMapper();
111 //      JsonNode jsonValue;
112 //
113 //
114 //      jsonValue = mapper.readTree(jsonParam.toString());
115 //      Iterator<Entry<String, JsonNode>> elements = jsonValue.fields();
116 //      while (elements.hasNext()) {
117 //        Entry<String, JsonNode> node = elements.next();
118 //        String childValue = node.getValue().asText();
119 //        if (childValue == null || "".equals(childValue)) {
120 //          errMsg.append(
121 //              "Both Chinese and English descriptions of this field cannot be empty: " + key + "/n");
122 //          break;
123 //        }
124 //      }
125 //
126 //      return errMsg.toString();
127 //    } catch (JsonProcessingException e) {
128 //      // TODO Auto-generated catch block
129 //      LOGGER.error("JsonProcessingException" , e);
130 //      return errMsg.toString();
131 //    } catch (IOException e) {
132 //      // TODO Auto-generated catch block
133 //      LOGGER.error("IOException" , e);
134 //      return errMsg.toString();
135 //    }
136   }
137 }