3680c59dca4ded150a7cabe8d86c814636718776
[so.git] / adapters / mso-catalog-db-adapter / src / main / java / org / onap / so / adapters / catalogdb / catalogrest / QueryVfModule.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T 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 java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import javax.xml.bind.annotation.XmlRootElement;
29
30 import org.onap.so.db.catalog.beans.HeatEnvironment;
31 import org.onap.so.db.catalog.beans.VfModuleCustomization;
32
33 @XmlRootElement(name = "vfModules")
34 public class QueryVfModule extends CatalogQuery {
35         private List<VfModuleCustomization> vfModules;
36         private static final String TEMPLATE =
37                 "\t{\n"+
38                 "\t\t\"modelInfo\"               : { \n"+
39                         "\t\t\t\"modelName\"              : <MODEL_NAME>,\n"+
40                         "\t\t\t\"modelUuid\"              : <MODEL_UUID>,\n"+
41                         "\t\t\t\"modelInvariantUuid\"     : <MODEL_INVARIANT_ID>,\n"+
42                         "\t\t\t\"modelVersion\"           : <MODEL_VERSION>,\n"+
43                         "\t\t\t\"modelCustomizationUuid\" : <MODEL_CUSTOMIZATION_UUID>\n"+
44                         "\t\t},"+
45                         "\t\t\"isBase\"                 : <IS_BASE>,\n"+
46                         "\t\t\"vfModuleLabel\"          : <VF_MODULE_LABEL>,\n"+
47                         "\t\t\"initialCount\"           : <INITIAL_COUNT>,\n"+
48                         "\t\t\"hasVolumeGroup\"           : <HAS_VOLUME_GROUP>\n"+
49                 "\t}";
50
51         public QueryVfModule() {
52             super();
53             vfModules = new ArrayList<>();
54         }
55         
56         public QueryVfModule(List<VfModuleCustomization> vlist) {
57             vfModules = new ArrayList<>();
58             if (vlist != null) {
59                 for (VfModuleCustomization o : vlist) {
60                     if(logger.isDebugEnabled())
61                         logger.debug (o.toString());
62                     vfModules.add(o);                   
63                 }
64             }
65         }
66
67         public List<VfModuleCustomization> getVfModule(){ return this.vfModules; }
68         public void setVfModule(List<VfModuleCustomization> v) { this.vfModules = v; }
69
70         @Override
71         public String toString () {
72                 StringBuilder sb = new StringBuilder();
73
74                 boolean first = true;
75                 int i = 1;
76                 for (VfModuleCustomization o : vfModules) {
77                         sb.append(i).append("\t");
78                         if (!first) 
79                             sb.append("\n");
80                         first = false;
81                         sb.append(o);
82                 }
83                 return sb.toString();
84     }
85
86         @Override
87         public String JSON2(boolean isArray, boolean isEmbed) {
88                 StringBuilder sb = new StringBuilder();
89                 if (!isEmbed && isArray) 
90                     sb.append("{ ");
91                 if (isArray) 
92                     sb.append("\"vfModules\": [");
93                 Map<String, String> valueMap = new HashMap<>();
94                 String sep = "";
95                 boolean first = true;
96
97                 for (VfModuleCustomization o : vfModules) {
98                         if (first) 
99                             sb.append("\n");
100                         first = false;
101
102                         boolean vfNull = o.getVfModule() == null ? true : false;
103                         boolean hasVolumeGroup = false;
104                         HeatEnvironment envt = o.getVolumeHeatEnv();
105                         if (envt != null) {
106                                 hasVolumeGroup = true;
107                         }
108
109                     put(valueMap, "MODEL_NAME",               vfNull ? null : o.getVfModule().getModelName());
110                     put(valueMap, "MODEL_UUID",               vfNull ? null : o.getVfModule().getModelUUID());
111                     put(valueMap, "MODEL_INVARIANT_ID",       vfNull ? null : o.getVfModule().getModelInvariantUUID());
112                     put(valueMap, "MODEL_VERSION",            vfNull ? null : o.getVfModule().getModelVersion());
113                     put(valueMap, "MODEL_CUSTOMIZATION_UUID", o.getModelCustomizationUUID());
114                         put(valueMap, "IS_BASE", vfNull ? false : o.getVfModule().getIsBase() ? true : false);
115                         put(valueMap, "VF_MODULE_LABEL",          o.getLabel());
116                     put(valueMap, "INITIAL_COUNT",            o.getInitialCount());
117                         put(valueMap, "HAS_VOLUME_GROUP", new Boolean(hasVolumeGroup));
118
119             sb.append(sep).append(this.setTemplate(TEMPLATE, valueMap));
120             sep = ",\n";
121                 }
122                 if (!first) 
123                     sb.append("\n");
124                 if (isArray) 
125                     sb.append("]");
126                 if (!isEmbed && isArray) 
127                     sb.append("}");
128                 return sb.toString();
129         }
130 }