2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.sdc.toscaparser.api.elements;
23 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
24 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
25 import org.onap.sdc.toscaparser.api.utils.ValidateUtils;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import java.util.HashMap;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
33 public abstract class ScalarUnit {
35 private static Logger log = LoggerFactory.getLogger(ScalarUnit.class.getName());
37 private static final String SCALAR_UNIT_SIZE = "scalar-unit.size";
38 private static final String SCALAR_UNIT_FREQUENCY = "scalar-unit.frequency";
39 private static final String SCALAR_UNIT_TIME = "scalar-unit.time";
41 public static final String[] SCALAR_UNIT_TYPES = {
42 SCALAR_UNIT_SIZE, SCALAR_UNIT_FREQUENCY, SCALAR_UNIT_TIME
46 private HashMap<String, Object> scalarUnitDict;
47 private String scalarUnitDefault;
49 public ScalarUnit(Object value) {
51 scalarUnitDict = new HashMap<>();
52 scalarUnitDefault = "";
55 void putToScalarUnitDict(String key, Object value) {
56 scalarUnitDict.put(key, value);
59 void setScalarUnitDefault(String scalarUnitDefault) {
60 this.scalarUnitDefault = scalarUnitDefault;
63 private String checkUnitInScalarStandardUnits(String inputUnit) {
64 // Check whether the input unit is following specified standard
66 // If unit is not following specified standard, convert it to standard
67 // unit after displaying a warning message.
69 if (scalarUnitDict.get(inputUnit) != null) {
72 for (String key : scalarUnitDict.keySet()) {
73 if (key.toUpperCase().equals(inputUnit.toUpperCase())) {
74 log.debug("ScalarUnit - checkUnitInScalarStandardUnits - \n"
75 + "The unit {} does not follow scalar unit standards\n"
81 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE007", String.format(
82 "'The unit \"%s\" is not valid. Valid units are \n%s",
83 inputUnit, scalarUnitDict.keySet().toString())));
88 public Object validateScalarUnit() {
89 Pattern pattern = Pattern.compile("([0-9.]+)\\s*(\\w+)");
90 Matcher matcher = pattern.matcher(value.toString());
92 ValidateUtils.strToNum(matcher.group(1));
93 String scalarUnit = checkUnitInScalarStandardUnits(matcher.group(2));
94 value = matcher.group(1) + " " + scalarUnit;
96 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE134", String.format(
97 "ValueError: \"%s\" is not a valid scalar-unit", value.toString())));
102 public double getNumFromScalarUnit(String unit) {
104 unit = checkUnitInScalarStandardUnits(unit);
106 unit = scalarUnitDefault;
108 Pattern pattern = Pattern.compile("([0-9.]+)\\s*(\\w+)");
109 Matcher matcher = pattern.matcher(value.toString());
110 if (matcher.find()) {
111 final double minimalNum = 0.0000000000001;
113 ValidateUtils.strToNum(matcher.group(1));
114 String scalarUnit = checkUnitInScalarStandardUnits(matcher.group(2));
115 value = matcher.group(1) + " " + scalarUnit;
116 Object on1 = ValidateUtils.strToNum(matcher.group(1)) != null ? ValidateUtils.strToNum(matcher.group(1)) : 0;
117 Object on2 = scalarUnitDict.get(matcher.group(2)) != null ? scalarUnitDict.get(matcher.group(2)) : 0;
118 Object on3 = scalarUnitDict.get(unit) != null ? scalarUnitDict.get(unit) : 0;
120 Double n1 = new Double(on1.toString());
121 Double n2 = new Double(on2.toString());
122 Double n3 = new Double(on3.toString());
123 double converted = n1 * n2 / n3;
125 if (Math.abs(converted - Math.round(converted)) < minimalNum) {
126 converted = Math.round(converted);
133 private static HashMap<String, String> scalarUnitMapping = getScalarUnitMappings();
135 private static HashMap<String, String> getScalarUnitMappings() {
136 HashMap<String, String> map = new HashMap<>();
137 map.put(SCALAR_UNIT_FREQUENCY, "ScalarUnitFrequency");
138 map.put(SCALAR_UNIT_SIZE, "ScalarUnitSize");
139 map.put(SCALAR_UNIT_TIME, "ScalarUnit_Time");
143 public static ScalarUnit getScalarunitClass(String type, Object val) {
144 if (type.equals(SCALAR_UNIT_SIZE)) {
145 return new ScalarUnitSize(val);
146 } else if (type.equals(SCALAR_UNIT_TIME)) {
147 return new ScalarUnitTime(val);
148 } else if (type.equals(SCALAR_UNIT_FREQUENCY)) {
149 return new ScalarUnitFrequency(val);
154 public static double getScalarunitValue(String type, Object value, String unit) {
155 if (type.equals(SCALAR_UNIT_SIZE)) {
156 return (new ScalarUnitSize(value)).getNumFromScalarUnit(unit);
158 if (type.equals(SCALAR_UNIT_TIME)) {
159 return (new ScalarUnitTime(value)).getNumFromScalarUnit(unit);
161 if (type.equals(SCALAR_UNIT_FREQUENCY)) {
162 return (new ScalarUnitFrequency(value)).getNumFromScalarUnit(unit);
164 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE135", String.format(
165 "TypeError: \"%s\" is not a valid scalar-unit type", type)));
173 from toscaparser.common.exception import ValidationIssueCollector
174 from toscaparser.utils.gettextutils import _
175 from toscaparser.utils import validateutils
177 log = logging.getLogger('tosca')
180 class ScalarUnit(object):
181 '''Parent class for scalar-unit type.'''
183 SCALAR_UNIT_TYPES = (
184 SCALAR_UNIT_SIZE, SCALAR_UNIT_FREQUENCY, SCALAR_UNIT_TIME
186 'scalar-unit.size', 'scalar-unit.frequency', 'scalar-unit.time'
189 def __init__(self, value):
192 def _check_unit_in_scalar_standard_units(self, input_unit):
193 """Check whether the input unit is following specified standard
195 If unit is not following specified standard, convert it to standard
196 unit after displaying a warning message.
198 if input_unit in self.scalarUnitDict.keys():
201 for key in self.scalarUnitDict.keys():
202 if key.upper() == input_unit.upper():
203 log.warning(_('The unit "%(unit)s" does not follow '
204 'scalar unit standards; using "%(key)s" '
205 'instead.') % {'unit': input_unit,
208 msg = (_('The unit "%(unit)s" is not valid. Valid units are '
209 '"%(valid_units)s".') %
211 'valid_units': sorted(self.scalarUnitDict.keys())})
212 ValidationIssueCollector.appendException(ValueError(msg))
214 def validate_scalar_unit(self):
215 regex = re.compile('([0-9.]+)\s*(\w+)')
217 result = regex.match(str(self.value)).groups()
218 validateutils.str_to_num(result[0])
219 scalar_unit = self._check_unit_in_scalar_standard_units(result[1])
220 self.value = ' '.join([result[0], scalar_unit])
224 ValidationIssueCollector.appendException(
225 ValueError(_('"%s" is not a valid scalar-unit.')
228 def get_num_from_scalar_unit(self, unit=None):
230 unit = self._check_unit_in_scalar_standard_units(unit)
232 unit = self.scalarUnitDefault
233 self.validate_scalar_unit()
235 regex = re.compile('([0-9.]+)\s*(\w+)')
236 result = regex.match(str(self.value)).groups()
237 converted = (float(validateutils.str_to_num(result[0]))
238 * self.scalarUnitDict[result[1]]
239 / self.scalarUnitDict[unit])
240 if converted - int(converted) < 0.0000000000001:
241 converted = int(converted)
245 class ScalarUnit_Size(ScalarUnit):
247 scalarUnitDefault = 'B'
248 scalarUnitDict = {'B': 1, 'kB': 1000, 'KiB': 1024, 'MB': 1000000,
249 'MiB': 1048576, 'GB': 1000000000,
250 'GiB': 1073741824, 'TB': 1000000000000,
251 'TiB': 1099511627776}
254 class ScalarUnit_Time(ScalarUnit):
256 scalarUnitDefault = 'ms'
257 scalarUnitDict = {'d': 86400, 'h': 3600, 'm': 60, 's': 1,
258 'ms': 0.001, 'us': 0.000001, 'ns': 0.000000001}
261 class ScalarUnit_Frequency(ScalarUnit):
263 scalarUnitDefault = 'GHz'
264 scalarUnitDict = {'Hz': 1, 'kHz': 1000,
265 'MHz': 1000000, 'GHz': 1000000000}
268 scalarunit_mapping = {
269 ScalarUnit.SCALAR_UNIT_FREQUENCY: ScalarUnit_Frequency,
270 ScalarUnit.SCALAR_UNIT_SIZE: ScalarUnit_Size,
271 ScalarUnit.SCALAR_UNIT_TIME: ScalarUnit_Time,
275 def get_scalarunit_class(type):
276 return scalarunit_mapping.get(type)
279 def get_scalarunit_value(type, value, unit=None):
280 if type in ScalarUnit.SCALAR_UNIT_TYPES:
281 ScalarUnit_Class = get_scalarunit_class(type)
282 return (ScalarUnit_Class(value).
283 get_num_from_scalar_unit(unit))
285 ValidationIssueCollector.appendException(
286 TypeError(_('"%s" is not a valid scalar-unit type.') % type))