94cf30412088b9ff587d75c3b1202b425841dbb6
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (c) 2019, CMCC Technologies Co., Ltd.
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.ServiceProxyResourceCustomization;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
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 = "serviceProxyCustomizations")
33 public class QueryServiceProxyCustomization extends CatalogQuery {
34
35     protected static Logger logger = LoggerFactory.getLogger(QueryServiceProxyCustomization.class);
36
37     private List<ServiceProxyResourceCustomization> serviceProxyResourceCustomizationList;
38
39     private static final String TEMPLATE =
40             "\t{\n" + "\t\t\"modelInfo\"                : {\n" + "\t\t\t\"modelName\"              : <MODEL_NAME>,\n"
41                     + "\t\t\t\"modelUuid\"              : <MODEL_UUID>,\n"
42                     + "\t\t\t\"modelInvariantUuid\"     : <MODEL_INVARIANT_UUID>,\n"
43                     + "\t\t\t\"modelVersion\"           : <MODEL_VERSION>,\n"
44                     + "\t\t\t\"modelCustomizationUuid\" : <MODEL_CUSTOMIZATION_UUID>,\n"
45                     + "\t\t\t\"modelInstanceName\"      : <MODEL_INSTANCE_NAME>\n" + "\t},\n"
46                     + "\t\t\"toscaNodeType\"            : <TOSCA_NODE_TYPE>,\n"
47                     + "\t\t\"description\"            : <DESCRIPTION>,\n"
48                     + "\t\t\"sourceModelUuid\"            : <SOURCE_SERVICE_MODEL_UUID>\n" + "\t}";
49
50     public QueryServiceProxyCustomization() {
51         super();
52         this.serviceProxyResourceCustomizationList = new ArrayList<>();
53     }
54
55     public QueryServiceProxyCustomization(
56             List<ServiceProxyResourceCustomization> serviceProxyResourceCustomizationList) {
57         this.serviceProxyResourceCustomizationList = serviceProxyResourceCustomizationList;
58     }
59
60     @Override
61     public String toString() {
62         StringBuilder sb = new StringBuilder();
63
64         boolean first = true;
65         int i = 1;
66         for (ServiceProxyResourceCustomization o : serviceProxyResourceCustomizationList) {
67             sb.append(i).append("\t");
68             if (!first)
69                 sb.append("\n");
70
71             first = false;
72             sb.append(o);
73         }
74         return sb.toString();
75     }
76
77     @Override
78     public String JSON2(boolean isArray, boolean isEmbed) {
79         StringBuilder sb = new StringBuilder();
80         if (!isEmbed && isArray)
81             sb.append("{ ");
82         if (isArray)
83             sb.append("\"serviceProxy\": [");
84         Map<String, String> valueMap = new HashMap<>();
85         String sep = "";
86         boolean first = true;
87
88         if (this.serviceProxyResourceCustomizationList != null) {
89             for (ServiceProxyResourceCustomization o : serviceProxyResourceCustomizationList) {
90                 if (first)
91                     sb.append("\n");
92
93                 first = false;
94
95                 boolean arNull = o == null;
96
97                 put(valueMap, "MODEL_CUSTOMIZATION_UUID", arNull ? null : o.getModelCustomizationUUID());
98                 put(valueMap, "MODEL_INSTANCE_NAME", arNull ? null : o.getModelInstanceName());
99                 put(valueMap, "MODEL_UUID", arNull ? null : o.getModelUUID());
100                 put(valueMap, "MODEL_INVARIANT_UUID", arNull ? null : o.getModelInvariantUUID());
101                 put(valueMap, "MODEL_VERSION", arNull ? null : o.getModelVersion());
102                 put(valueMap, "MODEL_NAME", arNull ? null : o.getModelName());
103                 put(valueMap, "TOSCA_NODE_TYPE", arNull ? null : o.getToscaNodeType());
104                 put(valueMap, "DESCRIPTION", arNull ? null : o.getDescription());
105                 put(valueMap, "SOURCE_SERVICE_MODEL_UUID", (String) (arNull ? null
106                         : o.getSourceService() == null ? null : o.getSourceService().getModelUUID()));
107
108                 sb.append(sep).append(this.setTemplate(TEMPLATE, valueMap));
109                 sep = ",\n";
110             }
111         }
112         if (!first)
113             sb.append("\n");
114
115         if (isArray)
116             sb.append("]");
117
118         if (!isEmbed && isArray)
119             sb.append("}");
120
121         return sb.toString();
122     }
123
124 }