13ea4331e8d3908b7ade8a6fff5e31e6b26550c7
[so.git] / adapters / mso-adapter-utils / src / main / java / org / openecomp / mso / openstack / utils / MsoHeatEnvironmentEntry.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
21 package org.openecomp.mso.openstack.utils;
22
23
24
25 import java.util.HashSet;
26 import java.util.ArrayList;
27 import java.util.Set;
28 import org.openecomp.mso.db.catalog.beans.HeatTemplateParam;
29
30 public class MsoHeatEnvironmentEntry {
31
32         private Set<MsoHeatEnvironmentParameter> parameters = null;
33         private Set<MsoHeatEnvironmentResource> resources = null;
34         private StringBuilder rawEntry = null;
35         private boolean valid = true;
36         private String errorString = null;
37         private StringBuilder resourceRegistryEntryRaw = null;
38         
39         public MsoHeatEnvironmentEntry() {
40                 super();
41         }
42         
43         public MsoHeatEnvironmentEntry(StringBuilder sb) {
44                 this();
45                 this.rawEntry = sb;
46                 this.processRawEntry();
47         }
48         
49         private void processRawEntry() {
50                 try {
51                         if (this.rawEntry == null || this.rawEntry.equals(""))
52                                 return;
53                         byte[] b = this.rawEntry.toString().getBytes();
54                         MsoYamlEditorWithEnvt yaml = new MsoYamlEditorWithEnvt(b);
55                         this.parameters = yaml.getParameterListFromEnvt();
56                         //this.resources = yaml.getResourceListFromEnvt();
57                         StringBuilder sb = this.getResourceRegistryRawEntry();
58                         if (sb == null) {
59                                 this.resourceRegistryEntryRaw = new StringBuilder("");
60                         } else {
61                                 this.resourceRegistryEntryRaw = sb;
62                         }
63                 } catch (Exception e) {
64                         this.valid = false;
65                         this.errorString = e.getMessage();
66                         //e.printStackTrace();
67                 }
68         }
69         
70         public boolean isValid() {
71                 return this.valid;
72         }
73         public String getErrorString() {
74                 return this.errorString;
75         }
76         
77         public Set<MsoHeatEnvironmentParameter> getParameters() {
78                 return this.parameters;
79         }
80         public Set<MsoHeatEnvironmentResource> getResources() {
81                 return this.resources;
82         }
83         public void setParameters(Set<MsoHeatEnvironmentParameter> paramSet) {
84                 if (paramSet == null) {
85                         this.parameters = null;
86                 } else {
87                         this.parameters = paramSet;
88                 }
89         }
90         public void setResources(Set<MsoHeatEnvironmentResource> resourceSet) {
91                 if (resourceSet == null) {
92                         this.resources = null;
93                 } else {
94                         this.resources = resourceSet;
95                 }
96         }
97         
98         public void addParameter(MsoHeatEnvironmentParameter hep) {
99                 if (this.parameters == null) {
100                         this.parameters = new HashSet<MsoHeatEnvironmentParameter>();
101                 }
102                 this.parameters.add(hep);
103         }
104         public void addResource(MsoHeatEnvironmentResource her) {
105                 if (this.resources == null) {
106                         this.resources = new HashSet<MsoHeatEnvironmentResource>();
107                 }
108                 this.resources.add(her);
109         }
110         
111         public int getNumberOfParameters() {
112                 return this.parameters.size();
113         }
114         public int getNumberOfResources() {
115                 return this.resources.size();
116         }
117         
118         public boolean hasResources() {
119                 if (this.resources != null && this.resources.size() > 0) {
120                         return true;
121                 } 
122                 return false;
123         }
124         public boolean hasParameters() {
125                 if (this.parameters != null && this.parameters.size() > 0) {
126                         return true;
127                 }
128                 return false;
129         }
130         
131         public boolean containsParameter(String paramName) {
132                 boolean contains = false;
133                 if (this.parameters == null || this.parameters.size() < 1) {
134                         return false;
135                 }
136                 if (this.parameters.contains(new MsoHeatEnvironmentParameter(paramName))) {
137                         contains = true;
138                 }
139                 return contains;
140         }
141         
142         public boolean containsParameter(String paramName, String paramAlias) {
143                 if (this.containsParameter(paramName)) {
144                         return true;
145                 }
146                 if (this.containsParameter(paramAlias)) {
147                         return true;
148                 }
149                 return false;
150         }
151         
152         public StringBuilder toFullStringExcludeNonParams(Set<HeatTemplateParam> params) {
153                 // Basically give back the envt - but exclude the params that aren't in the HeatTemplate 
154                 
155                 StringBuilder sb = new StringBuilder();
156                 ArrayList<String> paramNameList = new ArrayList<String>(params.size());
157                 for (HeatTemplateParam htp : params) {
158                         paramNameList.add(htp.getParamName());
159                 }
160                 
161                 if (this.hasParameters()) {
162                         sb.append("parameters:\n");
163                         for (MsoHeatEnvironmentParameter hep : this.parameters) {
164                                 String paramName = hep.getName();
165                                 if (paramNameList.contains(paramName)) {
166                                         // This parameter *is* in the Heat Template - so include it:
167                                         sb.append("  " + hep.getName() + ": " + hep.getValue() + "\n");
168                                         // New - 1607 - if any of the params mapped badly - JUST RETURN THE ORIGINAL ENVT!
169                                         if (hep.getValue().startsWith("_BAD")) {
170                                                 return this.rawEntry;
171                                         }
172                                 } 
173                         }
174                         sb.append("\n");
175                 }
176 //              if (this.hasResources()) {
177 //                      sb.append("resource_registry:\n");
178 //                      for (MsoHeatEnvironmentResource her : this.resources) {
179 //                              sb.append("   \"" + her.getName() + "\": " + her.getValue() + "\n");
180 //                      }
181 //              }
182                 sb.append("\n");
183                 sb.append(this.resourceRegistryEntryRaw);                               
184                 return sb;
185         }
186         
187         public StringBuilder toFullString() {
188                 StringBuilder sb = new StringBuilder();
189                 
190                 if (this.hasParameters()) {
191                         sb.append("parameters:\n");
192                         for (MsoHeatEnvironmentParameter hep : this.parameters) {
193                                 sb.append("   " + hep.getName() + ":  " + hep.getValue() + "\n");
194                         }
195                         sb.append("\n");
196                 }
197 //              if (this.hasResources()) {
198 //                      sb.append("resource_registry:\n");
199 //                      for (MsoHeatEnvironmentResource her : this.resources) {
200 //                              sb.append("   \"" + her.getName() + "\": " + her.getValue() + "\n");
201 //                      }
202 //              }
203                 sb.append("\n");
204                 sb.append(this.resourceRegistryEntryRaw);                       
205                 return sb;
206         }
207
208         public StringBuilder getRawEntry() {
209                 return this.rawEntry;
210         }
211         
212         private StringBuilder getResourceRegistryRawEntry() {
213                 
214                 if (this.rawEntry == null) {
215                         return null;
216                 }
217                 
218                 StringBuilder sb = new StringBuilder();
219                 int indexOf = this.rawEntry.indexOf("resource_registry:");
220                 if (indexOf < 0) { // no resource_registry:
221                         return null;
222                 }
223                 sb.append(this.rawEntry.substring(indexOf));
224                 return sb;
225         }
226         
227 }