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