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;
26 import java.util.HashSet;
27 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() {
78 public String getErrorString() {
79 return this.errorString;
82 public Set<MsoHeatEnvironmentParameter> getParameters() {
83 return this.parameters;
85 public Set<MsoHeatEnvironmentResource> getResources() {
86 return this.resources;
88 public void setParameters(Set<MsoHeatEnvironmentParameter> paramSet) {
89 if (paramSet == null) {
90 this.parameters = null;
92 this.parameters = paramSet;
95 public void setResources(Set<MsoHeatEnvironmentResource> resourceSet) {
96 if (resourceSet == null) {
97 this.resources = null;
99 this.resources = resourceSet;
103 public void addParameter(MsoHeatEnvironmentParameter hep) {
104 if (this.parameters == null) {
105 this.parameters = new HashSet<>();
107 this.parameters.add(hep);
109 public void addResource(MsoHeatEnvironmentResource her) {
110 if (this.resources == null) {
111 this.resources = new HashSet<>();
113 this.resources.add(her);
116 public int getNumberOfParameters() {
117 return this.parameters.size();
119 public int getNumberOfResources() {
120 return this.resources.size();
123 public boolean hasResources() {
124 if (this.resources != null && this.resources.size() > 0) {
129 public boolean hasParameters() {
130 if (this.parameters != null && this.parameters.size() > 0) {
136 public boolean containsParameter(String paramName) {
137 boolean contains = false;
138 if (this.parameters == null || this.parameters.size() < 1) {
141 if (this.parameters.contains(new MsoHeatEnvironmentParameter(paramName))) {
147 public boolean containsParameter(String paramName, String paramAlias) {
148 if (this.containsParameter(paramName)) {
151 if (this.containsParameter(paramAlias)) {
158 public String toString() {
159 return "MsoHeatEnvironmentEntry{" + "parameters=" + parameters +
160 ", resourceRegistryEntryRaw='" + resourceRegistryEntryRaw + '\'' +
164 public StringBuilder toFullStringExcludeNonParams(Set<HeatTemplateParam> params) {
165 // Basically give back the envt - but exclude the params that aren't in the HeatTemplate
167 StringBuilder sb = new StringBuilder();
168 ArrayList<String> paramNameList = new ArrayList<String>(params.size());
169 for (HeatTemplateParam htp : params) {
170 paramNameList.add(htp.getParamName());
173 if (this.hasParameters()) {
174 sb.append("parameters:\n");
175 for (MsoHeatEnvironmentParameter hep : this.parameters) {
176 String paramName = hep.getName();
177 if (paramNameList.contains(paramName)) {
178 // This parameter *is* in the Heat Template - so include it:
179 sb.append(" " + hep.getName() + ": " + hep.getValue() + "\n");
180 // New - 1607 - if any of the params mapped badly - JUST RETURN THE ORIGINAL ENVT!
181 if (hep.getValue().startsWith("_BAD")) {
182 return this.rawEntry;
188 // if (this.hasResources()) {
189 // sb.append("resource_registry:\n");
190 // for (MsoHeatEnvironmentResource her : this.resources) {
191 // sb.append(" \"" + her.getName() + "\": " + her.getValue() + "\n");
195 sb.append(this.resourceRegistryEntryRaw);
199 public StringBuilder toFullString() {
200 StringBuilder sb = new StringBuilder();
202 if (this.hasParameters()) {
203 sb.append("parameters:\n");
204 for (MsoHeatEnvironmentParameter hep : this.parameters) {
205 sb.append(" " + hep.getName() + ": " + hep.getValue() + "\n");
209 // if (this.hasResources()) {
210 // sb.append("resource_registry:\n");
211 // for (MsoHeatEnvironmentResource her : this.resources) {
212 // sb.append(" \"" + her.getName() + "\": " + her.getValue() + "\n");
216 sb.append(this.resourceRegistryEntryRaw);
220 public StringBuilder getRawEntry() {
221 return this.rawEntry;
224 private StringBuilder getResourceRegistryRawEntry() {
226 if (this.rawEntry == null) {
230 StringBuilder sb = new StringBuilder();
231 int indexOf = this.rawEntry.indexOf("resource_registry:");
232 if (indexOf < 0) { // no resource_registry:
235 sb.append(this.rawEntry.substring(indexOf));