2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.mso.openstack.utils;
25 import java.util.HashSet;
26 import java.util.ArrayList;
29 import org.openecomp.mso.db.catalog.beans.HeatTemplateParam;
30 import org.openecomp.mso.logger.MsoLogger;
32 public class MsoHeatEnvironmentEntry {
34 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
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;
43 public MsoHeatEnvironmentEntry() {
47 public MsoHeatEnvironmentEntry(StringBuilder sb) {
50 this.processRawEntry();
53 private void processRawEntry() {
55 if (this.rawEntry == null || "".equals(this.rawEntry))
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();
63 this.resourceRegistryEntryRaw = new StringBuilder("");
65 this.resourceRegistryEntryRaw = sb;
67 } catch (Exception e) {
68 LOGGER.debug("Exception:", e);
70 this.errorString = e.getMessage();
71 //e.printStackTrace();
75 public boolean isValid() {
79 public String getErrorString() {
80 return this.errorString;
83 public Set<MsoHeatEnvironmentParameter> getParameters() {
84 return this.parameters;
87 public Set<MsoHeatEnvironmentResource> getResources() {
88 return this.resources;
91 public void setParameters(Set<MsoHeatEnvironmentParameter> paramSet) {
92 if (paramSet == null) {
93 this.parameters = null;
95 this.parameters = paramSet;
99 public void setResources(Set<MsoHeatEnvironmentResource> resourceSet) {
100 if (resourceSet == null) {
101 this.resources = null;
103 this.resources = resourceSet;
107 public void addParameter(MsoHeatEnvironmentParameter hep) {
108 if (this.parameters == null) {
109 this.parameters = new HashSet<>();
111 this.parameters.add(hep);
114 public void addResource(MsoHeatEnvironmentResource her) {
115 if (this.resources == null) {
116 this.resources = new HashSet<>();
118 this.resources.add(her);
121 public int getNumberOfParameters() {
122 return this.parameters.size();
125 public int getNumberOfResources() {
126 return this.resources.size();
129 public boolean hasResources() {
130 if (this.resources != null && this.resources.size() > 0) {
136 public boolean hasParameters() {
137 if (this.parameters != null && this.parameters.size() > 0) {
143 public boolean containsParameter(String paramName) {
144 boolean contains = false;
145 if (this.parameters == null || this.parameters.size() < 1) {
148 if (this.parameters.contains(new MsoHeatEnvironmentParameter(paramName))) {
154 public boolean containsParameter(String paramName, String paramAlias) {
155 if (this.containsParameter(paramName)) {
158 if (this.containsParameter(paramAlias)) {
165 public String toString() {
166 return "MsoHeatEnvironmentEntry{" + "parameters=" + parameters +
167 ", resourceRegistryEntryRaw='" + resourceRegistryEntryRaw + '\'' +
171 public StringBuilder toFullStringExcludeNonParams(Set<HeatTemplateParam> params) {
172 // Basically give back the envt - but exclude the params that aren't in the HeatTemplate
174 StringBuilder sb = new StringBuilder();
175 ArrayList<String> paramNameList = new ArrayList<String>(params.size());
176 for (HeatTemplateParam htp : params) {
177 paramNameList.add(htp.getParamName());
180 if (this.hasParameters()) {
181 sb.append("parameters:\n");
182 for (MsoHeatEnvironmentParameter hep : this.parameters) {
183 String paramName = hep.getName();
184 if (paramNameList.contains(paramName)) {
185 // This parameter *is* in the Heat Template - so include it:
186 sb.append(" " + hep.getName() + ": " + hep.getValue() + "\n");
187 // New - 1607 - if any of the params mapped badly - JUST RETURN THE ORIGINAL ENVT!
188 if (hep.getValue().startsWith("_BAD")) {
189 return this.rawEntry;
195 // if (this.hasResources()) {
196 // sb.append("resource_registry:\n");
197 // for (MsoHeatEnvironmentResource her : this.resources) {
198 // sb.append(" \"" + her.getName() + "\": " + her.getValue() + "\n");
202 sb.append(this.resourceRegistryEntryRaw);
206 public StringBuilder toFullString() {
207 StringBuilder sb = new StringBuilder();
209 if (this.hasParameters()) {
210 sb.append("parameters:\n");
211 for (MsoHeatEnvironmentParameter hep : this.parameters) {
212 sb.append(" " + hep.getName() + ": " + hep.getValue() + "\n");
216 // if (this.hasResources()) {
217 // sb.append("resource_registry:\n");
218 // for (MsoHeatEnvironmentResource her : this.resources) {
219 // sb.append(" \"" + her.getName() + "\": " + her.getValue() + "\n");
223 sb.append(this.resourceRegistryEntryRaw);
227 public StringBuilder getRawEntry() {
228 return this.rawEntry;
231 private StringBuilder getResourceRegistryRawEntry() {
233 if (this.rawEntry == null) {
237 StringBuilder sb = new StringBuilder();
238 int indexOf = this.rawEntry.indexOf("resource_registry:");
239 if (indexOf < 0) { // no resource_registry:
242 sb.append(this.rawEntry.substring(indexOf));
246 public void setHPAParameters(StringBuilder hpasb) {
248 MsoYamlEditorWithEnvt yaml = new MsoYamlEditorWithEnvt(hpasb.toString().getBytes());
249 Set<MsoHeatEnvironmentParameter> hpaParams = yaml.getParameterListFromEnvt();
250 for (MsoHeatEnvironmentParameter hpaparam : hpaParams) {
251 for (MsoHeatEnvironmentParameter param : this.parameters) {
252 if (param.getName() == hpaparam.getName()) {
253 param.setValue(hpaparam.getValue());
257 } catch (Exception e) {
258 LOGGER.debug("Exception:", e);
259 this.errorString = e.getMessage();
260 //e.printStackTrace();