Replaced all tabs with spaces in java and pom.xml
[so.git] / adapters / mso-adapter-utils / src / main / java / org / onap / so / openstack / utils / MsoHeatEnvironmentEntry.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================
8  * Modifications Copyright (c) 2019 Samsung
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.so.openstack.utils;
25
26
27 import java.util.ArrayList;
28 import java.util.HashSet;
29 import java.util.Set;
30 import org.onap.so.db.catalog.beans.HeatTemplateParam;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class MsoHeatEnvironmentEntry {
35
36     private static final Logger logger = LoggerFactory.getLogger(MsoHeatEnvironmentEntry.class);
37
38     private Set<MsoHeatEnvironmentParameter> parameters = null;
39     private Set<MsoHeatEnvironmentResource> resources = null;
40     private StringBuilder rawEntry = null;
41     private boolean valid = true;
42     private String errorString = null;
43     private StringBuilder resourceRegistryEntryRaw = null;
44
45     public MsoHeatEnvironmentEntry() {
46         super();
47     }
48
49     public MsoHeatEnvironmentEntry(StringBuilder sb) {
50         this();
51         this.rawEntry = sb;
52         this.processRawEntry();
53     }
54
55     private void processRawEntry() {
56         try {
57             if (this.rawEntry == null || "".equals(this.rawEntry.toString()))
58                 return;
59             byte[] b = this.rawEntry.toString().getBytes();
60             MsoYamlEditorWithEnvt yaml = new MsoYamlEditorWithEnvt(b);
61             this.parameters = yaml.getParameterListFromEnvt();
62             // this.resources = yaml.getResourceListFromEnvt();
63             StringBuilder sb = this.getResourceRegistryRawEntry();
64             if (sb == null) {
65                 this.resourceRegistryEntryRaw = new StringBuilder("");
66             } else {
67                 this.resourceRegistryEntryRaw = sb;
68             }
69         } catch (Exception e) {
70             logger.debug("Exception:", e);
71             this.valid = false;
72             this.errorString = e.getMessage();
73             // e.printStackTrace();
74         }
75     }
76
77     public boolean isValid() {
78         return this.valid;
79     }
80
81     public String getErrorString() {
82         return this.errorString;
83     }
84
85     public Set<MsoHeatEnvironmentParameter> getParameters() {
86         return this.parameters;
87     }
88
89     public Set<MsoHeatEnvironmentResource> getResources() {
90         return this.resources;
91     }
92
93     public void setParameters(Set<MsoHeatEnvironmentParameter> paramSet) {
94         if (paramSet == null) {
95             this.parameters = null;
96         } else {
97             this.parameters = paramSet;
98         }
99     }
100
101     public void setResources(Set<MsoHeatEnvironmentResource> resourceSet) {
102         if (resourceSet == null) {
103             this.resources = null;
104         } else {
105             this.resources = resourceSet;
106         }
107     }
108
109     public void addParameter(MsoHeatEnvironmentParameter hep) {
110         if (this.parameters == null) {
111             this.parameters = new HashSet<>();
112         }
113         this.parameters.add(hep);
114     }
115
116     public void addResource(MsoHeatEnvironmentResource her) {
117         if (this.resources == null) {
118             this.resources = new HashSet<>();
119         }
120         this.resources.add(her);
121     }
122
123     public int getNumberOfParameters() {
124         return this.parameters.size();
125     }
126
127     public int getNumberOfResources() {
128         return this.resources.size();
129     }
130
131     public boolean hasResources() {
132         if (this.resources != null && this.resources.size() > 0) {
133             return true;
134         }
135         return false;
136     }
137
138     public boolean hasParameters() {
139         if (this.parameters != null && this.parameters.size() > 0) {
140             return true;
141         }
142         return false;
143     }
144
145     public boolean containsParameter(String paramName) {
146         boolean contains = false;
147         if (this.parameters == null || this.parameters.size() < 1) {
148             return false;
149         }
150         if (this.parameters.contains(new MsoHeatEnvironmentParameter(paramName))) {
151             contains = true;
152         }
153         return contains;
154     }
155
156     public boolean containsParameter(String paramName, String paramAlias) {
157         if (this.containsParameter(paramName)) {
158             return true;
159         }
160         if (this.containsParameter(paramAlias)) {
161             return true;
162         }
163         return false;
164     }
165
166     @Override
167     public String toString() {
168         return "MsoHeatEnvironmentEntry{" + "parameters=" + parameters + ", resourceRegistryEntryRaw='"
169                 + resourceRegistryEntryRaw + '\'' + '}';
170     }
171
172     public StringBuilder toFullStringExcludeNonParams(Set<HeatTemplateParam> params) {
173         // Basically give back the envt - but exclude the params that aren't in the HeatTemplate
174
175         StringBuilder sb = new StringBuilder();
176         ArrayList<String> paramNameList = new ArrayList<String>(params.size());
177         for (HeatTemplateParam htp : params) {
178             paramNameList.add(htp.getParamName());
179         }
180
181         if (this.hasParameters()) {
182             sb.append("parameters:\n");
183             for (MsoHeatEnvironmentParameter hep : this.parameters) {
184                 String paramName = hep.getName();
185                 if (paramNameList.contains(paramName)) {
186                     // This parameter *is* in the Heat Template - so include it:
187                     sb.append("  " + hep.getName() + ": " + hep.getValue() + "\n");
188                     // New - 1607 - if any of the params mapped badly - JUST RETURN THE ORIGINAL ENVT!
189                     if (hep.getValue().startsWith("_BAD")) {
190                         return this.rawEntry;
191                     }
192                 }
193             }
194             sb.append("\n");
195         }
196         // if (this.hasResources()) {
197         // sb.append("resource_registry:\n");
198         // for (MsoHeatEnvironmentResource her : this.resources) {
199         // sb.append(" \"" + her.getName() + "\": " + her.getValue() + "\n");
200         // }
201         // }
202         sb.append("\n");
203         sb.append(this.resourceRegistryEntryRaw);
204         return sb;
205     }
206
207     public StringBuilder toFullString() {
208         StringBuilder sb = new StringBuilder();
209
210         if (this.hasParameters()) {
211             sb.append("parameters:\n");
212             for (MsoHeatEnvironmentParameter hep : this.parameters) {
213                 sb.append("   " + hep.getName() + ":  " + hep.getValue() + "\n");
214             }
215             sb.append("\n");
216         }
217         // if (this.hasResources()) {
218         // sb.append("resource_registry:\n");
219         // for (MsoHeatEnvironmentResource her : this.resources) {
220         // sb.append(" \"" + her.getName() + "\": " + her.getValue() + "\n");
221         // }
222         // }
223         sb.append("\n");
224         sb.append(this.resourceRegistryEntryRaw);
225         return sb;
226     }
227
228     public StringBuilder getRawEntry() {
229         return this.rawEntry;
230     }
231
232     private StringBuilder getResourceRegistryRawEntry() {
233
234         if (this.rawEntry == null) {
235             return null;
236         }
237
238         StringBuilder sb = new StringBuilder();
239         int indexOf = this.rawEntry.indexOf("resource_registry:");
240         if (indexOf < 0) { // no resource_registry:
241             return null;
242         }
243         sb.append(this.rawEntry.substring(indexOf));
244         return sb;
245     }
246
247     public void setHPAParameters(StringBuilder hpasb) {
248         try {
249             MsoYamlEditorWithEnvt yaml = new MsoYamlEditorWithEnvt(hpasb.toString().getBytes());
250             Set<MsoHeatEnvironmentParameter> hpaParams = yaml.getParameterListFromEnvt();
251             for (MsoHeatEnvironmentParameter hpaparam : hpaParams) {
252                 for (MsoHeatEnvironmentParameter param : this.parameters) {
253                     if (param.getName() == hpaparam.getName()) {
254                         param.setValue(hpaparam.getValue());
255                     }
256                 }
257             }
258         } catch (Exception e) {
259             logger.debug("Exception:", e);
260             this.errorString = e.getMessage();
261             // e.printStackTrace();
262         }
263     }
264 }