a98778bf5d2b026e5b822986c3259be07217b169
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights 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.onap.so.adapters.catalogdb.catalogrest;
23
24 import java.io.IOException;
25 import java.util.Map;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31
32 public abstract class CatalogQuery {
33     protected static Logger logger = LoggerFactory.getLogger(CatalogQuery.class);
34     private static final boolean IS_EMBED = true;
35
36     public abstract String JSON2(boolean isArray, boolean isEmbed);
37
38     protected void put(Map<String, String> valueMap, String key, String value) {
39         valueMap.put(key, value == null ? "null" : '"' + value + '"');
40     }
41
42     protected void put(Map<String, String> valueMap, String key, Integer value) {
43         valueMap.put(key, value == null ? "null" : value.toString());
44     }
45
46     protected void put(Map<String, String> valueMap, String key, Boolean value) {
47         valueMap.put(key, value == null ? "null" : value ? "true" : "false");
48     }
49
50     protected String setTemplate(String template, Map<String, String> valueMap) {
51         StringBuffer result = new StringBuffer();
52
53         String pattern = "<.*>";
54         Pattern r = Pattern.compile(pattern);
55         Matcher m = r.matcher(template);
56
57         logger.debug("CatalogQuery template: {}", template);
58         while (m.find()) {
59             String key = template.substring(m.start() + 1, m.end() - 1);
60             logger.debug("CatalogQuery key: {} contains key? {}", key, valueMap.containsKey(key));
61             m.appendReplacement(result, Matcher.quoteReplacement(valueMap.getOrDefault(key, "\"TBD\"")));
62         }
63         m.appendTail(result);
64         logger.debug("CatalogQuery return: {}", result.toString());
65         return result.toString();
66     }
67
68     /**
69      * The simple, clean, generic way to handle the interface
70      */
71     protected String smartToJSON() {
72         String jsonString = null;
73         try {
74             ObjectMapper mapper = new ObjectMapper();
75             jsonString = mapper.writeValueAsString(this);
76         } catch (Exception e) {
77             logger.error("Error converting to JSON", e);
78             jsonString = "invalid"; // throws instead?
79         }
80         return jsonString;
81     }
82
83     public String toJsonString(String version, boolean isArray) {
84         switch (version) {
85             case "v1":
86                 return smartToJSON();
87             case "v2":
88                 return JSON2(isArray, !IS_EMBED);
89             default:
90                 return "invalid version: " + version;
91         }
92     }
93
94     @Override
95     public String toString() {
96         return smartToJSON();
97
98     }
99
100     protected boolean isJSONValid(String jsonInString) {
101         try {
102             if (jsonInString == null) {
103                 return false;
104             }
105             ObjectMapper mapper = new ObjectMapper();
106             mapper.readValue(jsonInString, String.class);
107             return true;
108         } catch (IOException e) {
109             return false;
110         }
111     }
112 }