AT&T 1712 and 1802 release code
[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
26 import java.util.HashSet;
27 import java.util.ArrayList;
28 import java.util.Set;
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         public String getErrorString() {
79                 return this.errorString;
80         }
81         
82         public Set<MsoHeatEnvironmentParameter> getParameters() {
83                 return this.parameters;
84         }
85         public Set<MsoHeatEnvironmentResource> getResources() {
86                 return this.resources;
87         }
88         public void setParameters(Set<MsoHeatEnvironmentParameter> paramSet) {
89                 if (paramSet == null) {
90                         this.parameters = null;
91                 } else {
92                         this.parameters = paramSet;
93                 }
94         }
95         public void setResources(Set<MsoHeatEnvironmentResource> resourceSet) {
96                 if (resourceSet == null) {
97                         this.resources = null;
98                 } else {
99                         this.resources = resourceSet;
100                 }
101         }
102         
103         public void addParameter(MsoHeatEnvironmentParameter hep) {
104                 if (this.parameters == null) {
105                         this.parameters = new HashSet<>();
106                 }
107                 this.parameters.add(hep);
108         }
109         public void addResource(MsoHeatEnvironmentResource her) {
110                 if (this.resources == null) {
111                         this.resources = new HashSet<>();
112                 }
113                 this.resources.add(her);
114         }
115         
116         public int getNumberOfParameters() {
117                 return this.parameters.size();
118         }
119         public int getNumberOfResources() {
120                 return this.resources.size();
121         }
122         
123         public boolean hasResources() {
124                 if (this.resources != null && this.resources.size() > 0) {
125                         return true;
126                 } 
127                 return false;
128         }
129         public boolean hasParameters() {
130                 if (this.parameters != null && this.parameters.size() > 0) {
131                         return true;
132                 }
133                 return false;
134         }
135         
136         public boolean containsParameter(String paramName) {
137                 boolean contains = false;
138                 if (this.parameters == null || this.parameters.size() < 1) {
139                         return false;
140                 }
141                 if (this.parameters.contains(new MsoHeatEnvironmentParameter(paramName))) {
142                         contains = true;
143                 }
144                 return contains;
145         }
146         
147         public boolean containsParameter(String paramName, String paramAlias) {
148                 if (this.containsParameter(paramName)) {
149                         return true;
150                 }
151                 if (this.containsParameter(paramAlias)) {
152                         return true;
153                 }
154                 return false;
155         }
156         
157         @Override
158         public String toString() {
159                 return "MsoHeatEnvironmentEntry{" + "parameters=" + parameters +
160                         ", resourceRegistryEntryRaw='" + resourceRegistryEntryRaw + '\'' +
161                         '}';
162         }
163         
164         public StringBuilder toFullStringExcludeNonParams(Set<HeatTemplateParam> params) {
165                 // Basically give back the envt - but exclude the params that aren't in the HeatTemplate 
166                 
167                 StringBuilder sb = new StringBuilder();
168                 ArrayList<String> paramNameList = new ArrayList<String>(params.size());
169                 for (HeatTemplateParam htp : params) {
170                         paramNameList.add(htp.getParamName());
171                 }
172                 
173                 if (this.hasParameters()) {
174                         sb.append("parameters:\n");
175                         for (MsoHeatEnvironmentParameter hep : this.parameters) {
176                                 String paramName = hep.getName();
177                                 if (paramNameList.contains(paramName)) {
178                                         // This parameter *is* in the Heat Template - so include it:
179                                         sb.append("  " + hep.getName() + ": " + hep.getValue() + "\n");
180                                         // New - 1607 - if any of the params mapped badly - JUST RETURN THE ORIGINAL ENVT!
181                                         if (hep.getValue().startsWith("_BAD")) {
182                                                 return this.rawEntry;
183                                         }
184                                 } 
185                         }
186                         sb.append("\n");
187                 }
188 //              if (this.hasResources()) {
189 //                      sb.append("resource_registry:\n");
190 //                      for (MsoHeatEnvironmentResource her : this.resources) {
191 //                              sb.append("   \"" + her.getName() + "\": " + her.getValue() + "\n");
192 //                      }
193 //              }
194                 sb.append("\n");
195                 sb.append(this.resourceRegistryEntryRaw);                               
196                 return sb;
197         }
198         
199         public StringBuilder toFullString() {
200                 StringBuilder sb = new StringBuilder();
201                 
202                 if (this.hasParameters()) {
203                         sb.append("parameters:\n");
204                         for (MsoHeatEnvironmentParameter hep : this.parameters) {
205                                 sb.append("   " + hep.getName() + ":  " + hep.getValue() + "\n");
206                         }
207                         sb.append("\n");
208                 }
209 //              if (this.hasResources()) {
210 //                      sb.append("resource_registry:\n");
211 //                      for (MsoHeatEnvironmentResource her : this.resources) {
212 //                              sb.append("   \"" + her.getName() + "\": " + her.getValue() + "\n");
213 //                      }
214 //              }
215                 sb.append("\n");
216                 sb.append(this.resourceRegistryEntryRaw);                       
217                 return sb;
218         }
219         
220         public StringBuilder getRawEntry() {
221                 return this.rawEntry;
222         }
223         
224         private StringBuilder getResourceRegistryRawEntry() {
225                 
226                 if (this.rawEntry == null) {
227                         return null;
228                 }
229                 
230                 StringBuilder sb = new StringBuilder();
231                 int indexOf = this.rawEntry.indexOf("resource_registry:");
232                 if (indexOf < 0) { // no resource_registry:
233                         return null;
234                 }
235                 sb.append(this.rawEntry.substring(indexOf));
236                 return sb;
237         }
238         
239 }