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.onap.so.openstack.utils;
26 import java.util.ArrayList;
27 import java.util.HashSet;
30 import org.onap.so.db.catalog.beans.HeatTemplateParam;
31 import org.onap.so.logger.MsoLogger;
33 public class MsoHeatEnvironmentEntry {
35 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA, MsoHeatEnvironmentEntry.class);
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;
44 public MsoHeatEnvironmentEntry() {
48 public MsoHeatEnvironmentEntry(StringBuilder sb) {
51 this.processRawEntry();
54 private void processRawEntry() {
56 if (this.rawEntry == null || "".equals(this.rawEntry.toString()))
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();
64 this.resourceRegistryEntryRaw = new StringBuilder("");
66 this.resourceRegistryEntryRaw = sb;
68 } catch (Exception e) {
69 LOGGER.debug("Exception:", e);
71 this.errorString = e.getMessage();
72 //e.printStackTrace();
76 public boolean isValid() {
79 public String getErrorString() {
80 return this.errorString;
83 public Set<MsoHeatEnvironmentParameter> getParameters() {
84 return this.parameters;
86 public Set<MsoHeatEnvironmentResource> getResources() {
87 return this.resources;
89 public void setParameters(Set<MsoHeatEnvironmentParameter> paramSet) {
90 if (paramSet == null) {
91 this.parameters = null;
93 this.parameters = paramSet;
96 public void setResources(Set<MsoHeatEnvironmentResource> resourceSet) {
97 if (resourceSet == null) {
98 this.resources = null;
100 this.resources = resourceSet;
104 public void addParameter(MsoHeatEnvironmentParameter hep) {
105 if (this.parameters == null) {
106 this.parameters = new HashSet<>();
108 this.parameters.add(hep);
110 public void addResource(MsoHeatEnvironmentResource her) {
111 if (this.resources == null) {
112 this.resources = new HashSet<>();
114 this.resources.add(her);
117 public int getNumberOfParameters() {
118 return this.parameters.size();
120 public int getNumberOfResources() {
121 return this.resources.size();
124 public boolean hasResources() {
125 if (this.resources != null && this.resources.size() > 0) {
130 public boolean hasParameters() {
131 if (this.parameters != null && this.parameters.size() > 0) {
137 public boolean containsParameter(String paramName) {
138 boolean contains = false;
139 if (this.parameters == null || this.parameters.size() < 1) {
142 if (this.parameters.contains(new MsoHeatEnvironmentParameter(paramName))) {
148 public boolean containsParameter(String paramName, String paramAlias) {
149 if (this.containsParameter(paramName)) {
152 if (this.containsParameter(paramAlias)) {
159 public String toString() {
160 return "MsoHeatEnvironmentEntry{" + "parameters=" + parameters +
161 ", resourceRegistryEntryRaw='" + resourceRegistryEntryRaw + '\'' +
165 public StringBuilder toFullStringExcludeNonParams(Set<HeatTemplateParam> params) {
166 // Basically give back the envt - but exclude the params that aren't in the HeatTemplate
168 StringBuilder sb = new StringBuilder();
169 ArrayList<String> paramNameList = new ArrayList<String>(params.size());
170 for (HeatTemplateParam htp : params) {
171 paramNameList.add(htp.getParamName());
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;
189 // if (this.hasResources()) {
190 // sb.append("resource_registry:\n");
191 // for (MsoHeatEnvironmentResource her : this.resources) {
192 // sb.append(" \"" + her.getName() + "\": " + her.getValue() + "\n");
196 sb.append(this.resourceRegistryEntryRaw);
200 public StringBuilder toFullString() {
201 StringBuilder sb = new StringBuilder();
203 if (this.hasParameters()) {
204 sb.append("parameters:\n");
205 for (MsoHeatEnvironmentParameter hep : this.parameters) {
206 sb.append(" " + hep.getName() + ": " + hep.getValue() + "\n");
210 // if (this.hasResources()) {
211 // sb.append("resource_registry:\n");
212 // for (MsoHeatEnvironmentResource her : this.resources) {
213 // sb.append(" \"" + her.getName() + "\": " + her.getValue() + "\n");
217 sb.append(this.resourceRegistryEntryRaw);
221 public StringBuilder getRawEntry() {
222 return this.rawEntry;
225 private StringBuilder getResourceRegistryRawEntry() {
227 if (this.rawEntry == null) {
231 StringBuilder sb = new StringBuilder();
232 int indexOf = this.rawEntry.indexOf("resource_registry:");
233 if (indexOf < 0) { // no resource_registry:
236 sb.append(this.rawEntry.substring(indexOf));
240 public void setHPAParameters(StringBuilder hpasb) {
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());
251 } catch (Exception e) {
252 LOGGER.debug("Exception:", e);
253 this.errorString = e.getMessage();
254 //e.printStackTrace();