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() {
81 public String getErrorString() {
82 return this.errorString;
85 public Set<MsoHeatEnvironmentParameter> getParameters() {
86 return this.parameters;
89 public Set<MsoHeatEnvironmentResource> getResources() {
90 return this.resources;
93 public void setParameters(Set<MsoHeatEnvironmentParameter> paramSet) {
94 if (paramSet == null) {
95 this.parameters = null;
97 this.parameters = paramSet;
101 public void setResources(Set<MsoHeatEnvironmentResource> resourceSet) {
102 if (resourceSet == null) {
103 this.resources = null;
105 this.resources = resourceSet;
109 public void addParameter(MsoHeatEnvironmentParameter hep) {
110 if (this.parameters == null) {
111 this.parameters = new HashSet<>();
113 this.parameters.add(hep);
116 public void addResource(MsoHeatEnvironmentResource her) {
117 if (this.resources == null) {
118 this.resources = new HashSet<>();
120 this.resources.add(her);
123 public int getNumberOfParameters() {
124 return this.parameters.size();
127 public int getNumberOfResources() {
128 return this.resources.size();
131 public boolean hasResources() {
132 if (this.resources != null && this.resources.size() > 0) {
138 public boolean hasParameters() {
139 if (this.parameters != null && this.parameters.size() > 0) {
145 public boolean containsParameter(String paramName) {
146 boolean contains = false;
147 if (this.parameters == null || this.parameters.size() < 1) {
150 if (this.parameters.contains(new MsoHeatEnvironmentParameter(paramName))) {
156 public boolean containsParameter(String paramName, String paramAlias) {
157 if (this.containsParameter(paramName)) {
160 if (this.containsParameter(paramAlias)) {
167 public String toString() {
168 return "MsoHeatEnvironmentEntry{" + "parameters=" + parameters + ", resourceRegistryEntryRaw='"
169 + resourceRegistryEntryRaw + '\'' + '}';
172 public StringBuilder toFullStringExcludeNonParams(Set<HeatTemplateParam> params) {
173 // Basically give back the envt - but exclude the params that aren't in the HeatTemplate
175 StringBuilder sb = new StringBuilder();
176 ArrayList<String> paramNameList = new ArrayList<String>(params.size());
177 for (HeatTemplateParam htp : params) {
178 paramNameList.add(htp.getParamName());
181 if (this.hasParameters()) {
182 sb.append("parameters:\n");
183 for (MsoHeatEnvironmentParameter hep : this.parameters) {
184 String paramName = hep.getName();
185 if (paramNameList.contains(paramName)) {
186 // This parameter *is* in the Heat Template - so include it:
187 sb.append(" " + hep.getName() + ": " + hep.getValue() + "\n");
188 // New - 1607 - if any of the params mapped badly - JUST RETURN THE ORIGINAL ENVT!
189 if (hep.getValue().startsWith("_BAD")) {
190 return this.rawEntry;
196 // if (this.hasResources()) {
197 // sb.append("resource_registry:\n");
198 // for (MsoHeatEnvironmentResource her : this.resources) {
199 // sb.append(" \"" + her.getName() + "\": " + her.getValue() + "\n");
203 sb.append(this.resourceRegistryEntryRaw);
207 public StringBuilder toFullString() {
208 StringBuilder sb = new StringBuilder();
210 if (this.hasParameters()) {
211 sb.append("parameters:\n");
212 for (MsoHeatEnvironmentParameter hep : this.parameters) {
213 sb.append(" " + hep.getName() + ": " + hep.getValue() + "\n");
217 // if (this.hasResources()) {
218 // sb.append("resource_registry:\n");
219 // for (MsoHeatEnvironmentResource her : this.resources) {
220 // sb.append(" \"" + her.getName() + "\": " + her.getValue() + "\n");
224 sb.append(this.resourceRegistryEntryRaw);
228 public StringBuilder getRawEntry() {
229 return this.rawEntry;
232 private StringBuilder getResourceRegistryRawEntry() {
234 if (this.rawEntry == null) {
238 StringBuilder sb = new StringBuilder();
239 int indexOf = this.rawEntry.indexOf("resource_registry:");
240 if (indexOf < 0) { // no resource_registry:
243 sb.append(this.rawEntry.substring(indexOf));
247 public void setHPAParameters(StringBuilder hpasb) {
249 MsoYamlEditorWithEnvt yaml = new MsoYamlEditorWithEnvt(hpasb.toString().getBytes());
250 Set<MsoHeatEnvironmentParameter> hpaParams = yaml.getParameterListFromEnvt();
251 for (MsoHeatEnvironmentParameter hpaparam : hpaParams) {
252 for (MsoHeatEnvironmentParameter param : this.parameters) {
253 if (param.getName() == hpaparam.getName()) {
254 param.setValue(hpaparam.getValue());
258 } catch (Exception e) {
259 logger.debug("Exception:", e);
260 this.errorString = e.getMessage();
261 // e.printStackTrace();