AT&T 1712 and 1802 release code
[so.git] / adapters / mso-catalog-db-adapter / src / main / java / org / openecomp / mso / adapters / catalogdb / catalogrest / CatalogQuery.java
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 package org.openecomp.mso.adapters.catalogdb.catalogrest;
22
23 import org.openecomp.mso.logger.MsoLogger;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25
26 import java.util.Map;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
30 public abstract class CatalogQuery {
31         protected static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
32         private static final boolean IS_EMBED = true;
33
34         public abstract String JSON2(boolean isArray, boolean isEmbed);
35
36         protected void put(Map<String, String> valueMap, String key, String value) {
37                 valueMap.put(key, value == null? "null": '"'+ value+ '"');
38         }
39
40         protected void put(Map<String, String> valueMap, String key, Integer value) {
41                 valueMap.put(key, value == null? "null": value.toString());
42         }
43
44         protected void put(Map<String, String> valueMap, String key, Boolean value) {
45                 valueMap.put(key, value == null? "null": value? "true": "false");
46         }
47
48         protected String setTemplate(String template, Map<String, String> valueMap) {
49                 LOGGER.debug("CatalogQuery setTemplate");
50                 StringBuffer result = new StringBuffer();
51
52                 String pattern = "<.*>";
53                 Pattern r = Pattern.compile(pattern);
54                 Matcher m = r.matcher(template);
55
56                 LOGGER.debug("CatalogQuery template:" + template);
57                 while (m.find()) {
58                         String key = template.substring(m.start() + 1, m.end() - 1);
59                         LOGGER.debug("CatalogQuery key:" + key + " contains key? " + valueMap.containsKey(key));
60                         m.appendReplacement(result, valueMap.getOrDefault(key, "\"TBD\""));
61                 }
62                 m.appendTail(result);
63                 LOGGER.debug("CatalogQuery return:" + result.toString());
64                 return result.toString();
65         }
66
67         /**
68          * The simple, clean, generic way to handle the interface
69          */
70         protected String smartToJSON() {
71                 String jsonString = null;
72                 try {
73                         ObjectMapper mapper = new ObjectMapper();
74                         jsonString = mapper.writeValueAsString(this);
75                 }
76                 catch (Exception e) {
77                     LOGGER.debug("Exception:", e);
78                         LOGGER.debug ("jsonString exception:"+e.getMessage());
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": return smartToJSON();
87                 case "v2": return JSON2(isArray, !IS_EMBED);
88                 default:
89                         return "invalid version: "+ version;
90                 }
91         }
92 }