a917cd9efe9f2384c2d29f369101599c18e856fd
[so.git] / adapters / mso-catalog-db-adapter / src / main / java / org / openecomp / mso / adapters / catalogdb / catalogrest / QueryServiceVnfs.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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 package org.openecomp.mso.adapters.catalogdb.catalogrest;
21 /* should be called QueryVnfResource.java */
22
23 import org.openecomp.mso.db.catalog.beans.VnfResource;
24 import org.jboss.resteasy.annotations.providers.NoJackson;
25
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 = "serviceVnfs")
33 @NoJackson
34 public class QueryServiceVnfs extends CatalogQuery {
35         private List<VnfResource> serviceVnfs;
36         private final String template =
37         "\t{ \"vnf\"                    : {\n"+
38                         "\t\t\"modelName\"              : <MODEL_NAME>,\n"+
39                         "\t\t\"modelUuid\"              : <MODEL_UUID>,\n"+
40                         "\t\t\"modelInvariantUuid\"     : <MODEL_INVARIANT_ID>,\n"+
41                         "\t\t\"modelVersion\"           : <MODEL_VERSION>,\n"+
42                         "\t\t\"modelCustomizationUuid\" : <MODEL_CUSTOMIZATION_UUID>,\n"+
43                         "\t\t\"modelInstanceName\"      : <MODEL_INSTANCE_NAME>,\n"+
44                         "<_VFMODULES_>\n"+
45                         "\t}}";
46
47         public QueryServiceVnfs() { super(); serviceVnfs = new ArrayList<VnfResource>(); }
48         public QueryServiceVnfs(List<VnfResource> vlist) {
49                 LOGGER.debug ("QueryServiceVnfs:");
50                 serviceVnfs = new ArrayList<VnfResource>();
51                 for (VnfResource o : vlist) {
52                         LOGGER.debug ("-- o is a  serviceVnfs ----");
53                         LOGGER.debug (o.toString());
54                         serviceVnfs.add(o);
55                         LOGGER.debug ("-------------------");
56                 }
57         }
58
59         public List<VnfResource> getServiceVnfs(){ return this.serviceVnfs; }
60         public void setServiceVnfs(List<VnfResource> v) { this.serviceVnfs = v; }
61
62         @Override
63         public String toString () {
64                 StringBuffer buf = new StringBuffer();
65
66                 boolean first = true;
67                 int i = 1;
68                 for (VnfResource o : serviceVnfs) {
69                         buf.append(i+"\t");
70                         if (!first) buf.append("\n"); first = false;
71                         buf.append(o);
72                 }
73                 return buf.toString();
74     }
75
76         @Override
77         public String JSON2(boolean isArray, boolean isEmbed) {
78                 StringBuffer buf = new StringBuffer();
79                 if (!isEmbed && isArray) buf.append("{ ");
80                 if (isArray) buf.append("\"serviceVnfs\": [");
81                 Map<String, String> valueMap = new HashMap<String, String>();
82                 String sep = "";
83                 boolean first = true;
84
85                 for (VnfResource o : serviceVnfs) {
86                         if (first) buf.append("\n"); first = false;
87
88                     put(valueMap, "MODEL_NAME",               o.getModelName());
89                     put(valueMap, "MODEL_UUID",               o.getModelUuid());
90                     put(valueMap, "MODEL_INVARIANT_ID",       o.getModelInvariantId());
91                     put(valueMap, "MODEL_VERSION",            o.getModelVersion());
92                     put(valueMap, "MODEL_CUSTOMIZATION_UUID", o.getModelCustomizationUuid());
93                     put(valueMap, "MODEL_INSTANCE_NAME",      o.getModelInstanceName());
94
95                     String subitem = new QueryVfModule(o.getVfModules()).JSON2(true, true);
96                     valueMap.put("_VFMODULES_",               subitem.replaceAll("(?m)^", "\t\t"));
97
98             buf.append(sep+ this.setTemplate(template, valueMap));
99             sep = ",\n";
100                 }
101                 if (!first) buf.append("\n");
102                 if (isArray) buf.append("]");
103                 if (!isEmbed && isArray) buf.append("}");
104                 return buf.toString();
105         }
106 }