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