diff options
author | 2019-10-01 16:47:26 +0100 | |
---|---|---|
committer | 2019-11-19 07:48:02 +0000 | |
commit | 35b5687d60558bac72c1e53ddd9044584964a3d8 (patch) | |
tree | dc2e184f2cef02c6d1aee0b5baf1585d2b7b3ffe /common/onap-tosca-datatype/src/main/java | |
parent | 0f9257b8265404c82ccdfae6aafce940f20a2947 (diff) |
Fix for scalar-unit type value.
Issue-ID: SDC-323
Change-Id: I7f19a7356e1cd34deca0a168b3cb707ef657b9cb
Signed-off-by: aribeiro <anderson.ribeiro@est.tech>
Diffstat (limited to 'common/onap-tosca-datatype/src/main/java')
2 files changed, 95 insertions, 20 deletions
diff --git a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/PropertyType.java b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/PropertyType.java index facdb2a7b1..1b91ca96d2 100644 --- a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/PropertyType.java +++ b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/PropertyType.java @@ -15,8 +15,17 @@ */ package org.onap.sdc.tosca.datatypes.model; -import java.util.*; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@AllArgsConstructor +@Getter public enum PropertyType { STRING("string"), @@ -28,27 +37,21 @@ public enum PropertyType { MAP("map"), LIST("list"), SCALAR_UNIT_SIZE("scalar-unit.size"), + SCALAR_UNIT_TIME("scalar-unit.time"), SCALAR_UNIT_FREQUENCY("scalar-unit.frequency"); - private static final Map<String, PropertyType> M_MAP = - Collections.unmodifiableMap(initializeMapping()); - private static final Set<String> SIMPLE_PROPERTY_TYPES = - Collections.unmodifiableSet(initializeSimplePropertyTypes()); + private static final Map<String, PropertyType> M_MAP = Collections.unmodifiableMap(initializeMapping()); + private static final Set<String> SIMPLE_PROPERTY_TYPES = Collections.unmodifiableSet(initializeSimplePropertyTypes()); private String displayName; - PropertyType(String displayName) { - - this.displayName = displayName; - } - /** * Initilize property type display name mapping. * @return Map */ public static Map<String, PropertyType> initializeMapping() { - Map<String, PropertyType> typeMap = new HashMap<>(); - for (PropertyType v : PropertyType.values()) { - typeMap.put(v.displayName, v); + final Map<String, PropertyType> typeMap = new HashMap<>(); + for (final PropertyType propertyType : PropertyType.values()) { + typeMap.put(propertyType.displayName, propertyType); } return typeMap; } @@ -58,7 +61,7 @@ public enum PropertyType { * @param displayName * @return PropertyType */ - public static PropertyType getPropertyTypeByDisplayName(String displayName) { + public static PropertyType getPropertyTypeByDisplayName(final String displayName) { if (M_MAP.containsKey(displayName)) { return M_MAP.get(displayName); } @@ -66,12 +69,14 @@ public enum PropertyType { } private static Set<String> initializeSimplePropertyTypes() { - final int setSize = 4; - Set<String> simplePropertyTypes = new HashSet<>(setSize); + final Set<String> simplePropertyTypes = new HashSet<>(); simplePropertyTypes.add(STRING.getDisplayName().toLowerCase()); simplePropertyTypes.add(INTEGER.getDisplayName().toLowerCase()); simplePropertyTypes.add(FLOAT.getDisplayName().toLowerCase()); simplePropertyTypes.add(BOOLEAN.getDisplayName().toLowerCase()); + simplePropertyTypes.add(SCALAR_UNIT_SIZE.getDisplayName().toLowerCase()); + simplePropertyTypes.add(SCALAR_UNIT_TIME.getDisplayName().toLowerCase()); + simplePropertyTypes.add(SCALAR_UNIT_FREQUENCY.getDisplayName().toLowerCase()); return simplePropertyTypes; } @@ -79,8 +84,4 @@ public enum PropertyType { return SIMPLE_PROPERTY_TYPES; } - public String getDisplayName() { - return displayName; - } - } diff --git a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/ScalarUnitValidator.java b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/ScalarUnitValidator.java new file mode 100644 index 0000000000..d7e538e0a7 --- /dev/null +++ b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/ScalarUnitValidator.java @@ -0,0 +1,74 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.tosca.datatypes.model; + +import java.util.Arrays; +import java.util.regex.Pattern; +import org.apache.commons.lang3.StringUtils; + +/** + * This Class is responsible for validating if a given value is a valid Tosca Scalar Unit Type. + */ +public class ScalarUnitValidator { + + private static ScalarUnitValidator scalarUnitValidator = new ScalarUnitValidator(); + + /** + * Tosca Scalar Unit Types structure. + */ + private final Pattern pattern = Pattern.compile("\\d+\\s*[a-zA-Z]{1,3}"); + + private ScalarUnitValidator() { + } + + public static ScalarUnitValidator getInstance() { + return scalarUnitValidator; + } + + /** + * Validates if the given String matches with the Tosca Scalar Unit Types structure. + * + * @param value String to be validated. + * @return an {@Boolean} + */ + public boolean isScalarUnit(final String value) { + if (value == null || value.isEmpty()) { + return true; + } + + return pattern.matcher(value).matches(); + } + + /** + * Validates if the given String has a Recognized Tosca unit. + * + * @param value String to be validated + * @param enumClass Enum that represents a Tosca Scalar Unit Type. + * @param <E> + * @return an Enum that represents the Tosca Scalar Unit Type. + */ + public <E extends Enum<E>> boolean isValueScalarUnit(final Object value, final Class<E> enumClass) { + final String stringToValidate = String.valueOf(value); + return isScalarUnit(stringToValidate) && Arrays.stream(StringUtils.split(stringToValidate)) + .anyMatch(strValue -> Arrays.stream(enumClass.getEnumConstants()) + .anyMatch(scalarUnit -> + scalarUnit.name().equalsIgnoreCase(strValue) || strValue.endsWith(scalarUnit.name()))); + } +} |