2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.openstack.utils;
25 import java.util.HashSet;
26 import java.util.ArrayList;
28 import org.openecomp.mso.db.catalog.beans.HeatTemplateParam;
30 public class MsoHeatEnvironmentEntry {
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;
39 public MsoHeatEnvironmentEntry() {
43 public MsoHeatEnvironmentEntry(StringBuilder sb) {
46 this.processRawEntry();
49 private void processRawEntry() {
51 if (this.rawEntry == null || this.rawEntry.equals(""))
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();
59 this.resourceRegistryEntryRaw = new StringBuilder("");
61 this.resourceRegistryEntryRaw = sb;
63 } catch (Exception e) {
65 this.errorString = e.getMessage();
66 //e.printStackTrace();
70 public boolean isValid() {
73 public String getErrorString() {
74 return this.errorString;
77 public Set<MsoHeatEnvironmentParameter> getParameters() {
78 return this.parameters;
80 public Set<MsoHeatEnvironmentResource> getResources() {
81 return this.resources;
83 public void setParameters(Set<MsoHeatEnvironmentParameter> paramSet) {
84 if (paramSet == null) {
85 this.parameters = null;
87 this.parameters = paramSet;
90 public void setResources(Set<MsoHeatEnvironmentResource> resourceSet) {
91 if (resourceSet == null) {
92 this.resources = null;
94 this.resources = resourceSet;
98 public void addParameter(MsoHeatEnvironmentParameter hep) {
99 if (this.parameters == null) {
100 this.parameters = new HashSet<MsoHeatEnvironmentParameter>();
102 this.parameters.add(hep);
104 public void addResource(MsoHeatEnvironmentResource her) {
105 if (this.resources == null) {
106 this.resources = new HashSet<MsoHeatEnvironmentResource>();
108 this.resources.add(her);
111 public int getNumberOfParameters() {
112 return this.parameters.size();
114 public int getNumberOfResources() {
115 return this.resources.size();
118 public boolean hasResources() {
119 if (this.resources != null && this.resources.size() > 0) {
124 public boolean hasParameters() {
125 if (this.parameters != null && this.parameters.size() > 0) {
131 public boolean containsParameter(String paramName) {
132 boolean contains = false;
133 if (this.parameters == null || this.parameters.size() < 1) {
136 if (this.parameters.contains(new MsoHeatEnvironmentParameter(paramName))) {
142 public boolean containsParameter(String paramName, String paramAlias) {
143 if (this.containsParameter(paramName)) {
146 if (this.containsParameter(paramAlias)) {
152 public StringBuilder toFullStringExcludeNonParams(Set<HeatTemplateParam> params) {
153 // Basically give back the envt - but exclude the params that aren't in the HeatTemplate
155 StringBuilder sb = new StringBuilder();
156 ArrayList<String> paramNameList = new ArrayList<String>(params.size());
157 for (HeatTemplateParam htp : params) {
158 paramNameList.add(htp.getParamName());
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;
176 // if (this.hasResources()) {
177 // sb.append("resource_registry:\n");
178 // for (MsoHeatEnvironmentResource her : this.resources) {
179 // sb.append(" \"" + her.getName() + "\": " + her.getValue() + "\n");
183 sb.append(this.resourceRegistryEntryRaw);
187 public StringBuilder toFullString() {
188 StringBuilder sb = new StringBuilder();
190 if (this.hasParameters()) {
191 sb.append("parameters:\n");
192 for (MsoHeatEnvironmentParameter hep : this.parameters) {
193 sb.append(" " + hep.getName() + ": " + hep.getValue() + "\n");
197 // if (this.hasResources()) {
198 // sb.append("resource_registry:\n");
199 // for (MsoHeatEnvironmentResource her : this.resources) {
200 // sb.append(" \"" + her.getName() + "\": " + her.getValue() + "\n");
204 sb.append(this.resourceRegistryEntryRaw);
208 public StringBuilder getRawEntry() {
209 return this.rawEntry;
212 private StringBuilder getResourceRegistryRawEntry() {
214 if (this.rawEntry == null) {
218 StringBuilder sb = new StringBuilder();
219 int indexOf = this.rawEntry.indexOf("resource_registry:");
220 if (indexOf < 0) { // no resource_registry:
223 sb.append(this.rawEntry.substring(indexOf));