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 * 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
14 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
24 package org.onap.so.openstack.utils;
27 import java.util.ArrayList;
28 import java.util.HashSet;
30 import org.onap.so.db.catalog.beans.HeatTemplateParam;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class MsoHeatEnvironmentEntry {
36 private static final Logger logger = LoggerFactory.getLogger(MsoHeatEnvironmentEntry.class);
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;
45 public MsoHeatEnvironmentEntry() {
49 public MsoHeatEnvironmentEntry(StringBuilder sb) {
52 this.processRawEntry();
55 private void processRawEntry() {
57 if (this.rawEntry == null || "".equals(this.rawEntry.toString()))
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();
65 this.resourceRegistryEntryRaw = new StringBuilder("");
67 this.resourceRegistryEntryRaw = sb;
69 } catch (Exception e) {
70 logger.debug("Exception:", e);
72 this.errorString = e.getMessage();
73 //e.printStackTrace();
77 public boolean isValid() {
80 public String getErrorString() {
81 return this.errorString;
84 public Set<MsoHeatEnvironmentParameter> getParameters() {
85 return this.parameters;
87 public Set<MsoHeatEnvironmentResource> getResources() {
88 return this.resources;
90 public void setParameters(Set<MsoHeatEnvironmentParameter> paramSet) {
91 if (paramSet == null) {
92 this.parameters = null;
94 this.parameters = paramSet;
97 public void setResources(Set<MsoHeatEnvironmentResource> resourceSet) {
98 if (resourceSet == null) {
99 this.resources = null;
101 this.resources = resourceSet;
105 public void addParameter(MsoHeatEnvironmentParameter hep) {
106 if (this.parameters == null) {
107 this.parameters = new HashSet<>();
109 this.parameters.add(hep);
111 public void addResource(MsoHeatEnvironmentResource her) {
112 if (this.resources == null) {
113 this.resources = new HashSet<>();
115 this.resources.add(her);
118 public int getNumberOfParameters() {
119 return this.parameters.size();
121 public int getNumberOfResources() {
122 return this.resources.size();
125 public boolean hasResources() {
126 if (this.resources != null && this.resources.size() > 0) {
131 public boolean hasParameters() {
132 if (this.parameters != null && this.parameters.size() > 0) {
138 public boolean containsParameter(String paramName) {
139 boolean contains = false;
140 if (this.parameters == null || this.parameters.size() < 1) {
143 if (this.parameters.contains(new MsoHeatEnvironmentParameter(paramName))) {
149 public boolean containsParameter(String paramName, String paramAlias) {
150 if (this.containsParameter(paramName)) {
153 if (this.containsParameter(paramAlias)) {
160 public String toString() {
161 return "MsoHeatEnvironmentEntry{" + "parameters=" + parameters +
162 ", resourceRegistryEntryRaw='" + resourceRegistryEntryRaw + '\'' +
166 public StringBuilder toFullStringExcludeNonParams(Set<HeatTemplateParam> params) {
167 // Basically give back the envt - but exclude the params that aren't in the HeatTemplate
169 StringBuilder sb = new StringBuilder();
170 ArrayList<String> paramNameList = new ArrayList<String>(params.size());
171 for (HeatTemplateParam htp : params) {
172 paramNameList.add(htp.getParamName());
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;
190 // if (this.hasResources()) {
191 // sb.append("resource_registry:\n");
192 // for (MsoHeatEnvironmentResource her : this.resources) {
193 // sb.append(" \"" + her.getName() + "\": " + her.getValue() + "\n");
197 sb.append(this.resourceRegistryEntryRaw);
201 public StringBuilder toFullString() {
202 StringBuilder sb = new StringBuilder();
204 if (this.hasParameters()) {
205 sb.append("parameters:\n");
206 for (MsoHeatEnvironmentParameter hep : this.parameters) {
207 sb.append(" " + hep.getName() + ": " + hep.getValue() + "\n");
211 // if (this.hasResources()) {
212 // sb.append("resource_registry:\n");
213 // for (MsoHeatEnvironmentResource her : this.resources) {
214 // sb.append(" \"" + her.getName() + "\": " + her.getValue() + "\n");
218 sb.append(this.resourceRegistryEntryRaw);
222 public StringBuilder getRawEntry() {
223 return this.rawEntry;
226 private StringBuilder getResourceRegistryRawEntry() {
228 if (this.rawEntry == null) {
232 StringBuilder sb = new StringBuilder();
233 int indexOf = this.rawEntry.indexOf("resource_registry:");
234 if (indexOf < 0) { // no resource_registry:
237 sb.append(this.rawEntry.substring(indexOf));
241 public void setHPAParameters(StringBuilder hpasb) {
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());
252 } catch (Exception e) {
253 logger.debug("Exception:", e);
254 this.errorString = e.getMessage();
255 //e.printStackTrace();