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 2097 additions and 99 deletions
...@@ -13,12 +13,14 @@ See the License for the specific language governing permissions and ...@@ -13,12 +13,14 @@ See the License for the specific language governing permissions and
13 limitations under the License.*/ 13 limitations under the License.*/
14 package org.onosproject.yangutils.datamodel; 14 package org.onosproject.yangutils.datamodel;
15 15
16 +import org.onosproject.yangutils.utils.builtindatatype.YangBuiltInDataTypeInfo;
17 +
16 /** 18 /**
17 * Represents single interval information of a range. 19 * Represents single interval information of a range.
18 * 20 *
19 - * @param <T> range type based on the data type. 21 + * @param <T> range type based on the data type
20 */ 22 */
21 -public class YangRangeInterval<T extends Comparable<T>> { 23 +public class YangRangeInterval<T extends YangBuiltInDataTypeInfo<T>> {
22 24
23 /** 25 /**
24 * Starting value of the range interval. 26 * Starting value of the range interval.
...@@ -39,7 +41,7 @@ public class YangRangeInterval<T extends Comparable<T>> { ...@@ -39,7 +41,7 @@ public class YangRangeInterval<T extends Comparable<T>> {
39 /** 41 /**
40 * Returns the starting value of the range interval. 42 * Returns the starting value of the range interval.
41 * 43 *
42 - * @return the starting value of the range interval. 44 + * @return the starting value of the range interval
43 */ 45 */
44 public T getStartValue() { 46 public T getStartValue() {
45 return startValue; 47 return startValue;
...@@ -48,7 +50,7 @@ public class YangRangeInterval<T extends Comparable<T>> { ...@@ -48,7 +50,7 @@ public class YangRangeInterval<T extends Comparable<T>> {
48 /** 50 /**
49 * Sets the starting value of the range interval. 51 * Sets the starting value of the range interval.
50 * 52 *
51 - * @param startValue the starting value of the range interval. 53 + * @param startValue the starting value of the range interval
52 */ 54 */
53 public void setStartValue(T startValue) { 55 public void setStartValue(T startValue) {
54 this.startValue = startValue; 56 this.startValue = startValue;
...@@ -57,7 +59,7 @@ public class YangRangeInterval<T extends Comparable<T>> { ...@@ -57,7 +59,7 @@ public class YangRangeInterval<T extends Comparable<T>> {
57 /** 59 /**
58 * Returns the last value of the range interval. 60 * Returns the last value of the range interval.
59 * 61 *
60 - * @return last value of the range interval. 62 + * @return last value of the range interval
61 */ 63 */
62 public T getEndValue() { 64 public T getEndValue() {
63 return endValue; 65 return endValue;
...@@ -66,7 +68,7 @@ public class YangRangeInterval<T extends Comparable<T>> { ...@@ -66,7 +68,7 @@ public class YangRangeInterval<T extends Comparable<T>> {
66 /** 68 /**
67 * Sets the last value of the range interval. 69 * Sets the last value of the range interval.
68 * 70 *
69 - * @param endValue last value of the range interval. 71 + * @param endValue last value of the range interval
70 */ 72 */
71 public void setEndValue(T endValue) { 73 public void setEndValue(T endValue) {
72 this.endValue = endValue; 74 this.endValue = endValue;
......
...@@ -20,7 +20,9 @@ import java.util.LinkedList; ...@@ -20,7 +20,9 @@ import java.util.LinkedList;
20 import java.util.List; 20 import java.util.List;
21 21
22 import org.onosproject.yangutils.datamodel.exceptions.DataModelException; 22 import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
23 +import org.onosproject.yangutils.utils.builtindatatype.YangBuiltInDataTypeInfo;
23 24
25 +import static org.onosproject.yangutils.utils.builtindatatype.BuiltInTypeObjectFactory.getDataObjectFromString;
24 import static com.google.common.base.Preconditions.checkNotNull; 26 import static com.google.common.base.Preconditions.checkNotNull;
25 27
26 /*- 28 /*-
...@@ -47,12 +49,14 @@ import static com.google.common.base.Preconditions.checkNotNull; ...@@ -47,12 +49,14 @@ import static com.google.common.base.Preconditions.checkNotNull;
47 * "max". "min" and "max" mean the minimum and maximum value accepted 49 * "max". "min" and "max" mean the minimum and maximum value accepted
48 * for the type being restricted, respectively. 50 * for the type being restricted, respectively.
49 */ 51 */
52 +
50 /** 53 /**
51 * Represents ascending range restriction information. 54 * Represents ascending range restriction information.
52 * 55 *
53 * @param <T> range type (data type) 56 * @param <T> range type (data type)
54 */ 57 */
55 -public class YangRangeRestriction<T extends Comparable<T>> implements YangDesc, YangReference, YangAppErrorInfo { 58 +public class YangRangeRestriction<T extends YangBuiltInDataTypeInfo<T>>
59 + implements YangDesc, YangReference, YangAppErrorInfo {
56 60
57 /** 61 /**
58 * Ascending list of range interval restriction. If the restriction is a 62 * Ascending list of range interval restriction. If the restriction is a
...@@ -171,6 +175,37 @@ public class YangRangeRestriction<T extends Comparable<T>> implements YangDesc, ...@@ -171,6 +175,37 @@ public class YangRangeRestriction<T extends Comparable<T>> implements YangDesc,
171 } 175 }
172 176
173 /** 177 /**
178 + * Check if the given value is correct as per the restriction.
179 + *
180 + * @param valueInString value
181 + * @return true, if the value is confirming to restriction, false otherwise
182 + * @throws DataModelException data model error
183 + */
184 + public boolean isValidValueString(String valueInString) throws DataModelException {
185 +
186 + if (getAscendingRangeIntervals() == null
187 + || getAscendingRangeIntervals().size() == 0) {
188 + // Throw exception, At least one default range needs to be set in constructor or in linker.
189 + throw new DataModelException("Range interval missing in range restriction.");
190 +
191 + }
192 +
193 + YangDataTypes type = getAscendingRangeIntervals().get(0).getStartValue().getYangType();
194 + YangBuiltInDataTypeInfo<?> value = getDataObjectFromString(valueInString, type);
195 +
196 + for (YangRangeInterval<T> interval : getAscendingRangeIntervals()) {
197 + int rangeStartCompareRes = interval.getStartValue().compareTo((T) value);
198 + int rangeEndCompareRes = interval.getEndValue().compareTo((T) value);
199 +
200 + if (rangeStartCompareRes <= 0 && rangeEndCompareRes >= 0) {
201 + return true;
202 + }
203 + }
204 +
205 + return false;
206 + }
207 +
208 + /**
174 * Returns the textual reference of the length restriction. 209 * Returns the textual reference of the length restriction.
175 * 210 *
176 * @return textual reference of the length restriction 211 * @return textual reference of the length restriction
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
16 16
17 package org.onosproject.yangutils.datamodel; 17 package org.onosproject.yangutils.datamodel;
18 18
19 -import java.math.BigInteger; 19 +import org.onosproject.yangutils.utils.builtindatatype.YangUint64;
20 20
21 /*- 21 /*-
22 * Reference RFC 6020. 22 * Reference RFC 6020.
...@@ -64,10 +64,11 @@ public class YangStringRestriction { ...@@ -64,10 +64,11 @@ public class YangStringRestriction {
64 * | reference | 7.19.4 | 0..1 | string | 64 * | reference | 7.19.4 | 0..1 | string |
65 * +---------------+---------+-------------+-----------------+ 65 * +---------------+---------+-------------+-----------------+
66 */ 66 */
67 +
67 /** 68 /**
68 * Length restriction information. 69 * Length restriction information.
69 */ 70 */
70 - private YangRangeRestriction<BigInteger> lengthRestriction; 71 + private YangRangeRestriction<YangUint64> lengthRestriction;
71 72
72 /** 73 /**
73 * Effective pattern restriction for the type. 74 * Effective pattern restriction for the type.
...@@ -85,7 +86,7 @@ public class YangStringRestriction { ...@@ -85,7 +86,7 @@ public class YangStringRestriction {
85 * 86 *
86 * @return length restriction on the string data 87 * @return length restriction on the string data
87 */ 88 */
88 - public YangRangeRestriction<BigInteger> getLengthRestriction() { 89 + public YangRangeRestriction<YangUint64> getLengthRestriction() {
89 return lengthRestriction; 90 return lengthRestriction;
90 } 91 }
91 92
...@@ -94,7 +95,7 @@ public class YangStringRestriction { ...@@ -94,7 +95,7 @@ public class YangStringRestriction {
94 * 95 *
95 * @param lengthRestriction length restriction on the string data 96 * @param lengthRestriction length restriction on the string data
96 */ 97 */
97 - public void setLengthRestriction(YangRangeRestriction<BigInteger> lengthRestriction) { 98 + public void setLengthRestriction(YangRangeRestriction<YangUint64> lengthRestriction) {
98 this.lengthRestriction = lengthRestriction; 99 this.lengthRestriction = lengthRestriction;
99 } 100 }
100 101
......
...@@ -46,6 +46,7 @@ import org.onosproject.yangutils.parser.impl.listeners.InputListener; ...@@ -46,6 +46,7 @@ import org.onosproject.yangutils.parser.impl.listeners.InputListener;
46 import org.onosproject.yangutils.parser.impl.listeners.KeyListener; 46 import org.onosproject.yangutils.parser.impl.listeners.KeyListener;
47 import org.onosproject.yangutils.parser.impl.listeners.LeafListListener; 47 import org.onosproject.yangutils.parser.impl.listeners.LeafListListener;
48 import org.onosproject.yangutils.parser.impl.listeners.LeafListener; 48 import org.onosproject.yangutils.parser.impl.listeners.LeafListener;
49 +import org.onosproject.yangutils.parser.impl.listeners.LengthRestrictionListener;
49 import org.onosproject.yangutils.parser.impl.listeners.ListListener; 50 import org.onosproject.yangutils.parser.impl.listeners.ListListener;
50 import org.onosproject.yangutils.parser.impl.listeners.MandatoryListener; 51 import org.onosproject.yangutils.parser.impl.listeners.MandatoryListener;
51 import org.onosproject.yangutils.parser.impl.listeners.MaxElementsListener; 52 import org.onosproject.yangutils.parser.impl.listeners.MaxElementsListener;
...@@ -55,6 +56,7 @@ import org.onosproject.yangutils.parser.impl.listeners.NotificationListener; ...@@ -55,6 +56,7 @@ import org.onosproject.yangutils.parser.impl.listeners.NotificationListener;
55 import org.onosproject.yangutils.parser.impl.listeners.NamespaceListener; 56 import org.onosproject.yangutils.parser.impl.listeners.NamespaceListener;
56 import org.onosproject.yangutils.parser.impl.listeners.OrganizationListener; 57 import org.onosproject.yangutils.parser.impl.listeners.OrganizationListener;
57 import org.onosproject.yangutils.parser.impl.listeners.OutputListener; 58 import org.onosproject.yangutils.parser.impl.listeners.OutputListener;
59 +import org.onosproject.yangutils.parser.impl.listeners.PatternRestrictionListener;
58 import org.onosproject.yangutils.parser.impl.listeners.PositionListener; 60 import org.onosproject.yangutils.parser.impl.listeners.PositionListener;
59 import org.onosproject.yangutils.parser.impl.listeners.PrefixListener; 61 import org.onosproject.yangutils.parser.impl.listeners.PrefixListener;
60 import org.onosproject.yangutils.parser.impl.listeners.PresenceListener; 62 import org.onosproject.yangutils.parser.impl.listeners.PresenceListener;
...@@ -586,7 +588,7 @@ public class TreeWalkListener implements GeneratedYangListener { ...@@ -586,7 +588,7 @@ public class TreeWalkListener implements GeneratedYangListener {
586 588
587 @Override 589 @Override
588 public void enterLengthStatement(GeneratedYangParser.LengthStatementContext ctx) { 590 public void enterLengthStatement(GeneratedYangParser.LengthStatementContext ctx) {
589 - // TODO: implement the method. 591 + LengthRestrictionListener.processLengthRestrictionEntry(this, ctx);
590 } 592 }
591 593
592 @Override 594 @Override
...@@ -596,7 +598,7 @@ public class TreeWalkListener implements GeneratedYangListener { ...@@ -596,7 +598,7 @@ public class TreeWalkListener implements GeneratedYangListener {
596 598
597 @Override 599 @Override
598 public void enterPatternStatement(GeneratedYangParser.PatternStatementContext ctx) { 600 public void enterPatternStatement(GeneratedYangParser.PatternStatementContext ctx) {
599 - // TODO: implement the method. 601 + PatternRestrictionListener.processPatternRestrictionEntry(this, ctx);
600 } 602 }
601 603
602 @Override 604 @Override
......
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.yangutils.parser.impl.listeners;
18 +
19 +import java.util.regex.Pattern;
20 +
21 +import org.onosproject.yangutils.datamodel.YangRangeRestriction;
22 +import org.onosproject.yangutils.datamodel.YangRangeInterval;
23 +import org.onosproject.yangutils.datamodel.YangStringRestriction;
24 +import org.onosproject.yangutils.datamodel.YangType;
25 +import org.onosproject.yangutils.datamodel.YangDataTypes;
26 +import org.onosproject.yangutils.datamodel.YangDerivedInfo;
27 +import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
28 +import org.onosproject.yangutils.parser.Parsable;
29 +import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
30 +import org.onosproject.yangutils.parser.exceptions.ParserException;
31 +import org.onosproject.yangutils.parser.impl.TreeWalkListener;
32 +import org.onosproject.yangutils.utils.YangConstructType;
33 +import org.onosproject.yangutils.utils.builtindatatype.DataTypeException;
34 +import org.onosproject.yangutils.utils.builtindatatype.YangBuiltInDataTypeInfo;
35 +
36 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
37 +import static org.onosproject.yangutils.utils.YangConstructType.LENGTH_DATA;
38 +import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
39 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
40 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
41 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
42 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
43 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
44 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
45 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
46 +import static org.onosproject.yangutils.utils.builtindatatype.BuiltInTypeObjectFactory.getDataObjectFromString;
47 +
48 +/*
49 + * Reference: RFC6020 and YANG ANTLR Grammar
50 + *
51 + * ABNF grammar as per RFC6020
52 + * length-stmt = length-keyword sep length-arg-str optsep
53 + * (";" /
54 + * "{" stmtsep
55 + * ;; these stmts can appear in any order
56 + * [error-message-stmt stmtsep]
57 + * [error-app-tag-stmt stmtsep]
58 + * [description-stmt stmtsep]
59 + * [reference-stmt stmtsep]
60 + * "}")
61 + *
62 + *
63 + * ANTLR grammar rule
64 + * lengthStatement : LENGTH_KEYWORD length
65 + * (STMTEND | LEFT_CURLY_BRACE commonStatements RIGHT_CURLY_BRACE);
66 + */
67 +
68 +/**
69 + * Represents listener based call back function corresponding to the "length"
70 + * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
71 + */
72 +public final class LengthRestrictionListener {
73 +
74 + private static final String PIPE = "|";
75 + private static final String LENGTH_INTERVAL = "..";
76 +
77 + /**
78 + * Creates a new length restriction listener.
79 + */
80 + private LengthRestrictionListener() {
81 + }
82 +
83 + /**
84 + * It is called when parser receives an input matching the grammar
85 + * rule (length), performs validation and updates the data model
86 + * tree.
87 + *
88 + * @param listener listener's object
89 + * @param ctx context object of the grammar rule
90 + */
91 + public static void processLengthRestrictionEntry(TreeWalkListener listener,
92 + GeneratedYangParser.LengthStatementContext ctx) {
93 +
94 + // Check for stack to be non empty.
95 + checkStackIsNotEmpty(listener, MISSING_HOLDER, LENGTH_DATA, ctx.length().getText(), ENTRY);
96 +
97 + Parsable tmpData = listener.getParsedDataStack().peek();
98 + if (tmpData.getYangConstructType() == TYPE_DATA) {
99 + YangType type = (YangType) tmpData;
100 + setLengthRestriction(type, ctx);
101 + } else {
102 + throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LENGTH_DATA,
103 + ctx.length().getText(), ENTRY));
104 + }
105 + }
106 +
107 + /**
108 + * Sets the length restriction to type.
109 + *
110 + * @param type Yang type for which length restriction to be set
111 + * @param ctx context object of the grammar rule
112 + */
113 + private static void setLengthRestriction(YangType type,
114 + GeneratedYangParser.LengthStatementContext ctx) {
115 +
116 + YangStringRestriction stringRestriction;
117 + YangBuiltInDataTypeInfo<?> startValue, endValue;
118 + YangRangeRestriction lengthRestriction = new YangRangeRestriction<>();
119 +
120 + if (type.getDataType() != YangDataTypes.STRING && type.getDataType() != YangDataTypes.DERIVED) {
121 +
122 + ParserException parserException = new ParserException("YANG file error : " +
123 + YangConstructType.getYangConstructType(LENGTH_DATA) + " name " + ctx.length().getText() +
124 + " can be used to restrict the built-in type string or types derived from string.");
125 + parserException.setLine(ctx.getStart().getLine());
126 + parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
127 + throw parserException;
128 + }
129 +
130 + if (type.getDataType() == YangDataTypes.STRING) {
131 + stringRestriction = (YangStringRestriction) type.getDataTypeExtendedInfo();
132 + } else {
133 + stringRestriction = (YangStringRestriction) ((YangDerivedInfo<?>) type
134 + .getDataTypeExtendedInfo()).getExtendedInfo();
135 + }
136 +
137 + if (stringRestriction == null) {
138 + stringRestriction = new YangStringRestriction();
139 + if (type.getDataType() == YangDataTypes.STRING) {
140 + type.setDataTypeExtendedInfo(stringRestriction);
141 + } else {
142 + ((YangDerivedInfo<YangStringRestriction>) type.getDataTypeExtendedInfo())
143 + .setExtendedInfo(stringRestriction);
144 + }
145 + }
146 +
147 + String rangeArgument = removeQuotesAndHandleConcat(ctx.length().getText());
148 + String[] rangeArguments = rangeArgument.trim().split(Pattern.quote(PIPE));
149 +
150 + for (String rangePart : rangeArguments) {
151 + String startInterval, endInterval;
152 + YangRangeInterval rangeInterval = new YangRangeInterval<>();
153 + String[] rangeBoundary = rangePart.trim().split(Pattern.quote(LENGTH_INTERVAL));
154 +
155 + if (rangeBoundary.length > 2) {
156 + ParserException parserException = new ParserException("YANG file error : " +
157 + YangConstructType.getYangConstructType(LENGTH_DATA) + " " + rangeArgument +
158 + " is not valid.");
159 + parserException.setLine(ctx.getStart().getLine());
160 + parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
161 + throw parserException;
162 + }
163 +
164 + if (rangeBoundary.length == 1) {
165 + startInterval = rangeBoundary[0];
166 + endInterval = rangeBoundary[0];
167 + } else {
168 + startInterval = rangeBoundary[0];
169 + endInterval = rangeBoundary[1];
170 + }
171 +
172 + try {
173 + startValue = getDataObjectFromString(startInterval, YangDataTypes.UINT64);
174 + endValue = getDataObjectFromString(endInterval, YangDataTypes.UINT64);
175 + } catch (DataTypeException e) {
176 + ParserException parserException = new ParserException(e.getMessage());
177 + parserException.setLine(ctx.getStart().getLine());
178 + parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
179 + throw parserException;
180 + }
181 +
182 + rangeInterval.setStartValue(startValue);
183 + rangeInterval.setEndValue(endValue);
184 +
185 + try {
186 + lengthRestriction.addRangeRestrictionInterval(rangeInterval);
187 + } catch (DataModelException e) {
188 + ParserException parserException = new ParserException(constructExtendedListenerErrorMessage(
189 + UNHANDLED_PARSED_DATA, LENGTH_DATA, rangeArgument, ENTRY, e.getMessage()));
190 + parserException.setLine(ctx.getStart().getLine());
191 + parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
192 + throw parserException;
193 + }
194 + }
195 +
196 + stringRestriction.setLengthRestriction(lengthRestriction);
197 + }
198 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.yangutils.parser.impl.listeners;
18 +
19 +import org.onosproject.yangutils.datamodel.YangDerivedInfo;
20 +import org.onosproject.yangutils.datamodel.YangType;
21 +import org.onosproject.yangutils.datamodel.YangStringRestriction;
22 +import org.onosproject.yangutils.datamodel.YangDataTypes;
23 +import org.onosproject.yangutils.parser.Parsable;
24 +import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
25 +import org.onosproject.yangutils.parser.exceptions.ParserException;
26 +import org.onosproject.yangutils.parser.impl.TreeWalkListener;
27 +import org.onosproject.yangutils.utils.YangConstructType;
28 +
29 +import static org.onosproject.yangutils.utils.YangConstructType.PATTERN_DATA;
30 +import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
31 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
32 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
33 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
34 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
35 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
36 +
37 +/*
38 + * Reference: RFC6020 and YANG ANTLR Grammar
39 + *
40 + * ABNF grammar as per RFC6020
41 + * pattern-stmt = pattern-keyword sep string optsep
42 + * (";" /
43 + * "{" stmtsep
44 + * ;; these stmts can appear in any order
45 + * [error-message-stmt stmtsep]
46 + * [error-app-tag-stmt stmtsep]
47 + * [description-stmt stmtsep]
48 + * [reference-stmt stmtsep]
49 + * "}")
50 + *
51 + * ANTLR grammar rule
52 + * patternStatement : PATTERN_KEYWORD string (STMTEND | LEFT_CURLY_BRACE commonStatements RIGHT_CURLY_BRACE);
53 + */
54 +
55 +/**
56 + * Represents listener based call back function corresponding to the "pattern"
57 + * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
58 + */
59 +public final class PatternRestrictionListener {
60 +
61 + private static final String EMPTY_STRING = "";
62 +
63 + /**
64 + * Creates a new pattern restriction listener.
65 + */
66 + private PatternRestrictionListener() {
67 + }
68 +
69 + /**
70 + * It is called when parser receives an input matching the grammar
71 + * rule (pattern), performs validation and updates the data model
72 + * tree.
73 + *
74 + * @param listener listener's object
75 + * @param ctx context object of the grammar rule
76 + */
77 + public static void processPatternRestrictionEntry(TreeWalkListener listener,
78 + GeneratedYangParser.PatternStatementContext ctx) {
79 +
80 + // Check for stack to be non empty.
81 + checkStackIsNotEmpty(listener, MISSING_HOLDER, PATTERN_DATA, ctx.string().getText(), ENTRY);
82 +
83 + Parsable tmpData = listener.getParsedDataStack().peek();
84 + if (tmpData.getYangConstructType() == TYPE_DATA) {
85 + YangType type = (YangType) tmpData;
86 + setPatternRestriction(type, ctx);
87 + } else {
88 + throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, PATTERN_DATA,
89 + ctx.string().getText(), ENTRY));
90 + }
91 + }
92 +
93 + /**
94 + * Sets the pattern restriction to type.
95 + *
96 + * @param type Yang type for which pattern restriction to be set
97 + * @param ctx context object of the grammar rule
98 + */
99 + private static void setPatternRestriction(YangType type,
100 + GeneratedYangParser.PatternStatementContext ctx) {
101 +
102 + YangStringRestriction stringRestriction;
103 +
104 + if (type.getDataType() != YangDataTypes.STRING && type.getDataType() != YangDataTypes.DERIVED) {
105 +
106 + ParserException parserException = new ParserException("YANG file error : " +
107 + YangConstructType.getYangConstructType(PATTERN_DATA) + " name " + ctx.string().getText() +
108 + " can be used to restrict the built-in type string or types derived from string.");
109 + parserException.setLine(ctx.getStart().getLine());
110 + parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
111 + throw parserException;
112 + }
113 +
114 + if (type.getDataType() == YangDataTypes.STRING) {
115 + stringRestriction = (YangStringRestriction) type.getDataTypeExtendedInfo();
116 + } else {
117 + stringRestriction = (YangStringRestriction) ((YangDerivedInfo<?>) type
118 + .getDataTypeExtendedInfo()).getExtendedInfo();
119 + }
120 +
121 + if (stringRestriction == null) {
122 + stringRestriction = new YangStringRestriction();
123 + if (type.getDataType() == YangDataTypes.STRING) {
124 + type.setDataTypeExtendedInfo(stringRestriction);
125 + } else {
126 + ((YangDerivedInfo<YangStringRestriction>) type.getDataTypeExtendedInfo())
127 + .setExtendedInfo(stringRestriction);
128 + }
129 + }
130 +
131 + String patternArgument = ctx.string().getText().replace("\"", EMPTY_STRING);
132 + stringRestriction.addPattern(patternArgument);
133 + }
134 +}
...@@ -16,29 +16,33 @@ ...@@ -16,29 +16,33 @@
16 16
17 package org.onosproject.yangutils.parser.impl.listeners; 17 package org.onosproject.yangutils.parser.impl.listeners;
18 18
19 -import java.math.BigInteger;
20 import java.util.regex.Pattern; 19 import java.util.regex.Pattern;
21 20
22 -import org.onosproject.yangutils.datamodel.YangType;
23 -import org.onosproject.yangutils.datamodel.YangRangeRestriction;
24 import org.onosproject.yangutils.datamodel.YangRangeInterval; 21 import org.onosproject.yangutils.datamodel.YangRangeInterval;
25 -import org.onosproject.yangutils.datamodel.YangDataTypes; 22 +import org.onosproject.yangutils.datamodel.YangRangeRestriction;
23 +import org.onosproject.yangutils.datamodel.YangType;
24 +import org.onosproject.yangutils.datamodel.YangDerivedInfo;
26 import org.onosproject.yangutils.datamodel.exceptions.DataModelException; 25 import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
26 +import org.onosproject.yangutils.datamodel.YangDataTypes;
27 import org.onosproject.yangutils.parser.Parsable; 27 import org.onosproject.yangutils.parser.Parsable;
28 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 28 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
29 import org.onosproject.yangutils.parser.exceptions.ParserException; 29 import org.onosproject.yangutils.parser.exceptions.ParserException;
30 import org.onosproject.yangutils.parser.impl.TreeWalkListener; 30 import org.onosproject.yangutils.parser.impl.TreeWalkListener;
31 +import org.onosproject.yangutils.utils.YangConstructType;
32 +import org.onosproject.yangutils.utils.builtindatatype.DataTypeException;
33 +import org.onosproject.yangutils.utils.builtindatatype.YangBuiltInDataTypeInfo;
31 34
32 -import static org.onosproject.yangutils.utils.YangConstructType.RANGE_DATA;
33 -import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
34 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; 35 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
35 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; 36 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
36 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; 37 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
37 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; 38 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
38 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; 39 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
39 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; 40 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
40 -import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
41 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat; 41 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
42 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
43 +import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
44 +import static org.onosproject.yangutils.utils.YangConstructType.RANGE_DATA;
45 +import static org.onosproject.yangutils.utils.builtindatatype.BuiltInTypeObjectFactory.getDataObjectFromString;
42 46
43 /* 47 /*
44 * Reference: RFC6020 and YANG ANTLR Grammar 48 * Reference: RFC6020 and YANG ANTLR Grammar
...@@ -78,8 +82,8 @@ public final class RangeRestrictionListener { ...@@ -78,8 +82,8 @@ public final class RangeRestrictionListener {
78 * rule (range), performs validation and updates the data model 82 * rule (range), performs validation and updates the data model
79 * tree. 83 * tree.
80 * 84 *
81 - * @param listener listener's object. 85 + * @param listener listener's object
82 - * @param ctx context object of the grammar rule. 86 + * @param ctx context object of the grammar rule
83 */ 87 */
84 public static void processRangeRestrictionEntry(TreeWalkListener listener, 88 public static void processRangeRestrictionEntry(TreeWalkListener listener,
85 GeneratedYangParser.RangeStatementContext ctx) { 89 GeneratedYangParser.RangeStatementContext ctx) {
...@@ -100,69 +104,72 @@ public final class RangeRestrictionListener { ...@@ -100,69 +104,72 @@ public final class RangeRestrictionListener {
100 /** 104 /**
101 * Sets the range restriction to type. 105 * Sets the range restriction to type.
102 * 106 *
103 - * @param type YANG type for which range restriction to be added. 107 + * @param type YANG type for which range restriction to be added
104 - * @param ctx context object of the grammar rule. 108 + * @param ctx context object of the grammar rule
105 */ 109 */
106 private static void setRangeRestriction(YangType type, 110 private static void setRangeRestriction(YangType type,
107 GeneratedYangParser.RangeStatementContext ctx) { 111 GeneratedYangParser.RangeStatementContext ctx) {
108 - YangRangeRestriction<?> rangeRestriction = null; 112 +
109 - YangRangeInterval rangeInterval; 113 + YangBuiltInDataTypeInfo<?> startValue, endValue;
114 + YangRangeRestriction rangeRestriction = new YangRangeRestriction();
110 115
111 String rangeArgument = removeQuotesAndHandleConcat(ctx.range().getText()); 116 String rangeArgument = removeQuotesAndHandleConcat(ctx.range().getText());
112 String[] rangeArguments = rangeArgument.trim().split(Pattern.quote(PIPE)); 117 String[] rangeArguments = rangeArgument.trim().split(Pattern.quote(PIPE));
113 118
114 for (String rangePart : rangeArguments) { 119 for (String rangePart : rangeArguments) {
120 + String startInterval, endInterval;
121 + YangRangeInterval rangeInterval = new YangRangeInterval();
115 String[] rangeBoundary = rangePart.trim().split(Pattern.quote(RANGE_INTERVAL)); 122 String[] rangeBoundary = rangePart.trim().split(Pattern.quote(RANGE_INTERVAL));
116 123
117 - if (rangeBoundary.length == 1) { 124 + if (rangeBoundary.length > 2) {
118 - rangeBoundary[1] = rangeBoundary[0]; 125 + ParserException parserException = new ParserException("YANG file error : " +
126 + YangConstructType.getYangConstructType(RANGE_DATA) + " " + rangeArgument +
127 + " is not valid.");
128 + parserException.setLine(ctx.getStart().getLine());
129 + parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
130 + throw parserException;
119 } 131 }
120 132
121 - if (type.getDataType() == YangDataTypes.INT8) { 133 + if (rangeBoundary.length == 1) {
122 - rangeRestriction = new YangRangeRestriction<Byte>(); 134 + startInterval = rangeBoundary[0];
123 - rangeInterval = new YangRangeInterval<Byte>(); 135 + endInterval = rangeBoundary[0];
124 - rangeInterval.setStartValue(Byte.parseByte(rangeBoundary[0]));
125 - rangeInterval.setEndValue(Byte.parseByte(rangeBoundary[1]));
126 - } else if ((type.getDataType() == YangDataTypes.INT16)
127 - || (type.getDataType() == YangDataTypes.UINT8)) {
128 - rangeRestriction = new YangRangeRestriction<Short>();
129 - rangeInterval = new YangRangeInterval<Short>();
130 - rangeInterval.setStartValue(Short.parseShort(rangeBoundary[0]));
131 - rangeInterval.setEndValue(Short.parseShort(rangeBoundary[1]));
132 - } else if ((type.getDataType() == YangDataTypes.INT32)
133 - || (type.getDataType() == YangDataTypes.UINT16)) {
134 - rangeRestriction = new YangRangeRestriction<Integer>();
135 - rangeInterval = new YangRangeInterval<Integer>();
136 - rangeInterval.setStartValue(Integer.parseInt(rangeBoundary[0]));
137 - rangeInterval.setEndValue(Integer.parseInt(rangeBoundary[1]));
138 - } else if ((type.getDataType() == YangDataTypes.INT64)
139 - || (type.getDataType() == YangDataTypes.UINT32)) {
140 - rangeRestriction = new YangRangeRestriction<Long>();
141 - rangeInterval = new YangRangeInterval<Long>();
142 - rangeInterval.setStartValue(Long.parseLong(rangeBoundary[0]));
143 - rangeInterval.setEndValue(Long.parseLong(rangeBoundary[1]));
144 - } else if (type.getDataType() == YangDataTypes.UINT64) {
145 - rangeRestriction = new YangRangeRestriction<BigInteger>();
146 - rangeInterval = new YangRangeInterval<BigInteger>();
147 - rangeInterval.setStartValue(new BigInteger(rangeBoundary[0]));
148 - rangeInterval.setEndValue(new BigInteger(rangeBoundary[0]));
149 } else { 136 } else {
150 - //TODO: support derived for base built in type of string 137 + startInterval = rangeBoundary[0];
151 - throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, RANGE_DATA, 138 + endInterval = rangeBoundary[1];
152 - rangeArgument, ENTRY));
153 } 139 }
154 140
155 try { 141 try {
142 + startValue = getDataObjectFromString(startInterval, type.getDataType());
143 + endValue = getDataObjectFromString(endInterval, type.getDataType());
144 + } catch (DataTypeException e) {
145 + ParserException parserException = new ParserException(e.getMessage());
146 + parserException.setLine(ctx.getStart().getLine());
147 + parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
148 + throw parserException;
149 + }
150 +
151 + rangeInterval.setStartValue(startValue);
152 + rangeInterval.setEndValue(endValue);
153 +
154 + try {
156 rangeRestriction.addRangeRestrictionInterval(rangeInterval); 155 rangeRestriction.addRangeRestrictionInterval(rangeInterval);
157 } catch (DataModelException e) { 156 } catch (DataModelException e) {
158 - throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, RANGE_DATA, 157 + ParserException parserException = new ParserException(constructExtendedListenerErrorMessage(
159 - rangeArgument, ENTRY, e.getMessage())); 158 + UNHANDLED_PARSED_DATA, RANGE_DATA, rangeArgument, ENTRY, e.getMessage()));
159 + parserException.setLine(ctx.getStart().getLine());
160 + parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
161 + throw parserException;
160 } 162 }
161 } 163 }
162 164
163 if (rangeRestriction != null) { 165 if (rangeRestriction != null) {
166 + if (type.getDataType() == YangDataTypes.DERIVED) {
167 + ((YangDerivedInfo<YangRangeRestriction>) type.getDataTypeExtendedInfo())
168 + .setExtendedInfo(rangeRestriction);
169 + } else {
164 type.setDataTypeExtendedInfo(rangeRestriction); 170 type.setDataTypeExtendedInfo(rangeRestriction);
165 } 171 }
172 + }
166 173
167 } 174 }
168 } 175 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -98,36 +98,6 @@ public final class TypeListener { ...@@ -98,36 +98,6 @@ public final class TypeListener {
98 type.setNodeIdentifier(nodeIdentifier); 98 type.setNodeIdentifier(nodeIdentifier);
99 type.setDataType(yangDataTypes); 99 type.setDataType(yangDataTypes);
100 100
101 - // Push the type to the stack.
102 - listener.getParsedDataStack().push(type);
103 - }
104 -
105 - /**
106 - * It is called when parser exits from grammar rule (type), it perform
107 - * validations and update the data model tree.
108 - *
109 - * @param listener Listener's object
110 - * @param ctx context object of the grammar rule
111 - */
112 - public static void processTypeExit(TreeWalkListener listener,
113 - GeneratedYangParser.TypeStatementContext ctx) {
114 -
115 - // Check for stack to be non empty.
116 - checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT);
117 -
118 - Parsable parsableType = listener.getParsedDataStack().pop();
119 - if (!(parsableType instanceof YangType)) {
120 - throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
121 - ctx.string().getText(), EXIT));
122 - }
123 -
124 - YangType<?> type = (YangType<?>) parsableType;
125 -
126 - // Check for stack to be non empty.
127 - checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT);
128 -
129 - YangDataTypes yangDataTypes = YangDataTypes.getType(ctx.string().getText());
130 -
131 int errorLine = ctx.getStart().getLine(); 101 int errorLine = ctx.getStart().getLine();
132 int errorPosition = ctx.getStart().getCharPositionInLine(); 102 int errorPosition = ctx.getStart().getCharPositionInLine();
133 103
...@@ -242,12 +212,35 @@ public final class TypeListener { ...@@ -242,12 +212,35 @@ public final class TypeListener {
242 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, 212 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
243 ctx.string().getText(), EXIT)); 213 ctx.string().getText(), EXIT));
244 } 214 }
215 +
216 + // Push the type to the stack.
217 + listener.getParsedDataStack().push(type);
218 + }
219 +
220 + /**
221 + * It is called when parser exits from grammar rule (type), it perform
222 + * validations and update the data model tree.
223 + *
224 + * @param listener Listener's object
225 + * @param ctx context object of the grammar rule
226 + */
227 + public static void processTypeExit(TreeWalkListener listener,
228 + GeneratedYangParser.TypeStatementContext ctx) {
229 +
230 + // Check for stack to be non empty.
231 + checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT);
232 +
233 + Parsable parsableType = listener.getParsedDataStack().pop();
234 + if (!(parsableType instanceof YangType)) {
235 + throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
236 + ctx.string().getText(), EXIT));
237 + }
245 } 238 }
246 239
247 /** 240 /**
248 - * Add to resolution list. 241 + * Adds to resolution list.
249 * 242 *
250 - * @param resolutionInfo resolution information. 243 + * @param resolutionInfo resolution information
251 * @param ctx context object of the grammar rule 244 * @param ctx context object of the grammar rule
252 */ 245 */
253 private static void addToResolutionList(YangResolutionInfo<YangType> resolutionInfo, 246 private static void addToResolutionList(YangResolutionInfo<YangType> resolutionInfo,
......
...@@ -277,7 +277,17 @@ public enum YangConstructType { ...@@ -277,7 +277,17 @@ public enum YangConstructType {
277 /** 277 /**
278 * Identifies the YANG range element parsed data. 278 * Identifies the YANG range element parsed data.
279 */ 279 */
280 - RANGE_DATA; 280 + RANGE_DATA,
281 +
282 + /**
283 + * Identifies the YANG length element parsed data.
284 + */
285 + LENGTH_DATA,
286 +
287 + /**
288 + * Identifies the YANG pattern element parsed data.
289 + */
290 + PATTERN_DATA;
281 291
282 /** 292 /**
283 * Returns the YANG construct keyword corresponding to enum values. 293 * Returns the YANG construct keyword corresponding to enum values.
...@@ -392,6 +402,10 @@ public enum YangConstructType { ...@@ -392,6 +402,10 @@ public enum YangConstructType {
392 return "union"; 402 return "union";
393 case RANGE_DATA: 403 case RANGE_DATA:
394 return "range"; 404 return "range";
405 + case LENGTH_DATA:
406 + return "length";
407 + case PATTERN_DATA:
408 + return "pattern";
395 default: 409 default:
396 return "yang"; 410 return "yang";
397 } 411 }
......
1 +/*-
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.yangutils.utils.builtindatatype;
17 +
18 +import org.onosproject.yangutils.datamodel.YangDataTypes;
19 +
20 +/**
21 + * Factory to create an object of required type.
22 + */
23 +public final class BuiltInTypeObjectFactory {
24 +
25 + /**
26 + * Utility factory class, hence the object creation is forbidden.
27 + */
28 + private BuiltInTypeObjectFactory() {
29 + }
30 +
31 + /**
32 + * Given the value represented in string return the corresponding types
33 + * object with the value initialized.
34 + *
35 + * @param valueInStr value represented in string
36 + * @param builtInType built in data type
37 + * @return the target data type object with the value initialized
38 + * @param <T> the data type of the target object
39 + */
40 + public static <T extends YangBuiltInDataTypeInfo<?>> T getDataObjectFromString(String valueInStr,
41 + YangDataTypes builtInType) {
42 +
43 + switch (builtInType) {
44 + case INT8: {
45 + return (T) new YangInt8(valueInStr);
46 + }
47 + case INT16: {
48 + return (T) new YangInt16(valueInStr);
49 + }
50 + case INT32: {
51 + return (T) new YangInt32(valueInStr);
52 + }
53 + case INT64: {
54 + return (T) new YangInt64(valueInStr);
55 + }
56 + case UINT8: {
57 + return (T) new YangUint8(valueInStr);
58 + }
59 + case UINT16: {
60 + return (T) new YangUint16(valueInStr);
61 + }
62 + case UINT32: {
63 + return (T) new YangUint32(valueInStr);
64 + }
65 + case UINT64: {
66 + return (T) new YangUint64(valueInStr);
67 + }
68 + default: {
69 + throw new DataTypeException("YANG file error : Unsupported data type");
70 + }
71 + }
72 +
73 + }
74 +
75 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.yangutils.utils.builtindatatype;
18 +
19 +/**
20 + * Base class for exceptions in data type.
21 + */
22 +public class DataTypeException extends RuntimeException {
23 +
24 + private static final long serialVersionUID = 20160211L;
25 +
26 + /**
27 + * Create a new data type exception.
28 + */
29 + public DataTypeException() {
30 + super();
31 + }
32 +
33 + /**
34 + * Creates a new data type exception with given message.
35 + *
36 + * @param message the detail of exception in string
37 + */
38 + public DataTypeException(String message) {
39 + super(message);
40 + }
41 +
42 + /**
43 + * Creates a new data type exception from given message and cause.
44 + *
45 + * @param message the detail of exception in string
46 + * @param cause underlying cause of the error
47 + */
48 + public DataTypeException(final String message, final Throwable cause) {
49 + super(message, cause);
50 + }
51 +
52 + /**
53 + * Creates a new data type exception from cause.
54 + *
55 + * @param cause underlying cause of the error
56 + */
57 + public DataTypeException(final Throwable cause) {
58 + super(cause);
59 + }
60 +
61 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.yangutils.utils.builtindatatype;
17 +
18 +import org.onosproject.yangutils.datamodel.YangDataTypes;
19 +
20 +/**
21 + * Represents the list of utility functions to be supported by YANG built in
22 + * data type implementations.
23 + *
24 + * @param <T> The target data type
25 + */
26 +public interface YangBuiltInDataTypeInfo<T> extends Comparable<T> {
27 +
28 + /**
29 + * Returns the YANG built in type.
30 + *
31 + * @return the YANG built in type
32 + */
33 + YangDataTypes getYangType();
34 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.yangutils.utils.builtindatatype;
17 +
18 +import org.onosproject.yangutils.datamodel.YangDataTypes;
19 +
20 +
21 +/**
22 + * Handles the YANG's int16 data type processing.
23 + *
24 + * int16 represents integer values between -32768 and 32767, inclusively.
25 + */
26 +public class YangInt16 implements YangBuiltInDataTypeInfo<YangInt16> {
27 +
28 + /**
29 + * YANG's min keyword.
30 + */
31 + private static final String MIN_KEYWORD = "min";
32 +
33 + /**
34 + * YANG's max keyword.
35 + */
36 + private static final String MAX_KEYWORD = "max";
37 +
38 + /**
39 + * Valid minimum value of YANG's int16.
40 + */
41 + public static final short MIN_VALUE = -32768;
42 +
43 + /**
44 + * Valid maximum value of YANG's int16.
45 + */
46 + public static final short MAX_VALUE = 32767;
47 +
48 + /**
49 + * The value of YANG's int16.
50 + */
51 + private final short value;
52 +
53 + /**
54 + * Creates an object with the value initialized with value represented in
55 + * string.
56 + *
57 + * @param valueInString value of the object in string
58 + */
59 + public YangInt16(String valueInString) {
60 +
61 + if (valueInString.matches(MIN_KEYWORD)) {
62 + value = MIN_VALUE;
63 + } else if (valueInString.matches(MAX_KEYWORD)) {
64 + value = MAX_VALUE;
65 + } else {
66 + try {
67 + value = Short.parseShort(valueInString);
68 + } catch (Exception e) {
69 + throw new DataTypeException("YANG file error : " + valueInString + " is not valid.");
70 + }
71 + }
72 + }
73 +
74 + /**
75 + * Returns YANG's int16 value.
76 + *
77 + * @return value of YANG's int16
78 + */
79 + public short getValue() {
80 + return value;
81 + }
82 +
83 + @Override
84 + public int compareTo(YangInt16 anotherYangInt16) {
85 + return Short.compare(value, anotherYangInt16.value);
86 + }
87 +
88 + @Override
89 + public YangDataTypes getYangType() {
90 + return YangDataTypes.INT16;
91 + }
92 +
93 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.yangutils.utils.builtindatatype;
17 +
18 +import org.onosproject.yangutils.datamodel.YangDataTypes;
19 +
20 +/**
21 + * Handles the YANG's int32 data type processing.
22 + *
23 + * int32 represents integer values between -2147483648 and 2147483647, inclusively.
24 + */
25 +public class YangInt32 implements YangBuiltInDataTypeInfo<YangInt32> {
26 +
27 + /**
28 + * YANG's min keyword.
29 + */
30 + private static final String MIN_KEYWORD = "min";
31 +
32 + /**
33 + * YANG's max keyword.
34 + */
35 + private static final String MAX_KEYWORD = "max";
36 +
37 + /**
38 + * Valid minimum value of YANG's int32.
39 + */
40 + public static final int MIN_VALUE = -2147483648;
41 +
42 + /**
43 + * Valid maximum value of YANG's int32.
44 + */
45 + public static final int MAX_VALUE = 2147483647;
46 +
47 + /**
48 + * The value of YANG's int32.
49 + */
50 + private final int value;
51 +
52 + /**
53 + * Creates an object with the value initialized with value represented in
54 + * string.
55 + *
56 + * @param valueInString value of the object in string
57 + */
58 + public YangInt32(String valueInString) {
59 +
60 + if (valueInString.matches(MIN_KEYWORD)) {
61 + value = MIN_VALUE;
62 + } else if (valueInString.matches(MAX_KEYWORD)) {
63 + value = MAX_VALUE;
64 + } else {
65 + try {
66 + value = Integer.parseInt(valueInString);
67 + } catch (Exception e) {
68 + throw new DataTypeException("YANG file error : " + valueInString + " is not valid.");
69 + }
70 + }
71 + }
72 +
73 +
74 + /**
75 + * Returns YANG's int32 value.
76 + *
77 + * @return value of YANG's int32
78 + */
79 + public int getValue() {
80 + return value;
81 + }
82 +
83 + @Override
84 + public int compareTo(YangInt32 anotherYangInt32) {
85 + return Integer.compare(value, anotherYangInt32.value);
86 + }
87 +
88 + @Override
89 + public YangDataTypes getYangType() {
90 + return YangDataTypes.INT32;
91 + }
92 +
93 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.yangutils.utils.builtindatatype;
17 +
18 +import org.onosproject.yangutils.datamodel.YangDataTypes;
19 +
20 +/**
21 + * Handles the YANG's int8 data type processing.
22 + *
23 + * int8 represents integer values between -9223372036854775808 and 9223372036854775807, inclusively.
24 + */
25 +public class YangInt64 implements YangBuiltInDataTypeInfo<YangInt64> {
26 +
27 + /**
28 + * YANG's min keyword.
29 + */
30 + private static final String MIN_KEYWORD = "min";
31 +
32 + /**
33 + * YANG's max keyword.
34 + */
35 + private static final String MAX_KEYWORD = "max";
36 +
37 + /**
38 + * Valid minimum value of YANG's int64.
39 + */
40 + public static final Long MIN_VALUE = 0x8000000000000000L;
41 +
42 + /**
43 + * Valid maximum value of YANG's int64.
44 + */
45 + public static final long MAX_VALUE = 0x7fffffffffffffffL;
46 +
47 + /**
48 + * The value of YANG's int64.
49 + */
50 + private final long value;
51 +
52 + /**
53 + * Creates an object with the value initialized with value represented in
54 + * string.
55 + *
56 + * @param valueInString value of the object in string
57 + */
58 + public YangInt64(String valueInString) {
59 +
60 + if (valueInString.matches(MIN_KEYWORD)) {
61 + value = MIN_VALUE;
62 + } else if (valueInString.matches(MAX_KEYWORD)) {
63 + value = MAX_VALUE;
64 + } else {
65 + try {
66 + value = Long.parseLong(valueInString);
67 + } catch (Exception e) {
68 + throw new DataTypeException("YANG file error : " + valueInString + " is not valid.");
69 + }
70 + }
71 + }
72 +
73 +
74 + /**
75 + * Returns YANG's int64 value.
76 + *
77 + * @return value of YANG's int64
78 + */
79 + public long getValue() {
80 + return value;
81 + }
82 +
83 + @Override
84 + public int compareTo(YangInt64 anotherYangInt64) {
85 + return Long.compare(value, anotherYangInt64.value);
86 + }
87 +
88 + @Override
89 + public YangDataTypes getYangType() {
90 + return YangDataTypes.INT64;
91 + }
92 +
93 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.yangutils.utils.builtindatatype;
17 +
18 +import org.onosproject.yangutils.datamodel.YangDataTypes;
19 +
20 +/**
21 + * Handles the YANG's int8 data type processing.
22 + *
23 + * int8 represents integer values between -128 and 127, inclusively.
24 + */
25 +public class YangInt8 implements YangBuiltInDataTypeInfo<YangInt8> {
26 +
27 + /**
28 + * YANG's min keyword.
29 + */
30 + private static final String MIN_KEYWORD = "min";
31 +
32 + /**
33 + * YANG's max keyword.
34 + */
35 + private static final String MAX_KEYWORD = "max";
36 +
37 + /**
38 + * Valid minimum value of YANG's int8.
39 + */
40 + public static final byte MIN_VALUE = -128;
41 +
42 + /**
43 + * Valid maximum value of YANG's int8.
44 + */
45 + public static final byte MAX_VALUE = 127;
46 +
47 + /**
48 + * The value of YANG's int8.
49 + */
50 + private final byte value;
51 +
52 + /**
53 + * Creates an object with the value initialized with value represented in
54 + * string.
55 + *
56 + * @param valueInString value of the object in string
57 + */
58 + public YangInt8(String valueInString) {
59 +
60 + if (valueInString.matches(MIN_KEYWORD)) {
61 + value = MIN_VALUE;
62 + } else if (valueInString.matches(MAX_KEYWORD)) {
63 + value = MAX_VALUE;
64 + } else {
65 + try {
66 + value = Byte.parseByte(valueInString);
67 + } catch (Exception e) {
68 + throw new DataTypeException("YANG file error : " + valueInString + " is not valid.");
69 + }
70 + }
71 + }
72 +
73 + /**
74 + * Returns YANG's int8 value.
75 + *
76 + * @return value of YANG's int8
77 + */
78 + public byte getValue() {
79 + return value;
80 + }
81 +
82 + @Override
83 + public int compareTo(YangInt8 anotherYangInt8) {
84 + return Byte.compare(value, anotherYangInt8.value);
85 + }
86 +
87 + @Override
88 + public YangDataTypes getYangType() {
89 + return YangDataTypes.INT8;
90 + }
91 +
92 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.yangutils.utils.builtindatatype;
17 +
18 +import org.onosproject.yangutils.datamodel.YangDataTypes;
19 +
20 +/**
21 + * Handles the YANG's Uint16 data type processing.
22 + *
23 + * Uint16 represents integer values between 0 and 65535, inclusively.
24 + */
25 +public class YangUint16 implements YangBuiltInDataTypeInfo<YangUint16> {
26 +
27 + /**
28 + * YANG's min keyword.
29 + */
30 + private static final String MIN_KEYWORD = "min";
31 +
32 + /**
33 + * YANG's max keyword.
34 + */
35 + private static final String MAX_KEYWORD = "max";
36 +
37 + /**
38 + * Valid minimum value of YANG's Uint16.
39 + */
40 + public static final int MIN_VALUE = 0;
41 +
42 + /**
43 + * Valid maximum value of YANG's Uint16.
44 + */
45 + public static final int MAX_VALUE = 65535;
46 +
47 + /**
48 + * Value of the object.
49 + */
50 + private int value;
51 +
52 + /**
53 + * Creates an object with the value initialized with value represented in
54 + * string.
55 + *
56 + * @param valueInString value of the object in string
57 + */
58 + YangUint16(String valueInString) {
59 +
60 + if (valueInString.matches(MIN_KEYWORD)) {
61 + value = MIN_VALUE;
62 + } else if (valueInString.matches(MAX_KEYWORD)) {
63 + value = MAX_VALUE;
64 + } else {
65 + try {
66 + value = Integer.parseInt(valueInString);
67 + } catch (Exception e) {
68 + throw new DataTypeException("YANG file error : " + valueInString + " is not valid.");
69 + }
70 + }
71 +
72 + if (value < MIN_VALUE) {
73 + throw new DataTypeException("YANG file error : " + valueInString + " is lesser than minimum value "
74 + + MIN_VALUE + ".");
75 + } else if (value > MAX_VALUE) {
76 + throw new DataTypeException("YANG file error : " + valueInString + " is greater than maximum value "
77 + + MAX_VALUE + ".");
78 + }
79 + }
80 +
81 + /**
82 + * Returns YANG's uint16 value.
83 + *
84 + * @return value of YANG's uint16
85 + */
86 + public int getValue() {
87 + return value;
88 + }
89 +
90 + @Override
91 + public int compareTo(YangUint16 another) {
92 + return Integer.compare(value, another.value);
93 + }
94 +
95 + @Override
96 + public YangDataTypes getYangType() {
97 + return YangDataTypes.UINT16;
98 + }
99 +
100 +}
101 +
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.yangutils.utils.builtindatatype;
17 +
18 +import org.onosproject.yangutils.datamodel.YangDataTypes;
19 +
20 +/**
21 + * Handles the YANG's Uint32 data type processing.
22 + *
23 + * Uint32 represents integer values between 0 and 4294967295, inclusively.
24 + */
25 +public class YangUint32 implements YangBuiltInDataTypeInfo<YangUint32> {
26 +
27 + private static final String MIN_KEYWORD = "min";
28 + private static final String MAX_KEYWORD = "max";
29 +
30 + /**
31 + * Valid minimum value of YANG's Uint32.
32 + */
33 + public static final long MIN_VALUE = 0;
34 +
35 + /**
36 + * Valid maximum value of YANG's Uint32.
37 + */
38 + public static final long MAX_VALUE = 4294967295L;
39 +
40 + /**
41 + * Value of the object.
42 + */
43 + private long value;
44 +
45 + /**
46 + * Creates an object with the value initialized with value represented in
47 + * string.
48 + *
49 + * @param valueInString value of the object in string
50 + */
51 + YangUint32(String valueInString) {
52 +
53 + if (valueInString.matches(MIN_KEYWORD)) {
54 + value = MIN_VALUE;
55 + } else if (valueInString.matches(MAX_KEYWORD)) {
56 + value = MAX_VALUE;
57 + } else {
58 + try {
59 + value = Long.parseLong(valueInString);
60 + } catch (Exception e) {
61 + throw new DataTypeException("YANG file error : " + valueInString + " is not valid.");
62 + }
63 + }
64 +
65 + if (value < MIN_VALUE) {
66 + throw new DataTypeException("YANG file error : " + valueInString + " is lesser than minimum value "
67 + + MIN_VALUE + ".");
68 + } else if (value > MAX_VALUE) {
69 + throw new DataTypeException("YANG file error : " + valueInString + " is greater than maximum value "
70 + + MAX_VALUE + ".");
71 + }
72 + }
73 +
74 + /**
75 + * Returns YANG's uint32 value.
76 + *
77 + * @return value of YANG's uint32
78 + */
79 + public long getValue() {
80 + return value;
81 + }
82 +
83 + @Override
84 + public int compareTo(YangUint32 another) {
85 + return Long.compare(value, another.value);
86 + }
87 +
88 + @Override
89 + public YangDataTypes getYangType() {
90 + return YangDataTypes.UINT32;
91 + }
92 +
93 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.yangutils.utils.builtindatatype;
17 +
18 +import java.math.BigInteger;
19 +import java.util.regex.Pattern;
20 +import org.onosproject.yangutils.datamodel.YangDataTypes;
21 +
22 +/**
23 + * Handles the YANG's Uint16 data type processing.
24 + *
25 + * Uint64 represents integer values between 0 and 18446744073709551615, inclusively.
26 + */
27 +public class YangUint64 implements YangBuiltInDataTypeInfo<YangUint64> {
28 +
29 + /**
30 + * YANG's min keyword.
31 + */
32 + private static final String MIN_KEYWORD = "min";
33 +
34 + /**
35 + * YANG's max keyword.
36 + */
37 + private static final String MAX_KEYWORD = "max";
38 +
39 + /**
40 + * YANG's Integer value pattern.
41 + */
42 + private static final Pattern NON_NEGATIVE_INTEGER_PATTERN = Pattern.compile("[0-9]+");
43 +
44 + /**
45 + * Valid minimum value of YANG's Uint64.
46 + */
47 + public static final BigInteger MIN_VALUE = BigInteger.valueOf(0);
48 +
49 + /**
50 + * Valid maximum value of YANG's Uint64.
51 + */
52 + public static final BigInteger MAX_VALUE = new BigInteger("18446744073709551615");
53 +
54 + /**
55 + * Value of the object.
56 + */
57 + private BigInteger value;
58 +
59 + /**
60 + * Creates an object with the value initialized with value represented in
61 + * string.
62 + *
63 + * @param valueInString value of the object in string
64 + */
65 + YangUint64(String valueInString) {
66 +
67 + if (valueInString.matches(MIN_KEYWORD)) {
68 + value = MIN_VALUE;
69 + } else if (valueInString.matches(MAX_KEYWORD)) {
70 + value = MAX_VALUE;
71 + } else if (NON_NEGATIVE_INTEGER_PATTERN.matcher(valueInString).matches()) {
72 + value = new BigInteger(valueInString);
73 + } else {
74 + throw new DataTypeException("YANG file error : " + valueInString + " is not valid.");
75 + }
76 +
77 + if (value.compareTo(MIN_VALUE) < 0) {
78 + throw new DataTypeException("YANG file error : " + valueInString + " is lesser than minimum value "
79 + + MIN_VALUE + ".");
80 + } else if (value.compareTo(MAX_VALUE) > 0) {
81 + throw new DataTypeException("YANG file error : " + valueInString + " is greater than maximum value "
82 + + MAX_VALUE + ".");
83 + }
84 + }
85 +
86 + /**
87 + * Returns YANG's uint64 value.
88 + *
89 + * @return value of YANG's uint64
90 + */
91 + public BigInteger getValue() {
92 + return value;
93 + }
94 +
95 + @Override
96 + public int compareTo(YangUint64 another) {
97 + return value.compareTo(another.value);
98 + }
99 +
100 + @Override
101 + public YangDataTypes getYangType() {
102 + return YangDataTypes.UINT64;
103 + }
104 +
105 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.yangutils.utils.builtindatatype;
17 +
18 +import org.onosproject.yangutils.datamodel.YangDataTypes;
19 +
20 +/**
21 + * Handles the YANG's Uint8 data type processing.
22 + *
23 + * Uint8 represents integer values between 0 and 255, inclusively.
24 + */
25 +public class YangUint8 implements YangBuiltInDataTypeInfo<YangUint8> {
26 +
27 + /**
28 + * YANG's min keyword.
29 + */
30 + private static final String MIN_KEYWORD = "min";
31 +
32 + /**
33 + * YANG's max keyword.
34 + */
35 + private static final String MAX_KEYWORD = "max";
36 +
37 + /**
38 + * Valid minimum value of YANG's Uint8.
39 + */
40 + public static final short MIN_VALUE = 0;
41 +
42 + /**
43 + * Valid maximum value of YANG's Uint8.
44 + */
45 + public static final short MAX_VALUE = 255;
46 +
47 + /**
48 + * Value of the object.
49 + */
50 + private short value;
51 +
52 + /**
53 + * Creates an object with the value initialized with value represented in
54 + * string.
55 + *
56 + * @param valueInString value of the object in string
57 + */
58 + YangUint8(String valueInString) {
59 +
60 + if (valueInString.matches(MIN_KEYWORD)) {
61 + value = MIN_VALUE;
62 + } else if (valueInString.matches(MAX_KEYWORD)) {
63 + value = MAX_VALUE;
64 + } else {
65 + try {
66 + value = Short.parseShort(valueInString);
67 + } catch (Exception e) {
68 + throw new DataTypeException("YANG file error : " + valueInString + " is not valid.");
69 + }
70 + }
71 +
72 + if (value < MIN_VALUE) {
73 + throw new DataTypeException("YANG file error : " + valueInString + " is lesser than minimum value "
74 + + MIN_VALUE + ".");
75 + } else if (value > MAX_VALUE) {
76 + throw new DataTypeException("YANG file error : " + valueInString + " is greater than maximum value "
77 + + MAX_VALUE + ".");
78 + }
79 + }
80 +
81 + /**
82 + * Returns YANG's uint8 value.
83 + *
84 + * @return value of YANG's uint8
85 + */
86 + public short getValue() {
87 + return value;
88 + }
89 +
90 + @Override
91 + public int compareTo(YangUint8 another) {
92 + return Short.compare(value, another.value);
93 + }
94 +
95 + @Override
96 + public YangDataTypes getYangType() {
97 + return YangDataTypes.UINT8;
98 + }
99 +
100 +}
1 +/*-
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +/**
18 + * Utilities for YANG built in data types.
19 + */
20 +package org.onosproject.yangutils.utils.builtindatatype;
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.yangutils.parser.impl.listeners;
18 +
19 +import java.io.IOException;
20 +import java.math.BigInteger;
21 +import java.util.ListIterator;
22 +
23 +import org.junit.Rule;
24 +import org.junit.Test;
25 +
26 +import org.junit.rules.ExpectedException;
27 +import org.onosproject.yangutils.datamodel.YangNode;
28 +import org.onosproject.yangutils.datamodel.YangModule;
29 +import org.onosproject.yangutils.datamodel.YangNodeType;
30 +import org.onosproject.yangutils.datamodel.YangLeaf;
31 +import org.onosproject.yangutils.datamodel.YangLeafList;
32 +import org.onosproject.yangutils.datamodel.YangDataTypes;
33 +import org.onosproject.yangutils.datamodel.YangStringRestriction;
34 +import org.onosproject.yangutils.datamodel.YangRangeInterval;
35 +import org.onosproject.yangutils.datamodel.YangRangeRestriction;
36 +import org.onosproject.yangutils.datamodel.YangTypeDef;
37 +import org.onosproject.yangutils.parser.exceptions.ParserException;
38 +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
39 +import org.onosproject.yangutils.utils.builtindatatype.YangUint64;
40 +
41 +import static org.hamcrest.MatcherAssert.assertThat;
42 +import static org.hamcrest.core.Is.is;
43 +
44 +/**
45 + * Test cases for length restriction listener.
46 + */
47 +public class LengthRestrictionListenerTest {
48 +
49 + @Rule
50 + public ExpectedException thrown = ExpectedException.none();
51 +
52 + private final YangUtilsParserManager manager = new YangUtilsParserManager();
53 +
54 + /**
55 + * Checks valid length statement as sub-statement of leaf statement.
56 + */
57 + @Test
58 + public void processValidLengthStatement() throws IOException, ParserException {
59 +
60 + YangNode node = manager.getDataModel("src/test/resources/ValidLengthStatement.yang");
61 +
62 + assertThat((node instanceof YangModule), is(true));
63 + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
64 + YangModule yangNode = (YangModule) node;
65 + assertThat(yangNode.getName(), is("Test"));
66 +
67 + ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
68 + YangLeaf leafInfo = leafIterator.next();
69 +
70 + assertThat(leafInfo.getLeafName(), is("invalid-interval"));
71 + assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
72 + assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
73 + YangStringRestriction stringRestriction = (YangStringRestriction) leafInfo
74 + .getDataType().getDataTypeExtendedInfo();
75 + YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction();
76 +
77 + ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals()
78 + .listIterator();
79 +
80 + YangRangeInterval rangeInterval = lengthListIterator.next();
81 +
82 + assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(0)));
83 + assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(100)));
84 + }
85 +
86 + /**
87 + * Checks valid length statement as sub-statement of leaf-list.
88 + */
89 + @Test
90 + public void processLengthStatementInsideLeafList() throws IOException, ParserException {
91 +
92 + YangNode node = manager.getDataModel("src/test/resources/LengthStatementInsideLeafList.yang");
93 +
94 + assertThat((node instanceof YangModule), is(true));
95 + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
96 + YangModule yangNode = (YangModule) node;
97 + assertThat(yangNode.getName(), is("Test"));
98 +
99 + ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
100 + YangLeafList leafListInfo = leafListIterator.next();
101 +
102 + assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
103 + assertThat(leafListInfo.getDataType().getDataTypeName(), is("string"));
104 + assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
105 + YangStringRestriction stringRestriction = (YangStringRestriction) leafListInfo
106 + .getDataType().getDataTypeExtendedInfo();
107 + YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction();
108 +
109 + ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals()
110 + .listIterator();
111 +
112 + YangRangeInterval rangeInterval = lengthListIterator.next();
113 + assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(1)));
114 + assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(100)));
115 + }
116 +
117 + /**
118 + * Checks valid length statement as sub-statement of typedef.
119 + */
120 + @Test
121 + public void processLengthStatementInsideTypeDef() throws IOException, ParserException {
122 +
123 + YangNode node = manager.getDataModel("src/test/resources/LengthStatementInsideTypeDef.yang");
124 +
125 + assertThat((node instanceof YangModule), is(true));
126 + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
127 + YangModule yangNode = (YangModule) node;
128 + assertThat(yangNode.getName(), is("Test"));
129 +
130 + YangTypeDef typedef = (YangTypeDef) yangNode.getChild();
131 + YangStringRestriction stringRestriction = (YangStringRestriction) typedef.getDataType()
132 + .getDataTypeExtendedInfo();
133 +
134 + YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction();
135 + ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals()
136 + .listIterator();
137 + YangRangeInterval rangeInterval = lengthListIterator.next();
138 + assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(1)));
139 + assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(100)));
140 + }
141 +
142 + /**
143 + * Checks length statement with invalid type.
144 + */
145 + @Test
146 + public void processLengthWithInvalidType() throws IOException, ParserException {
147 + thrown.expect(ParserException.class);
148 + thrown.expectMessage("YANG file error : length name \"1..100\" can be used to restrict the built-in type" +
149 + " string or types derived from string.");
150 + YangNode node = manager.getDataModel("src/test/resources/LengthWithInvalidType.yang");
151 + }
152 +
153 + /**
154 + * Checks length statement with only start interval.
155 + */
156 + @Test
157 + public void processLengthWithOneInterval() throws IOException, ParserException {
158 +
159 +
160 + YangNode node = manager.getDataModel("src/test/resources/LengthWithOneInterval.yang");
161 +
162 + assertThat((node instanceof YangModule), is(true));
163 + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
164 + YangModule yangNode = (YangModule) node;
165 + assertThat(yangNode.getName(), is("Test"));
166 +
167 + ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
168 + YangLeafList leafListInfo = leafListIterator.next();
169 +
170 + assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
171 + assertThat(leafListInfo.getDataType().getDataTypeName(), is("string"));
172 + assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
173 + YangStringRestriction stringRestriction = (YangStringRestriction) leafListInfo
174 + .getDataType().getDataTypeExtendedInfo();
175 + YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction();
176 +
177 + ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals()
178 + .listIterator();
179 +
180 + YangRangeInterval rangeInterval = lengthListIterator.next();
181 + assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(1)));
182 + assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(1)));
183 + }
184 +
185 + /**
186 + * Checks length statement with min and max.
187 + */
188 + @Test
189 + public void processLengthWithMinMax() throws IOException, ParserException {
190 +
191 +
192 + YangNode node = manager.getDataModel("src/test/resources/LengthWithMinMax.yang");
193 +
194 + assertThat((node instanceof YangModule), is(true));
195 + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
196 + YangModule yangNode = (YangModule) node;
197 + assertThat(yangNode.getName(), is("Test"));
198 +
199 + ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
200 + YangLeafList leafListInfo = leafListIterator.next();
201 +
202 + assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
203 + assertThat(leafListInfo.getDataType().getDataTypeName(), is("string"));
204 + assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
205 + YangStringRestriction stringRestriction = (YangStringRestriction) leafListInfo
206 + .getDataType().getDataTypeExtendedInfo();
207 + YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction();
208 +
209 + ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals()
210 + .listIterator();
211 +
212 + YangRangeInterval rangeInterval = lengthListIterator.next();
213 + assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(0)));
214 + assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(new BigInteger("18446744073709551615")));
215 + }
216 +
217 + /**
218 + * Checks length statement with invalid integer pattern.
219 + */
220 + @Test
221 + public void processLengthWithInvalidIntegerPattern() throws IOException, ParserException {
222 + thrown.expect(ParserException.class);
223 + thrown.expectMessage("YANG file error : a is not valid.");
224 + YangNode node = manager.getDataModel("src/test/resources/LengthWithInvalidIntegerPattern.yang");
225 + }
226 +
227 + /**
228 + * Checks length statement with invalid interval.
229 + */
230 + @Test
231 + public void processLengthWithInvalidInterval() throws IOException, ParserException {
232 + thrown.expect(ParserException.class);
233 + thrown.expectMessage("YANG file error : 18446744073709551617 is greater than maximum value" +
234 + " 18446744073709551615.");
235 + YangNode node = manager.getDataModel("src/test/resources/LengthWithInvalidInterval.yang");
236 + }
237 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.yangutils.parser.impl.listeners;
18 +
19 +import java.io.IOException;
20 +import java.util.ListIterator;
21 +
22 +import org.junit.Test;
23 +import org.onosproject.yangutils.datamodel.YangNode;
24 +import org.onosproject.yangutils.datamodel.YangNodeType;
25 +import org.onosproject.yangutils.datamodel.YangModule;
26 +import org.onosproject.yangutils.datamodel.YangLeaf;
27 +import org.onosproject.yangutils.datamodel.YangLeafList;
28 +import org.onosproject.yangutils.datamodel.YangDataTypes;
29 +import org.onosproject.yangutils.datamodel.YangTypeDef;
30 +import org.onosproject.yangutils.datamodel.YangPatternRestriction;
31 +import org.onosproject.yangutils.datamodel.YangStringRestriction;
32 +import org.onosproject.yangutils.parser.exceptions.ParserException;
33 +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
34 +
35 +import static org.hamcrest.MatcherAssert.assertThat;
36 +import static org.hamcrest.core.Is.is;
37 +
38 +/**
39 + * Test cases for pattern restriction listener.
40 + */
41 +public class PatternRestrictionListenerTest {
42 +
43 + private final YangUtilsParserManager manager = new YangUtilsParserManager();
44 +
45 + /**
46 + * Checks valid pattern statement as sub-statement of leaf statement.
47 + */
48 + @Test
49 + public void processValidPatternStatement() throws IOException, ParserException {
50 +
51 + YangNode node = manager.getDataModel("src/test/resources/ValidPatternStatement.yang");
52 +
53 + assertThat((node instanceof YangModule), is(true));
54 + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
55 + YangModule yangNode = (YangModule) node;
56 + assertThat(yangNode.getName(), is("Test"));
57 +
58 + ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
59 + YangLeaf leafInfo = leafIterator.next();
60 +
61 + assertThat(leafInfo.getLeafName(), is("invalid-interval"));
62 + assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
63 + assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
64 + YangStringRestriction stringRestriction = (YangStringRestriction) leafInfo
65 + .getDataType().getDataTypeExtendedInfo();
66 + ListIterator<String> patternListIterator = stringRestriction.getPatternRestriction()
67 + .getPatternList().listIterator();
68 + assertThat(patternListIterator.next(), is("[a-zA-Z]"));
69 + }
70 +
71 + /**
72 + * Checks valid pattern statement as sub-statement of leaf-list.
73 + */
74 + @Test
75 + public void processPatternStatementInsideLeafList() throws IOException, ParserException {
76 +
77 + YangNode node = manager.getDataModel("src/test/resources/PatternStatementInsideLeafList.yang");
78 +
79 + assertThat((node instanceof YangModule), is(true));
80 + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
81 + YangModule yangNode = (YangModule) node;
82 + assertThat(yangNode.getName(), is("Test"));
83 +
84 + ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
85 + YangLeafList leafListInfo = leafListIterator.next();
86 +
87 + assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
88 + assertThat(leafListInfo.getDataType().getDataTypeName(), is("string"));
89 + assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
90 + YangStringRestriction stringRestriction = (YangStringRestriction) leafListInfo
91 + .getDataType().getDataTypeExtendedInfo();
92 + ListIterator<String> patternListIterator = stringRestriction.getPatternRestriction()
93 + .getPatternList().listIterator();
94 + assertThat(patternListIterator.next(), is("[a-zA-Z]"));
95 + }
96 +
97 + /**
98 + * Checks valid pattern statement as sub-statement of typedef.
99 + */
100 + @Test
101 + public void processPatternStatementInsideTypeDef() throws IOException, ParserException {
102 +
103 + YangNode node = manager.getDataModel("src/test/resources/PatternStatementInsideTypeDef.yang");
104 +
105 + assertThat((node instanceof YangModule), is(true));
106 + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
107 + YangModule yangNode = (YangModule) node;
108 + assertThat(yangNode.getName(), is("Test"));
109 +
110 + YangTypeDef typedef = (YangTypeDef) yangNode.getChild();
111 + YangStringRestriction stringRestriction = (YangStringRestriction) typedef.getDataType()
112 + .getDataTypeExtendedInfo();
113 +
114 + YangPatternRestriction yangPatternRestriction = stringRestriction.getPatternRestriction();
115 + assertThat(yangPatternRestriction.getPatternList().listIterator().next(), is("[a-zA-Z]"));
116 + }
117 +
118 + /**
119 + * Checks valid multiple pattern statements.
120 + */
121 + @Test
122 + public void processMultiplePatternStatement() throws IOException, ParserException {
123 +
124 + YangNode node = manager.getDataModel("src/test/resources/MultiplePatternStatement.yang");
125 +
126 + assertThat((node instanceof YangModule), is(true));
127 + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
128 + YangModule yangNode = (YangModule) node;
129 + assertThat(yangNode.getName(), is("Test"));
130 +
131 + ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
132 + YangLeafList leafListInfo = leafListIterator.next();
133 +
134 + assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
135 + assertThat(leafListInfo.getDataType().getDataTypeName(), is("string"));
136 + assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
137 + YangStringRestriction stringRestriction = (YangStringRestriction) leafListInfo
138 + .getDataType().getDataTypeExtendedInfo();
139 + ListIterator<String> patternListIterator = stringRestriction.getPatternRestriction()
140 + .getPatternList().listIterator();
141 + assertThat(patternListIterator.next(), is("[a-zA-Z]"));
142 + }
143 +
144 + /**
145 + * Checks valid pattern statement with plus symbol in pattern.
146 + */
147 + @Test
148 + public void processPatternStatementWithPlus() throws IOException, ParserException {
149 +
150 + YangNode node = manager.getDataModel("src/test/resources/PatternStatementWithPlus.yang");
151 +
152 + assertThat((node instanceof YangModule), is(true));
153 + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
154 + YangModule yangNode = (YangModule) node;
155 + assertThat(yangNode.getName(), is("Test"));
156 +
157 + ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
158 + YangLeafList leafListInfo = leafListIterator.next();
159 +
160 + assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
161 + assertThat(leafListInfo.getDataType().getDataTypeName(), is("string"));
162 + assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
163 + YangStringRestriction stringRestriction = (YangStringRestriction) leafListInfo
164 + .getDataType().getDataTypeExtendedInfo();
165 + ListIterator<String> patternListIterator = stringRestriction.getPatternRestriction()
166 + .getPatternList().listIterator();
167 + assertThat(patternListIterator.next(), is("-[0-9]+|[0-9]+"));
168 + }
169 +}
...\ No newline at end of file ...\ No newline at end of file
...@@ -16,11 +16,12 @@ ...@@ -16,11 +16,12 @@
16 16
17 package org.onosproject.yangutils.parser.impl.listeners; 17 package org.onosproject.yangutils.parser.impl.listeners;
18 18
19 -
20 import java.io.IOException; 19 import java.io.IOException;
21 import java.util.ListIterator; 20 import java.util.ListIterator;
22 21
22 +import org.junit.Rule;
23 import org.junit.Test; 23 import org.junit.Test;
24 +import org.junit.rules.ExpectedException;
24 import org.onosproject.yangutils.datamodel.YangNode; 25 import org.onosproject.yangutils.datamodel.YangNode;
25 import org.onosproject.yangutils.datamodel.YangLeaf; 26 import org.onosproject.yangutils.datamodel.YangLeaf;
26 import org.onosproject.yangutils.datamodel.YangLeafList; 27 import org.onosproject.yangutils.datamodel.YangLeafList;
...@@ -31,6 +32,7 @@ import org.onosproject.yangutils.datamodel.YangNodeType; ...@@ -31,6 +32,7 @@ import org.onosproject.yangutils.datamodel.YangNodeType;
31 import org.onosproject.yangutils.datamodel.YangDataTypes; 32 import org.onosproject.yangutils.datamodel.YangDataTypes;
32 import org.onosproject.yangutils.parser.exceptions.ParserException; 33 import org.onosproject.yangutils.parser.exceptions.ParserException;
33 import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; 34 import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
35 +import org.onosproject.yangutils.utils.builtindatatype.YangInt32;
34 36
35 import static org.hamcrest.MatcherAssert.assertThat; 37 import static org.hamcrest.MatcherAssert.assertThat;
36 import static org.hamcrest.core.Is.is; 38 import static org.hamcrest.core.Is.is;
...@@ -40,6 +42,9 @@ import static org.hamcrest.core.Is.is; ...@@ -40,6 +42,9 @@ import static org.hamcrest.core.Is.is;
40 */ 42 */
41 public class RangeRestrictionListenerTest { 43 public class RangeRestrictionListenerTest {
42 44
45 + @Rule
46 + public ExpectedException thrown = ExpectedException.none();
47 +
43 private final YangUtilsParserManager manager = new YangUtilsParserManager(); 48 private final YangUtilsParserManager manager = new YangUtilsParserManager();
44 49
45 /** 50 /**
...@@ -67,8 +72,8 @@ public class RangeRestrictionListenerTest { ...@@ -67,8 +72,8 @@ public class RangeRestrictionListenerTest {
67 ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals() 72 ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
68 .listIterator(); 73 .listIterator();
69 YangRangeInterval rangeInterval = rangeListIterator.next(); 74 YangRangeInterval rangeInterval = rangeListIterator.next();
70 - assertThat(rangeInterval.getStartValue(), is(10)); 75 + assertThat(((YangInt32) rangeInterval.getStartValue()).getValue(), is(1));
71 - assertThat(rangeInterval.getEndValue(), is(20)); 76 + assertThat(((YangInt32) rangeInterval.getEndValue()).getValue(), is(4));
72 } 77 }
73 78
74 /** 79 /**
...@@ -97,7 +102,77 @@ public class RangeRestrictionListenerTest { ...@@ -97,7 +102,77 @@ public class RangeRestrictionListenerTest {
97 .listIterator(); 102 .listIterator();
98 YangRangeInterval rangeInterval = rangeListIterator.next(); 103 YangRangeInterval rangeInterval = rangeListIterator.next();
99 104
100 - assertThat(rangeInterval.getStartValue(), is(10)); 105 + assertThat(((YangInt32) rangeInterval.getStartValue()).getValue(), is(1));
101 - assertThat(rangeInterval.getEndValue(), is(20)); 106 + assertThat(((YangInt32) rangeInterval.getEndValue()).getValue(), is(4));
107 + }
108 +
109 + /**
110 + * Checks valid range statement with one interval.
111 + */
112 + @Test
113 + public void processRangeWithOneInterval() throws IOException, ParserException {
114 +
115 + YangNode node = manager.getDataModel("src/test/resources/RangeWithOneInterval.yang");
116 +
117 + assertThat((node instanceof YangModule), is(true));
118 + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
119 + YangModule yangNode = (YangModule) node;
120 + assertThat(yangNode.getName(), is("Test"));
121 +
122 + ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
123 + YangLeafList leafListInfo = leafListIterator.next();
124 +
125 + assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
126 + assertThat(leafListInfo.getDataType().getDataTypeName(), is("int32"));
127 + assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.INT32));
128 + YangRangeRestriction rangeRestriction = (YangRangeRestriction) leafListInfo
129 + .getDataType().getDataTypeExtendedInfo();
130 +
131 + ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
132 + .listIterator();
133 + YangRangeInterval rangeInterval = rangeListIterator.next();
134 +
135 + assertThat(((YangInt32) rangeInterval.getStartValue()).getValue(), is(1));
136 + assertThat(((YangInt32) rangeInterval.getEndValue()).getValue(), is(1));
137 + }
138 +
139 + /**
140 + * Checks valid range statement with min and max.
141 + */
142 + @Test
143 + public void processRangeWithMinMax() throws IOException, ParserException {
144 +
145 + YangNode node = manager.getDataModel("src/test/resources/RangeWithMinMax.yang");
146 +
147 + assertThat((node instanceof YangModule), is(true));
148 + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
149 + YangModule yangNode = (YangModule) node;
150 + assertThat(yangNode.getName(), is("Test"));
151 +
152 + ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
153 + YangLeafList leafListInfo = leafListIterator.next();
154 +
155 + assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
156 + assertThat(leafListInfo.getDataType().getDataTypeName(), is("int32"));
157 + assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.INT32));
158 + YangRangeRestriction rangeRestriction = (YangRangeRestriction) leafListInfo
159 + .getDataType().getDataTypeExtendedInfo();
160 +
161 + ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
162 + .listIterator();
163 + YangRangeInterval rangeInterval = rangeListIterator.next();
164 +
165 + assertThat(((YangInt32) rangeInterval.getStartValue()).getValue(), is(-2147483648));
166 + assertThat(((YangInt32) rangeInterval.getEndValue()).getValue(), is(2147483647));
167 + }
168 +
169 + /**
170 + * Checks valid range statement with invalid integer pattern.
171 + */
172 + @Test
173 + public void processRangeWithInvalidIntegerPattern() throws IOException, ParserException {
174 + thrown.expect(ParserException.class);
175 + thrown.expectMessage("YANG file error : a is not valid.");
176 + YangNode node = manager.getDataModel("src/test/resources/RangeWithInvalidIntegerPattern.yang");
102 } 177 }
103 } 178 }
...\ No newline at end of file ...\ No newline at end of file
......
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf-list invalid-interval {
6 + type string {
7 + length "1..100";
8 + }
9 + }
10 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + typedef invalid-interval {
6 + type string {
7 + length "1..100";
8 + }
9 + }
10 + leaf xyz {
11 + type invalid-interval {
12 + length "2..100";
13 + }
14 + }
15 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf-list invalid-interval {
6 + type string {
7 + length "a..z";
8 + }
9 + }
10 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf-list invalid-interval {
6 + type string {
7 + length "0..18446744073709551617";
8 + }
9 + }
10 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf-list invalid-interval {
6 + type int8 {
7 + length "1..100";
8 + }
9 + }
10 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf-list invalid-interval {
6 + type string {
7 + length "min..max";
8 + }
9 + }
10 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf-list invalid-interval {
6 + type string {
7 + length "1";
8 + }
9 + }
10 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf-list invalid-interval {
6 + type string {
7 + pattern "[a-zA-Z]";
8 + pattern "[a-z]";
9 + }
10 + }
11 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf-list invalid-interval {
6 + type string {
7 + pattern "[a-zA-Z]";
8 + }
9 + }
10 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + typedef invalid-interval {
6 + type string {
7 + pattern "[a-zA-Z]";
8 + }
9 + }
10 + leaf xyz {
11 + type invalid-interval {
12 + pattern "[a-z]";
13 + }
14 + }
15 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf-list invalid-interval {
6 + type string {
7 + pattern "-[0-9]+|[0-9]+";
8 + }
9 + }
10 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf-list invalid-interval {
6 + type int32 {
7 + range "a..z";
8 + }
9 + }
10 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf-list invalid-interval {
6 + type int32 {
7 + range "min..max";
8 + }
9 + }
10 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf-list invalid-interval {
6 + type int32 {
7 + range "1";
8 + }
9 + }
10 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf invalid-interval {
6 + type string {
7 + length "0..100";
8 + }
9 + }
10 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf invalid-interval {
6 + type string {
7 + pattern "[a-zA-Z]";
8 + }
9 + }
10 +}