diff options
Diffstat (limited to 'sdc-distribution-client/src/main/java/org/onap/sdc/utils')
12 files changed, 952 insertions, 0 deletions
diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/utils/ArtifactTypeEnum.java b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/ArtifactTypeEnum.java new file mode 100644 index 0000000..8df257e --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/ArtifactTypeEnum.java @@ -0,0 +1,46 @@ +/*- + * ============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; + +public enum ArtifactTypeEnum { + HEAT, + HEAT_VOL, + HEAT_NET, + MURANO_PKG, + HEAT_ENV, + YANG_XML, + OTHER, + VF_LICENSE, + VENDOR_LICENSE, + MODEL_INVENTORY_PROFILE, + MODEL_QUERY_SPEC, + APPC_CONFIG, + VNF_CATALOG, + HEAT_NESTED, + HEAT_ARTIFACT, + VF_MODULES_METADATA, + //DCAE Artifacts + DCAE_TOSCA, DCAE_JSON, DCAE_POLICY, DCAE_DOC, + DCAE_EVENT, DCAE_INVENTORY_TOSCA, DCAE_INVENTORY_JSON, + DCAE_INVENTORY_POLICY, DCAE_INVENTORY_DOC, + DCAE_INVENTORY_BLUEPRINT, DCAE_INVENTORY_EVENT; + +} diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/utils/DistributionActionResultEnum.java b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/DistributionActionResultEnum.java new file mode 100644 index 0000000..fe871f8 --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/DistributionActionResultEnum.java @@ -0,0 +1,58 @@ +/*- + * ============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; + +public enum DistributionActionResultEnum { + SUCCESS, + FAIL, + GENERAL_ERROR, + BAD_REQUEST, + DISTRIBUTION_CLIENT_NOT_INITIALIZED, + DISTRIBUTION_CLIENT_IS_TERMINATED, + DISTRIBUTION_CLIENT_ALREADY_INITIALIZED, + DISTRIBUTION_CLIENT_ALREADY_STARTED, + + DATA_INTEGRITY_PROBLEM, + ARTIFACT_NOT_FOUND, + + CONFIGURATION_IS_MISSING, + CONF_MISSING_USERNAME, + CONF_MISSING_PASSWORD, + CONF_MISSING_ASDC_FQDN, + CONF_MISSING_ARTIFACT_TYPES, + CONF_CONTAINS_INVALID_ARTIFACT_TYPES, + CONF_MISSING_CONSUMER_ID, + CONF_MISSING_ENVIRONMENT_NAME, + CONF_MISSING_CONSUMER_GROUP, + CONF_INVALID_ASDC_FQDN, + CONF_INVALID_CONSUME_PRODUCE_STATUS_TOPIC_FALG, + CONF_MISSING_MSG_BUS_ADDRESS, + CONF_INVALID_MSG_BUS_ADDRESS, + ASDC_AUTHENTICATION_FAILED, + ASDC_AUTHORIZATION_FAILED, + ASDC_NOT_FOUND, + ASDC_SERVER_PROBLEM, + ASDC_CONNECTION_FAILED, + ASDC_SERVER_TIMEOUT, + + CAMBRIA_INIT_FAILED, + UEB_KEYS_CREATION_FAILED +} diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/utils/DistributionClientConstants.java b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/DistributionClientConstants.java new file mode 100644 index 0000000..1dcbca3 --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/DistributionClientConstants.java @@ -0,0 +1,44 @@ +/*- + * ============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; + +import java.util.regex.Pattern; + +/** + * Constants Used By Distribution Client + * @author mshitrit + * + */ +public final class DistributionClientConstants { + public static final String CLIENT_DESCRIPTION = "ASDC Distribution Client Key for %s"; + public static final Pattern FQDN_PATTERN = Pattern.compile("^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])(\\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]))*(:[0-9]{2,4})*$", Pattern.CASE_INSENSITIVE); + public static final String EMAIL = ""; + public static final int MIN_POLLING_INTERVAL_SEC = 15; + public static final int POOL_SIZE = 10; + public static final int POLLING_TIMEOUT_SEC = 15; + + public static final String HEADER_INSTANCE_ID = "X-ECOMP-InstanceID"; + public static final String HEADER_REQUEST_ID = "X-ECOMP-RequestID"; + public static final String APPLICATION_JSON = "application/json"; + public static final String HEADER_CONTENT_TYPE = "Content-Type"; + + private DistributionClientConstants(){ throw new UnsupportedOperationException();} +} diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/utils/DistributionStatusEnum.java b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/DistributionStatusEnum.java new file mode 100644 index 0000000..e620195 --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/DistributionStatusEnum.java @@ -0,0 +1,56 @@ +/*- + * ============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; + +public enum DistributionStatusEnum { + /**Can be sent when ONAP component successfully downloaded the specific artifact*/ + DOWNLOAD_OK, + + /**Can be sent when ONAP component failed to download the specific artifact (corrupted file)*/ + DOWNLOAD_ERROR, + + /**Can be sent only if the repeated distribution notification event is sent when the ONAP component already downloaded the artifact , but still not stored it in the local repository .*/ + ALREADY_DOWNLOADED, + + /**Can be sent when ONAP component successfully deployed the specific artifact in the local repository*/ + DEPLOY_OK, + + /**Can be sent when ONAP component failed to store the downloaded artifact in the local repository*/ + DEPLOY_ERROR, + + /**Sent when the repeated distribution notification event is sent for already stored in the local repository service artifact ( artifact's version and checksum match the one stored in the local repository)*/ + ALREADY_DEPLOYED, + /** + * ONAP component is requested to publish this status once component successfully complete downloading and storing all the data it needs from the service. + */ + COMPONENT_DONE_OK, + + /**ONAP component is requested to publish this status when component failed to download or failed to store one or more of the mandatory information it requires from the service model. + + It is recommended to populate the errorReason field with appropriate description of the error +*/ + COMPONENT_DONE_ERROR, + /** The DISTRIBUTION_COMPLETE_OK/ERROR status indicating the overall ONAP components status of retrieving and storing the information. +*/ + DISTRIBUTION_COMPLETE_OK, + + DISTRIBUTION_COMPLETE_ERROR +} diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/utils/GeneralUtils.java b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/GeneralUtils.java new file mode 100644 index 0000000..a69e479 --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/GeneralUtils.java @@ -0,0 +1,92 @@ +/*- + * ============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; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; + +import org.apache.commons.codec.binary.Base64; +import org.onap.sdc.api.results.IDistributionClientResult; +import org.onap.sdc.impl.DistributionClientResultImpl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import fj.data.Either; + +public class GeneralUtils { + private static final Logger log = LoggerFactory.getLogger(GeneralUtils.class.getName()); + public static String calculateMD5 (String data){ + String calculatedMd5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(data); + // encode base-64 result + byte[] encodeBase64 = Base64.encodeBase64(calculatedMd5.getBytes()); + String encodeBase64Str = new String(encodeBase64); + return encodeBase64Str; + + } + + public static String calculateMD5(byte[] decodedPayload) { + String decodedMd5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(decodedPayload); + byte[] encodeMd5 = Base64.encodeBase64(decodedMd5.getBytes()); + return new String(encodeMd5); + } + + public static boolean isBase64Encoded(String str){ + boolean isEncoded = false; + try + { + // If no exception is caught, then it is possibly a base64 encoded string + byte[] data = Base64.decodeBase64(str); + // checks if the string was properly padded to the + isEncoded= ((str.length() % 4 == 0) && (Pattern.matches("\\A[a-zA-Z0-9/+]+={1,2}\\z", str))); + + } + catch (Exception e) + { + // If exception is caught, then it is not a base64 encoded string + isEncoded= false; + } + return isEncoded; + } + + + public static Either<List<String>, IDistributionClientResult> convertToValidHostName(List<String> msgBusAddresses) { + List<String> uebLocalHostsNames = new ArrayList<>(); + for(String name : msgBusAddresses){ + try { + uebLocalHostsNames.add(InetAddress.getByName(name).getHostName()); + } catch (UnknownHostException e) { + log.debug("UnknownHost: {}", e.getMessage(), e); + } + } + Either<List<String>, IDistributionClientResult> response; + if( uebLocalHostsNames.isEmpty() ){ + response = Either.right(new DistributionClientResultImpl(DistributionActionResultEnum.CONF_INVALID_MSG_BUS_ADDRESS, "configuration is invalid: " + DistributionActionResultEnum.CONF_INVALID_MSG_BUS_ADDRESS.name())); + + } + else{ + response = Either.left(uebLocalHostsNames); + } + return response; + } +} diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/utils/Pair.java b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/Pair.java new file mode 100644 index 0000000..097cafb --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/Pair.java @@ -0,0 +1,39 @@ +/*- + * ============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; + +public final class Pair<F, S> { + private final F first; + private final S second; + + public Pair(F first, S second){ + this.first = first; + this.second = second; + } + + public F getFirst() { + return first; + } + + public S getSecond() { + return second; + } +} diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/utils/Wrapper.java b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/Wrapper.java new file mode 100644 index 0000000..d1d799c --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/Wrapper.java @@ -0,0 +1,45 @@ +/*- + * ============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; +/** + * Very Basic Wrapper class. + * @author mshitrit + * + * @param <T> + */ +public class Wrapper<T>{ + private T innerElement; + public Wrapper(T innerElement){ + this.innerElement = innerElement; + } + public Wrapper(){ + this.innerElement = null; + } + public T getInnerElement() { + return innerElement; + } + public void setInnerElement(T innerElement) { + this.innerElement = innerElement; + } + public boolean isEmpty(){ + return innerElement == null; + } +} diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/utils/YamlToObjectConverter.java b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/YamlToObjectConverter.java new file mode 100644 index 0000000..9c294d2 --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/YamlToObjectConverter.java @@ -0,0 +1,155 @@ +/*- + * ============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; + +import java.beans.IntrospectionException; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.HashMap; + +import org.onap.sdc.utils.heat.HeatConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.yaml.snakeyaml.TypeDescription; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.introspector.BeanAccess; +import org.yaml.snakeyaml.introspector.Property; +import org.yaml.snakeyaml.introspector.PropertyUtils; + +public class YamlToObjectConverter { + + private static Logger log = LoggerFactory + .getLogger(YamlToObjectConverter.class.getName()); + + private static HashMap<String, Yaml> yamls = new HashMap<String, Yaml>(); + + private static Yaml defaultYaml = new Yaml(); + + static { + + org.yaml.snakeyaml.constructor.Constructor heatConstructor = new org.yaml.snakeyaml.constructor.Constructor(HeatConfiguration.class); + TypeDescription heatDescription = new TypeDescription(HeatConfiguration.class); + //heatDescription.putListPropertyType("parameters", HeatParameterConfiguration.class); + heatConstructor.addTypeDescription(heatDescription); + PropertyUtils propertyUtils = new PropertyUtils() { + @Override + //This is in order to workaround "default" field in HeatParameterEntry, since default is Java keyword + public Property getProperty(Class<? extends Object> type, String name, BeanAccess bAccess) + throws IntrospectionException { + name = name.substring(0, 1).toLowerCase() + name.substring(1); + return super.getProperty(type, name, bAccess); + } + + }; + //Skip properties which are not found - we only are interested in "parameters" + propertyUtils.setSkipMissingProperties(true); + heatConstructor.setPropertyUtils(propertyUtils); + + Yaml yaml = new Yaml(heatConstructor); + + yamls.put(HeatConfiguration.class.getName(), yaml); + + } + + private static <T> Yaml getYamlByClassName(Class<T> className) { + + Yaml yaml = yamls.get(className.getName()); + if (yaml == null) { + yaml = defaultYaml; + } + + return yaml; + } + + public <T> T convert(String dirPath, Class<T> className, + String configFileName) { + + T config = null; + + try { + + String fullFileName = dirPath + File.separator + configFileName; + + config = convert(fullFileName, className); + + } catch (Exception e) { + log.error("Failed to convert yaml file " + configFileName + + " to object.", e); + } + + return config; + } + + public <T> T convert(String fullFileName, Class<T> className) { + + T config = null; + + Yaml yaml = getYamlByClassName(className); + + InputStream in = null; + try { + + File f = new File(fullFileName); + if (false == f.exists()) { + log.warn("The file " + fullFileName + + " cannot be found. Ignore reading configuration."); + return null; + } + in = Files.newInputStream(Paths.get(fullFileName)); + + config = yaml.loadAs(in, className); + + // System.out.println(config.toString()); + } catch (Exception e) { + log.error("Failed to convert yaml file " + fullFileName + + " to object.", e); + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + + return config; + } + + public <T> T convertFromString(String yamlContents, Class<T> className) { + + T config = null; + + Yaml yaml = getYamlByClassName(className); + + try { + config = yaml.loadAs(yamlContents, className); + } catch (Exception e){ + log.error("Failed to convert YAML {} to object." , yamlContents, e); + } + + return config; + } +} 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; + } +} |