Vidyashree Rama
Committed by Gerrit Code Review

YANG string restriction listener + YANG Range restriction listener review comment fix

Change-Id: I9e7af9c67f8fbf918d4e0d8ff147a560889ff264
Showing 40 changed files with 1860 additions and 99 deletions
......@@ -13,12 +13,14 @@ See the License for the specific language governing permissions and
limitations under the License.*/
package org.onosproject.yangutils.datamodel;
import org.onosproject.yangutils.utils.builtindatatype.YangBuiltInDataTypeInfo;
/**
* Represents single interval information of a range.
*
* @param <T> range type based on the data type.
* @param <T> range type based on the data type
*/
public class YangRangeInterval<T extends Comparable<T>> {
public class YangRangeInterval<T extends YangBuiltInDataTypeInfo<T>> {
/**
* Starting value of the range interval.
......@@ -39,7 +41,7 @@ public class YangRangeInterval<T extends Comparable<T>> {
/**
* Returns the starting value of the range interval.
*
* @return the starting value of the range interval.
* @return the starting value of the range interval
*/
public T getStartValue() {
return startValue;
......@@ -48,7 +50,7 @@ public class YangRangeInterval<T extends Comparable<T>> {
/**
* Sets the starting value of the range interval.
*
* @param startValue the starting value of the range interval.
* @param startValue the starting value of the range interval
*/
public void setStartValue(T startValue) {
this.startValue = startValue;
......@@ -57,7 +59,7 @@ public class YangRangeInterval<T extends Comparable<T>> {
/**
* Returns the last value of the range interval.
*
* @return last value of the range interval.
* @return last value of the range interval
*/
public T getEndValue() {
return endValue;
......@@ -66,7 +68,7 @@ public class YangRangeInterval<T extends Comparable<T>> {
/**
* Sets the last value of the range interval.
*
* @param endValue last value of the range interval.
* @param endValue last value of the range interval
*/
public void setEndValue(T endValue) {
this.endValue = endValue;
......
......@@ -20,7 +20,9 @@ import java.util.LinkedList;
import java.util.List;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.utils.builtindatatype.YangBuiltInDataTypeInfo;
import static org.onosproject.yangutils.utils.builtindatatype.BuiltInTypeObjectFactory.getDataObjectFromString;
import static com.google.common.base.Preconditions.checkNotNull;
/*-
......@@ -47,12 +49,14 @@ import static com.google.common.base.Preconditions.checkNotNull;
* "max". "min" and "max" mean the minimum and maximum value accepted
* for the type being restricted, respectively.
*/
/**
* Represents ascending range restriction information.
*
* @param <T> range type (data type)
*/
public class YangRangeRestriction<T extends Comparable<T>> implements YangDesc, YangReference, YangAppErrorInfo {
public class YangRangeRestriction<T extends YangBuiltInDataTypeInfo<T>>
implements YangDesc, YangReference, YangAppErrorInfo {
/**
* Ascending list of range interval restriction. If the restriction is a
......@@ -171,6 +175,37 @@ public class YangRangeRestriction<T extends Comparable<T>> implements YangDesc,
}
/**
* Check if the given value is correct as per the restriction.
*
* @param valueInString value
* @return true, if the value is confirming to restriction, false otherwise
* @throws DataModelException data model error
*/
public boolean isValidValueString(String valueInString) throws DataModelException {
if (getAscendingRangeIntervals() == null
|| getAscendingRangeIntervals().size() == 0) {
// Throw exception, At least one default range needs to be set in constructor or in linker.
throw new DataModelException("Range interval missing in range restriction.");
}
YangDataTypes type = getAscendingRangeIntervals().get(0).getStartValue().getYangType();
YangBuiltInDataTypeInfo<?> value = getDataObjectFromString(valueInString, type);
for (YangRangeInterval<T> interval : getAscendingRangeIntervals()) {
int rangeStartCompareRes = interval.getStartValue().compareTo((T) value);
int rangeEndCompareRes = interval.getEndValue().compareTo((T) value);
if (rangeStartCompareRes <= 0 && rangeEndCompareRes >= 0) {
return true;
}
}
return false;
}
/**
* Returns the textual reference of the length restriction.
*
* @return textual reference of the length restriction
......
......@@ -16,7 +16,7 @@
package org.onosproject.yangutils.datamodel;
import java.math.BigInteger;
import org.onosproject.yangutils.utils.builtindatatype.YangUint64;
/*-
* Reference RFC 6020.
......@@ -64,10 +64,11 @@ public class YangStringRestriction {
* | reference | 7.19.4 | 0..1 | string |
* +---------------+---------+-------------+-----------------+
*/
/**
* Length restriction information.
*/
private YangRangeRestriction<BigInteger> lengthRestriction;
private YangRangeRestriction<YangUint64> lengthRestriction;
/**
* Effective pattern restriction for the type.
......@@ -85,7 +86,7 @@ public class YangStringRestriction {
*
* @return length restriction on the string data
*/
public YangRangeRestriction<BigInteger> getLengthRestriction() {
public YangRangeRestriction<YangUint64> getLengthRestriction() {
return lengthRestriction;
}
......@@ -94,7 +95,7 @@ public class YangStringRestriction {
*
* @param lengthRestriction length restriction on the string data
*/
public void setLengthRestriction(YangRangeRestriction<BigInteger> lengthRestriction) {
public void setLengthRestriction(YangRangeRestriction<YangUint64> lengthRestriction) {
this.lengthRestriction = lengthRestriction;
}
......
......@@ -46,6 +46,7 @@ import org.onosproject.yangutils.parser.impl.listeners.InputListener;
import org.onosproject.yangutils.parser.impl.listeners.KeyListener;
import org.onosproject.yangutils.parser.impl.listeners.LeafListListener;
import org.onosproject.yangutils.parser.impl.listeners.LeafListener;
import org.onosproject.yangutils.parser.impl.listeners.LengthRestrictionListener;
import org.onosproject.yangutils.parser.impl.listeners.ListListener;
import org.onosproject.yangutils.parser.impl.listeners.MandatoryListener;
import org.onosproject.yangutils.parser.impl.listeners.MaxElementsListener;
......@@ -55,6 +56,7 @@ import org.onosproject.yangutils.parser.impl.listeners.NotificationListener;
import org.onosproject.yangutils.parser.impl.listeners.NamespaceListener;
import org.onosproject.yangutils.parser.impl.listeners.OrganizationListener;
import org.onosproject.yangutils.parser.impl.listeners.OutputListener;
import org.onosproject.yangutils.parser.impl.listeners.PatternRestrictionListener;
import org.onosproject.yangutils.parser.impl.listeners.PositionListener;
import org.onosproject.yangutils.parser.impl.listeners.PrefixListener;
import org.onosproject.yangutils.parser.impl.listeners.PresenceListener;
......@@ -586,7 +588,7 @@ public class TreeWalkListener implements GeneratedYangListener {
@Override
public void enterLengthStatement(GeneratedYangParser.LengthStatementContext ctx) {
// TODO: implement the method.
LengthRestrictionListener.processLengthRestrictionEntry(this, ctx);
}
@Override
......@@ -596,7 +598,7 @@ public class TreeWalkListener implements GeneratedYangListener {
@Override
public void enterPatternStatement(GeneratedYangParser.PatternStatementContext ctx) {
// TODO: implement the method.
PatternRestrictionListener.processPatternRestrictionEntry(this, ctx);
}
@Override
......
/*
* Copyright 2016 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 java.util.regex.Pattern;
import org.onosproject.yangutils.datamodel.YangRangeRestriction;
import org.onosproject.yangutils.datamodel.YangRangeInterval;
import org.onosproject.yangutils.datamodel.YangStringRestriction;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangDerivedInfo;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import org.onosproject.yangutils.utils.YangConstructType;
import org.onosproject.yangutils.utils.builtindatatype.DataTypeException;
import org.onosproject.yangutils.utils.builtindatatype.YangBuiltInDataTypeInfo;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
import static org.onosproject.yangutils.utils.YangConstructType.LENGTH_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
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.ListenerErrorType.UNHANDLED_PARSED_DATA;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.utils.builtindatatype.BuiltInTypeObjectFactory.getDataObjectFromString;
/*
* Reference: RFC6020 and YANG ANTLR Grammar
*
* ABNF grammar as per RFC6020
* length-stmt = length-keyword sep length-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
* lengthStatement : LENGTH_KEYWORD length
* (STMTEND | LEFT_CURLY_BRACE commonStatements RIGHT_CURLY_BRACE);
*/
/**
* Represents listener based call back function corresponding to the "length"
* rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
*/
public final class LengthRestrictionListener {
private static final String PIPE = "|";
private static final String LENGTH_INTERVAL = "..";
/**
* Creates a new length restriction listener.
*/
private LengthRestrictionListener() {
}
/**
* It is called when parser receives an input matching the grammar
* rule (length), performs validation and updates the data model
* tree.
*
* @param listener listener's object
* @param ctx context object of the grammar rule
*/
public static void processLengthRestrictionEntry(TreeWalkListener listener,
GeneratedYangParser.LengthStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, LENGTH_DATA, ctx.length().getText(), ENTRY);
Parsable tmpData = listener.getParsedDataStack().peek();
if (tmpData.getYangConstructType() == TYPE_DATA) {
YangType type = (YangType) tmpData;
setLengthRestriction(type, ctx);
} else {
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LENGTH_DATA,
ctx.length().getText(), ENTRY));
}
}
/**
* Sets the length restriction to type.
*
* @param type Yang type for which length restriction to be set
* @param ctx context object of the grammar rule
*/
private static void setLengthRestriction(YangType type,
GeneratedYangParser.LengthStatementContext ctx) {
YangStringRestriction stringRestriction;
YangBuiltInDataTypeInfo<?> startValue, endValue;
YangRangeRestriction lengthRestriction = new YangRangeRestriction<>();
if (type.getDataType() != YangDataTypes.STRING && type.getDataType() != YangDataTypes.DERIVED) {
ParserException parserException = new ParserException("YANG file error : " +
YangConstructType.getYangConstructType(LENGTH_DATA) + " name " + ctx.length().getText() +
" can be used to restrict the built-in type string or types derived from string.");
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
}
if (type.getDataType() == YangDataTypes.STRING) {
stringRestriction = (YangStringRestriction) type.getDataTypeExtendedInfo();
} else {
stringRestriction = (YangStringRestriction) ((YangDerivedInfo<?>) type
.getDataTypeExtendedInfo()).getExtendedInfo();
}
if (stringRestriction == null) {
stringRestriction = new YangStringRestriction();
if (type.getDataType() == YangDataTypes.STRING) {
type.setDataTypeExtendedInfo(stringRestriction);
} else {
((YangDerivedInfo<YangStringRestriction>) type.getDataTypeExtendedInfo())
.setExtendedInfo(stringRestriction);
}
}
String rangeArgument = removeQuotesAndHandleConcat(ctx.length().getText());
String[] rangeArguments = rangeArgument.trim().split(Pattern.quote(PIPE));
for (String rangePart : rangeArguments) {
String startInterval, endInterval;
YangRangeInterval rangeInterval = new YangRangeInterval<>();
String[] rangeBoundary = rangePart.trim().split(Pattern.quote(LENGTH_INTERVAL));
if (rangeBoundary.length > 2) {
ParserException parserException = new ParserException("YANG file error : " +
YangConstructType.getYangConstructType(LENGTH_DATA) + " " + rangeArgument +
" is not valid.");
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
}
if (rangeBoundary.length == 1) {
startInterval = rangeBoundary[0];
endInterval = rangeBoundary[0];
} else {
startInterval = rangeBoundary[0];
endInterval = rangeBoundary[1];
}
try {
startValue = getDataObjectFromString(startInterval, YangDataTypes.UINT64);
endValue = getDataObjectFromString(endInterval, YangDataTypes.UINT64);
} catch (DataTypeException e) {
ParserException parserException = new ParserException(e.getMessage());
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
}
rangeInterval.setStartValue(startValue);
rangeInterval.setEndValue(endValue);
try {
lengthRestriction.addRangeRestrictionInterval(rangeInterval);
} catch (DataModelException e) {
ParserException parserException = new ParserException(constructExtendedListenerErrorMessage(
UNHANDLED_PARSED_DATA, LENGTH_DATA, rangeArgument, ENTRY, e.getMessage()));
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
}
}
stringRestriction.setLengthRestriction(lengthRestriction);
}
}
/*
* Copyright 2016 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 org.onosproject.yangutils.datamodel.YangDerivedInfo;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangStringRestriction;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import org.onosproject.yangutils.utils.YangConstructType;
import static org.onosproject.yangutils.utils.YangConstructType.PATTERN_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.TYPE_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;
/*
* Reference: RFC6020 and YANG ANTLR Grammar
*
* ABNF grammar as per RFC6020
* pattern-stmt = pattern-keyword sep string 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
* patternStatement : PATTERN_KEYWORD string (STMTEND | LEFT_CURLY_BRACE commonStatements RIGHT_CURLY_BRACE);
*/
/**
* Represents listener based call back function corresponding to the "pattern"
* rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
*/
public final class PatternRestrictionListener {
private static final String EMPTY_STRING = "";
/**
* Creates a new pattern restriction listener.
*/
private PatternRestrictionListener() {
}
/**
* It is called when parser receives an input matching the grammar
* rule (pattern), performs validation and updates the data model
* tree.
*
* @param listener listener's object
* @param ctx context object of the grammar rule
*/
public static void processPatternRestrictionEntry(TreeWalkListener listener,
GeneratedYangParser.PatternStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, PATTERN_DATA, ctx.string().getText(), ENTRY);
Parsable tmpData = listener.getParsedDataStack().peek();
if (tmpData.getYangConstructType() == TYPE_DATA) {
YangType type = (YangType) tmpData;
setPatternRestriction(type, ctx);
} else {
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, PATTERN_DATA,
ctx.string().getText(), ENTRY));
}
}
/**
* Sets the pattern restriction to type.
*
* @param type Yang type for which pattern restriction to be set
* @param ctx context object of the grammar rule
*/
private static void setPatternRestriction(YangType type,
GeneratedYangParser.PatternStatementContext ctx) {
YangStringRestriction stringRestriction;
if (type.getDataType() != YangDataTypes.STRING && type.getDataType() != YangDataTypes.DERIVED) {
ParserException parserException = new ParserException("YANG file error : " +
YangConstructType.getYangConstructType(PATTERN_DATA) + " name " + ctx.string().getText() +
" can be used to restrict the built-in type string or types derived from string.");
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
}
if (type.getDataType() == YangDataTypes.STRING) {
stringRestriction = (YangStringRestriction) type.getDataTypeExtendedInfo();
} else {
stringRestriction = (YangStringRestriction) ((YangDerivedInfo<?>) type
.getDataTypeExtendedInfo()).getExtendedInfo();
}
if (stringRestriction == null) {
stringRestriction = new YangStringRestriction();
if (type.getDataType() == YangDataTypes.STRING) {
type.setDataTypeExtendedInfo(stringRestriction);
} else {
((YangDerivedInfo<YangStringRestriction>) type.getDataTypeExtendedInfo())
.setExtendedInfo(stringRestriction);
}
}
String patternArgument = ctx.string().getText().replace("\"", EMPTY_STRING);
stringRestriction.addPattern(patternArgument);
}
}
......@@ -16,29 +16,33 @@
package org.onosproject.yangutils.parser.impl.listeners;
import java.math.BigInteger;
import java.util.regex.Pattern;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangRangeRestriction;
import org.onosproject.yangutils.datamodel.YangRangeInterval;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangRangeRestriction;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangDerivedInfo;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import org.onosproject.yangutils.utils.YangConstructType;
import org.onosproject.yangutils.utils.builtindatatype.DataTypeException;
import org.onosproject.yangutils.utils.builtindatatype.YangBuiltInDataTypeInfo;
import static org.onosproject.yangutils.utils.YangConstructType.RANGE_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
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.ListenerErrorType.UNHANDLED_PARSED_DATA;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.RANGE_DATA;
import static org.onosproject.yangutils.utils.builtindatatype.BuiltInTypeObjectFactory.getDataObjectFromString;
/*
* Reference: RFC6020 and YANG ANTLR Grammar
......@@ -78,8 +82,8 @@ public final class RangeRestrictionListener {
* rule (range), performs validation and updates the data model
* tree.
*
* @param listener listener's object.
* @param ctx context object of the grammar rule.
* @param listener listener's object
* @param ctx context object of the grammar rule
*/
public static void processRangeRestrictionEntry(TreeWalkListener listener,
GeneratedYangParser.RangeStatementContext ctx) {
......@@ -100,69 +104,72 @@ public final class RangeRestrictionListener {
/**
* Sets the range restriction to type.
*
* @param type YANG type for which range restriction to be added.
* @param ctx context object of the grammar rule.
* @param type YANG type for which range restriction to be added
* @param ctx context object of the grammar rule
*/
private static void setRangeRestriction(YangType type,
GeneratedYangParser.RangeStatementContext ctx) {
YangRangeRestriction<?> rangeRestriction = null;
YangRangeInterval rangeInterval;
YangBuiltInDataTypeInfo<?> startValue, endValue;
YangRangeRestriction rangeRestriction = new YangRangeRestriction();
String rangeArgument = removeQuotesAndHandleConcat(ctx.range().getText());
String[] rangeArguments = rangeArgument.trim().split(Pattern.quote(PIPE));
for (String rangePart : rangeArguments) {
String startInterval, endInterval;
YangRangeInterval rangeInterval = new YangRangeInterval();
String[] rangeBoundary = rangePart.trim().split(Pattern.quote(RANGE_INTERVAL));
if (rangeBoundary.length == 1) {
rangeBoundary[1] = rangeBoundary[0];
if (rangeBoundary.length > 2) {
ParserException parserException = new ParserException("YANG file error : " +
YangConstructType.getYangConstructType(RANGE_DATA) + " " + rangeArgument +
" is not valid.");
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
}
if (type.getDataType() == YangDataTypes.INT8) {
rangeRestriction = new YangRangeRestriction<Byte>();
rangeInterval = new YangRangeInterval<Byte>();
rangeInterval.setStartValue(Byte.parseByte(rangeBoundary[0]));
rangeInterval.setEndValue(Byte.parseByte(rangeBoundary[1]));
} else if ((type.getDataType() == YangDataTypes.INT16)
|| (type.getDataType() == YangDataTypes.UINT8)) {
rangeRestriction = new YangRangeRestriction<Short>();
rangeInterval = new YangRangeInterval<Short>();
rangeInterval.setStartValue(Short.parseShort(rangeBoundary[0]));
rangeInterval.setEndValue(Short.parseShort(rangeBoundary[1]));
} else if ((type.getDataType() == YangDataTypes.INT32)
|| (type.getDataType() == YangDataTypes.UINT16)) {
rangeRestriction = new YangRangeRestriction<Integer>();
rangeInterval = new YangRangeInterval<Integer>();
rangeInterval.setStartValue(Integer.parseInt(rangeBoundary[0]));
rangeInterval.setEndValue(Integer.parseInt(rangeBoundary[1]));
} else if ((type.getDataType() == YangDataTypes.INT64)
|| (type.getDataType() == YangDataTypes.UINT32)) {
rangeRestriction = new YangRangeRestriction<Long>();
rangeInterval = new YangRangeInterval<Long>();
rangeInterval.setStartValue(Long.parseLong(rangeBoundary[0]));
rangeInterval.setEndValue(Long.parseLong(rangeBoundary[1]));
} else if (type.getDataType() == YangDataTypes.UINT64) {
rangeRestriction = new YangRangeRestriction<BigInteger>();
rangeInterval = new YangRangeInterval<BigInteger>();
rangeInterval.setStartValue(new BigInteger(rangeBoundary[0]));
rangeInterval.setEndValue(new BigInteger(rangeBoundary[0]));
if (rangeBoundary.length == 1) {
startInterval = rangeBoundary[0];
endInterval = rangeBoundary[0];
} else {
//TODO: support derived for base built in type of string
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, RANGE_DATA,
rangeArgument, ENTRY));
startInterval = rangeBoundary[0];
endInterval = rangeBoundary[1];
}
try {
startValue = getDataObjectFromString(startInterval, type.getDataType());
endValue = getDataObjectFromString(endInterval, type.getDataType());
} catch (DataTypeException e) {
ParserException parserException = new ParserException(e.getMessage());
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
}
rangeInterval.setStartValue(startValue);
rangeInterval.setEndValue(endValue);
try {
rangeRestriction.addRangeRestrictionInterval(rangeInterval);
} catch (DataModelException e) {
throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, RANGE_DATA,
rangeArgument, ENTRY, e.getMessage()));
ParserException parserException = new ParserException(constructExtendedListenerErrorMessage(
UNHANDLED_PARSED_DATA, RANGE_DATA, rangeArgument, ENTRY, e.getMessage()));
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
}
}
if (rangeRestriction != null) {
if (type.getDataType() == YangDataTypes.DERIVED) {
((YangDerivedInfo<YangRangeRestriction>) type.getDataTypeExtendedInfo())
.setExtendedInfo(rangeRestriction);
} else {
type.setDataTypeExtendedInfo(rangeRestriction);
}
}
}
}
\ No newline at end of file
......
......@@ -98,36 +98,6 @@ public final class TypeListener {
type.setNodeIdentifier(nodeIdentifier);
type.setDataType(yangDataTypes);
// Push the type to the stack.
listener.getParsedDataStack().push(type);
}
/**
* It is called when parser exits from grammar rule (type), it perform
* validations and update the data model tree.
*
* @param listener Listener's object
* @param ctx context object of the grammar rule
*/
public static void processTypeExit(TreeWalkListener listener,
GeneratedYangParser.TypeStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT);
Parsable parsableType = listener.getParsedDataStack().pop();
if (!(parsableType instanceof YangType)) {
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
ctx.string().getText(), EXIT));
}
YangType<?> type = (YangType<?>) parsableType;
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT);
YangDataTypes yangDataTypes = YangDataTypes.getType(ctx.string().getText());
int errorLine = ctx.getStart().getLine();
int errorPosition = ctx.getStart().getCharPositionInLine();
......@@ -242,12 +212,35 @@ public final class TypeListener {
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
ctx.string().getText(), EXIT));
}
// Push the type to the stack.
listener.getParsedDataStack().push(type);
}
/**
* It is called when parser exits from grammar rule (type), it perform
* validations and update the data model tree.
*
* @param listener Listener's object
* @param ctx context object of the grammar rule
*/
public static void processTypeExit(TreeWalkListener listener,
GeneratedYangParser.TypeStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT);
Parsable parsableType = listener.getParsedDataStack().pop();
if (!(parsableType instanceof YangType)) {
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
ctx.string().getText(), EXIT));
}
}
/**
* Add to resolution list.
* Adds to resolution list.
*
* @param resolutionInfo resolution information.
* @param resolutionInfo resolution information
* @param ctx context object of the grammar rule
*/
private static void addToResolutionList(YangResolutionInfo<YangType> resolutionInfo,
......
......@@ -277,7 +277,17 @@ public enum YangConstructType {
/**
* Identifies the YANG range element parsed data.
*/
RANGE_DATA;
RANGE_DATA,
/**
* Identifies the YANG length element parsed data.
*/
LENGTH_DATA,
/**
* Identifies the YANG pattern element parsed data.
*/
PATTERN_DATA;
/**
* Returns the YANG construct keyword corresponding to enum values.
......@@ -392,6 +402,10 @@ public enum YangConstructType {
return "union";
case RANGE_DATA:
return "range";
case LENGTH_DATA:
return "length";
case PATTERN_DATA:
return "pattern";
default:
return "yang";
}
......
/*-
* Copyright 2016 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.utils.builtindatatype;
import org.onosproject.yangutils.datamodel.YangDataTypes;
/**
* Factory to create an object of required type.
*/
public final class BuiltInTypeObjectFactory {
/**
* Utility factory class, hence the object creation is forbidden.
*/
private BuiltInTypeObjectFactory() {
}
/**
* Given the value represented in string return the corresponding types
* object with the value initialized.
*
* @param valueInStr value represented in string
* @param builtInType built in data type
* @return the target data type object with the value initialized
* @param <T> the data type of the target object
*/
public static <T extends YangBuiltInDataTypeInfo<?>> T getDataObjectFromString(String valueInStr,
YangDataTypes builtInType) {
switch (builtInType) {
case INT8: {
return (T) new YangInt8(valueInStr);
}
case INT16: {
return (T) new YangInt16(valueInStr);
}
case INT32: {
return (T) new YangInt32(valueInStr);
}
case INT64: {
return (T) new YangInt64(valueInStr);
}
case UINT8: {
return (T) new YangUint8(valueInStr);
}
case UINT16: {
return (T) new YangUint16(valueInStr);
}
case UINT32: {
return (T) new YangUint32(valueInStr);
}
case UINT64: {
return (T) new YangUint64(valueInStr);
}
default: {
throw new DataTypeException("YANG file error : Unsupported data type");
}
}
}
}
/*
* Copyright 2016 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.utils.builtindatatype;
/**
* Base class for exceptions in data type.
*/
public class DataTypeException extends RuntimeException {
private static final long serialVersionUID = 20160211L;
/**
* Create a new data type exception.
*/
public DataTypeException() {
super();
}
/**
* Creates a new data type exception with given message.
*
* @param message the detail of exception in string
*/
public DataTypeException(String message) {
super(message);
}
/**
* Creates a new data type exception from given message and cause.
*
* @param message the detail of exception in string
* @param cause underlying cause of the error
*/
public DataTypeException(final String message, final Throwable cause) {
super(message, cause);
}
/**
* Creates a new data type exception from cause.
*
* @param cause underlying cause of the error
*/
public DataTypeException(final Throwable cause) {
super(cause);
}
}
/*
* Copyright 2016 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.utils.builtindatatype;
import org.onosproject.yangutils.datamodel.YangDataTypes;
/**
* Represents the list of utility functions to be supported by YANG built in
* data type implementations.
*
* @param <T> The target data type
*/
public interface YangBuiltInDataTypeInfo<T> extends Comparable<T> {
/**
* Returns the YANG built in type.
*
* @return the YANG built in type
*/
YangDataTypes getYangType();
}
/*
* Copyright 2016 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.utils.builtindatatype;
import org.onosproject.yangutils.datamodel.YangDataTypes;
/**
* Handles the YANG's int16 data type processing.
*
* int16 represents integer values between -32768 and 32767, inclusively.
*/
public class YangInt16 implements YangBuiltInDataTypeInfo<YangInt16> {
/**
* 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 int16.
*/
public static final short MIN_VALUE = -32768;
/**
* Valid maximum value of YANG's int16.
*/
public static final short MAX_VALUE = 32767;
/**
* The value of YANG's int16.
*/
private final short value;
/**
* Creates an object with the value initialized with value represented in
* string.
*
* @param valueInString value of the object in string
*/
public YangInt16(String valueInString) {
if (valueInString.matches(MIN_KEYWORD)) {
value = MIN_VALUE;
} else if (valueInString.matches(MAX_KEYWORD)) {
value = MAX_VALUE;
} else {
try {
value = Short.parseShort(valueInString);
} catch (Exception e) {
throw new DataTypeException("YANG file error : " + valueInString + " is not valid.");
}
}
}
/**
* Returns YANG's int16 value.
*
* @return value of YANG's int16
*/
public short getValue() {
return value;
}
@Override
public int compareTo(YangInt16 anotherYangInt16) {
return Short.compare(value, anotherYangInt16.value);
}
@Override
public YangDataTypes getYangType() {
return YangDataTypes.INT16;
}
}
/*
* Copyright 2016 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.utils.builtindatatype;
import org.onosproject.yangutils.datamodel.YangDataTypes;
/**
* Handles the YANG's int32 data type processing.
*
* int32 represents integer values between -2147483648 and 2147483647, inclusively.
*/
public class YangInt32 implements YangBuiltInDataTypeInfo<YangInt32> {
/**
* 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 int32.
*/
public static final int MIN_VALUE = -2147483648;
/**
* Valid maximum value of YANG's int32.
*/
public static final int MAX_VALUE = 2147483647;
/**
* The value of YANG's int32.
*/
private final int value;
/**
* Creates an object with the value initialized with value represented in
* string.
*
* @param valueInString value of the object in string
*/
public YangInt32(String valueInString) {
if (valueInString.matches(MIN_KEYWORD)) {
value = MIN_VALUE;
} else if (valueInString.matches(MAX_KEYWORD)) {
value = MAX_VALUE;
} else {
try {
value = Integer.parseInt(valueInString);
} catch (Exception e) {
throw new DataTypeException("YANG file error : " + valueInString + " is not valid.");
}
}
}
/**
* Returns YANG's int32 value.
*
* @return value of YANG's int32
*/
public int getValue() {
return value;
}
@Override
public int compareTo(YangInt32 anotherYangInt32) {
return Integer.compare(value, anotherYangInt32.value);
}
@Override
public YangDataTypes getYangType() {
return YangDataTypes.INT32;
}
}
/*
* Copyright 2016 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.utils.builtindatatype;
import org.onosproject.yangutils.datamodel.YangDataTypes;
/**
* Handles the YANG's int8 data type processing.
*
* int8 represents integer values between -9223372036854775808 and 9223372036854775807, inclusively.
*/
public class YangInt64 implements YangBuiltInDataTypeInfo<YangInt64> {
/**
* 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 int64.
*/
public static final Long MIN_VALUE = 0x8000000000000000L;
/**
* Valid maximum value of YANG's int64.
*/
public static final long MAX_VALUE = 0x7fffffffffffffffL;
/**
* The value of YANG's int64.
*/
private final long value;
/**
* Creates an object with the value initialized with value represented in
* string.
*
* @param valueInString value of the object in string
*/
public YangInt64(String valueInString) {
if (valueInString.matches(MIN_KEYWORD)) {
value = MIN_VALUE;
} else if (valueInString.matches(MAX_KEYWORD)) {
value = MAX_VALUE;
} else {
try {
value = Long.parseLong(valueInString);
} catch (Exception e) {
throw new DataTypeException("YANG file error : " + valueInString + " is not valid.");
}
}
}
/**
* Returns YANG's int64 value.
*
* @return value of YANG's int64
*/
public long getValue() {
return value;
}
@Override
public int compareTo(YangInt64 anotherYangInt64) {
return Long.compare(value, anotherYangInt64.value);
}
@Override
public YangDataTypes getYangType() {
return YangDataTypes.INT64;
}
}
/*
* Copyright 2016 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.utils.builtindatatype;
import org.onosproject.yangutils.datamodel.YangDataTypes;
/**
* Handles the YANG's int8 data type processing.
*
* int8 represents integer values between -128 and 127, inclusively.
*/
public class YangInt8 implements YangBuiltInDataTypeInfo<YangInt8> {
/**
* 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 int8.
*/
public static final byte MIN_VALUE = -128;
/**
* Valid maximum value of YANG's int8.
*/
public static final byte MAX_VALUE = 127;
/**
* The value of YANG's int8.
*/
private final byte value;
/**
* Creates an object with the value initialized with value represented in
* string.
*
* @param valueInString value of the object in string
*/
public YangInt8(String valueInString) {
if (valueInString.matches(MIN_KEYWORD)) {
value = MIN_VALUE;
} else if (valueInString.matches(MAX_KEYWORD)) {
value = MAX_VALUE;
} else {
try {
value = Byte.parseByte(valueInString);
} catch (Exception e) {
throw new DataTypeException("YANG file error : " + valueInString + " is not valid.");
}
}
}
/**
* Returns YANG's int8 value.
*
* @return value of YANG's int8
*/
public byte getValue() {
return value;
}
@Override
public int compareTo(YangInt8 anotherYangInt8) {
return Byte.compare(value, anotherYangInt8.value);
}
@Override
public YangDataTypes getYangType() {
return YangDataTypes.INT8;
}
}
/*
* Copyright 2016 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.utils.builtindatatype;
import org.onosproject.yangutils.datamodel.YangDataTypes;
/**
* Handles the YANG's Uint16 data type processing.
*
* Uint16 represents integer values between 0 and 65535, inclusively.
*/
public class YangUint16 implements YangBuiltInDataTypeInfo<YangUint16> {
/**
* 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 Uint16.
*/
public static final int MIN_VALUE = 0;
/**
* Valid maximum value of YANG's Uint16.
*/
public static final int MAX_VALUE = 65535;
/**
* Value of the object.
*/
private int value;
/**
* Creates an object with the value initialized with value represented in
* string.
*
* @param valueInString value of the object in string
*/
YangUint16(String valueInString) {
if (valueInString.matches(MIN_KEYWORD)) {
value = MIN_VALUE;
} else if (valueInString.matches(MAX_KEYWORD)) {
value = MAX_VALUE;
} else {
try {
value = Integer.parseInt(valueInString);
} catch (Exception e) {
throw new DataTypeException("YANG file error : " + valueInString + " is not valid.");
}
}
if (value < MIN_VALUE) {
throw new DataTypeException("YANG file error : " + valueInString + " is lesser than minimum value "
+ MIN_VALUE + ".");
} else if (value > MAX_VALUE) {
throw new DataTypeException("YANG file error : " + valueInString + " is greater than maximum value "
+ MAX_VALUE + ".");
}
}
/**
* Returns YANG's uint16 value.
*
* @return value of YANG's uint16
*/
public int getValue() {
return value;
}
@Override
public int compareTo(YangUint16 another) {
return Integer.compare(value, another.value);
}
@Override
public YangDataTypes getYangType() {
return YangDataTypes.UINT16;
}
}
/*
* Copyright 2016 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.utils.builtindatatype;
import org.onosproject.yangutils.datamodel.YangDataTypes;
/**
* Handles the YANG's Uint32 data type processing.
*
* Uint32 represents integer values between 0 and 4294967295, inclusively.
*/
public class YangUint32 implements YangBuiltInDataTypeInfo<YangUint32> {
private static final String MIN_KEYWORD = "min";
private static final String MAX_KEYWORD = "max";
/**
* Valid minimum value of YANG's Uint32.
*/
public static final long MIN_VALUE = 0;
/**
* Valid maximum value of YANG's Uint32.
*/
public static final long MAX_VALUE = 4294967295L;
/**
* Value of the object.
*/
private long value;
/**
* Creates an object with the value initialized with value represented in
* string.
*
* @param valueInString value of the object in string
*/
YangUint32(String valueInString) {
if (valueInString.matches(MIN_KEYWORD)) {
value = MIN_VALUE;
} else if (valueInString.matches(MAX_KEYWORD)) {
value = MAX_VALUE;
} else {
try {
value = Long.parseLong(valueInString);
} catch (Exception e) {
throw new DataTypeException("YANG file error : " + valueInString + " is not valid.");
}
}
if (value < MIN_VALUE) {
throw new DataTypeException("YANG file error : " + valueInString + " is lesser than minimum value "
+ MIN_VALUE + ".");
} else if (value > MAX_VALUE) {
throw new DataTypeException("YANG file error : " + valueInString + " is greater than maximum value "
+ MAX_VALUE + ".");
}
}
/**
* Returns YANG's uint32 value.
*
* @return value of YANG's uint32
*/
public long getValue() {
return value;
}
@Override
public int compareTo(YangUint32 another) {
return Long.compare(value, another.value);
}
@Override
public YangDataTypes getYangType() {
return YangDataTypes.UINT32;
}
}
/*
* Copyright 2016 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.utils.builtindatatype;
import java.math.BigInteger;
import java.util.regex.Pattern;
import org.onosproject.yangutils.datamodel.YangDataTypes;
/**
* Handles the YANG's Uint16 data type processing.
*
* Uint64 represents integer values between 0 and 18446744073709551615, inclusively.
*/
public class YangUint64 implements YangBuiltInDataTypeInfo<YangUint64> {
/**
* YANG's min keyword.
*/
private static final String MIN_KEYWORD = "min";
/**
* YANG's max keyword.
*/
private static final String MAX_KEYWORD = "max";
/**
* YANG's Integer value pattern.
*/
private static final Pattern NON_NEGATIVE_INTEGER_PATTERN = Pattern.compile("[0-9]+");
/**
* Valid minimum value of YANG's Uint64.
*/
public static final BigInteger MIN_VALUE = BigInteger.valueOf(0);
/**
* Valid maximum value of YANG's Uint64.
*/
public static final BigInteger MAX_VALUE = new BigInteger("18446744073709551615");
/**
* Value of the object.
*/
private BigInteger value;
/**
* Creates an object with the value initialized with value represented in
* string.
*
* @param valueInString value of the object in string
*/
YangUint64(String valueInString) {
if (valueInString.matches(MIN_KEYWORD)) {
value = MIN_VALUE;
} else if (valueInString.matches(MAX_KEYWORD)) {
value = MAX_VALUE;
} else if (NON_NEGATIVE_INTEGER_PATTERN.matcher(valueInString).matches()) {
value = new BigInteger(valueInString);
} else {
throw new DataTypeException("YANG file error : " + valueInString + " is not valid.");
}
if (value.compareTo(MIN_VALUE) < 0) {
throw new DataTypeException("YANG file error : " + valueInString + " is lesser than minimum value "
+ MIN_VALUE + ".");
} else if (value.compareTo(MAX_VALUE) > 0) {
throw new DataTypeException("YANG file error : " + valueInString + " is greater than maximum value "
+ MAX_VALUE + ".");
}
}
/**
* Returns YANG's uint64 value.
*
* @return value of YANG's uint64
*/
public BigInteger getValue() {
return value;
}
@Override
public int compareTo(YangUint64 another) {
return value.compareTo(another.value);
}
@Override
public YangDataTypes getYangType() {
return YangDataTypes.UINT64;
}
}
/*
* Copyright 2016 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.utils.builtindatatype;
import org.onosproject.yangutils.datamodel.YangDataTypes;
/**
* Handles the YANG's Uint8 data type processing.
*
* Uint8 represents integer values between 0 and 255, inclusively.
*/
public class YangUint8 implements YangBuiltInDataTypeInfo<YangUint8> {
/**
* 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 Uint8.
*/
public static final short MIN_VALUE = 0;
/**
* Valid maximum value of YANG's Uint8.
*/
public static final short MAX_VALUE = 255;
/**
* Value of the object.
*/
private short value;
/**
* Creates an object with the value initialized with value represented in
* string.
*
* @param valueInString value of the object in string
*/
YangUint8(String valueInString) {
if (valueInString.matches(MIN_KEYWORD)) {
value = MIN_VALUE;
} else if (valueInString.matches(MAX_KEYWORD)) {
value = MAX_VALUE;
} else {
try {
value = Short.parseShort(valueInString);
} catch (Exception e) {
throw new DataTypeException("YANG file error : " + valueInString + " is not valid.");
}
}
if (value < MIN_VALUE) {
throw new DataTypeException("YANG file error : " + valueInString + " is lesser than minimum value "
+ MIN_VALUE + ".");
} else if (value > MAX_VALUE) {
throw new DataTypeException("YANG file error : " + valueInString + " is greater than maximum value "
+ MAX_VALUE + ".");
}
}
/**
* Returns YANG's uint8 value.
*
* @return value of YANG's uint8
*/
public short getValue() {
return value;
}
@Override
public int compareTo(YangUint8 another) {
return Short.compare(value, another.value);
}
@Override
public YangDataTypes getYangType() {
return YangDataTypes.UINT8;
}
}
/*-
* Copyright 2016 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.
*/
/**
* Utilities for YANG built in data types.
*/
package org.onosproject.yangutils.utils.builtindatatype;
/*
* Copyright 2016 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 java.io.IOException;
import java.util.ListIterator;
import org.junit.Test;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangNodeType;
import org.onosproject.yangutils.datamodel.YangModule;
import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangLeafList;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.YangPatternRestriction;
import org.onosproject.yangutils.datamodel.YangStringRestriction;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
/**
* Test cases for pattern restriction listener.
*/
public class PatternRestrictionListenerTest {
private final YangUtilsParserManager manager = new YangUtilsParserManager();
/**
* Checks valid pattern statement as sub-statement of leaf statement.
*/
@Test
public void processValidPatternStatement() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/ValidPatternStatement.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.getLeafName(), is("invalid-interval"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
YangStringRestriction stringRestriction = (YangStringRestriction) leafInfo
.getDataType().getDataTypeExtendedInfo();
ListIterator<String> patternListIterator = stringRestriction.getPatternRestriction()
.getPatternList().listIterator();
assertThat(patternListIterator.next(), is("[a-zA-Z]"));
}
/**
* Checks valid pattern statement as sub-statement of leaf-list.
*/
@Test
public void processPatternStatementInsideLeafList() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/PatternStatementInsideLeafList.yang");
assertThat((node instanceof YangModule), is(true));
assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
YangLeafList leafListInfo = leafListIterator.next();
assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
assertThat(leafListInfo.getDataType().getDataTypeName(), is("string"));
assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
YangStringRestriction stringRestriction = (YangStringRestriction) leafListInfo
.getDataType().getDataTypeExtendedInfo();
ListIterator<String> patternListIterator = stringRestriction.getPatternRestriction()
.getPatternList().listIterator();
assertThat(patternListIterator.next(), is("[a-zA-Z]"));
}
/**
* Checks valid pattern statement as sub-statement of typedef.
*/
@Test
public void processPatternStatementInsideTypeDef() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/PatternStatementInsideTypeDef.yang");
assertThat((node instanceof YangModule), is(true));
assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
YangTypeDef typedef = (YangTypeDef) yangNode.getChild();
YangStringRestriction stringRestriction = (YangStringRestriction) typedef.getDataType()
.getDataTypeExtendedInfo();
YangPatternRestriction yangPatternRestriction = stringRestriction.getPatternRestriction();
assertThat(yangPatternRestriction.getPatternList().listIterator().next(), is("[a-zA-Z]"));
}
/**
* Checks valid multiple pattern statements.
*/
@Test
public void processMultiplePatternStatement() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/MultiplePatternStatement.yang");
assertThat((node instanceof YangModule), is(true));
assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
YangLeafList leafListInfo = leafListIterator.next();
assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
assertThat(leafListInfo.getDataType().getDataTypeName(), is("string"));
assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
YangStringRestriction stringRestriction = (YangStringRestriction) leafListInfo
.getDataType().getDataTypeExtendedInfo();
ListIterator<String> patternListIterator = stringRestriction.getPatternRestriction()
.getPatternList().listIterator();
assertThat(patternListIterator.next(), is("[a-zA-Z]"));
}
/**
* Checks valid pattern statement with plus symbol in pattern.
*/
@Test
public void processPatternStatementWithPlus() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/PatternStatementWithPlus.yang");
assertThat((node instanceof YangModule), is(true));
assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
YangLeafList leafListInfo = leafListIterator.next();
assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
assertThat(leafListInfo.getDataType().getDataTypeName(), is("string"));
assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
YangStringRestriction stringRestriction = (YangStringRestriction) leafListInfo
.getDataType().getDataTypeExtendedInfo();
ListIterator<String> patternListIterator = stringRestriction.getPatternRestriction()
.getPatternList().listIterator();
assertThat(patternListIterator.next(), is("-[0-9]+|[0-9]+"));
}
}
\ No newline at end of file
......@@ -16,11 +16,12 @@
package org.onosproject.yangutils.parser.impl.listeners;
import java.io.IOException;
import java.util.ListIterator;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangLeafList;
......@@ -31,6 +32,7 @@ import org.onosproject.yangutils.datamodel.YangNodeType;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
import org.onosproject.yangutils.utils.builtindatatype.YangInt32;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
......@@ -40,6 +42,9 @@ import static org.hamcrest.core.Is.is;
*/
public class RangeRestrictionListenerTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
private final YangUtilsParserManager manager = new YangUtilsParserManager();
/**
......@@ -67,8 +72,8 @@ public class RangeRestrictionListenerTest {
ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
.listIterator();
YangRangeInterval rangeInterval = rangeListIterator.next();
assertThat(rangeInterval.getStartValue(), is(10));
assertThat(rangeInterval.getEndValue(), is(20));
assertThat(((YangInt32) rangeInterval.getStartValue()).getValue(), is(1));
assertThat(((YangInt32) rangeInterval.getEndValue()).getValue(), is(4));
}
/**
......@@ -97,7 +102,77 @@ public class RangeRestrictionListenerTest {
.listIterator();
YangRangeInterval rangeInterval = rangeListIterator.next();
assertThat(rangeInterval.getStartValue(), is(10));
assertThat(rangeInterval.getEndValue(), is(20));
assertThat(((YangInt32) rangeInterval.getStartValue()).getValue(), is(1));
assertThat(((YangInt32) rangeInterval.getEndValue()).getValue(), is(4));
}
/**
* Checks valid range statement with one interval.
*/
@Test
public void processRangeWithOneInterval() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/RangeWithOneInterval.yang");
assertThat((node instanceof YangModule), is(true));
assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
YangLeafList leafListInfo = leafListIterator.next();
assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
assertThat(leafListInfo.getDataType().getDataTypeName(), is("int32"));
assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.INT32));
YangRangeRestriction rangeRestriction = (YangRangeRestriction) leafListInfo
.getDataType().getDataTypeExtendedInfo();
ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
.listIterator();
YangRangeInterval rangeInterval = rangeListIterator.next();
assertThat(((YangInt32) rangeInterval.getStartValue()).getValue(), is(1));
assertThat(((YangInt32) rangeInterval.getEndValue()).getValue(), is(1));
}
/**
* Checks valid range statement with min and max.
*/
@Test
public void processRangeWithMinMax() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/RangeWithMinMax.yang");
assertThat((node instanceof YangModule), is(true));
assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
YangLeafList leafListInfo = leafListIterator.next();
assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
assertThat(leafListInfo.getDataType().getDataTypeName(), is("int32"));
assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.INT32));
YangRangeRestriction rangeRestriction = (YangRangeRestriction) leafListInfo
.getDataType().getDataTypeExtendedInfo();
ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
.listIterator();
YangRangeInterval rangeInterval = rangeListIterator.next();
assertThat(((YangInt32) rangeInterval.getStartValue()).getValue(), is(-2147483648));
assertThat(((YangInt32) rangeInterval.getEndValue()).getValue(), is(2147483647));
}
/**
* Checks valid range statement with invalid integer pattern.
*/
@Test
public void processRangeWithInvalidIntegerPattern() throws IOException, ParserException {
thrown.expect(ParserException.class);
thrown.expectMessage("YANG file error : a is not valid.");
YangNode node = manager.getDataModel("src/test/resources/RangeWithInvalidIntegerPattern.yang");
}
}
\ No newline at end of file
......
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf-list invalid-interval {
type string {
length "1..100";
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
typedef invalid-interval {
type string {
length "1..100";
}
}
leaf xyz {
type invalid-interval {
length "2..100";
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf-list invalid-interval {
type string {
length "a..z";
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf-list invalid-interval {
type string {
length "0..18446744073709551617";
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf-list invalid-interval {
type int8 {
length "1..100";
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf-list invalid-interval {
type string {
length "min..max";
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf-list invalid-interval {
type string {
length "1";
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf-list invalid-interval {
type string {
pattern "[a-zA-Z]";
pattern "[a-z]";
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf-list invalid-interval {
type string {
pattern "[a-zA-Z]";
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
typedef invalid-interval {
type string {
pattern "[a-zA-Z]";
}
}
leaf xyz {
type invalid-interval {
pattern "[a-z]";
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf-list invalid-interval {
type string {
pattern "-[0-9]+|[0-9]+";
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf-list invalid-interval {
type int32 {
range "a..z";
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf-list invalid-interval {
type int32 {
range "min..max";
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf-list invalid-interval {
type int32 {
range "1";
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf invalid-interval {
type string {
length "0..100";
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf invalid-interval {
type string {
pattern "[a-zA-Z]";
}
}
}