ed2526f4007faeea359e7e2eece560d159778d22
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2019 Huawei Intellectual Property. All rights reserved.
6  *
7  * Copyright (C) 2019 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.adapters.catalogdb.catalogrest;
24
25 import org.onap.so.db.catalog.beans.InstanceGroup;
26 import org.onap.so.db.catalog.beans.VnfcInstanceGroupCustomization;
27 import javax.xml.bind.annotation.XmlRootElement;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 @XmlRootElement(name = "groups")
34 public class QueryGroups extends CatalogQuery {
35
36     private List<VnfcInstanceGroupCustomization> vnfcInstanceGroupCustomizations;
37     private static final String TEMPLATE = "\n" + "\t{ \"modelInfo\"                    : {\n"
38             + "\t\t\"modelName\"              : <MODEL_NAME>,\n" + "\t\t\"modelUuid\"              : <MODEL_UUID>,\n"
39             + "\t\t\"modelInvariantUuid\"     : <MODEL_INVARIANT_ID>,\n"
40             + "\t\t\"modelVersion\"           : <MODEL_VERSION>\n" + "\t\t},\n" + "<_VNFCS_>\n" + "\t}";
41
42     public QueryGroups() {
43         super();
44         vnfcInstanceGroupCustomizations = new ArrayList<>();
45
46     }
47
48     public QueryGroups(List<VnfcInstanceGroupCustomization> vnfcInstanceGroupCustomizations) {
49         this.vnfcInstanceGroupCustomizations = new ArrayList<>();
50         if (vnfcInstanceGroupCustomizations != null) {
51             for (VnfcInstanceGroupCustomization g : vnfcInstanceGroupCustomizations) {
52                 if (logger.isDebugEnabled()) {
53                     logger.debug(g.toString());
54                 }
55                 this.vnfcInstanceGroupCustomizations.add(g);
56             }
57         }
58     }
59
60     @Override
61     public String JSON2(boolean isArray, boolean isEmbed) {
62         StringBuilder sb = new StringBuilder();
63         if (!isEmbed && isArray)
64             sb.append("{ ");
65         if (isArray)
66             sb.append("\"groups\": [");
67         Map<String, String> valueMap = new HashMap<>();
68         String sep = "";
69         boolean first = true;
70
71         for (VnfcInstanceGroupCustomization o : vnfcInstanceGroupCustomizations) {
72             if (first)
73                 sb.append("\n");
74             first = false;
75
76             boolean vnfcCustomizationNull = o.getVnfcCustomizations() == null;
77             InstanceGroup instanceGroup = o.getInstanceGroup();
78
79             if (instanceGroup != null) {
80                 put(valueMap, "MODEL_NAME", instanceGroup.getModelName());
81                 put(valueMap, "MODEL_UUID", instanceGroup.getModelUUID());
82                 put(valueMap, "MODEL_INVARIANT_ID", instanceGroup.getModelInvariantUUID());
83                 put(valueMap, "MODEL_VERSION", instanceGroup.getModelVersion());
84             }
85
86             String subItem = new QueryVnfcs(vnfcCustomizationNull ? null : o.getVnfcCustomizations()).JSON2(true, true);
87             valueMap.put("_VNFCS_", subItem.replaceAll("(?m)^", "\t\t"));
88             sb.append(sep).append(this.setTemplate(TEMPLATE, valueMap));
89             sep = ",\n";
90         }
91         if (!first)
92             sb.append("\n");
93         if (isArray)
94             sb.append("]");
95         if (!isEmbed && isArray)
96             sb.append("}");
97         return sb.toString();
98     }
99 }