Mahesh Poojary Huawei
Committed by Ray Milkey

[ONOS-4348] Yang Bits, Binary and Decimal64

Change-Id: I8e4e54a19a8f9634cbc56a07579a1730174f53f6
Showing 44 changed files with 1997 additions and 524 deletions
/*-
* Copyright 2016 Open Networking Laboratory
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -16,6 +16,7 @@
package org.onosproject.yangutils.datamodel;
import java.io.Serializable;
import java.math.BigDecimal;
import org.onosproject.yangutils.datamodel.utils.builtindatatype.DataTypeException;
import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangBuiltInDataTypeInfo;
import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
......@@ -78,11 +79,12 @@ public final class BuiltInTypeObjectFactory implements Serializable {
case UINT64: {
return (T) new YangUint64(valueInStr);
}
case DECIMAL64: {
return (T) new YangDecimal64(new BigDecimal(valueInStr));
}
default: {
throw new DataTypeException("YANG file error : Unsupported data type");
}
}
}
}
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* 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.
*/
package org.onosproject.yangutils.datamodel;
import java.io.Serializable;
import java.util.Base64;
import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangBuiltInDataTypeInfo;
import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
/*
* Reference RFC 6020.
*
* The binary built-in type represents any binary data,
* i.e., a sequence of octets.
*/
public class YangBinary implements YangBuiltInDataTypeInfo<YangBinary>, Serializable, Comparable<YangBinary> {
private static final long serialVersionUID = 2106201608L;
// Binary data is a decoded value by base64 decoding scheme from data input (jason)
private byte[] binaryData;
/**
* Creates a binary object corresponding to the base 64 encoding value.
*
* @param strValue base64 encoded value
*/
public YangBinary(String strValue) {
setBinaryData(Base64.getDecoder().decode(strValue));
}
/**
* Retrieves decoded binary data.
*
* @return binary data
*/
public byte[] getBinaryData() {
return binaryData;
}
/**
* Sets binary data.
*
* @param binaryData binary data
*/
public void setBinaryData(byte[] binaryData) {
this.binaryData = binaryData;
}
/**
* Encodes binary data by base64 encoding scheme.
*
* @return encoded binary data
*/
public String toString() {
return Base64.getEncoder()
.encodeToString(binaryData);
}
@Override
public YangDataTypes getYangType() {
return YangDataTypes.BINARY;
}
@Override
public int compareTo(YangBinary o) {
for (int i = 0, j = 0; i < this.binaryData.length && j < o.binaryData.length; i++, j++) {
int a = (this.binaryData[i] & 0xff);
int b = (o.binaryData[j] & 0xff);
if (a != b) {
return a - b;
}
}
return this.binaryData.length - o.binaryData.length;
}
}
......@@ -17,8 +17,10 @@
package org.onosproject.yangutils.datamodel;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import java.util.BitSet;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.datamodel.utils.Parsable;
......@@ -38,36 +40,142 @@ import org.onosproject.yangutils.datamodel.utils.YangConstructType;
public class YangBits implements Parsable, Serializable {
private static final long serialVersionUID = 806201641L;
private static final String SPACE = " ";
// Bits information set.
private Set<YangBit> bitSet;
// BITS name.
// Bits name
private String bitsName;
// Bits data contains bit-positions will be used to send to ONOS application
private BitSet bitDataSet;
/**
* Mapping bit name to YangBit. In data input (jason), only bit name will be received.
* By using the bit name corresponding (yang) bit-position will be retrieved from bitNameMap map.
*/
private Map<String, YangBit> bitNameMap;
/**
* Mapping bit position to YangBit. The bit-position received from ONOS application
* will be converted into bit-name by using bitPositionMap map to send (jason) output data as response.
*/
private Map<Integer, YangBit> bitPositionMap;
/**
* Creates a YANG bits type object.
*/
public YangBits() {
setBitSet(new HashSet<YangBit>());
bitDataSet = new BitSet();
setBitNameMap(new HashMap<>());
setBitPositionMap(new HashMap<>());
}
/**
* Returns the bit set.
* Creates an instance of YANG bits.
*
* @return the bit set
* @param bits set of bit names
* @throws DataModelException due to violation in data model rules
*/
public Set<YangBit> getBitSet() {
return bitSet;
public YangBits(String bits) throws DataModelException {
String[] bitNames = bits.trim().split(Pattern.quote(SPACE));
setBitDataSet(bitNames);
}
/**
* Sets the bit set.
* Returns the bits name.
*
* @param bitSet the bit set
* @return the bits name
*/
private void setBitSet(Set<YangBit> bitSet) {
this.bitSet = bitSet;
public String getBitsName() {
return bitsName;
}
/**
* Sets the bits name.
*
* @param bitsName the bits name
*/
public void setBitsName(String bitsName) {
this.bitsName = bitsName;
}
/**
* Returns the bit data set.
*
* @return the bit data set
*/
public BitSet getBitDataSet() {
return bitDataSet;
}
/**
* Sets the bit data set.
*
* @param bitNames the set of bit names
* @throws DataModelException due to violation in data model rules
*/
public void setBitDataSet(String[] bitNames) throws DataModelException {
YangBit bit;
for (String bitName : bitNames) {
bit = bitNameMap.get(bitName);
if (bit == null) {
throw new DataModelException("YANG file error: Unable to find " +
"corresponding bit position for bit name: " + bitName);
}
bitDataSet.set(bit.getPosition());
}
}
/**
* Returns the bit name map.
*
* @return the bit name map
*/
public Map<String, YangBit> getBitNameMap() {
return bitNameMap;
}
/**
* Sets the bit name map.
*
* @param bitNameMap the bit name map
*/
public void setBitNameMap(Map<String, YangBit> bitNameMap) {
this.bitNameMap = bitNameMap;
}
/**
* Checks whether bit name already available.
*
* @param bitName bit name
* @return true if bit name already available otherwise returns false
*/
public boolean isBitNameExists(String bitName) {
return bitNameMap.containsKey(bitName);
}
/**
* Returns the bit position map.
*
* @return the bit position map
*/
public Map<Integer, YangBit> getBitPositionMap() {
return bitPositionMap;
}
/**
* Sets the bit position map.
*
* @param bitPositionMap the bit position map
*/
public void setBitPositionMap(Map<Integer, YangBit> bitPositionMap) {
this.bitPositionMap = bitPositionMap;
}
/**
* Checks whether bit position already available.
*
* @param bitPosition bit position
* @return true if bit position already available otherwise returns false
*/
public boolean isBitPositionExists(Integer bitPosition) {
return bitPositionMap.containsKey(bitPosition);
}
/**
......@@ -77,10 +185,14 @@ public class YangBits implements Parsable, Serializable {
* @throws DataModelException due to violation in data model rules
*/
public void addBitInfo(YangBit bitInfo) throws DataModelException {
if (!getBitSet().add(bitInfo)) {
throw new DataModelException("YANG file error: Duplicate identifier detected, same as bit \""
if (bitNameMap.put(bitInfo.getBitName(), bitInfo) != null) {
throw new DataModelException("YANG file error: Duplicate bit name detected, same as bit name \""
+ bitInfo.getBitName() + "\"");
}
if (bitPositionMap.put(bitInfo.getPosition(), bitInfo) != null) {
throw new DataModelException("YANG file error: Duplicate bit position detected, same as bit position \""
+ bitInfo.getPosition() + "\"");
}
}
/**
......@@ -93,22 +205,36 @@ public class YangBits implements Parsable, Serializable {
return YangConstructType.BITS_DATA;
}
/**
* Returns the bits name.
*
* @return name of the bits
*/
public String getBitsName() {
return bitsName;
@Override
public String toString() {
YangBit bit;
String bits = new String();
for (int i = bitDataSet.nextSetBit(0); i >= 0; i = bitDataSet.nextSetBit(i + 1)) {
bit = bitPositionMap.get(i);
if (bit == null) {
return null;
}
if (bits.isEmpty()) {
bits = bit.getBitName();
} else {
bits += " " + bit.getBitName();
}
}
return bits.trim();
}
/**
* Sets bits name.
* Returns the object of YANG bits based on specific set of bit names.
*
* @param bitsName bit name to be set
* @param bits set of bit names
* @return Object of YANG bits
*/
public void setBitsName(String bitsName) {
this.bitsName = bitsName;
public static YangBits fromString(String bits) {
try {
return new YangBits(bits);
} catch (Exception e) {
}
return null;
}
/**
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* 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.
*/
package org.onosproject.yangutils.datamodel;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.datamodel.utils.FractionDigits;
import org.onosproject.yangutils.datamodel.utils.Parsable;
import org.onosproject.yangutils.datamodel.utils.YangConstructType;
import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangBuiltInDataTypeInfo;
import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ListIterator;
/**
* Represents YANG decimal 64.
*/
public class YangDecimal64<T>
implements YangBuiltInDataTypeInfo<YangDecimal64>, Parsable, Serializable, Comparable<YangDecimal64> {
private static final long serialVersionUID = 8006201668L;
/**
* YANG's min keyword.
*/
private static final String MIN_KEYWORD = "min";
/**
* YANG's max keyword.
*/
private static final String MAX_KEYWORD = "max";
/**
* Valid minimum value of YANG's fraction-digits.
*/
public static final int MIN_FRACTION_DIGITS_VALUE = 1;
/**
* Valid maximum value of YANG's fraction-digits.
*/
public static final int MAX_FRACTION_DIGITS_VALUE = 18;
// Decimal64 value
private BigDecimal value;
// fraction-digits
private int fractionDigit;
/**
* Additional information about range restriction.
*/
private T rangeRestrictedExtendedInfo;
/**
* Creates an instance of YANG decimal64.
*/
public YangDecimal64() {
}
/**
* Creates an instance of YANG decimal64.
*
* @param value of decimal64
*/
public YangDecimal64(BigDecimal value) {
setValue(value);
}
/**
* Creates an instance of YANG decimal64.
*
* @param value of decimal64 in string
* @throws DataModelException a violation of data model rules
*/
public YangDecimal64(String value) throws DataModelException {
fromString(value);
}
/**
* Returns decimal64 value.
*
* @return value
*/
public BigDecimal getValue() {
return value;
}
/**
* Sets the decimal64 value.
*
* @param value of decimal64
*/
public void setValue(BigDecimal value) {
this.value = value;
}
/**
* Returns fraction digit.
*
* @return the fractionDigit
*/
public int getFractionDigit() {
return fractionDigit;
}
/**
* Sets fraction digit.
*
* @param fractionDigit fraction digits.
*/
public void setFractionDigit(int fractionDigit) {
this.fractionDigit = fractionDigit;
}
/**
* Returns additional information about range restriction.
*
* @return resolved range restricted extended information
*/
public T getRangeRestrictedExtendedInfo() {
return rangeRestrictedExtendedInfo;
}
/**
* Sets additional information about range restriction.
*
* @param resolvedExtendedInfo resolved range restricted extended information
*/
public void setRangeRestrictedExtendedInfo(T resolvedExtendedInfo) {
this.rangeRestrictedExtendedInfo = resolvedExtendedInfo;
}
/**
* Returns object of YANG decimal64.
*
* @param value of decimal64
* @return YANG decimal64 object
*/
public static YangDecimal64 of(BigDecimal value) {
return new YangDecimal64(value);
}
@Override
public YangDataTypes getYangType() {
return YangDataTypes.DECIMAL64;
}
@Override
public YangConstructType getYangConstructType() {
return YangConstructType.DECIMAL64_DATA;
}
@Override
public String toString() {
return value.toString();
}
/**
* Returns the object of YANG decimal64 from input string.
*
* @param valInString input String
* @return Object of YANG decimal64
* @throws DataModelException a violation of data model rules
*/
public static YangDecimal64 fromString(String valInString) throws DataModelException {
YangDecimal64 decimal64;
decimal64 = of(new BigDecimal(valInString));
decimal64.validateValue();
return decimal64;
}
/**
* Validate decimal64 value.
*
* @throws DataModelException a violation of data model rules
*/
public void validateValue() throws DataModelException {
if (!(FractionDigits.isValidDecimal64(this.value, this.fractionDigit))) {
throw new DataModelException("YANG file error : decimal64 validation failed.");
}
}
/**
* Validate range restriction values based on fraction-digits decimal64 range value.
*
* @throws DataModelException a violation of data model rules
*/
public void validateRange() throws DataModelException {
YangRangeRestriction rangeRestriction = (YangRangeRestriction) getRangeRestrictedExtendedInfo();
if (rangeRestriction == null) {
return;
}
ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
.listIterator();
while (rangeListIterator.hasNext()) {
YangRangeInterval rangeInterval = rangeListIterator.next();
if (!(FractionDigits.isValidDecimal64(((YangDecimal64) rangeInterval.getStartValue()).getValue(),
getFractionDigit()))) {
throw new DataModelException("YANG file error : decimal64 validation failed.");
}
if (!(FractionDigits.isValidDecimal64(((YangDecimal64) rangeInterval.getEndValue()).getValue(),
getFractionDigit()))) {
throw new DataModelException("YANG file error : decimal64 validation failed.");
}
}
}
@Override
public int compareTo(YangDecimal64 o) {
return Double.compare(value.doubleValue(), o.value.doubleValue());
}
/**
* Returns decimal64 default range restriction based on fraction-digits.
* If range restriction is not provided then this default range value will be applicable.
*
* @return range restriction
* @throws DataModelException a violation of data model rules
*/
public YangRangeRestriction getDefaultRangeRestriction() throws DataModelException {
YangRangeRestriction refRangeRestriction = new YangRangeRestriction();
YangRangeInterval rangeInterval = new YangRangeInterval<>();
FractionDigits.Range range = FractionDigits.getRange(this.fractionDigit);
rangeInterval.setStartValue(new YangDecimal64(new BigDecimal((range.getMin()))));
rangeInterval.setEndValue(new YangDecimal64(new BigDecimal((range.getMax()))));
refRangeRestriction.addRangeRestrictionInterval(rangeInterval);
return refRangeRestriction;
}
/**
* Validates the data on entering the corresponding parse tree node.
*
* @throws DataModelException a violation of data model rules
*/
@Override
public void validateDataOnEntry() throws DataModelException {
// TODO auto-generated method stub, to be implemented by parser
}
/**
* Validates the data on exiting the corresponding parse tree node.
*
* @throws DataModelException a violation of data model rules
*/
@Override
public void validateDataOnExit() throws DataModelException {
// TODO auto-generated method stub, to be implemented by parser
}
}
......@@ -32,6 +32,7 @@ import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangData
import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BINARY;
import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BITS;
import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BOOLEAN;
import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DECIMAL64;
import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DERIVED;
import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.EMPTY;
import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.ENUMERATION;
......@@ -242,29 +243,21 @@ public class YangDerivedInfo<T>
* otherwise take from the base type of type itself.
*/
if (baseType.getDataType() == DERIVED) {
/*
* Check whether the referred typedef is resolved.
*/
if (baseType.getResolvableStatus() != INTRA_FILE_RESOLVED && baseType.getResolvableStatus() != RESOLVED) {
throw new DataModelException("Linker Error: Referred typedef is not resolved for type.");
}
/*
* Check if the referred typedef is intra file resolved, if yes sets
* current status also to intra file resolved .
*/
if (getReferredTypeDef().getTypeDefBaseType().getResolvableStatus() == INTRA_FILE_RESOLVED) {
return INTRA_FILE_RESOLVED;
ResolvableStatus resolvableStatus = resolveTypeDerivedInfo(baseType);
if (resolvableStatus != null) {
return resolvableStatus;
}
setEffectiveBuiltInType(((YangDerivedInfo<?>) baseType.getDataTypeExtendedInfo())
.getEffectiveBuiltInType());
YangDerivedInfo refDerivedInfo = (YangDerivedInfo<?>) baseType.getDataTypeExtendedInfo();
} else if ((baseType.getDataType() == LEAFREF) || (baseType.getDataType() == IDENTITYREF)) {
setEffectiveBuiltInType(baseType.getDataType());
return RESOLVED;
} else {
setEffectiveBuiltInType(baseType.getDataType());
/*
* Check whether the effective built-in type can have range
* restrictions, if yes call resolution of range.
*/
if (isOfRangeRestrictedType(getEffectiveBuiltInType())) {
if (refDerivedInfo.getResolvedExtendedInfo() == null) {
if (baseType.getDataTypeExtendedInfo() == null) {
resolveRangeRestriction(null);
/*
* Return the resolution status as resolved, if it's not
......@@ -273,11 +266,11 @@ public class YangDerivedInfo<T>
*/
return RESOLVED;
} else {
if (!(refDerivedInfo.getResolvedExtendedInfo() instanceof YangRangeRestriction)) {
if (!(baseType.getDataTypeExtendedInfo() instanceof YangRangeRestriction)) {
throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " +
"type.");
}
resolveRangeRestriction((YangRangeRestriction) refDerivedInfo.getResolvedExtendedInfo());
resolveRangeRestriction((YangRangeRestriction) baseType.getDataTypeExtendedInfo());
/*
* Return the resolution status as resolved, if it's not
* resolve range/string restriction will throw exception in
......@@ -290,7 +283,7 @@ public class YangDerivedInfo<T>
* string resolution.
*/
} else if (getEffectiveBuiltInType() == STRING) {
if (refDerivedInfo.getResolvedExtendedInfo() == null) {
if (baseType.getDataTypeExtendedInfo() == null) {
resolveStringRestriction(null);
/*
* Return the resolution status as resolved, if it's not
......@@ -299,11 +292,11 @@ public class YangDerivedInfo<T>
*/
return RESOLVED;
} else {
if (!(refDerivedInfo.getResolvedExtendedInfo() instanceof YangStringRestriction)) {
if (!(baseType.getDataTypeExtendedInfo() instanceof YangStringRestriction)) {
throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " +
"type.");
}
resolveStringRestriction((YangStringRestriction) refDerivedInfo.getResolvedExtendedInfo());
resolveStringRestriction((YangStringRestriction) baseType.getDataTypeExtendedInfo());
/*
* Return the resolution status as resolved, if it's not
* resolve range/string restriction will throw exception in
......@@ -312,8 +305,8 @@ public class YangDerivedInfo<T>
return RESOLVED;
}
} else if (getEffectiveBuiltInType() == BINARY) {
if (refDerivedInfo.getResolvedExtendedInfo() == null) {
resolveLengthRestriction(null);
if (baseType.getDataTypeExtendedInfo() == null) {
resolveBinaryRestriction(null);
/*
* Return the resolution status as resolved, if it's not
* resolve length restriction will throw exception in
......@@ -321,11 +314,11 @@ public class YangDerivedInfo<T>
*/
return RESOLVED;
} else {
if (!(refDerivedInfo.getResolvedExtendedInfo() instanceof YangRangeRestriction)) {
if (!(baseType.getDataTypeExtendedInfo() instanceof YangRangeRestriction)) {
throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " +
"type.");
}
resolveLengthRestriction((YangRangeRestriction) refDerivedInfo.getResolvedExtendedInfo());
resolveBinaryRestriction((YangRangeRestriction) baseType.getDataTypeExtendedInfo());
/*
* Return the resolution status as resolved, if it's not
* resolve length restriction will throw exception in
......@@ -333,18 +326,89 @@ public class YangDerivedInfo<T>
*/
return RESOLVED;
}
} else if (getEffectiveBuiltInType() == DECIMAL64) {
if (baseType.getDataTypeExtendedInfo() != null) {
if (((YangDecimal64) baseType.getDataTypeExtendedInfo()).getRangeRestrictedExtendedInfo() == null) {
resolveRangeRestriction(null);
/*
* Return the resolution status as resolved, if it's not;
* resolve range restriction will throw exception in
* previous function.
*/
return RESOLVED;
} else {
if (!(((YangDecimal64) baseType.getDataTypeExtendedInfo())
.getRangeRestrictedExtendedInfo() instanceof YangRangeRestriction)) {
throw new DataModelException("Linker error: Referred typedef restriction info is" +
" of invalid type.");
}
} else if ((baseType.getDataType() == LEAFREF) || (baseType.getDataType() == IDENTITYREF)) {
setEffectiveBuiltInType(baseType.getDataType());
resolveRangeRestriction((YangRangeRestriction) ((YangDecimal64) baseType
.getDataTypeExtendedInfo()).getRangeRestrictedExtendedInfo());
/*
* Return the resolution status as resolved, if it's not
* resolve range/string restriction will throw exception in
* previous function.
*/
return RESOLVED;
}
} else {
setEffectiveBuiltInType(baseType.getDataType());
throw new DataModelException("Linker error: Unable to find type extended info for decimal64.");
}
}
}
/*
* Check if the data type is the one which can't be restricted, in this
* case check whether no self restrictions should be present.
*/
if (isOfValidNonRestrictedType(getEffectiveBuiltInType())) {
if (Strings.isNullOrEmpty(getLengthRestrictionString())
&& Strings.isNullOrEmpty(getRangeRestrictionString())
&& getPatternRestriction() == null) {
return RESOLVED;
} else {
throw new DataModelException("YANG file error: Restrictions can't be applied to a given type");
}
}
// Throw exception for unsupported types
throw new DataModelException("Linker error: Unable to process the derived type.");
}
/**
* Resolves the type derived info, by obtaining the effective built-in type
* and resolving the restrictions.
*
* @param baseType base type of typedef
* @return resolution status
* @throws DataModelException a violation in data mode rule
*/
public ResolvableStatus resolveTypeDerivedInfo(YangType<?> baseType)
throws DataModelException {
/*
* Check whether the referred typedef is resolved.
*/
if (baseType.getResolvableStatus() != INTRA_FILE_RESOLVED && baseType.getResolvableStatus() != RESOLVED) {
throw new DataModelException("Linker Error: Referred typedef is not resolved for type.");
}
/*
* Check if the referred typedef is intra file resolved, if yes sets
* current status also to intra file resolved .
*/
if (getReferredTypeDef().getTypeDefBaseType().getResolvableStatus() == INTRA_FILE_RESOLVED) {
return INTRA_FILE_RESOLVED;
}
setEffectiveBuiltInType(((YangDerivedInfo<?>) baseType.getDataTypeExtendedInfo())
.getEffectiveBuiltInType());
YangDerivedInfo refDerivedInfo = (YangDerivedInfo<?>) baseType.getDataTypeExtendedInfo();
/*
* Check whether the effective built-in type can have range
* restrictions, if yes call resolution of range.
*/
if (isOfRangeRestrictedType(getEffectiveBuiltInType())) {
if (baseType.getDataTypeExtendedInfo() == null) {
if (refDerivedInfo.getResolvedExtendedInfo() == null) {
resolveRangeRestriction(null);
/*
* Return the resolution status as resolved, if it's not
......@@ -353,11 +417,11 @@ public class YangDerivedInfo<T>
*/
return RESOLVED;
} else {
if (!(baseType.getDataTypeExtendedInfo() instanceof YangRangeRestriction)) {
if (!(refDerivedInfo.getResolvedExtendedInfo() instanceof YangRangeRestriction)) {
throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " +
"type.");
}
resolveRangeRestriction((YangRangeRestriction) baseType.getDataTypeExtendedInfo());
resolveRangeRestriction((YangRangeRestriction) refDerivedInfo.getResolvedExtendedInfo());
/*
* Return the resolution status as resolved, if it's not
* resolve range/string restriction will throw exception in
......@@ -370,7 +434,7 @@ public class YangDerivedInfo<T>
* string resolution.
*/
} else if (getEffectiveBuiltInType() == STRING) {
if (baseType.getDataTypeExtendedInfo() == null) {
if (refDerivedInfo.getResolvedExtendedInfo() == null) {
resolveStringRestriction(null);
/*
* Return the resolution status as resolved, if it's not
......@@ -379,11 +443,11 @@ public class YangDerivedInfo<T>
*/
return RESOLVED;
} else {
if (!(baseType.getDataTypeExtendedInfo() instanceof YangStringRestriction)) {
if (!(refDerivedInfo.getResolvedExtendedInfo() instanceof YangStringRestriction)) {
throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " +
"type.");
}
resolveStringRestriction((YangStringRestriction) baseType.getDataTypeExtendedInfo());
resolveStringRestriction((YangStringRestriction) refDerivedInfo.getResolvedExtendedInfo());
/*
* Return the resolution status as resolved, if it's not
* resolve range/string restriction will throw exception in
......@@ -392,8 +456,8 @@ public class YangDerivedInfo<T>
return RESOLVED;
}
} else if (getEffectiveBuiltInType() == BINARY) {
if (baseType.getDataTypeExtendedInfo() == null) {
resolveLengthRestriction(null);
if (refDerivedInfo.getResolvedExtendedInfo() == null) {
resolveBinaryRestriction(null);
/*
* Return the resolution status as resolved, if it's not
* resolve length restriction will throw exception in
......@@ -401,11 +465,11 @@ public class YangDerivedInfo<T>
*/
return RESOLVED;
} else {
if (!(baseType.getDataTypeExtendedInfo() instanceof YangRangeRestriction)) {
if (!(refDerivedInfo.getResolvedExtendedInfo() instanceof YangRangeRestriction)) {
throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " +
"type.");
}
resolveLengthRestriction((YangRangeRestriction) baseType.getDataTypeExtendedInfo());
resolveBinaryRestriction((YangRangeRestriction) refDerivedInfo.getResolvedExtendedInfo());
/*
* Return the resolution status as resolved, if it's not
* resolve length restriction will throw exception in
......@@ -413,23 +477,35 @@ public class YangDerivedInfo<T>
*/
return RESOLVED;
}
}
}
} else if (getEffectiveBuiltInType() == DECIMAL64) {
if ((refDerivedInfo.getResolvedExtendedInfo() == null) ||
(((YangDecimal64) refDerivedInfo.getResolvedExtendedInfo())
.getRangeRestrictedExtendedInfo() == null)) {
resolveRangeRestriction(null);
/*
* Check if the data type is the one which can't be restricted, in this
* case check whether no self restrictions should be present.
* Return the resolution status as resolved, if it's not;
* resolve range restriction will throw exception in
* previous function.
*/
if (isOfValidNonRestrictedType(getEffectiveBuiltInType())) {
if (Strings.isNullOrEmpty(getLengthRestrictionString())
&& Strings.isNullOrEmpty(getRangeRestrictionString())
&& getPatternRestriction() == null) {
return RESOLVED;
} else {
throw new DataModelException("YANG file error: Restrictions can't be applied to a given type");
if (!(((YangDecimal64) refDerivedInfo.getResolvedExtendedInfo())
.getRangeRestrictedExtendedInfo() instanceof YangRangeRestriction)) {
throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " +
"type.");
}
resolveRangeRestriction((YangRangeRestriction) ((YangDecimal64) refDerivedInfo
.getResolvedExtendedInfo()).getRangeRestrictedExtendedInfo());
/*
* Return the resolution status as resolved, if it's not
* resolve range/string restriction will throw exception in
* previous function.
*/
return RESOLVED;
}
}
// Throw exception for unsupported types
throw new DataModelException("Linker error: Unable to process the derived type.");
return null;
}
/**
......@@ -490,6 +566,30 @@ public class YangDerivedInfo<T>
}
/**
* Resolves the binary restrictions.
*
* @param refLengthRestriction referred length restriction of typedef
* @throws DataModelException a violation in data model rule
*/
private void resolveBinaryRestriction(YangRangeRestriction refLengthRestriction)
throws DataModelException {
if (rangeRestrictionString != null || patternRestriction != null) {
DataModelException dataModelException =
new DataModelException("YANG file error: for binary " +
"range restriction or pattern restriction is not allowed.");
dataModelException.setLine(lineNumber);
dataModelException.setCharPosition(charPositionInLine);
throw dataModelException;
}
// Get the final resolved length restriction
YangRangeRestriction lengthRestriction = resolveLengthRestriction(refLengthRestriction);
// Set the lenght restriction.
setResolvedExtendedInfo((T) lengthRestriction);
}
/**
* Resolves pattern restriction.
*
* @param refPatternRestriction referred pattern restriction of typedef
......
......@@ -102,6 +102,7 @@ public class YangLengthRestriction implements YangDesc, YangReference, YangAppEr
* Creates a YANG length restriction object.
*/
public YangLengthRestriction() {
setLengthRestriction(new YangRangeRestriction<YangUint64>());
}
/**
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* 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.
*/
package org.onosproject.yangutils.datamodel.utils;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import java.math.BigDecimal;
import java.util.ArrayList;
/**
* The "fraction-digits" statement, which is a substatement to the
* "type" statement, MUST be present if the type is "decimal64". It
* takes as an argument an integer between 1 and 18, inclusively. It
* controls the size of the minimum difference between values of a
* decimal64 type, by restricting the value space to numbers that are
* expressible as "i x 10^-n" where n is the fraction-digits argument.
*
* +----------------+-----------------------+----------------------+
* | fraction-digit | min | max |
* +----------------+-----------------------+----------------------+
* | 1 | -922337203685477580.8 | 922337203685477580.7 |
* | 2 | -92233720368547758.08 | 92233720368547758.07 |
* | 3 | -9223372036854775.808 | 9223372036854775.807 |
* | 4 | -922337203685477.5808 | 922337203685477.5807 |
* | 5 | -92233720368547.75808 | 92233720368547.75807 |
* | 6 | -9223372036854.775808 | 9223372036854.775807 |
* | 7 | -922337203685.4775808 | 922337203685.4775807 |
* | 8 | -92233720368.54775808 | 92233720368.54775807 |
* | 9 | -9223372036.854775808 | 9223372036.854775807 |
* | 10 | -922337203.6854775808 | 922337203.6854775807 |
* | 11 | -92233720.36854775808 | 92233720.36854775807 |
* | 12 | -9223372.036854775808 | 9223372.036854775807 |
* | 13 | -922337.2036854775808 | 922337.2036854775807 |
* | 14 | -92233.72036854775808 | 92233.72036854775807 |
* | 15 | -9223.372036854775808 | 9223.372036854775807 |
* | 16 | -922.3372036854775808 | 922.3372036854775807 |
* | 17 | -92.23372036854775808 | 92.23372036854775807 |
* | 18 | -9.223372036854775808 | 9.223372036854775807 |
* +----------------+-----------------------+----------------------+
*/
/**
* Represents the decimal64 value range based on fraction-digits.
*/
public final class FractionDigits {
public static class Range {
private double min;
private double max;
/**
* Creates an instance of range.
*
* @param min minimum value of decimal64
* @param max maximum value of decimal64
*/
protected Range(double min, double max) {
this.min = min;
this.max = max;
}
/**
* Retrieve minimum value range.
*
* @return minimum value range
*/
public double getMin() {
return min;
}
/**
* Retrieve maximum value range.
*
* @return maximum value range
*/
public double getMax() {
return max;
}
}
private static ArrayList<Range> decimal64ValueRange = null;
/**
* Creates a fraction-digits instance.
*/
private FractionDigits() {
}
/**
* Generates decimal64 value range based on fraction-digits.
*
* @return decimal64 value range by fraction-digits as index
*/
private static ArrayList<Range> getDecimal64ValueRange() {
if (decimal64ValueRange == null) {
decimal64ValueRange = new ArrayList<>();
decimal64ValueRange.add(new Range(-922337203685477580.8, 922337203685477580.7)); // fraction-digit: 1
decimal64ValueRange.add(new Range(-92233720368547758.08, 92233720368547758.07)); // fraction-digit: 2
decimal64ValueRange.add(new Range(-9223372036854775.808, 9223372036854775.807)); // fraction-digit: 3
decimal64ValueRange.add(new Range(-922337203685477.5808, 922337203685477.5807)); // fraction-digit: 4
decimal64ValueRange.add(new Range(-92233720368547.75808, 92233720368547.75807)); // fraction-digit: 5
decimal64ValueRange.add(new Range(-9223372036854.775808, 9223372036854.775807)); // fraction-digit: 6
decimal64ValueRange.add(new Range(-922337203685.4775808, 922337203685.4775807)); // fraction-digit: 7
decimal64ValueRange.add(new Range(-92233720368.54775808, 92233720368.54775807)); // fraction-digit: 8
decimal64ValueRange.add(new Range(-9223372036.854775808, 9223372036.854775807)); // fraction-digit: 9
decimal64ValueRange.add(new Range(-922337203.6854775808, 922337203.6854775807)); // fraction-digit: 10
decimal64ValueRange.add(new Range(-92233720.36854775808, 92233720.36854775807)); // fraction-digit: 11
decimal64ValueRange.add(new Range(-9223372.036854775808, 9223372.036854775807)); // fraction-digit: 12
decimal64ValueRange.add(new Range(-922337.2036854775808, 922337.2036854775807)); // fraction-digit: 13
decimal64ValueRange.add(new Range(-92233.72036854775808, 92233.72036854775807)); // fraction-digit: 14
decimal64ValueRange.add(new Range(-9223.372036854775808, 9223.372036854775807)); // fraction-digit: 15
decimal64ValueRange.add(new Range(-922.3372036854775808, 922.3372036854775807)); // fraction-digit: 16
decimal64ValueRange.add(new Range(-92.23372036854775808, 92.23372036854775807)); // fraction-digit: 17
decimal64ValueRange.add(new Range(-9.223372036854775808, 9.223372036854775807)); // fraction-digit: 18
}
return decimal64ValueRange;
}
/**
* Checks given decimal64 value is in the specific range based on given fraction-digit.
*
* @param value decimal64 value
* @param fractionDigit fraction-digits
* @return success when it is in specific range otherwise false
*/
public static boolean isValidDecimal64(BigDecimal value, int fractionDigit) {
if (!((fractionDigit >= 1) && (fractionDigit <= 18))) {
return false;
}
// ArrayList index starts from 0.
Range range = getDecimal64ValueRange().get(fractionDigit - 1);
if ((value.doubleValue() >= range.min) && (value.doubleValue() <= range.max)) {
return true;
}
return false;
}
/**
* Retrieve range based on fraction-digits.
*
* @param fractionDigit fraction-digits
* @return range
* @throws DataModelException a violation of data model rules
*/
public static Range getRange(int fractionDigit) throws DataModelException {
if (!((fractionDigit >= 1) && (fractionDigit <= 18))) {
throw new DataModelException("YANG file error : given fraction-digit is not in its range (1..18).");
}
return getDecimal64ValueRange().get(fractionDigit - 1);
}
}
......@@ -95,6 +95,16 @@ public enum YangConstructType {
BITS_DATA,
/**
* Identifies the YANG decimal64 parsed data.
*/
DECIMAL64_DATA,
/**
* Identifies the YANG fraction-digits parsed data.
*/
FRACTION_DIGITS_DATA,
/**
* Identifies the YANG enum parsed data.
*/
ENUM_DATA,
......@@ -413,6 +423,10 @@ public enum YangConstructType {
return "bit";
case BITS_DATA:
return "bits";
case DECIMAL64_DATA:
return "decimal64";
case FRACTION_DIGITS_DATA:
return "fraction-digits";
case ENUM_DATA:
return "enum";
case IMPORT_DATA:
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* 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.
*/
package org.onosproject.yangutils.datamodel.utils.builtindatatype;
import java.io.Serializable;
import java.util.Objects;
import com.google.common.base.MoreObjects;
/**
* Represents binary data type.
*/
public final class YangBinary implements Serializable {
private static final long serialVersionUID = 8006201670L;
private byte[] byteArray;
/**
* Creates an instance of YANG binary.
*/
private YangBinary() {
}
/**
* Creates an instance of YANG binary.
*
* @param bytes byte array
*/
public YangBinary(byte[] bytes) {
byteArray = bytes;
}
/**
* Returns object of YANG binary.
*
* @param bytes byte array
* @return object of YANG binary
*/
public static YangBinary of(byte[] bytes) {
return new YangBinary(bytes);
}
/**
* Returns byte array.
*
* @return byte array
*/
public byte[] byteArray() {
return byteArray;
}
@Override
public int hashCode() {
return Objects.hash(byteArray);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof YangBinary) {
YangBinary other = (YangBinary) obj;
return Objects.equals(byteArray, other.byteArray);
}
return false;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("byteArray", byteArray)
.toString();
}
/**
* Returns the object of YANG binary fromString input String.
*
* @param valInString input String
* @return Object of YANG binary
*/
public static YangBinary fromString(String valInString) {
try {
byte[] tmpVal = valInString.getBytes();
return of(tmpVal);
} catch (Exception e) {
}
return null;
}
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* 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.
*/
package org.onosproject.yangutils.datamodel.utils.builtindatatype;
import java.io.Serializable;
import java.util.Objects;
import com.google.common.base.MoreObjects;
/**
* Represents bits data type.
*/
public class YangBits implements Serializable {
private static final long serialVersionUID = 8006201669L;
private byte[] byteArray;
/**
* Creates an instance of YANG bits.
*/
private YangBits() {
}
/**
* Creates an instance of YANG bits.
*
* @param bytes byte array
*/
public YangBits(byte[] bytes) {
byteArray = bytes;
}
/**
* Returns object of YANG bits.
*
* @param bytes byte array
* @return object of YANG bits
*/
public static YangBits of(byte[] bytes) {
return new YangBits(bytes);
}
/**
* Returns byte array.
*
* @return byte array
*/
public byte[] byteArray() {
return byteArray;
}
@Override
public int hashCode() {
return Objects.hash(byteArray);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof YangBits) {
YangBits other = (YangBits) obj;
return Objects.equals(byteArray, other.byteArray);
}
return false;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("byteArray", byteArray)
.toString();
}
/**
* Returns the object of YANG bits fromString input String.
*
* @param valInString input String
* @return Object of YANG bits
*/
public static YangBits fromString(String valInString) {
try {
byte[] tmpVal = valInString.getBytes();
return of(tmpVal);
} catch (Exception e) {
}
return null;
}
}
......@@ -16,7 +16,6 @@
package org.onosproject.yangutils.datamodel.utils.builtindatatype;
import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DECIMAL64;
import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT16;
import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT32;
import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT64;
......@@ -51,7 +50,6 @@ public final class YangDataTypeUtils {
|| dataType == UINT8
|| dataType == UINT16
|| dataType == UINT32
|| dataType == UINT64
|| dataType == DECIMAL64;
|| dataType == UINT64;
}
}
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* 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.
*/
package org.onosproject.yangutils.datamodel.utils.builtindatatype;
import java.io.Serializable;
import java.util.Objects;
import com.google.common.base.MoreObjects;
/**
* Represents YANG decimal 64.
*/
public class YangDecimal64 implements Serializable {
private static final long serialVersionUID = 8006201668L;
private int fractionDigit;
/**
* Creates an instance of YANG decimal64.
*/
public YangDecimal64() {
}
/**
* Creates an instance of of YANG decimal64.
*
* @param fractionDigit fraction digit
*/
public YangDecimal64(int fractionDigit) {
setFractionDigit(fractionDigit);
}
/**
* Returns fraction digit.
*
* @return the fractionDigit
*/
public int getFractionDigit() {
return fractionDigit;
}
/**
* Sets fraction digit.
*
* @param fractionDigit fraction digits.
*/
public void setFractionDigit(int fractionDigit) {
this.fractionDigit = fractionDigit;
}
/**
* Returns object of YANG decimal64.
*
* @param value fraction digit
* @return YANG decimal64
*/
public static YangDecimal64 of(int value) {
return new YangDecimal64(value);
}
@Override
public int hashCode() {
return Objects.hash(fractionDigit);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof YangDecimal64) {
YangDecimal64 other = (YangDecimal64) obj;
return Objects.equals(fractionDigit, other.fractionDigit);
}
return false;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("fractionDigit", fractionDigit)
.toString();
}
/**
* Returns the object of YANG decimal64 fromString input String.
*
* @param valInString input String
* @return Object of YANG decimal64
*/
public static YangDecimal64 fromString(String valInString) {
try {
int tmpVal = Integer.parseInt(valInString);
return of(tmpVal);
} catch (Exception e) {
}
return null;
}
}
......@@ -632,6 +632,22 @@ public interface GeneratedYangListener extends ParseTreeListener {
*
* @param currentContext current context in the parsed tree
*/
void enterFractionDigitStatement(GeneratedYangParser.FractionDigitStatementContext currentContext);
/**
* Exits a parse tree produced by GeneratedYangParser for grammar rule
* numericalRestrictions.
*
* @param currentContext current context in the parsed tree
*/
void exitFractionDigitStatement(GeneratedYangParser.FractionDigitStatementContext currentContext);
/**
* Enters a parse tree produced by GeneratedYangParser for grammar rule
* numericalRestrictions.
*
* @param currentContext current context in the parsed tree
*/
void enterNumericalRestrictions(GeneratedYangParser.NumericalRestrictionsContext currentContext);
/**
......
......@@ -37,11 +37,13 @@ import org.onosproject.yangutils.parser.impl.listeners.ChoiceListener;
import org.onosproject.yangutils.parser.impl.listeners.ConfigListener;
import org.onosproject.yangutils.parser.impl.listeners.ContactListener;
import org.onosproject.yangutils.parser.impl.listeners.ContainerListener;
import org.onosproject.yangutils.parser.impl.listeners.Decimal64Listener;
import org.onosproject.yangutils.parser.impl.listeners.DefaultListener;
import org.onosproject.yangutils.parser.impl.listeners.DescriptionListener;
import org.onosproject.yangutils.parser.impl.listeners.EnumListener;
import org.onosproject.yangutils.parser.impl.listeners.EnumerationListener;
import org.onosproject.yangutils.parser.impl.listeners.FeatureListener;
import org.onosproject.yangutils.parser.impl.listeners.FractionDigitsListener;
import org.onosproject.yangutils.parser.impl.listeners.GroupingListener;
import org.onosproject.yangutils.parser.impl.listeners.IdentityrefListener;
import org.onosproject.yangutils.parser.impl.listeners.IfFeatureListener;
......@@ -563,12 +565,22 @@ public class TreeWalkListener implements GeneratedYangListener {
@Override
public void enterDecimal64Specification(GeneratedYangParser.Decimal64SpecificationContext ctx) {
// TODO: implement the method.
Decimal64Listener.processDecimal64Entry(this, ctx);
}
@Override
public void exitDecimal64Specification(GeneratedYangParser.Decimal64SpecificationContext ctx) {
// TODO: implement the method.
Decimal64Listener.processDecimal64Exit(this, ctx);
}
@Override
public void enterFractionDigitStatement(GeneratedYangParser.FractionDigitStatementContext ctx) {
FractionDigitsListener.processFractionDigitsEntry(this, ctx);
}
@Override
public void exitFractionDigitStatement(GeneratedYangParser.FractionDigitStatementContext currentContext) {
// do nothing
}
@Override
......
......@@ -61,6 +61,8 @@ package org.onosproject.yangutils.parser.impl.listeners;
* ;
*/
import java.util.Map;
import org.onosproject.yangutils.datamodel.YangBit;
import org.onosproject.yangutils.datamodel.YangBits;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
......@@ -141,12 +143,13 @@ public final class BitListener {
int maxPosition = 0;
boolean isPositionPresent = false;
for (YangBit curBit : yangBits.getBitSet()) {
if (maxPosition <= curBit.getPosition()) {
maxPosition = curBit.getPosition();
for (Map.Entry<Integer, YangBit> element : yangBits.getBitPositionMap().entrySet()) {
if (maxPosition <= element.getKey()) {
maxPosition = element.getKey();
isPositionPresent = true;
}
}
if (isPositionPresent) {
maxPosition++;
}
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* 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.
*/
package org.onosproject.yangutils.parser.impl.listeners;
/*
* Reference: RFC6020 and YANG ANTLR Grammar
*
* ABNF grammar as per RFC6020
* type-body-stmts = numerical-restrictions /
* decimal64-specification /
* string-restrictions /
* enum-specification /
* leafref-specification /
* identityref-specification /
* instance-identifier-specification /
* bits-specification /
* union-specification
*
* decimal64-specification = fraction-digits-stmt [range-stmt stmtsep]
*
* fraction-digits-stmt = fraction-digits-keyword sep
* fraction-digits-arg-str stmtend
*
* fraction-digits-arg-str = < a string that matches the rule
* fraction-digits-arg >
*
* fraction-digits-arg = ("1" ["0" / "1" / "2" / "3" / "4" /
* "5" / "6" / "7" / "8"])
* / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9"
*
* range-stmt = range-keyword sep range-arg-str optsep
* (";" /
* "{" stmtsep
* ;; these stmts can appear in any order
* [error-message-stmt stmtsep]
* [error-app-tag-stmt stmtsep]
* [description-stmt stmtsep]
* [reference-stmt stmtsep]
* "}")
* ANTLR grammar rule
*
* typeBodyStatements : numericalRestrictions | decimal64Specification | stringRestrictions | enumSpecification
* | leafrefSpecification | identityrefSpecification | instanceIdentifierSpecification
* | bitsSpecification | unionSpecification;
*
* decimal64Specification : fractionDigitStatement rangeStatement?;
*
* fractionDigitStatement : FRACTION_DIGITS_KEYWORD fraction STMTEND;
*
* fraction : string;
*/
import org.onosproject.yangutils.datamodel.YangDecimal64;
import org.onosproject.yangutils.datamodel.YangRangeRestriction;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.datamodel.utils.Parsable;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import static org.onosproject.yangutils.datamodel.utils.YangConstructType.DECIMAL64_DATA;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
/**
* Represents listener based call back function corresponding to the "decimal64" rule
* defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
*/
public final class Decimal64Listener {
/**
* Creates a new Decimal64 listener.
*/
private Decimal64Listener() {
}
/**
* It is called when parser enters grammar rule (decimal64), it perform
* validations and updates the data model tree.
*
* @param listener listener's object
* @param ctx context object of the grammar rule
*/
public static void processDecimal64Entry(TreeWalkListener listener,
GeneratedYangParser.Decimal64SpecificationContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, DECIMAL64_DATA, "", ENTRY);
Parsable tmpNode = listener.getParsedDataStack().peek();
if (tmpNode instanceof YangType) {
YangType<YangDecimal64<YangRangeRestriction>> typeNode =
(YangType<YangDecimal64<YangRangeRestriction>>) tmpNode;
YangDecimal64 decimal64Node = new YangDecimal64();
typeNode.setDataTypeExtendedInfo(decimal64Node);
} else {
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, DECIMAL64_DATA, "", ENTRY));
}
}
/**
* Performs validation and updates the data model tree.
* It is called when parser exits from grammar rule (decimal64).
*
* @param listener listener's object
* @param ctx context object of the grammar rule
*/
public static void processDecimal64Exit(TreeWalkListener listener,
GeneratedYangParser.Decimal64SpecificationContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, DECIMAL64_DATA, "", EXIT);
Parsable tmpNode = listener.getParsedDataStack().peek();
if (tmpNode instanceof YangType) {
YangType<YangDecimal64<YangRangeRestriction>> typeNode =
(YangType<YangDecimal64<YangRangeRestriction>>) tmpNode;
YangDecimal64<YangRangeRestriction> decimal64Node = typeNode.getDataTypeExtendedInfo();
try {
decimal64Node.validateRange();
} catch (DataModelException e) {
throw new ParserException(e);
}
} else {
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, DECIMAL64_DATA, "", EXIT));
}
}
}
\ No newline at end of file
/*
* Copyright 2016-present Open Networking Laboratory
*
* 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.
*/
package org.onosproject.yangutils.parser.impl.listeners;
/*
* Reference: RFC6020 and YANG ANTLR Grammar
*
* ABNF grammar as per RFC6020
* decimal64-specification = fraction-digits-stmt
*
* fraction-digits-stmt = fraction-digits-keyword sep
* fraction-digits-arg-str stmtend
*
* fraction-digits-arg-str = < a string that matches the rule
* fraction-digits-arg >
*
* fraction-digits-arg = ("1" ["0" / "1" / "2" / "3" / "4" /
* "5" / "6" / "7" / "8"])
* / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9"
*
* ANTLR grammar rule
* decimal64Specification : FRACTION_DIGITS_KEYWORD fraction STMTEND;
*
* fraction : string;
*/
import org.onosproject.yangutils.datamodel.YangDecimal64;
import org.onosproject.yangutils.datamodel.YangRangeRestriction;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.utils.Parsable;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import static org.onosproject.yangutils.datamodel.utils.YangConstructType.FRACTION_DIGITS_DATA;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
/**
* Represents listener based call back function corresponding to the "fraction-digits"
* rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
*/
public final class FractionDigitsListener {
/**
* Creates a new bit listener.
*/
private FractionDigitsListener() {
}
/**
* It is called when parser enters grammar rule (fraction-digits), it perform
* validations and updates the data model tree.
*
* @param listener listener's object
* @param ctx context object of the grammar rule
*/
public static void processFractionDigitsEntry(TreeWalkListener listener,
GeneratedYangParser.FractionDigitStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, FRACTION_DIGITS_DATA, ctx.fraction().getText(), ENTRY);
int value = getValidFractionDigits(ctx);
Parsable tmpNode = listener.getParsedDataStack().peek();
if (tmpNode instanceof YangType) {
YangType<YangDecimal64<YangRangeRestriction>> typeNode =
(YangType<YangDecimal64<YangRangeRestriction>>) tmpNode;
YangDecimal64 decimal64Node = typeNode.getDataTypeExtendedInfo();
decimal64Node.setFractionDigit(value);
} else {
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, FRACTION_DIGITS_DATA,
ctx.fraction().getText(), ENTRY));
}
}
/**
* Validate fraction digits.
*
* @param ctx context object of the grammar rule
* @return validated fraction-digits
*/
public static int getValidFractionDigits(GeneratedYangParser.FractionDigitStatementContext ctx) {
String value = ctx.fraction().getText().trim();
ParserException parserException;
int fractionDigits = Integer.parseInt(value);
if ((fractionDigits >= YangDecimal64.MIN_FRACTION_DIGITS_VALUE) &&
(fractionDigits <= YangDecimal64.MAX_FRACTION_DIGITS_VALUE)) {
return fractionDigits;
} else {
parserException =
new ParserException("YANG file error : fraction-digits value should be between 1 and 18.");
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
}
}
}
......@@ -109,17 +109,14 @@ public final class PositionListener {
switch (tmpNode.getYangConstructType()) {
case BITS_DATA: {
YangBits yangBits = (YangBits) tmpNode;
for (YangBit curBit : yangBits.getBitSet()) {
if (positionValue == curBit.getPosition()) {
listener.getParsedDataStack().push(bitNode);
if (yangBits.isBitPositionExists(positionValue)) {
ParserException parserException = new ParserException("YANG file error: Duplicate value of " +
"position is invalid.");
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
}
}
listener.getParsedDataStack().push(bitNode);
return positionValue;
}
default:
......
......@@ -16,6 +16,7 @@
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangDecimal64;
import org.onosproject.yangutils.datamodel.YangDerivedInfo;
import org.onosproject.yangutils.datamodel.YangRangeRestriction;
import org.onosproject.yangutils.datamodel.YangType;
......@@ -25,6 +26,7 @@ import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DECIMAL64;
import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DERIVED;
import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypeUtils.isOfRangeRestrictedType;
import static org.onosproject.yangutils.datamodel.utils.RestrictionResolver.processRangeRestriction;
......@@ -112,7 +114,7 @@ public final class RangeRestrictionListener {
return;
}
if (!(isOfRangeRestrictedType(type.getDataType()))) {
if (!(isOfRangeRestrictedType(type.getDataType())) && (type.getDataType() != DECIMAL64)) {
ParserException parserException = new ParserException("YANG file error: Range restriction can't be " +
"applied to a given type");
parserException.setLine(ctx.getStart().getLine());
......@@ -122,8 +124,17 @@ public final class RangeRestrictionListener {
YangRangeRestriction rangeRestriction = null;
try {
if (type.getDataType() == DECIMAL64) {
YangDecimal64 yangDecimal64 = (YangDecimal64) type.getDataTypeExtendedInfo();
rangeRestriction = processRangeRestriction(yangDecimal64.getDefaultRangeRestriction(),
ctx.getStart().getLine(),
ctx.getStart().getCharPositionInLine(),
true, ctx.range().getText(), type.getDataType());
} else {
rangeRestriction = processRangeRestriction(null, ctx.getStart().getLine(),
ctx.getStart().getCharPositionInLine(), false, ctx.range().getText(), type.getDataType());
ctx.getStart().getCharPositionInLine(),
false, ctx.range().getText(), type.getDataType());
}
} catch (DataModelException e) {
ParserException parserException = new ParserException(e.getMessage());
parserException.setCharPosition(e.getCharPositionInLine());
......@@ -132,8 +143,13 @@ public final class RangeRestrictionListener {
}
if (rangeRestriction != null) {
if (type.getDataType() == DECIMAL64) {
((YangDecimal64<YangRangeRestriction>) type.getDataTypeExtendedInfo())
.setRangeRestrictedExtendedInfo(rangeRestriction);
} else {
type.setDataTypeExtendedInfo(rangeRestriction);
}
}
listener.getParsedDataStack().push(rangeRestriction);
}
......
......@@ -94,6 +94,7 @@ public final class TypeListener {
// Obtain the YANG data type.
YangDataTypes yangDataTypes = YangDataTypes.getType(ctx.string().getText());
// validate type sub-statement cardinality
validateTypeSubStatementCardinality(ctx, yangDataTypes);
// Create YANG type object and fill the values.
......@@ -306,6 +307,10 @@ public final class TypeListener {
parserException = new ParserException("YANG file error : a type bits" +
" must have atleast one bit statement.");
break;
case DECIMAL64:
parserException = new ParserException("YANG file error : a type decimal64" +
" must have fraction-digits statement.");
break;
case LEAFREF:
parserException = new ParserException("YANG file error : a type leafref" +
" must have one path statement.");
......@@ -314,7 +319,6 @@ public final class TypeListener {
parserException = new ParserException("YANG file error : a type identityref" +
" must have base statement.");
break;
// TODO : decimal64,
default:
return;
}
......
......@@ -37,10 +37,13 @@ import org.onosproject.yangutils.utils.io.impl.YangToJavaNamingConflictUtil;
import static org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModelUtils.getCurNodePackage;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
import static org.onosproject.yangutils.utils.UtilConstants.BIG_INTEGER;
import static org.onosproject.yangutils.utils.UtilConstants.BIG_DECIMAL;
import static org.onosproject.yangutils.utils.UtilConstants.BIT_SET;
import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_DATA_TYPE;
import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_WRAPPER;
import static org.onosproject.yangutils.utils.UtilConstants.BYTE;
import static org.onosproject.yangutils.utils.UtilConstants.BYTE_WRAPPER;
import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS;
import static org.onosproject.yangutils.utils.UtilConstants.INT;
import static org.onosproject.yangutils.utils.UtilConstants.INTEGER_WRAPPER;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
......@@ -50,11 +53,8 @@ import static org.onosproject.yangutils.utils.UtilConstants.LONG_WRAPPER;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.SHORT;
import static org.onosproject.yangutils.utils.UtilConstants.SHORT_WRAPPER;
import static org.onosproject.yangutils.utils.UtilConstants.SQUARE_BRACKETS;
import static org.onosproject.yangutils.utils.UtilConstants.STRING_DATA_TYPE;
import static org.onosproject.yangutils.utils.UtilConstants.YANG_BINARY_CLASS;
import static org.onosproject.yangutils.utils.UtilConstants.YANG_BITS_CLASS;
import static org.onosproject.yangutils.utils.UtilConstants.YANG_DECIMAL64_CLASS;
import static org.onosproject.yangutils.utils.UtilConstants.YANG_TYPES_PKG;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
......@@ -97,10 +97,12 @@ public final class AttributesJavaDataType {
return LONG;
case UINT64:
return BIG_INTEGER;
case BITS:
return BIT_SET;
case BINARY:
return YANG_BINARY_CLASS;
return BYTE + SQUARE_BRACKETS;
case DECIMAL64:
return YANG_DECIMAL64_CLASS;
return BIG_DECIMAL;
case STRING:
return STRING_DATA_TYPE;
case BOOLEAN:
......@@ -146,7 +148,7 @@ public final class AttributesJavaDataType {
case UINT64:
return BIG_INTEGER;
case DECIMAL64:
return YANG_DECIMAL64_CLASS;
return BIG_DECIMAL;
case STRING:
return STRING_DATA_TYPE;
case BOOLEAN:
......@@ -156,9 +158,9 @@ public final class AttributesJavaDataType {
getCamelCase(((YangJavaEnumeration) yangType.getDataTypeExtendedInfo()).getName(),
pluginConfig));
case BITS:
return YANG_BITS_CLASS;
return BIT_SET;
case BINARY:
return YANG_BINARY_CLASS;
return BYTE + SQUARE_BRACKETS;
case LEAFREF:
YangType<?> referredType = getReferredTypeFromLeafref(yangType);
return getJavaImportClass(referredType, isListAttr, pluginConfig);
......@@ -184,8 +186,6 @@ public final class AttributesJavaDataType {
switch (type) {
case UINT64:
return BIG_INTEGER;
case DECIMAL64:
return YANG_DECIMAL64_CLASS;
case STRING:
return STRING_DATA_TYPE;
case ENUMERATION:
......@@ -193,9 +193,9 @@ public final class AttributesJavaDataType {
getCamelCase(((YangJavaEnumeration) yangType.getDataTypeExtendedInfo()).getName(),
pluginConfig));
case BITS:
return YANG_BITS_CLASS;
case BINARY:
return YANG_BINARY_CLASS;
return BIT_SET;
case DECIMAL64:
return BIG_DECIMAL;
case LEAFREF:
YangType<?> referredType = getReferredTypeFromLeafref(yangType);
return getJavaImportClass(referredType, isListAttr, pluginConfig);
......@@ -241,18 +241,18 @@ public final class AttributesJavaDataType {
case UINT8:
case UINT16:
case UINT32:
case BINARY:
case STRING:
case BOOLEAN:
case EMPTY:
return JAVA_LANG;
case UINT64:
case DECIMAL64:
return JAVA_MATH;
case ENUMERATION:
return getEnumsPackage(yangType, conflictResolver);
case DECIMAL64:
case BITS:
case BINARY:
return YANG_TYPES_PKG;
return COLLECTION_IMPORTS;
case LEAFREF:
YangType<?> referredType = getReferredTypeFromLeafref(yangType);
return getJavaImportPackage(referredType, isListAttr, conflictResolver);
......@@ -270,22 +270,20 @@ public final class AttributesJavaDataType {
} else {
switch (type) {
case UINT64:
case DECIMAL64:
return JAVA_MATH;
case EMPTY:
case STRING:
return JAVA_LANG;
case ENUMERATION:
return getEnumsPackage(yangType, conflictResolver);
case DECIMAL64:
case BITS:
case BINARY:
return YANG_TYPES_PKG;
return COLLECTION_IMPORTS;
case LEAFREF:
YangType<?> referredType = getReferredTypeFromLeafref(yangType);
return getJavaImportPackage(referredType, isListAttr, conflictResolver);
case IDENTITYREF:
return getIdentityRefPackage(yangType, conflictResolver);
case EMPTY:
return JAVA_LANG;
case UNION:
return getUnionPackage(yangType, conflictResolver);
case INSTANCE_IDENTIFIER:
......
......@@ -35,6 +35,7 @@ import static org.onosproject.yangutils.utils.UtilConstants.AND;
import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO;
import static org.onosproject.yangutils.utils.UtilConstants.BIG_INTEGER;
import static org.onosproject.yangutils.utils.UtilConstants.BIG_DECIMAL;
import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_DATA_TYPE;
import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_WRAPPER;
import static org.onosproject.yangutils.utils.UtilConstants.BUILD;
......@@ -54,6 +55,7 @@ import static org.onosproject.yangutils.utils.UtilConstants.DEACTIVATE_ANNOTATIO
import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT;
import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.DOUBLE;
import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.EQUAL;
......@@ -64,6 +66,7 @@ import static org.onosproject.yangutils.utils.UtilConstants.FALSE;
import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.FROM_STRING_METHOD_NAME;
import static org.onosproject.yangutils.utils.UtilConstants.FROM_STRING_PARAM_NAME;
import static org.onosproject.yangutils.utils.UtilConstants.GET_BYTES;
import static org.onosproject.yangutils.utils.UtilConstants.GET_METHOD_PREFIX;
import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_METHOD_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.HASH;
......@@ -133,6 +136,7 @@ import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getSmallCase;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BINARY;
/**
* Represents generator for methods of generated files based on the file type.
......@@ -296,6 +300,7 @@ public final class MethodsGenerator {
case INT:
case SHORT:
case LONG:
case DOUBLE:
return "0";
case BOOLEAN_DATA_TYPE:
return FALSE;
......@@ -748,11 +753,16 @@ public final class MethodsGenerator {
JavaAttributeInfo fromStringAttributeInfo) {
String targetDataType = getReturnType(attr);
if (fromStringAttributeInfo.getAttributeType().getDataType() == BINARY) {
return targetDataType + SPACE + TMP_VAL + SPACE + EQUAL + SPACE + FROM_STRING_PARAM_NAME
+ PERIOD + GET_BYTES + OPEN_PARENTHESIS + CLOSE_PARENTHESIS;
} else {
String parseFromStringMethod = getParseFromStringMethod(targetDataType,
fromStringAttributeInfo.getAttributeType());
return targetDataType + SPACE + TMP_VAL + SPACE + EQUAL + SPACE + parseFromStringMethod
+ OPEN_PARENTHESIS + FROM_STRING_PARAM_NAME + CLOSE_PARENTHESIS;
}
}
/**
* Returns hash code method open strings.
......@@ -1092,14 +1102,14 @@ public final class MethodsGenerator {
return LONG_WRAPPER + PERIOD + PARSE_LONG;
case UINT64:
return NEW + SPACE + BIG_INTEGER;
case DECIMAL64:
return NEW + SPACE + BIG_DECIMAL;
case STRING:
return EMPTY_STRING;
case EMPTY:
case BOOLEAN:
return BOOLEAN_WRAPPER + PERIOD + PARSE_BOOLEAN;
case DECIMAL64:
case BITS:
case BINARY:
case UNION:
case ENUMERATION:
case DERIVED:
......
......@@ -197,6 +197,11 @@ public final class UtilConstants {
public static final String PARSE_BYTE = "parseByte";
/**
* Static attribute for get bytes.
*/
public static final String GET_BYTES = "getBytes";
/**
* Static attribute for parse boolean.
*/
public static final String PARSE_BOOLEAN = "parseBoolean";
......@@ -567,6 +572,11 @@ public final class UtilConstants {
public static final String CLOSE_CURLY_BRACKET = "}";
/**
* Static attribute for square brackets syntax.
*/
public static final String SQUARE_BRACKETS = "[]";
/**
* Static attribute for getter method prefix.
*/
public static final String GET_METHOD_PREFIX = "get";
......@@ -687,6 +697,16 @@ public final class UtilConstants {
public static final String BIG_INTEGER = "BigInteger";
/**
* BigDecimal built in java type.
*/
public static final String BIG_DECIMAL = "BigDecimal";
/**
* BitSet built in java type.
*/
public static final String BIT_SET = "BitSet";
/**
* Byte java built in type.
*/
public static final String BYTE = "byte";
......@@ -747,6 +767,11 @@ public final class UtilConstants {
public static final String YANG_UINT64 = "YangUint64";
/**
* Double java built in wrapper type.
*/
public static final String DOUBLE_WRAPPER = "Double";
/**
* List of keywords in java, this is used for checking if the input does not contain these keywords.
*/
public static final List<String> JAVA_KEY_WORDS = Arrays.asList(
......@@ -1136,16 +1161,6 @@ public final class UtilConstants {
public static final String EVENT_LISTENER = "EventListener";
/**
* Static attribute for YangBinary class.
*/
public static final String YANG_BINARY_CLASS = "YangBinary";
/**
* Static attribute for YangBinary class.
*/
public static final String YANG_BITS_CLASS = "YangBits";
/**
* Static attribute for YANG types package.
*/
public static final String YANG_TYPES_PKG = "org.onosproject.yangutils.datamodel.utils.builtindatatype";
......@@ -1156,11 +1171,6 @@ public final class UtilConstants {
public static final String MATH_CONTEXT = "MathContext";
/**
* Static attribute for DECIMAL64 class.
*/
public static final String YANG_DECIMAL64_CLASS = "YangDecimal64";
/**
* Static attribute for YANG file error.
*/
public static final String YANG_FILE_ERROR = "YANG file error : ";
......
......@@ -375,6 +375,13 @@ package org.onosproject.yangutils.parser.antlrgencode;
| bitsSpecification | unionSpecification;
/**
* decimal64-specification = ;; these stmts can appear in any order
* fraction-digits-stmt
* [range-stmt]
*/
decimal64Specification : fractionDigitStatement rangeStatement?;
/**
* fraction-digits-stmt = fraction-digits-keyword sep
* fraction-digits-arg-str stmtend
*
......@@ -385,7 +392,7 @@ package org.onosproject.yangutils.parser.antlrgencode;
* "5" / "6" / "7" / "8"])
* / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9"
*/
decimal64Specification : FRACTION_DIGITS_KEYWORD fraction STMTEND;
fractionDigitStatement : FRACTION_DIGITS_KEYWORD fraction STMTEND;
/**
* numerical-restrictions = range-stmt stmtsep
......
......@@ -35,7 +35,7 @@ import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
import java.io.IOException;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import java.util.Map;
/**
* Test cases for bit listener.
......@@ -71,14 +71,38 @@ public class BitListenerTest {
assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
is("mybits"));
Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitSet();
for (YangBit tmp : bitSet) {
if (tmp.getBitName().equals("disable-nagle")) {
assertThat(tmp.getPosition(), is(0));
} else if (tmp.getBitName().equals("auto-sense-speed")) {
assertThat(tmp.getPosition(), is(1));
} else if (tmp.getBitName().equals("Ten-Mb-only")) {
assertThat(tmp.getPosition(), is(2));
// Check bit name map
Map<String, YangBit> bitNameMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitNameMap();
assertThat(bitNameMap.size(), is(3));
for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
String bitName = element.getKey();
YangBit yangBit = element.getValue();
if (bitName.equals("disable-nagle")) {
assertThat(yangBit.getPosition(), is(0));
} else if (bitName.equals("auto-sense-speed")) {
assertThat(yangBit.getPosition(), is(1));
} else if (bitName.equals("Ten-Mb-only")) {
assertThat(yangBit.getPosition(), is(2));
} else {
throw new IOException("Invalid bit name: " + bitName);
}
}
// Check bit position map
Map<Integer, YangBit> bitPositionMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo())
.getBitPositionMap();
assertThat(bitPositionMap.size(), is(3));
for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
int position = element.getKey();
YangBit yangBit = element.getValue();
if (position == 0) {
assertThat(yangBit.getBitName(), is("disable-nagle"));
} else if (position == 1) {
assertThat(yangBit.getBitName(), is("auto-sense-speed"));
} else if (position == 2) {
assertThat(yangBit.getBitName(), is("Ten-Mb-only"));
} else {
throw new IOException("Invalid bit position: " + position);
}
}
}
......@@ -107,12 +131,38 @@ public class BitListenerTest {
YangType type = typedef.getTypeList().iterator().next();
assertThat(type.getDataType(), is(YangDataTypes.BITS));
assertThat(type.getDataTypeName(), is("bits"));
Set<YangBit> bitSet = ((YangBits) type.getDataTypeExtendedInfo()).getBitSet();
for (YangBit tmp : bitSet) {
if (tmp.getBitName().equals("disable-nagle")) {
assertThat(tmp.getPosition(), is(0));
} else if (tmp.getBitName().equals("auto-sense-speed")) {
assertThat(tmp.getPosition(), is(1));
// Check bit name map
Map<String, YangBit> bitNameMap = ((YangBits) type.getDataTypeExtendedInfo()).getBitNameMap();
assertThat(bitNameMap.size(), is(3));
for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
String bitName = element.getKey();
YangBit yangBit = element.getValue();
if (bitName.equals("disable-nagle")) {
assertThat(yangBit.getPosition(), is(0));
} else if (bitName.equals("auto-sense-speed")) {
assertThat(yangBit.getPosition(), is(1));
} else if (bitName.equals("Mb-only")) {
assertThat(yangBit.getPosition(), is(2));
} else {
throw new IOException("Invalid bit name: " + bitName);
}
}
// Check bit position map
Map<Integer, YangBit> bitPositionMap = ((YangBits) type.getDataTypeExtendedInfo()).getBitPositionMap();
assertThat(bitPositionMap.size(), is(3));
for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
int position = element.getKey();
YangBit yangBit = element.getValue();
if (position == 0) {
assertThat(yangBit.getBitName(), is("disable-nagle"));
} else if (position == 1) {
assertThat(yangBit.getBitName(), is("auto-sense-speed"));
} else if (position == 2) {
assertThat(yangBit.getBitName(), is("Mb-only"));
} else {
throw new IOException("Invalid bit position: " + position);
}
}
}
......@@ -151,12 +201,38 @@ public class BitListenerTest {
assertThat(yangType.getDataType(), is(YangDataTypes.BITS));
assertThat(yangType.getDataTypeName(), is("bits"));
Set<YangBit> bitSet = ((YangBits) yangType.getDataTypeExtendedInfo()).getBitSet();
for (YangBit tmp : bitSet) {
if (tmp.getBitName().equals("disable-nagle")) {
assertThat(tmp.getPosition(), is(0));
} else if (tmp.getBitName().equals("auto-sense-speed")) {
assertThat(tmp.getPosition(), is(1));
// Check bit name map
Map<String, YangBit> bitNameMap = ((YangBits) yangType.getDataTypeExtendedInfo()).getBitNameMap();
assertThat(bitNameMap.size(), is(3));
for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
String bitName = element.getKey();
YangBit yangBit = element.getValue();
if (bitName.equals("disable-nagle")) {
assertThat(yangBit.getPosition(), is(0));
} else if (bitName.equals("auto-sense-speed")) {
assertThat(yangBit.getPosition(), is(1));
} else if (bitName.equals("Mb-only")) {
assertThat(yangBit.getPosition(), is(2));
} else {
throw new IOException("Invalid bit name: " + bitName);
}
}
// Check bit position map
Map<Integer, YangBit> bitPositionMap = ((YangBits) yangType.getDataTypeExtendedInfo()).getBitPositionMap();
assertThat(bitPositionMap.size(), is(3));
for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
int position = element.getKey();
YangBit yangBit = element.getValue();
if (position == 0) {
assertThat(yangBit.getBitName(), is("disable-nagle"));
} else if (position == 1) {
assertThat(yangBit.getBitName(), is("auto-sense-speed"));
} else if (position == 2) {
assertThat(yangBit.getBitName(), is("Mb-only"));
} else {
throw new IOException("Invalid bit position: " + position);
}
}
}
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* 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.
*/
package org.onosproject.yangutils.parser.impl.listeners;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.onosproject.yangutils.datamodel.YangDecimal64;
import org.onosproject.yangutils.datamodel.YangDerivedInfo;
import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangModule;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangNodeType;
import org.onosproject.yangutils.datamodel.YangRangeRestriction;
import org.onosproject.yangutils.datamodel.YangRangeInterval;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ListIterator;
/**
* Test cases for decimal64 listener.
*/
public class Decimal64ListenerTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
private final YangUtilsParserManager manager = new YangUtilsParserManager();
/**
* Checks decimal64 statement with fraction-digits.
*/
@Test
public void processDecimal64TypeStatement() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/Decimal64TypeStatement.yang");
// Check whether the data model tree returned is of type module.
assertThat((node instanceof YangModule), is(true));
// Check whether the node type is set properly to module.
assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
// Check whether the module name is set correctly.
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
YangLeaf leafInfo = leafIterator.next();
assertThat(leafInfo.getName(), is("validDecimal"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64));
assertThat(((YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo()).getFractionDigit(),
is(2));
}
/**
* Checks decimal64 statement with range statement.
*/
@Test
public void processDecimal64TypeWithRangeStatement() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/Decimal64TypeWithRangeStatement.yang");
// Check whether the data model tree returned is of type module.
assertThat((node instanceof YangModule), is(true));
// Check whether the node type is set properly to module.
assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
// Check whether the module name is set correctly.
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
YangLeaf leafInfo = leafIterator.next();
assertThat(leafInfo.getName(), is("validDecimal"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64));
assertThat(((YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo()).getFractionDigit(),
is(8));
YangRangeRestriction rangeRestriction = ((YangDecimal64<YangRangeRestriction>) leafInfo.getDataType()
.getDataTypeExtendedInfo())
.getRangeRestrictedExtendedInfo();
ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
.listIterator();
YangRangeInterval rangeInterval = rangeListIterator.next();
assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(-92233720368.54775808));
assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(92233720368.54775807));
}
/**
* Successful validation of decimal64 statement.
*/
@Test
public void processDecimal64ValueSuccessfulValidation() throws IOException, ParserException, DataModelException {
YangNode node = manager.getDataModel("src/test/resources/Decimal64TypeValidation.yang");
// Check whether the data model tree returned is of type module.
assertThat((node instanceof YangModule), is(true));
// Check whether the node type is set properly to module.
assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
// Check whether the module name is set correctly.
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
YangLeaf leafInfo = leafIterator.next();
YangDecimal64 decimal64 = (YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo();
assertThat(leafInfo.getName(), is("validDecimal"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64));
assertThat(decimal64.getFractionDigit(), is(18));
decimal64.setValue(new BigDecimal(-9.223372036854775808));
decimal64.validateValue();
decimal64.setValue(new BigDecimal(9.223372036854775807));
decimal64.validateValue();
}
/**
* Failure validation of decimal64 statement.
*/
@Test
public void processDecimal64ValueFailureValidation() throws IOException, ParserException, DataModelException {
thrown.expect(DataModelException.class);
thrown.expectMessage("YANG file error : decimal64 validation failed.");
YangNode node = manager.getDataModel("src/test/resources/Decimal64TypeValidation.yang");
// Check whether the data model tree returned is of type module.
assertThat((node instanceof YangModule), is(true));
// Check whether the node type is set properly to module.
assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
// Check whether the module name is set correctly.
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
YangLeaf leafInfo = leafIterator.next();
YangDecimal64 decimal64 = (YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo();
assertThat(leafInfo.getName(), is("validDecimal"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64));
assertThat(decimal64.getFractionDigit(), is(18));
decimal64.setValue(new BigDecimal(-92233720368547758.08));
// validation should fail
decimal64.validateValue();
}
/**
* Validation of invalid maximum value limit of fraction-digits.
*/
@Test
public void processDecimal64InvalidMaxFraction() throws IOException, ParserException, DataModelException {
thrown.expect(ParserException.class);
thrown.expectMessage("YANG file error : fraction-digits value should be between 1 and 18.");
manager.getDataModel("src/test/resources/Decimal64TypeInvalidMaxValueFraction.yang");
}
/**
* Validation of invalid (0) minimum value limit of fraction-digits.
*/
@Test
public void processDecimal64InvalidMinFraction1() throws IOException, ParserException, DataModelException {
thrown.expect(ParserException.class);
thrown.expectMessage("YANG file error : fraction-digits value should be between 1 and 18.");
manager.getDataModel("src/test/resources/Decimal64TypeInvalidMinValueFraction1.yang");
}
/**
* Validation of invalid (-1) minimum value limit of fraction-digits.
*/
@Test
public void processDecimal64InvalidMinFraction2() throws IOException, ParserException, DataModelException {
thrown.expect(ParserException.class);
thrown.expectMessage("YANG file error : fraction-digits value should be between 1 and 18.");
manager.getDataModel("src/test/resources/Decimal64TypeInvalidMinValueFraction2.yang");
}
/**
* Validation of decimal64 range statement.
*/
@Test
public void processDecimal64TypeWithMultiValueRangeStatement() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/Decimal64TypeWithMultiValueRangeStmnt.yang");
// Check whether the data model tree returned is of type module.
assertThat((node instanceof YangModule), is(true));
// Check whether the node type is set properly to module.
assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
// Check whether the module name is set correctly.
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
YangLeaf leafInfo = leafIterator.next();
assertThat(leafInfo.getName(), is("validDecimal"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64));
assertThat(((YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo()).getFractionDigit(),
is(18));
YangRangeRestriction rangeRestriction = ((YangDecimal64<YangRangeRestriction>) leafInfo.getDataType()
.getDataTypeExtendedInfo())
.getRangeRestrictedExtendedInfo();
ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
.listIterator();
// range "1 .. 3.14 | 10 | 20..max";
// check first range 1 .. 3.14
YangRangeInterval rangeInterval = rangeListIterator.next();
assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(-9.22));
assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(7.22));
// check second range 10
rangeInterval = rangeListIterator.next();
assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(8.0));
assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(8.0));
// check third range 20..max
rangeInterval = rangeListIterator.next();
assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(9.0));
assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(9.223372036854776));
}
/**
* Validation of decimal64 with invalid range.
*/
@Test
public void processDecimal64InvalidRange() throws IOException, ParserException, DataModelException {
thrown.expect(ParserException.class);
thrown.expectMessage("YANG file error : decimal64 validation failed.");
manager.getDataModel("src/test/resources/Decimal64TypeInvalidRangeStmnt.yang");
}
/**
* Validation of decimal64 without fraction-digits. Fraction-digits must be present for decimal64.
*/
@Test
public void processDecimal64WithoutFraction() throws IOException, ParserException, DataModelException {
thrown.expect(ParserException.class);
thrown.expectMessage("YANG file error : a type decimal64 must have fraction-digits statement.");
manager.getDataModel("src/test/resources/Decimal64TypeWithoutFraction.yang");
}
/**
* Checks decimal64 with typedef statement.
*/
@Test
public void processDecimal64TypedefStatement() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/Decimal64TypedefStatement.yang");
// Check whether the data model tree returned is of type module.
assertThat((node instanceof YangModule), is(true));
// Check whether the node type is set properly to module.
assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
// Check whether the module name is set correctly.
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
YangLeaf leafInfo = leafIterator.next();
assertThat(leafInfo.getName(), is("setFourDecimal"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("validDecimal"));
YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
YangTypeDef typedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
assertThat(typedef.getName(), is("validDecimal"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DERIVED));
YangType type = typedef.getTypeList().iterator().next();
assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
assertThat(type.getDataTypeName(), is("decimal64"));
YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) typedef.getTypeList().iterator().next();
YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
assertThat(decimal64.getFractionDigit(), is(4));
}
/**
* Checks decimal64 with multiple typedef statement.
*/
@Test
public void processDecimal64MultiTypedefStatement() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/Decimal64MultiTypedefStatement.yang");
// Check whether the data model tree returned is of type module.
assertThat((node instanceof YangModule), is(true));
// Check whether the node type is set properly to module.
assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
// Check whether the module name is set correctly.
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
YangLeaf leafInfo = leafIterator.next();
// check leaf type
assertThat(leafInfo.getName(), is("setFourDecimal"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("validDecimal"));
YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
assertThat(derivedInfoType.getDataType(), is(YangDataTypes.DERIVED));
YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
// check previous typedef
YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
assertThat(prevTypedef.getName(), is("validDecimal"));
YangType type = prevTypedef.getTypeList().iterator().next();
assertThat(type.getDataType(), is(YangDataTypes.DERIVED));
derivedInfo = (YangDerivedInfo) type.getDataTypeExtendedInfo();
// check top typedef
YangTypeDef topTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
assertThat(topTypedef.getName(), is("topDecimal"));
type = topTypedef.getTypeList().iterator().next();
assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
assertThat(type.getDataTypeName(), is("decimal64"));
YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) type;
YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
assertThat(decimal64.getFractionDigit(), is(4));
}
}
......@@ -138,6 +138,37 @@ public class LengthRestrictionListenerTest {
}
/**
* Checks valid length statement as sub-statement of binary statement.
*/
@Test
public void processValidBinaryLengthStatement() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/ValidBinaryLengthStatement.yang");
assertThat((node instanceof YangModule), is(true));
assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
YangLeaf leafInfo = leafIterator.next();
assertThat(leafInfo.getName(), is("message"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("binary"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BINARY));
YangRangeRestriction lengthRestriction = (YangRangeRestriction) leafInfo
.getDataType().getDataTypeExtendedInfo();
ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals()
.listIterator();
YangRangeInterval rangeInterval = lengthListIterator.next();
assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(4)));
assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(4)));
}
/**
* Checks length statement with invalid type.
*/
@Test
......
......@@ -31,7 +31,7 @@ import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
import java.io.IOException;
import java.util.ListIterator;
import java.util.Set;
import java.util.Map;
/**
* Test cases for position listener.
......@@ -67,14 +67,38 @@ public class PositionListenerTest {
assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
is("mybits"));
Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitSet();
for (YangBit tmp : bitSet) {
if (tmp.getBitName().equals("disable-nagle")) {
assertThat(tmp.getPosition(), is(0));
} else if (tmp.getBitName().equals("auto-sense-speed")) {
assertThat(tmp.getPosition(), is(1));
} else if (tmp.getBitName().equals("Ten-Mb-only")) {
assertThat(tmp.getPosition(), is(2));
// Check bit name map
Map<String, YangBit> bitNameMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitNameMap();
assertThat(bitNameMap.size(), is(3));
for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
String bitName = element.getKey();
YangBit yangBit = element.getValue();
if (bitName.equals("disable-nagle")) {
assertThat(yangBit.getPosition(), is(0));
} else if (bitName.equals("auto-sense-speed")) {
assertThat(yangBit.getPosition(), is(1));
} else if (bitName.equals("Ten-Mb-only")) {
assertThat(yangBit.getPosition(), is(2));
} else {
throw new IOException("Invalid bit name: " + bitName);
}
}
// Check bit position map
Map<Integer, YangBit> bitPositionMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo())
.getBitPositionMap();
assertThat(bitPositionMap.size(), is(3));
for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
int position = element.getKey();
YangBit yangBit = element.getValue();
if (position == 0) {
assertThat(yangBit.getBitName(), is("disable-nagle"));
} else if (position == 1) {
assertThat(yangBit.getBitName(), is("auto-sense-speed"));
} else if (position == 2) {
assertThat(yangBit.getBitName(), is("Ten-Mb-only"));
} else {
throw new IOException("Invalid bit position: " + position);
}
}
}
......@@ -106,14 +130,38 @@ public class PositionListenerTest {
assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
is("mybits"));
Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitSet();
for (YangBit tmp : bitSet) {
if (tmp.getBitName().equals("disable-nagle")) {
assertThat(tmp.getPosition(), is(0));
} else if (tmp.getBitName().equals("auto-sense-speed")) {
assertThat(tmp.getPosition(), is(1));
} else if (tmp.getBitName().equals("Ten-Mb-only")) {
assertThat(tmp.getPosition(), is(2));
// Check bit name map
Map<String, YangBit> bitNameMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitNameMap();
assertThat(bitNameMap.size(), is(3));
for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
String bitName = element.getKey();
YangBit yangBit = element.getValue();
if (bitName.equals("disable-nagle")) {
assertThat(yangBit.getPosition(), is(0));
} else if (bitName.equals("auto-sense-speed")) {
assertThat(yangBit.getPosition(), is(1));
} else if (bitName.equals("Ten-Mb-only")) {
assertThat(yangBit.getPosition(), is(2));
} else {
throw new IOException("Invalid bit name: " + bitName);
}
}
// Check bit position map
Map<Integer, YangBit> bitPositionMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo())
.getBitPositionMap();
assertThat(bitPositionMap.size(), is(3));
for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
int position = element.getKey();
YangBit yangBit = element.getValue();
if (position == 0) {
assertThat(yangBit.getBitName(), is("disable-nagle"));
} else if (position == 1) {
assertThat(yangBit.getBitName(), is("auto-sense-speed"));
} else if (position == 2) {
assertThat(yangBit.getBitName(), is("Ten-Mb-only"));
} else {
throw new IOException("Invalid bit position: " + position);
}
}
}
......@@ -145,14 +193,38 @@ public class PositionListenerTest {
assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
is("mybits"));
Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitSet();
for (YangBit tmp : bitSet) {
if (tmp.getBitName().equals("disable-nagle")) {
assertThat(tmp.getPosition(), is(0));
} else if (tmp.getBitName().equals("auto-sense-speed")) {
assertThat(tmp.getPosition(), is(1));
} else if (tmp.getBitName().equals("Ten-Mb-only")) {
assertThat(tmp.getPosition(), is(2));
// Check bit name map
Map<String, YangBit> bitNameMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitNameMap();
assertThat(bitNameMap.size(), is(3));
for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
String bitName = element.getKey();
YangBit yangBit = element.getValue();
if (bitName.equals("disable-nagle")) {
assertThat(yangBit.getPosition(), is(0));
} else if (bitName.equals("auto-sense-speed")) {
assertThat(yangBit.getPosition(), is(1));
} else if (bitName.equals("Ten-Mb-only")) {
assertThat(yangBit.getPosition(), is(2));
} else {
throw new IOException("Invalid bit name: " + bitName);
}
}
// Check bit position map
Map<Integer, YangBit> bitPositionMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo())
.getBitPositionMap();
assertThat(bitPositionMap.size(), is(3));
for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
int position = element.getKey();
YangBit yangBit = element.getValue();
if (position == 0) {
assertThat(yangBit.getBitName(), is("disable-nagle"));
} else if (position == 1) {
assertThat(yangBit.getBitName(), is("auto-sense-speed"));
} else if (position == 2) {
assertThat(yangBit.getBitName(), is("Ten-Mb-only"));
} else {
throw new IOException("Invalid bit position: " + position);
}
}
}
......
......@@ -26,7 +26,9 @@ module Test {
"Interface has time-division multiplex capabilities.";
leaf minimum-lsp-bandwidth {
type decimal64;
type decimal64 {
fraction-digits 4;
}
}
}
}
......
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
typedef topDecimal {
type decimal64 {
fraction-digits 4;
}
}
typedef validDecimal {
type topDecimal;
}
leaf setFourDecimal {
type validDecimal;
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf invalidDecimal1 {
type decimal64 {
fraction-digits 19;
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf invalidDecimal2 {
type decimal64 {
fraction-digits 0;
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf invalidDecimal3 {
type decimal64 {
fraction-digits -1;
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf validDecimal {
type decimal64 {
fraction-digits 18;
range "1 .. 20.14";
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf validDecimal {
type decimal64 {
fraction-digits 2;
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf validDecimal {
type decimal64 {
fraction-digits 18;
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf validDecimal {
type decimal64 {
fraction-digits 18;
range "-9.22..7.22 | 8 | 9..max";
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf validDecimal {
type decimal64 {
fraction-digits 8;
range "-92233720368.54775808 .. 92233720368.54775807";
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf validDecimal {
type decimal64;
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
typedef validDecimal {
type decimal64 {
fraction-digits 4;
}
}
leaf setFourDecimal {
type validDecimal;
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf message {
type binary {
length "4";
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf message {
type binary {
length "4";
}
}
}
......@@ -430,8 +430,8 @@
}
leaf unidirectional-packet-loss {
type decimal64 {
/*fraction-digits 6;
range "0 .. 50.331642";*/
fraction-digits 6;
range "0 .. 50.331642";
}
description
"Packet loss as a percentage of the total traffic sent
......@@ -440,7 +440,7 @@
}
leaf unidirectional-residual-bandwidth {
type decimal64 {
/*fraction-digits 2;*/
fraction-digits 2;
}
description
"Residual bandwidth that subtracts tunnel
......@@ -450,7 +450,7 @@
}
leaf unidirectional-available-bandwidth {
type decimal64 {
/*fraction-digits 2;*/
fraction-digits 2;
}
description
"Available bandwidth that is defined to be residual
......@@ -461,7 +461,7 @@
}
leaf unidirectional-utilized-bandwidth {
type decimal64 {
/*fraction-digits 2;*/
fraction-digits 2;
}
description
"Bandwidth utilization that represents the actual
......@@ -753,7 +753,7 @@
}
leaf max-link-bandwidth {
type decimal64 {
/*fraction-digits 2;*/
fraction-digits 2;
}
description
"Maximum bandwidth that can be seen on this link in this
......@@ -765,7 +765,7 @@
}
leaf max-resv-link-bandwidth {
type decimal64 {
/*fraction-digits 2;*/
fraction-digits 2;
}
description
"Maximum amount of bandwidth that can be reserved in this
......@@ -793,7 +793,7 @@
}
leaf bandwidth {
type decimal64 {
/*fraction-digits 2;*/
fraction-digits 2;
}
description
"Unreserved bandwidth for this level.";
......@@ -884,7 +884,7 @@
}
leaf bandwidth {
type decimal64 {
/*fraction-digits 2;*/
fraction-digits 2;
}
description
"Max LSP Bandwidth for this level";
......@@ -899,7 +899,7 @@
leaf minimum-lsp-bandwidth {
type decimal64 {
/*fraction-digits 2;*/
fraction-digits 2;
}
description
"Minimum LSP Bandwidth. Units in bytes per second.";
......@@ -951,7 +951,7 @@
}
leaf bandwidth {
type decimal64 {
/*fraction-digits 2;*/
fraction-digits 2;
}
description
"Max LSP Bandwidth for this level.";
......
......@@ -580,7 +580,7 @@
typedef admin-group {
type binary {
// length 32;
length 32;
}
description
"Administrative group/Resource class/Color.";
......