3aa77c2c7a57ef8c21674b88aad5fc29407795a8
[so.git] /
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         public StringBuilder toFullStringExcludeNonParams(Set<HeatTemplateParam> params) {
158                 // Basically give back the envt - but exclude the params that aren't in the HeatTemplate 
159                 
160                 StringBuilder sb = new StringBuilder();
161                 ArrayList<String> paramNameList = new ArrayList<String>(params.size());
162                 for (HeatTemplateParam htp : params) {
163                         paramNameList.add(htp.getParamName());
164                 }
165                 
166                 if (this.hasParameters()) {
167                         sb.append("parameters:\n");
168                         for (MsoHeatEnvironmentParameter hep : this.parameters) {
169                                 String paramName = hep.getName();
170                                 if (paramNameList.contains(paramName)) {
171                                         // This parameter *is* in the Heat Template - so include it:
172                                         sb.append("  " + hep.getName() + ": " + hep.getValue() + "\n");
173                                         // New - 1607 - if any of the params mapped badly - JUST RETURN THE ORIGINAL ENVT!
174                                         if (hep.getValue().startsWith("_BAD")) {
175                                                 return this.rawEntry;
176                                         }
177                                 } 
178                         }
179                         sb.append("\n");
180                 }
181 //              if (this.hasResources()) {
182 //                      sb.append("resource_registry:\n");
183 //                      for (MsoHeatEnvironmentResource her : this.resources) {
184 //                              sb.append("   \"" + her.getName() + "\": " + her.getValue() + "\n");
185 //                      }
186 //              }
187                 sb.append("\n");
188                 sb.append(this.resourceRegistryEntryRaw);                               
189                 return sb;
190         }
191         
192         public StringBuilder toFullString() {
193                 StringBuilder sb = new StringBuilder();
194                 
195                 if (this.hasParameters()) {
196                         sb.append("parameters:\n");
197                         for (MsoHeatEnvironmentParameter hep : this.parameters) {
198                                 sb.append("   " + hep.getName() + ":  " + hep.getValue() + "\n");
199                         }
200                         sb.append("\n");
201                 }
202 //              if (this.hasResources()) {
203 //                      sb.append("resource_registry:\n");
204 //                      for (MsoHeatEnvironmentResource her : this.resources) {
205 //                              sb.append("   \"" + her.getName() + "\": " + her.getValue() + "\n");
206 //                      }
207 //              }
208                 sb.append("\n");
209                 sb.append(this.resourceRegistryEntryRaw);                       
210                 return sb;
211         }
212
213         public StringBuilder getRawEntry() {
214                 return this.rawEntry;
215         }
216         
217         private StringBuilder getResourceRegistryRawEntry() {
218                 
219                 if (this.rawEntry == null) {
220                         return null;
221                 }
222                 
223                 StringBuilder sb = new StringBuilder();
224                 int indexOf = this.rawEntry.indexOf("resource_registry:");
225                 if (indexOf < 0) { // no resource_registry:
226                         return null;
227                 }
228                 sb.append(this.rawEntry.substring(indexOf));
229                 return sb;
230         }
231         
232 }