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