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