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