diff options
Diffstat (limited to 'sdc-distribution-client/src/main/java/org/onap/sdc/utils/heat')
4 files changed, 417 insertions, 0 deletions
diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/utils/heat/HeatConfiguration.java b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/heat/HeatConfiguration.java new file mode 100644 index 0000000..ee272f4 --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/heat/HeatConfiguration.java @@ -0,0 +1,42 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.utils.heat; + +import java.util.Map; + +public class HeatConfiguration { + + //All the rest of heat file is not needed for now... + Map<String, HeatParameter> parameters; + + + public Map<String, HeatParameter> getParameters() { + return parameters; + } + + + public void setParameters(Map<String, HeatParameter> parameters) { + this.parameters = parameters; + } + + + +} diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/utils/heat/HeatParameter.java b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/heat/HeatParameter.java new file mode 100644 index 0000000..3638bcf --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/heat/HeatParameter.java @@ -0,0 +1,207 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.utils.heat; + +import java.util.ArrayList; +import java.util.List; + +public class HeatParameter{ + + String type; + String label; + String description; + //This is in order to workaround "default" field in HeatParameterEntry, since default is Java keyword + //YAML constructor will lowercase it during parsing + String Default; + String hidden = "false";//Default value according to OpenStack spec + List<HeatParameterConstraint> constraints; + + + + public String getHidden() { + return hidden; + } + public void setHidden(String hidden) { + this.hidden = hidden; + } + + public List<HeatParameterConstraint> getConstraints() { + return constraints; + } + public void setConstraints(List<HeatParameterConstraint> constraints) { + this.constraints = constraints; + } + public String getType() { + return type; + } + public void setType(String type) { + this.type = type; + } + public String getLabel() { + return label; + } + public void setLabel(String label) { + this.label = label; + } + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } + public String getDefault() { + return Default; + } + public void setDefault(String default1) { + Default = default1; + } + + + + // Getting specific constraints + /** + * Get the first "length" constraint from HEAT parameter. + * No additional "length" constraint is searched for. + * + * @return first "length" constraint found for this parameter, + * or null if no such constraint exists. + */ + public HeatParameterConstraint getLengthConstraint(){ + HeatParameterConstraint res = null; + if (constraints != null){ + for (HeatParameterConstraint entry : constraints){ + if (entry.getLength() != null){ + res = entry; + break; + } + } + } + return res; + } + + + /** + * Get the first "range" constraint from HEAT parameter. + * No additional "range" constraint is searched for. + * + * @return first "range" constraint found for this parameter, + * or null if no such constraint exists. + */ + public HeatParameterConstraint getRangeConstraint(){ + HeatParameterConstraint res = null; + if (constraints != null){ + for (HeatParameterConstraint entry : constraints){ + if (entry.getRange() != null){ + res = entry; + break; + } + } + } + return res; + } + + /** + * Get the first "allowed_values" constraint from HEAT parameter. + * No additional "allowed_values" constraint is searched for. + * + * @return first "allowed_values" constraint found for this parameter, + * or null if no such constraint exists. + */ + public HeatParameterConstraint getAllowedValuesConstraint(){ + HeatParameterConstraint res = null; + if (constraints != null){ + for (HeatParameterConstraint entry : constraints){ + if (entry.getAllowed_values() != null){ + res = entry; + break; + } + } + } + return res; + } + + /** + * Get the "allowed_pattern" constraint list from HEAT parameter. + * + * @return "allowed_pattern" constraint list found for this parameter, + * or null if no such constraint exists. + */ + public List<HeatParameterConstraint> getAllowedPatternConstraint(){ + List<HeatParameterConstraint> res = null; + if (constraints != null){ + for (HeatParameterConstraint entry : constraints){ + if (entry.getAllowed_pattern() != null){ + if (res == null){ + res = new ArrayList<>(); + } + res.add(entry); + } + } + } + return res; + } + + /** + * Get the "custom_constraint" constraint list from HEAT parameter. + * + * @return "custom_constraint" constraint list found for this parameter, + * or null if no such constraint exists. + */ + public List<HeatParameterConstraint> getCustomConstraintConstraint(){ + List<HeatParameterConstraint> res = null; + if (constraints != null){ + for (HeatParameterConstraint entry : constraints){ + if (entry.getCustom_constraint() != null){ + if (res == null){ + res = new ArrayList<>(); + } + res.add(entry); + } + } + } + return res; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if (type != null){ + sb.append("type:"+type+", "); + } + if (label != null){ + sb.append("label:"+label+", "); + } + if (Default != null){ + sb.append("default:"+Default+", "); + } + if (hidden != null){ + sb.append("hidden:"+hidden+", "); + } + if (constraints != null){ + sb.append("constraints:"+constraints+", "); + } + if (description != null){ + sb.append("description:"+description); + } + return sb.toString(); + } + +} diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/utils/heat/HeatParameterConstraint.java b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/heat/HeatParameterConstraint.java new file mode 100644 index 0000000..d618caf --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/heat/HeatParameterConstraint.java @@ -0,0 +1,93 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.utils.heat; + +import java.util.List; +import java.util.Map; + +public class HeatParameterConstraint { + + Map<String, String> length; + Map<String, String> range; + List<String> allowed_values; + String allowed_pattern; + String custom_constraint; + String description; + + + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } + public Map<String, String> getLength() { + return length; + } + public void setLength(Map<String, String> length) { + this.length = length; + } + public Map<String, String> getRange() { + return range; + } + public void setRange(Map<String, String> range) { + this.range = range; + } + public List<String> getAllowed_values() { + return allowed_values; + } + public void setAllowed_values(List<String> allowed_values) { + this.allowed_values = allowed_values; + } + public String getAllowed_pattern() { + return allowed_pattern; + } + public void setAllowed_pattern(String allowed_pattern) { + this.allowed_pattern = allowed_pattern; + } + public String getCustom_constraint() { + return custom_constraint; + } + public void setCustom_constraint(String custom_constraint) { + this.custom_constraint = custom_constraint; + } + + @Override + public String toString() { + String constraintTypeValue = "<empty>"; + String descriptionStr = "<empty>"; + if (length != null){ + constraintTypeValue = "length:"+length; + } else if (range != null){ + constraintTypeValue = "range:"+range; + } else if (allowed_values != null){ + constraintTypeValue = "allowed_values:"+allowed_values; + } else if (allowed_pattern != null){ + constraintTypeValue = "allowed_pattern:"+allowed_pattern; + } else if (custom_constraint != null){ + constraintTypeValue = "custom_constraint:"+custom_constraint; + } + if (description != null){ + descriptionStr = "description:"+description; + } + return new StringBuilder().append(constraintTypeValue).append(", ").append(descriptionStr).toString(); + } +} diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/utils/heat/HeatParser.java b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/heat/HeatParser.java new file mode 100644 index 0000000..340a59d --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/heat/HeatParser.java @@ -0,0 +1,75 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.utils.heat; + +import java.util.Map; + +import org.onap.sdc.utils.YamlToObjectConverter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HeatParser { + + private static Logger log = LoggerFactory.getLogger(HeatParser.class.getName()); + + + /** + * Parses and returns the contents of the "parameters" section of YAML-formatted HEAT template. + * + * @param heatFileContents - the string contents of HEAT template + * @return map of parameter name to HeatParameter object. + * For the following YAML snippet: + * <b>parameters: + * image_name_1: + * type: string + * label: Image Name + * description: SCOIMAGE Specify an image name for instance1 + * default: cirros-0.3.1-x86_64 + * </b> + * the map with one entry will be returned, the key will be "image_name_1". + * For a HeatParameter object, getConstraints() returns the list of all constraints, + * regardless of constraint type. + * For that reason, for each constraint type a sugaring function were added on the HeatParameter type, + * for example getLengthConstraint(). A correct way to fetch the "length" constraint values map would be + * parameter.getLengthConstraint().getLength(). Same logic was implemented for all other constraint types. + * + * In case of parse error, null will be returned. + * + */ + public Map<String, HeatParameter> getHeatParameters(String heatFileContents){ + log.debug("Start of extracting HEAT parameters from file, file contents: {}", heatFileContents); + Map<String, HeatParameter> heatParameters = null; + YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter(); + HeatConfiguration heatConfiguration = yamlToObjectConverter.convertFromString(heatFileContents, HeatConfiguration.class); + if (heatConfiguration != null){ + heatParameters = heatConfiguration.getParameters(); + } else { + log.error("Couldn't parse HEAT template."); + } + if (heatParameters != null && heatParameters.size() > 0){ + System.out.println("Found HEAT parameters: "+heatParameters.toString()); + log.debug("Found HEAT parameters: {}", heatParameters.toString()); + } else { + log.warn("HEAT template parameters section wasn't found or is empty."); + } + return heatParameters; + } +} |