aboutsummaryrefslogtreecommitdiffstats
path: root/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/ScalarUnit.java
blob: 42e94eb1af1441ebd1f196d0d02f088d949f7387 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package org.openecomp.sdc.toscaparser.api.elements;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openecomp.sdc.toscaparser.api.common.ExceptionCollector;
import org.openecomp.sdc.toscaparser.api.utils.ValidateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class ScalarUnit {

	private static Logger log = LoggerFactory.getLogger(ScalarUnit.class.getName());

	private static final String SCALAR_UNIT_SIZE = "scalar-unit.size";
	private static final String SCALAR_UNIT_FREQUENCY = "scalar-unit.frequency";
	private static final String SCALAR_UNIT_TIME = "scalar-unit.time";
	
	public static final String SCALAR_UNIT_TYPES[] = {
            SCALAR_UNIT_SIZE, SCALAR_UNIT_FREQUENCY, SCALAR_UNIT_TIME
	};

	private Object value;
	protected HashMap<String,Object> SCALAR_UNIT_DICT;
	protected String SCALAR_UNIT_DEFAULT;
	
	public ScalarUnit(Object _value) {
		value = _value;
		SCALAR_UNIT_DICT = new HashMap<>();
		SCALAR_UNIT_DEFAULT = "";
	}
	
	
	private String _checkUnitInScalarStandardUnits(String inputUnit) {
        // Check whether the input unit is following specified standard
		
        // If unit is not following specified standard, convert it to standard
        // unit after displaying a warning message.
		
		if(SCALAR_UNIT_DICT.get(inputUnit) != null) {
			return inputUnit;
		}
		else {
			for(String key: SCALAR_UNIT_DICT.keySet()) {
				if(key.toUpperCase().equals(inputUnit.toUpperCase())) {
					log.debug("ScalarUnit - _checkUnitInScalarStandardUnits - \n" +
						"The unit {} does not follow scalar unit standards\n" +
						"using {} instead",
						inputUnit, key);
					return key;
				}
			}
            ExceptionCollector.appendWarning(String.format(
            	"'The unit \"%s\" is not valid. Valid units are \n%s",
                inputUnit,SCALAR_UNIT_DICT.keySet().toString()));
            return inputUnit;
		}
	}
	
	public Object validateScalarUnit() {
		Pattern pattern = Pattern.compile("([0-9.]+)\\s*(\\w+)");
		Matcher matcher = pattern.matcher(value.toString());
		if(matcher.find()) {
			ValidateUtils.strToNum(matcher.group(1));
			String scalarUnit = _checkUnitInScalarStandardUnits(matcher.group(2));
			value = matcher.group(1) + " " + scalarUnit;
		}
		else {
            ExceptionCollector.appendException(String.format(
                "ValueError: \"%s\" is not a valid scalar-unit",value.toString()));
		}
		return value;
	}
	
	public double getNumFromScalarUnit(String unit) {
		if(unit != null) {
			unit = _checkUnitInScalarStandardUnits(unit);
		}
		else {
			unit = SCALAR_UNIT_DEFAULT;
		}
		Pattern pattern = Pattern.compile("([0-9.]+)\\s*(\\w+)");
		Matcher matcher = pattern.matcher(value.toString());
		if(matcher.find()) {
			ValidateUtils.strToNum(matcher.group(1));
			String scalarUnit = _checkUnitInScalarStandardUnits(matcher.group(2));
			value = matcher.group(1) + " " + scalarUnit;
			Object on1 = ValidateUtils.strToNum(matcher.group(1)) != null ? ValidateUtils.strToNum(matcher.group(1)) : 0;
			Object on2 = SCALAR_UNIT_DICT.get(matcher.group(2)) != null ? SCALAR_UNIT_DICT.get(matcher.group(2)) : 0; 
			Object on3 = SCALAR_UNIT_DICT.get(unit) != null ? SCALAR_UNIT_DICT.get(unit) : 0;
			
			Double n1 = new Double(on1.toString());
			Double n2 = new Double(on2.toString());
			Double n3 = new Double(on3.toString());
			double converted = n1 * n2 / n3; 
	        if(Math.abs(converted - Math.round(converted)) < 0.0000000000001 ) {
	            converted = Math.round(converted);
	        }
	        return converted;
		}
		return 0l; //???
	}
	
	protected static HashMap<String,String> scalarunitMapping = _getScalarunitMappings();
	
	private static HashMap<String,String> _getScalarunitMappings() {
		HashMap<String,String> map = new HashMap<>();
	    map.put(SCALAR_UNIT_FREQUENCY,"ScalarUnitFrequency");
	    map.put(SCALAR_UNIT_SIZE, "ScalarUnitSize");
	    map.put(SCALAR_UNIT_TIME, "ScalarUnit_Time");
	    return map;
	}

	public static ScalarUnit getScalarunitClass(String type,Object val) {
		if(type.equals(SCALAR_UNIT_SIZE)) {
			return new ScalarUnitSize(val);
		}
		else if(type.equals(SCALAR_UNIT_TIME)) {
			return new ScalarUnitTime(val);
		}
		else if(type.equals(SCALAR_UNIT_FREQUENCY)) {
			return new ScalarUnitFrequency(val);
		}
		return null;
	}

	public static double getScalarunitValue(String type, Object value, String unit) {
		if(type.equals(SCALAR_UNIT_SIZE)) {
			return (new ScalarUnitSize(value)).getNumFromScalarUnit(unit);
		}
		if(type.equals(SCALAR_UNIT_TIME)) {
			return (new ScalarUnitTime(value)).getNumFromScalarUnit(unit);
		}
		if(type.equals(SCALAR_UNIT_FREQUENCY)) {
			return (new ScalarUnitFrequency(value)).getNumFromScalarUnit(unit);
		}
        ExceptionCollector.appendException(String.format(
	            "TypeError: \"%s\" is not a valid scalar-unit type",type));
        return 0.0;
	}
	
}

/*python

from toscaparser.common.exception import ExceptionCollector
from toscaparser.utils.gettextutils import _
from toscaparser.utils import validateutils

log = logging.getLogger('tosca')


class ScalarUnit(object):
    '''Parent class for scalar-unit type.'''

    SCALAR_UNIT_TYPES = (
        SCALAR_UNIT_SIZE, SCALAR_UNIT_FREQUENCY, SCALAR_UNIT_TIME
    ) = (
        'scalar-unit.size', 'scalar-unit.frequency', 'scalar-unit.time'
    )

    def __init__(self, value):
        self.value = value

    def _check_unit_in_scalar_standard_units(self, input_unit):
        """Check whether the input unit is following specified standard

        If unit is not following specified standard, convert it to standard
        unit after displaying a warning message.
        """
        if input_unit in self.SCALAR_UNIT_DICT.keys():
            return input_unit
        else:
            for key in self.SCALAR_UNIT_DICT.keys():
                if key.upper() == input_unit.upper():
                    log.warning(_('The unit "%(unit)s" does not follow '
                                  'scalar unit standards; using "%(key)s" '
                                  'instead.') % {'unit': input_unit,
                                                 'key': key})
                    return key
            msg = (_('The unit "%(unit)s" is not valid. Valid units are '
                     '"%(valid_units)s".') %
                   {'unit': input_unit,
                    'valid_units': sorted(self.SCALAR_UNIT_DICT.keys())})
            ExceptionCollector.appendException(ValueError(msg))

    def validate_scalar_unit(self):
        regex = re.compile('([0-9.]+)\s*(\w+)')
        try:
            result = regex.match(str(self.value)).groups()
            validateutils.str_to_num(result[0])
            scalar_unit = self._check_unit_in_scalar_standard_units(result[1])
            self.value = ' '.join([result[0], scalar_unit])
            return self.value

        except Exception:
            ExceptionCollector.appendException(
                ValueError(_('"%s" is not a valid scalar-unit.')
                           % self.value))

    def get_num_from_scalar_unit(self, unit=None):
        if unit:
            unit = self._check_unit_in_scalar_standard_units(unit)
        else:
            unit = self.SCALAR_UNIT_DEFAULT
        self.validate_scalar_unit()

        regex = re.compile('([0-9.]+)\s*(\w+)')
        result = regex.match(str(self.value)).groups()
        converted = (float(validateutils.str_to_num(result[0]))
                     * self.SCALAR_UNIT_DICT[result[1]]
                     / self.SCALAR_UNIT_DICT[unit])
        if converted - int(converted) < 0.0000000000001:
            converted = int(converted)
        return converted


class ScalarUnit_Size(ScalarUnit):

    SCALAR_UNIT_DEFAULT = 'B'
    SCALAR_UNIT_DICT = {'B': 1, 'kB': 1000, 'KiB': 1024, 'MB': 1000000,
                        'MiB': 1048576, 'GB': 1000000000,
                        'GiB': 1073741824, 'TB': 1000000000000,
                        'TiB': 1099511627776}


class ScalarUnit_Time(ScalarUnit):

    SCALAR_UNIT_DEFAULT = 'ms'
    SCALAR_UNIT_DICT = {'d': 86400, 'h': 3600, 'm': 60, 's': 1,
                        'ms': 0.001, 'us': 0.000001, 'ns': 0.000000001}


class ScalarUnit_Frequency(ScalarUnit):

    SCALAR_UNIT_DEFAULT = 'GHz'
    SCALAR_UNIT_DICT = {'Hz': 1, 'kHz': 1000,
                        'MHz': 1000000, 'GHz': 1000000000}


scalarunit_mapping = {
    ScalarUnit.SCALAR_UNIT_FREQUENCY: ScalarUnit_Frequency,
    ScalarUnit.SCALAR_UNIT_SIZE: ScalarUnit_Size,
    ScalarUnit.SCALAR_UNIT_TIME: ScalarUnit_Time,
    }


def get_scalarunit_class(type):
    return scalarunit_mapping.get(type)


def get_scalarunit_value(type, value, unit=None):
    if type in ScalarUnit.SCALAR_UNIT_TYPES:
        ScalarUnit_Class = get_scalarunit_class(type)
        return (ScalarUnit_Class(value).
                get_num_from_scalar_unit(unit))
    else:
        ExceptionCollector.appendException(
            TypeError(_('"%s" is not a valid scalar-unit type.') % type))
*/