Committed by
Gerrit Code Review
[ONOS-3897] Yang Listener for Enumeration Data Type
Change-Id: If257c73da8fe2dcc2f4111f103967cfcdd7fa273
Showing
16 changed files
with
843 additions
and
95 deletions
| ... | @@ -20,6 +20,8 @@ import org.onosproject.yangutils.datamodel.exceptions.DataModelException; | ... | @@ -20,6 +20,8 @@ import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 20 | import org.onosproject.yangutils.parser.Parsable; | 20 | import org.onosproject.yangutils.parser.Parsable; |
| 21 | import org.onosproject.yangutils.parser.ParsableDataType; | 21 | import org.onosproject.yangutils.parser.ParsableDataType; |
| 22 | 22 | ||
| 23 | +import java.util.Objects; | ||
| 24 | + | ||
| 23 | /*- | 25 | /*- |
| 24 | * The "ENUM" statement, which is a sub-statement to the "type" | 26 | * The "ENUM" statement, which is a sub-statement to the "type" |
| 25 | * statement, MUST be present if the type is "enumeration". It is | 27 | * statement, MUST be present if the type is "enumeration". It is |
| ... | @@ -188,6 +190,23 @@ public class YangEnum implements YangCommonInfo, Parsable { | ... | @@ -188,6 +190,23 @@ public class YangEnum implements YangCommonInfo, Parsable { |
| 188 | return ParsableDataType.ENUM_DATA; | 190 | return ParsableDataType.ENUM_DATA; |
| 189 | } | 191 | } |
| 190 | 192 | ||
| 193 | + @Override | ||
| 194 | + public boolean equals(Object obj) { | ||
| 195 | + if (this == obj) { | ||
| 196 | + return true; | ||
| 197 | + } | ||
| 198 | + if (obj instanceof YangEnum) { | ||
| 199 | + final YangEnum other = (YangEnum) obj; | ||
| 200 | + return Objects.equals(this.namedValue, other.namedValue); | ||
| 201 | + } | ||
| 202 | + return false; | ||
| 203 | + } | ||
| 204 | + | ||
| 205 | + @Override | ||
| 206 | + public int hashCode() { | ||
| 207 | + return Objects.hashCode(this.namedValue); | ||
| 208 | + } | ||
| 209 | + | ||
| 191 | /** | 210 | /** |
| 192 | * Validate the data on entering the corresponding parse tree node. | 211 | * Validate the data on entering the corresponding parse tree node. |
| 193 | * | 212 | * | ... | ... |
| ... | @@ -16,13 +16,12 @@ | ... | @@ -16,13 +16,12 @@ |
| 16 | 16 | ||
| 17 | package org.onosproject.yangutils.datamodel; | 17 | package org.onosproject.yangutils.datamodel; |
| 18 | 18 | ||
| 19 | -import java.util.HashSet; | ||
| 20 | -import java.util.Set; | ||
| 21 | - | ||
| 22 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; | 19 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 23 | import org.onosproject.yangutils.parser.Parsable; | 20 | import org.onosproject.yangutils.parser.Parsable; |
| 24 | import org.onosproject.yangutils.parser.ParsableDataType; | 21 | import org.onosproject.yangutils.parser.ParsableDataType; |
| 25 | -import org.onosproject.yangutils.translator.CachedFileHandle; | 22 | + |
| 23 | +import java.util.HashSet; | ||
| 24 | +import java.util.Set; | ||
| 26 | 25 | ||
| 27 | /* | 26 | /* |
| 28 | * The enumeration built-in type represents values from a set of | 27 | * The enumeration built-in type represents values from a set of |
| ... | @@ -32,20 +31,19 @@ import org.onosproject.yangutils.translator.CachedFileHandle; | ... | @@ -32,20 +31,19 @@ import org.onosproject.yangutils.translator.CachedFileHandle; |
| 32 | /** | 31 | /** |
| 33 | * Maintains the enumeration data type information. | 32 | * Maintains the enumeration data type information. |
| 34 | */ | 33 | */ |
| 35 | -public class YangEnumeration extends YangNode implements Parsable { | 34 | +public class YangEnumeration implements Parsable { |
| 36 | 35 | ||
| 37 | - /** | 36 | + // Enumeration info set. |
| 38 | - * Enumeration info set. | ||
| 39 | - */ | ||
| 40 | private Set<YangEnum> enumSet; | 37 | private Set<YangEnum> enumSet; |
| 41 | 38 | ||
| 39 | + // Enumeration name. | ||
| 40 | + private String enumerationName; | ||
| 41 | + | ||
| 42 | /** | 42 | /** |
| 43 | - * Create an enumeration object. | 43 | + * Creates an enumeration object. |
| 44 | */ | 44 | */ |
| 45 | public YangEnumeration() { | 45 | public YangEnumeration() { |
| 46 | - super(YangNodeType.ENUMERATION_NODE); | ||
| 47 | setEnumSet(new HashSet<YangEnum>()); | 46 | setEnumSet(new HashSet<YangEnum>()); |
| 48 | - | ||
| 49 | } | 47 | } |
| 50 | 48 | ||
| 51 | /** | 49 | /** |
| ... | @@ -67,12 +65,33 @@ public class YangEnumeration extends YangNode implements Parsable { | ... | @@ -67,12 +65,33 @@ public class YangEnumeration extends YangNode implements Parsable { |
| 67 | } | 65 | } |
| 68 | 66 | ||
| 69 | /** | 67 | /** |
| 70 | - * Add ENUM value. | 68 | + * Add ENUM information. |
| 69 | + * | ||
| 70 | + * @param enumInfo the ENUM information to be added. | ||
| 71 | + * @throws DataModelException due to violation in data model rules. | ||
| 72 | + */ | ||
| 73 | + public void addEnumInfo(YangEnum enumInfo) throws DataModelException { | ||
| 74 | + if (!getEnumSet().add(enumInfo)) { | ||
| 75 | + throw new DataModelException("YANG ENUM already exists"); | ||
| 76 | + } | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + /** | ||
| 80 | + * Return enumeration name. | ||
| 71 | * | 81 | * |
| 72 | - * @param enumInfo the ENUM value of string | 82 | + * @return the enumeration name |
| 73 | */ | 83 | */ |
| 74 | - public void addEnumInfo(YangEnum enumInfo) { | 84 | + public String getEnumerationName() { |
| 85 | + return enumerationName; | ||
| 86 | + } | ||
| 75 | 87 | ||
| 88 | + /** | ||
| 89 | + * Set the enumeration name. | ||
| 90 | + * | ||
| 91 | + * @param enumerationName enumeration name | ||
| 92 | + */ | ||
| 93 | + public void setEnumerationName(String enumerationName) { | ||
| 94 | + this.enumerationName = enumerationName; | ||
| 76 | } | 95 | } |
| 77 | 96 | ||
| 78 | /** | 97 | /** |
| ... | @@ -104,70 +123,4 @@ public class YangEnumeration extends YangNode implements Parsable { | ... | @@ -104,70 +123,4 @@ public class YangEnumeration extends YangNode implements Parsable { |
| 104 | public void validateDataOnExit() throws DataModelException { | 123 | public void validateDataOnExit() throws DataModelException { |
| 105 | // TODO auto-generated method stub, to be implemented by parser | 124 | // TODO auto-generated method stub, to be implemented by parser |
| 106 | } | 125 | } |
| 107 | - | ||
| 108 | - /* (non-Javadoc) | ||
| 109 | - * @see org.onosproject.yangutils.datamodel.YangNode#getName() | ||
| 110 | - */ | ||
| 111 | - @Override | ||
| 112 | - public String getName() { | ||
| 113 | - // TODO Auto-generated method stub | ||
| 114 | - return null; | ||
| 115 | - } | ||
| 116 | - | ||
| 117 | - /* (non-Javadoc) | ||
| 118 | - * @see org.onosproject.yangutils.datamodel.YangNode#setName(java.lang.String) | ||
| 119 | - */ | ||
| 120 | - @Override | ||
| 121 | - public void setName(String name) { | ||
| 122 | - // TODO Auto-generated method stub | ||
| 123 | - | ||
| 124 | - } | ||
| 125 | - | ||
| 126 | - /* (non-Javadoc) | ||
| 127 | - * @see org.onosproject.yangutils.datamodel.YangNode#getPackage() | ||
| 128 | - */ | ||
| 129 | - @Override | ||
| 130 | - public String getPackage() { | ||
| 131 | - // TODO Auto-generated method stub | ||
| 132 | - return null; | ||
| 133 | - } | ||
| 134 | - | ||
| 135 | - /* (non-Javadoc) | ||
| 136 | - * @see org.onosproject.yangutils.datamodel.YangNode#setPackage(java.lang.String) | ||
| 137 | - */ | ||
| 138 | - @Override | ||
| 139 | - public void setPackage(String pkg) { | ||
| 140 | - // TODO Auto-generated method stub | ||
| 141 | - | ||
| 142 | - } | ||
| 143 | - | ||
| 144 | - /* (non-Javadoc) | ||
| 145 | - * @see org.onosproject.yangutils.translator.CodeGenerator#generateJavaCodeEntry() | ||
| 146 | - */ | ||
| 147 | - @Override | ||
| 148 | - public void generateJavaCodeEntry() { | ||
| 149 | - // TODO Auto-generated method stub | ||
| 150 | - | ||
| 151 | - } | ||
| 152 | - | ||
| 153 | - /* (non-Javadoc) | ||
| 154 | - * @see org.onosproject.yangutils.translator.CodeGenerator#generateJavaCodeExit() | ||
| 155 | - */ | ||
| 156 | - @Override | ||
| 157 | - public void generateJavaCodeExit() { | ||
| 158 | - // TODO Auto-generated method stub | ||
| 159 | - | ||
| 160 | - } | ||
| 161 | - | ||
| 162 | - @Override | ||
| 163 | - public CachedFileHandle getFileHandle() { | ||
| 164 | - // TODO Auto-generated method stub | ||
| 165 | - return null; | ||
| 166 | - } | ||
| 167 | - | ||
| 168 | - @Override | ||
| 169 | - public void setFileHandle(CachedFileHandle fileHandle) { | ||
| 170 | - // TODO Auto-generated method stub | ||
| 171 | - | ||
| 172 | - } | ||
| 173 | } | 126 | } | ... | ... |
| ... | @@ -215,6 +215,11 @@ public enum ParsableDataType { | ... | @@ -215,6 +215,11 @@ public enum ParsableDataType { |
| 215 | DEFAULT_DATA, | 215 | DEFAULT_DATA, |
| 216 | 216 | ||
| 217 | /** | 217 | /** |
| 218 | + * Identifies the YANG value element parsed data. | ||
| 219 | + */ | ||
| 220 | + VALUE_DATA, | ||
| 221 | + | ||
| 222 | + /** | ||
| 218 | * Identifies the YANG organization parsed data. | 223 | * Identifies the YANG organization parsed data. |
| 219 | */ | 224 | */ |
| 220 | ORGANIZATION_DATA; | 225 | ORGANIZATION_DATA; |
| ... | @@ -306,6 +311,8 @@ public enum ParsableDataType { | ... | @@ -306,6 +311,8 @@ public enum ParsableDataType { |
| 306 | return "prefix"; | 311 | return "prefix"; |
| 307 | case ORGANIZATION_DATA: | 312 | case ORGANIZATION_DATA: |
| 308 | return "organization"; | 313 | return "organization"; |
| 314 | + case VALUE_DATA: | ||
| 315 | + return "value"; | ||
| 309 | case DEFAULT_DATA: | 316 | case DEFAULT_DATA: |
| 310 | return "default"; | 317 | return "default"; |
| 311 | default: | 318 | default: | ... | ... |
This diff is collapsed. Click to expand it.
utils/yangutils/src/main/java/org/onosproject/yangutils/parser/impl/listeners/EnumListener.java
0 → 100644
| 1 | +/* | ||
| 2 | + * Copyright 2014-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 | +/* | ||
| 20 | + * Reference: RFC6020 and YANG ANTLR Grammar | ||
| 21 | + * | ||
| 22 | + * ABNF grammar as per RFC6020 | ||
| 23 | + * enum-stmt = enum-keyword sep string optsep | ||
| 24 | + * (";" / | ||
| 25 | + * "{" stmtsep | ||
| 26 | + * ;; these stmts can appear in any order | ||
| 27 | + * [value-stmt stmtsep] | ||
| 28 | + * [status-stmt stmtsep] | ||
| 29 | + * [description-stmt stmtsep] | ||
| 30 | + * [reference-stmt stmtsep] | ||
| 31 | + * "}") | ||
| 32 | + * | ||
| 33 | + * ANTLR grammar rule | ||
| 34 | + * enumStatement : ENUM_KEYWORD string (STMTEND | LEFT_CURLY_BRACE enumStatementBody RIGHT_CURLY_BRACE); | ||
| 35 | + * | ||
| 36 | + * enumStatementBody : valueStatement? statusStatement? descriptionStatement? referenceStatement? | ||
| 37 | + * | valueStatement? statusStatement? referenceStatement? descriptionStatement? | ||
| 38 | + * | valueStatement? descriptionStatement? statusStatement? referenceStatement? | ||
| 39 | + * | valueStatement? descriptionStatement? referenceStatement? statusStatement? | ||
| 40 | + * | valueStatement? referenceStatement? statusStatement? descriptionStatement? | ||
| 41 | + * | valueStatement? referenceStatement? descriptionStatement? statusStatement? | ||
| 42 | + * | statusStatement? valueStatement? descriptionStatement? referenceStatement? | ||
| 43 | + * | statusStatement? valueStatement? referenceStatement? descriptionStatement? | ||
| 44 | + * | statusStatement? descriptionStatement? descriptionStatement? valueStatement? | ||
| 45 | + * | statusStatement? descriptionStatement? valueStatement? descriptionStatement? | ||
| 46 | + * | statusStatement? referenceStatement? valueStatement? descriptionStatement? | ||
| 47 | + * | statusStatement? referenceStatement? descriptionStatement? valueStatement? | ||
| 48 | + * | descriptionStatement? valueStatement? statusStatement? referenceStatement? | ||
| 49 | + * | descriptionStatement? valueStatement? referenceStatement? statusStatement? | ||
| 50 | + * | descriptionStatement? statusStatement? valueStatement? referenceStatement? | ||
| 51 | + * | descriptionStatement? statusStatement? referenceStatement? valueStatement? | ||
| 52 | + * | descriptionStatement? referenceStatement? valueStatement? statusStatement? | ||
| 53 | + * | descriptionStatement? referenceStatement? statusStatement? valueStatement? | ||
| 54 | + * | referenceStatement? valueStatement? descriptionStatement? statusStatement? | ||
| 55 | + * | referenceStatement? valueStatement? statusStatement? descriptionStatement? | ||
| 56 | + * | referenceStatement? statusStatement? descriptionStatement? valueStatement? | ||
| 57 | + * | referenceStatement? statusStatement? valueStatement? descriptionStatement? | ||
| 58 | + * | referenceStatement? descriptionStatement? valueStatement? statusStatement? | ||
| 59 | + * | referenceStatement? descriptionStatement? statusStatement? valueStatement? | ||
| 60 | + * ; | ||
| 61 | + */ | ||
| 62 | + | ||
| 63 | +import org.onosproject.yangutils.datamodel.YangEnum; | ||
| 64 | +import org.onosproject.yangutils.datamodel.YangEnumeration; | ||
| 65 | +import org.onosproject.yangutils.datamodel.exceptions.DataModelException; | ||
| 66 | +import org.onosproject.yangutils.parser.Parsable; | ||
| 67 | +import static org.onosproject.yangutils.parser.ParsableDataType.ENUM_DATA; | ||
| 68 | +import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; | ||
| 69 | +import org.onosproject.yangutils.parser.exceptions.ParserException; | ||
| 70 | +import org.onosproject.yangutils.parser.impl.TreeWalkListener; | ||
| 71 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; | ||
| 72 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; | ||
| 73 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; | ||
| 74 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; | ||
| 75 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.DUPLICATE_ENTRY; | ||
| 76 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; | ||
| 77 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; | ||
| 78 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; | ||
| 79 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; | ||
| 80 | + | ||
| 81 | +/** | ||
| 82 | + * Implements listener based call back function corresponding to the "enum" rule | ||
| 83 | + * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. | ||
| 84 | + */ | ||
| 85 | +public final class EnumListener { | ||
| 86 | + | ||
| 87 | + /** | ||
| 88 | + * Creates a new enum listener. | ||
| 89 | + */ | ||
| 90 | + private EnumListener() { | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + /** | ||
| 94 | + * It is called when parser enters grammar rule (enum), it perform | ||
| 95 | + * validations and updates the data model tree. | ||
| 96 | + * | ||
| 97 | + * @param listener listener's object. | ||
| 98 | + * @param ctx context object of the grammar rule. | ||
| 99 | + */ | ||
| 100 | + public static void processEnumEntry(TreeWalkListener listener, GeneratedYangParser.EnumStatementContext ctx) { | ||
| 101 | + | ||
| 102 | + // Check for stack to be non empty. | ||
| 103 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUM_DATA, ctx.string().getText(), ENTRY); | ||
| 104 | + | ||
| 105 | + YangEnum enumNode = new YangEnum(); | ||
| 106 | + enumNode.setNamedValue(ctx.string().getText()); | ||
| 107 | + listener.getParsedDataStack().push(enumNode); | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + /** | ||
| 111 | + * It is called when parser exits from grammar rule (enum), it perform | ||
| 112 | + * validations and update the data model tree. | ||
| 113 | + * | ||
| 114 | + * @param listener Listener's object. | ||
| 115 | + * @param ctx context object of the grammar rule. | ||
| 116 | + */ | ||
| 117 | + public static void processEnumExit(TreeWalkListener listener, GeneratedYangParser.EnumStatementContext ctx) { | ||
| 118 | + | ||
| 119 | + // Check for stack to be non empty. | ||
| 120 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUM_DATA, ctx.string().getText(), EXIT); | ||
| 121 | + | ||
| 122 | + Parsable tmpEnumNode = listener.getParsedDataStack().peek(); | ||
| 123 | + if (tmpEnumNode instanceof YangEnum) { | ||
| 124 | + listener.getParsedDataStack().pop(); | ||
| 125 | + | ||
| 126 | + // Check for stack to be non empty. | ||
| 127 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUM_DATA, ctx.string().getText(), EXIT); | ||
| 128 | + | ||
| 129 | + Parsable tmpNode = listener.getParsedDataStack().peek(); | ||
| 130 | + switch (tmpNode.getParsableDataType()) { | ||
| 131 | + case ENUMERATION_DATA: { | ||
| 132 | + YangEnumeration yangEnumeration = (YangEnumeration) tmpNode; | ||
| 133 | + if ((ctx.enumStatementBody() == null) || (ctx.enumStatementBody().valueStatement() == null)) { | ||
| 134 | + int maxValue = 0; | ||
| 135 | + boolean isValuePresent = false; | ||
| 136 | + | ||
| 137 | + for (YangEnum curEnum : yangEnumeration.getEnumSet()) { | ||
| 138 | + if (maxValue <= curEnum.getValue()) { | ||
| 139 | + maxValue = curEnum.getValue(); | ||
| 140 | + isValuePresent = true; | ||
| 141 | + } | ||
| 142 | + } | ||
| 143 | + if (isValuePresent) { | ||
| 144 | + maxValue++; | ||
| 145 | + } | ||
| 146 | + ((YangEnum) tmpEnumNode).setValue(maxValue); | ||
| 147 | + } | ||
| 148 | + try { | ||
| 149 | + yangEnumeration.addEnumInfo((YangEnum) tmpEnumNode); | ||
| 150 | + } catch (DataModelException e) { | ||
| 151 | + ParserException parserException = new ParserException(constructExtendedListenerErrorMessage( | ||
| 152 | + DUPLICATE_ENTRY, ENUM_DATA, ctx.string().getText(), EXIT, e.getMessage())); | ||
| 153 | + parserException.setLine(ctx.string().STRING(0).getSymbol().getLine()); | ||
| 154 | + parserException.setCharPosition(ctx.string().STRING(0).getSymbol().getCharPositionInLine()); | ||
| 155 | + throw parserException; | ||
| 156 | + } | ||
| 157 | + break; | ||
| 158 | + } | ||
| 159 | + default: | ||
| 160 | + throw new ParserException( | ||
| 161 | + constructListenerErrorMessage(INVALID_HOLDER, ENUM_DATA, ctx.string().getText(), EXIT)); | ||
| 162 | + } | ||
| 163 | + } else { | ||
| 164 | + throw new ParserException( | ||
| 165 | + constructListenerErrorMessage(MISSING_CURRENT_HOLDER, ENUM_DATA, ctx.string().getText(), EXIT)); | ||
| 166 | + } | ||
| 167 | + } | ||
| 168 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2014-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 | +/* | ||
| 20 | + * Reference: RFC6020 and YANG ANTLR Grammar | ||
| 21 | + * | ||
| 22 | + * ABNF grammar as per RFC6020 | ||
| 23 | + * type-body-stmts = numerical-restrictions / | ||
| 24 | + * decimal64-specification / | ||
| 25 | + * string-restrictions / | ||
| 26 | + * enum-specification / | ||
| 27 | + * leafref-specification / | ||
| 28 | + * identityref-specification / | ||
| 29 | + * instance-identifier-specification / | ||
| 30 | + * bits-specification / | ||
| 31 | + * union-specification | ||
| 32 | + * | ||
| 33 | + * enum-specification = 1*(enum-stmt stmtsep) | ||
| 34 | + * | ||
| 35 | + * ANTLR grammar rule | ||
| 36 | + * | ||
| 37 | + * typeBodyStatements : numericalRestrictions | stringRestrictions | enumSpecification | ||
| 38 | + * | leafrefSpecification | identityrefSpecification | instanceIdentifierSpecification | ||
| 39 | + * | bitsSpecification | unionSpecification; | ||
| 40 | + * | ||
| 41 | + * enumSpecification : enumStatement+; | ||
| 42 | + */ | ||
| 43 | + | ||
| 44 | +import org.onosproject.yangutils.datamodel.YangEnumeration; | ||
| 45 | +import org.onosproject.yangutils.datamodel.YangLeaf; | ||
| 46 | +import org.onosproject.yangutils.datamodel.YangLeafList; | ||
| 47 | +import org.onosproject.yangutils.datamodel.YangType; | ||
| 48 | +import org.onosproject.yangutils.parser.Parsable; | ||
| 49 | +import static org.onosproject.yangutils.parser.ParsableDataType.ENUMERATION_DATA; | ||
| 50 | +import static org.onosproject.yangutils.parser.ParsableDataType.TYPE_DATA; | ||
| 51 | +import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; | ||
| 52 | +import org.onosproject.yangutils.parser.exceptions.ParserException; | ||
| 53 | +import org.onosproject.yangutils.parser.impl.TreeWalkListener; | ||
| 54 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; | ||
| 55 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; | ||
| 56 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; | ||
| 57 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; | ||
| 58 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; | ||
| 59 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; | ||
| 60 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; | ||
| 61 | + | ||
| 62 | +/** | ||
| 63 | + * Implements listener based call back function corresponding to the | ||
| 64 | + * "enumeration" rule defined in ANTLR grammar file for corresponding ABNF rule | ||
| 65 | + * in RFC 6020. | ||
| 66 | + */ | ||
| 67 | +public final class EnumerationListener { | ||
| 68 | + | ||
| 69 | + /** | ||
| 70 | + * Creates a new enumeration listener. | ||
| 71 | + */ | ||
| 72 | + private EnumerationListener() { | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + /** | ||
| 76 | + * It is called when parser enters grammar rule (enumeration), it perform | ||
| 77 | + * validations and updates the data model tree. | ||
| 78 | + * | ||
| 79 | + * @param listener listener's object. | ||
| 80 | + * @param ctx context object of the grammar rule. | ||
| 81 | + */ | ||
| 82 | + public static void processEnumerationEntry(TreeWalkListener listener, | ||
| 83 | + GeneratedYangParser.EnumSpecificationContext ctx) { | ||
| 84 | + | ||
| 85 | + // Check for stack to be non empty. | ||
| 86 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", ENTRY); | ||
| 87 | + | ||
| 88 | + if (listener.getParsedDataStack().peek() instanceof YangType) { | ||
| 89 | + YangEnumeration enumerationNode = new YangEnumeration(); | ||
| 90 | + Parsable typeData = listener.getParsedDataStack().pop(); | ||
| 91 | + | ||
| 92 | + // Check for stack to be non empty. | ||
| 93 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", ENTRY); | ||
| 94 | + | ||
| 95 | + Parsable tmpData = listener.getParsedDataStack().peek(); | ||
| 96 | + | ||
| 97 | + switch (tmpData.getParsableDataType()) { | ||
| 98 | + case LEAF_DATA: | ||
| 99 | + enumerationNode.setEnumerationName(((YangLeaf) tmpData).getLeafName()); | ||
| 100 | + break; | ||
| 101 | + case LEAF_LIST_DATA: | ||
| 102 | + enumerationNode.setEnumerationName(((YangLeafList) tmpData).getLeafName()); | ||
| 103 | + break; | ||
| 104 | + // TODO typedef, union, deviate. | ||
| 105 | + default: | ||
| 106 | + throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, | ||
| 107 | + ((YangType) typeData).getDataTypeName(), ENTRY)); | ||
| 108 | + } | ||
| 109 | + listener.getParsedDataStack().push(typeData); | ||
| 110 | + listener.getParsedDataStack().push(enumerationNode); | ||
| 111 | + } else { | ||
| 112 | + throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, ENUMERATION_DATA, "", ENTRY)); | ||
| 113 | + } | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + /** | ||
| 117 | + * It is called when parser exits from grammar rule (enumeration), it | ||
| 118 | + * perform validations and update the data model tree. | ||
| 119 | + * | ||
| 120 | + * @param listener Listener's object. | ||
| 121 | + * @param ctx context object of the grammar rule. | ||
| 122 | + */ | ||
| 123 | + public static void processEnumerationExit(TreeWalkListener listener, | ||
| 124 | + GeneratedYangParser.EnumSpecificationContext ctx) { | ||
| 125 | + | ||
| 126 | + // Check for stack to be non empty. | ||
| 127 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", EXIT); | ||
| 128 | + | ||
| 129 | + Parsable tmpEnumerationNode = listener.getParsedDataStack().peek(); | ||
| 130 | + if (tmpEnumerationNode instanceof YangEnumeration) { | ||
| 131 | + listener.getParsedDataStack().pop(); | ||
| 132 | + | ||
| 133 | + // Check for stack to be non empty. | ||
| 134 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", EXIT); | ||
| 135 | + | ||
| 136 | + Parsable tmpNode = listener.getParsedDataStack().peek(); | ||
| 137 | + switch (tmpNode.getParsableDataType()) { | ||
| 138 | + case TYPE_DATA: { | ||
| 139 | + YangType typeNode = (YangType) tmpNode; | ||
| 140 | + typeNode.setDataTypeExtendedInfo((YangEnumeration) tmpEnumerationNode); | ||
| 141 | + break; | ||
| 142 | + } | ||
| 143 | + default: | ||
| 144 | + throw new ParserException( | ||
| 145 | + constructListenerErrorMessage(INVALID_HOLDER, ENUMERATION_DATA, "", EXIT)); | ||
| 146 | + } | ||
| 147 | + } else { | ||
| 148 | + throw new ParserException( | ||
| 149 | + constructListenerErrorMessage(MISSING_CURRENT_HOLDER, ENUMERATION_DATA, "", EXIT)); | ||
| 150 | + } | ||
| 151 | + } | ||
| 152 | +} |
| ... | @@ -21,18 +21,18 @@ import org.onosproject.yangutils.datamodel.YangLeaf; | ... | @@ -21,18 +21,18 @@ import org.onosproject.yangutils.datamodel.YangLeaf; |
| 21 | import org.onosproject.yangutils.datamodel.YangLeafList; | 21 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 22 | import org.onosproject.yangutils.datamodel.YangType; | 22 | import org.onosproject.yangutils.datamodel.YangType; |
| 23 | import org.onosproject.yangutils.parser.Parsable; | 23 | import org.onosproject.yangutils.parser.Parsable; |
| 24 | +import static org.onosproject.yangutils.parser.ParsableDataType.TYPE_DATA; | ||
| 24 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; | 25 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 25 | import org.onosproject.yangutils.parser.exceptions.ParserException; | 26 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 26 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; | 27 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 27 | - | ||
| 28 | -import static org.onosproject.yangutils.parser.ParsableDataType.TYPE_DATA; | ||
| 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; | 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 29 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; | ||
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; | 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; | 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 32 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; | ||
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; | 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; | 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 34 | 35 | ||
| 35 | - | ||
| 36 | /* | 36 | /* |
| 37 | * Reference: RFC6020 and YANG ANTLR Grammar | 37 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 38 | * | 38 | * |
| ... | @@ -48,8 +48,8 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidati | ... | @@ -48,8 +48,8 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidati |
| 48 | */ | 48 | */ |
| 49 | 49 | ||
| 50 | /** | 50 | /** |
| 51 | - * Implements listener based call back function corresponding to the "type" | 51 | + * Implements listener based call back function corresponding to the "type" rule |
| 52 | - * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. | 52 | + * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 53 | */ | 53 | */ |
| 54 | public final class TypeListener { | 54 | public final class TypeListener { |
| 55 | 55 | ||
| ... | @@ -60,9 +60,8 @@ public final class TypeListener { | ... | @@ -60,9 +60,8 @@ public final class TypeListener { |
| 60 | } | 60 | } |
| 61 | 61 | ||
| 62 | /** | 62 | /** |
| 63 | - * It is called when parser receives an input matching the grammar | 63 | + * It is called when parser receives an input matching the grammar rule |
| 64 | - * rule (type), performs validation and updates the data model | 64 | + * (type), performs validation and updates the data model tree. |
| 65 | - * tree. | ||
| 66 | * | 65 | * |
| 67 | * @param listener listener's object. | 66 | * @param listener listener's object. |
| 68 | * @param ctx context object of the grammar rule. | 67 | * @param ctx context object of the grammar rule. |
| ... | @@ -78,21 +77,46 @@ public final class TypeListener { | ... | @@ -78,21 +77,46 @@ public final class TypeListener { |
| 78 | type.setDataTypeName(ctx.string().getText()); | 77 | type.setDataTypeName(ctx.string().getText()); |
| 79 | type.setDataType(yangDataTypes); | 78 | type.setDataType(yangDataTypes); |
| 80 | 79 | ||
| 80 | + listener.getParsedDataStack().push(type); | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + /** | ||
| 84 | + * It is called when parser exits from grammar rule (type), it perform | ||
| 85 | + * validations and update the data model tree. | ||
| 86 | + * | ||
| 87 | + * @param listener Listener's object. | ||
| 88 | + * @param ctx context object of the grammar rule. | ||
| 89 | + */ | ||
| 90 | + public static void processTypeExit(TreeWalkListener listener, | ||
| 91 | + GeneratedYangParser.TypeStatementContext ctx) { | ||
| 92 | + | ||
| 93 | + // Check for stack to be non empty. | ||
| 94 | + checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT); | ||
| 95 | + | ||
| 96 | + Parsable type = listener.getParsedDataStack().pop(); | ||
| 97 | + if (!(type instanceof YangType)) { | ||
| 98 | + throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, | ||
| 99 | + ctx.string().getText(), EXIT)); | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + // Check for stack to be non empty. | ||
| 103 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT); | ||
| 104 | + | ||
| 81 | Parsable tmpData = listener.getParsedDataStack().peek(); | 105 | Parsable tmpData = listener.getParsedDataStack().peek(); |
| 82 | switch (tmpData.getParsableDataType()) { | 106 | switch (tmpData.getParsableDataType()) { |
| 83 | case LEAF_DATA: | 107 | case LEAF_DATA: |
| 84 | YangLeaf leaf = (YangLeaf) tmpData; | 108 | YangLeaf leaf = (YangLeaf) tmpData; |
| 85 | - leaf.setDataType(type); | 109 | + leaf.setDataType((YangType) type); |
| 86 | break; | 110 | break; |
| 87 | case LEAF_LIST_DATA: | 111 | case LEAF_LIST_DATA: |
| 88 | YangLeafList leafList = (YangLeafList) tmpData; | 112 | YangLeafList leafList = (YangLeafList) tmpData; |
| 89 | - leafList.setDataType(type); | 113 | + leafList.setDataType((YangType) type); |
| 90 | break; | 114 | break; |
| 91 | case TYPEDEF_DATA: //TODO | 115 | case TYPEDEF_DATA: //TODO |
| 92 | break; | 116 | break; |
| 93 | default: | 117 | default: |
| 94 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, | 118 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, |
| 95 | - ctx.string().getText(), ENTRY)); | 119 | + ctx.string().getText(), EXIT)); |
| 96 | } | 120 | } |
| 97 | } | 121 | } |
| 98 | } | 122 | } | ... | ... |
utils/yangutils/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ValueListener.java
0 → 100644
| 1 | +/* | ||
| 2 | + * Copyright 2014-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 | +/* | ||
| 20 | + * Reference: RFC6020 and YANG ANTLR Grammar | ||
| 21 | + * | ||
| 22 | + * ABNF grammar as per RFC6020 | ||
| 23 | + * value-stmt = value-keyword sep integer-value stmtend | ||
| 24 | + * | ||
| 25 | + * ANTLR grammar rule | ||
| 26 | + * valueStatement : VALUE_KEYWORD ((MINUS INTEGER) | INTEGER) STMTEND; | ||
| 27 | + */ | ||
| 28 | + | ||
| 29 | +import org.onosproject.yangutils.datamodel.YangEnum; | ||
| 30 | +import org.onosproject.yangutils.datamodel.YangEnumeration; | ||
| 31 | +import org.onosproject.yangutils.parser.Parsable; | ||
| 32 | +import static org.onosproject.yangutils.parser.ParsableDataType.VALUE_DATA; | ||
| 33 | +import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; | ||
| 34 | +import org.onosproject.yangutils.parser.exceptions.ParserException; | ||
| 35 | +import org.onosproject.yangutils.parser.impl.TreeWalkListener; | ||
| 36 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; | ||
| 37 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; | ||
| 38 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; | ||
| 39 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; | ||
| 40 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; | ||
| 41 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; | ||
| 42 | + | ||
| 43 | +/** | ||
| 44 | + * Implements listener based call back function corresponding to the "value" | ||
| 45 | + * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. | ||
| 46 | + */ | ||
| 47 | +public final class ValueListener { | ||
| 48 | + | ||
| 49 | + /** | ||
| 50 | + * Creates a new value listener. | ||
| 51 | + */ | ||
| 52 | + private ValueListener() { | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + /** | ||
| 56 | + * It is called when parser receives an input matching the grammar rule | ||
| 57 | + * (value), perform validations and update the data model tree. | ||
| 58 | + * | ||
| 59 | + * @param listener Listener's object. | ||
| 60 | + * @param ctx context object of the grammar rule. | ||
| 61 | + */ | ||
| 62 | + public static void processValueEntry(TreeWalkListener listener, GeneratedYangParser.ValueStatementContext ctx) { | ||
| 63 | + | ||
| 64 | + // Check for stack to be non empty. | ||
| 65 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), ENTRY); | ||
| 66 | + | ||
| 67 | + // Obtain the node of the stack. | ||
| 68 | + Parsable tmpNode = listener.getParsedDataStack().peek(); | ||
| 69 | + switch (tmpNode.getParsableDataType()) { | ||
| 70 | + case ENUM_DATA: { | ||
| 71 | + YangEnum enumNode = (YangEnum) tmpNode; | ||
| 72 | + if (!isEnumValueValid(listener, ctx)) { | ||
| 73 | + ParserException parserException = new ParserException("Input version not supported"); | ||
| 74 | + parserException.setLine(ctx.INTEGER().getSymbol().getLine()); | ||
| 75 | + parserException.setCharPosition(ctx.INTEGER().getSymbol().getCharPositionInLine()); | ||
| 76 | + throw parserException; | ||
| 77 | + } | ||
| 78 | + enumNode.setValue(Integer.valueOf(ctx.INTEGER().getText())); | ||
| 79 | + break; | ||
| 80 | + } | ||
| 81 | + default: | ||
| 82 | + throw new ParserException( | ||
| 83 | + constructListenerErrorMessage(INVALID_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), ENTRY)); | ||
| 84 | + } | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + /** | ||
| 88 | + * Validates ENUM value uniqueness. | ||
| 89 | + * | ||
| 90 | + * @param listener Listener's object. | ||
| 91 | + * @param ctx context object of the grammar rule. | ||
| 92 | + * @return validation result | ||
| 93 | + */ | ||
| 94 | + private static boolean isEnumValueValid(TreeWalkListener listener, GeneratedYangParser.ValueStatementContext ctx) { | ||
| 95 | + Parsable enumNode = listener.getParsedDataStack().pop(); | ||
| 96 | + Parsable tmpNode = listener.getParsedDataStack().peek(); | ||
| 97 | + switch (tmpNode.getParsableDataType()) { | ||
| 98 | + case ENUMERATION_DATA: { | ||
| 99 | + YangEnumeration yangEnumeration = (YangEnumeration) tmpNode; | ||
| 100 | + for (YangEnum curEnum : yangEnumeration.getEnumSet()) { | ||
| 101 | + if (Integer.valueOf(ctx.INTEGER().getText()) == curEnum.getValue()) { | ||
| 102 | + listener.getParsedDataStack().push(enumNode); | ||
| 103 | + return false; | ||
| 104 | + } | ||
| 105 | + } | ||
| 106 | + listener.getParsedDataStack().push(enumNode); | ||
| 107 | + return true; | ||
| 108 | + } | ||
| 109 | + default: | ||
| 110 | + listener.getParsedDataStack().push(enumNode); | ||
| 111 | + throw new ParserException( | ||
| 112 | + constructListenerErrorMessage(INVALID_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), EXIT)); | ||
| 113 | + } | ||
| 114 | + } | ||
| 115 | +} |
| ... | @@ -21,22 +21,26 @@ package org.onosproject.yangutils.parser.impl.parserutils; | ... | @@ -21,22 +21,26 @@ package org.onosproject.yangutils.parser.impl.parserutils; |
| 21 | */ | 21 | */ |
| 22 | public enum ListenerErrorType { | 22 | public enum ListenerErrorType { |
| 23 | /** | 23 | /** |
| 24 | - * Represents the parent holder in parsable stack for given YANG construct is invalid. | 24 | + * Represents the parent holder in parsable stack for given YANG construct |
| 25 | + * is invalid. | ||
| 25 | */ | 26 | */ |
| 26 | INVALID_HOLDER(), | 27 | INVALID_HOLDER(), |
| 27 | 28 | ||
| 28 | /** | 29 | /** |
| 29 | - * Represents the parent holder in parsable stack for given YANG construct is missing. | 30 | + * Represents the parent holder in parsable stack for given YANG construct |
| 31 | + * is missing. | ||
| 30 | */ | 32 | */ |
| 31 | MISSING_HOLDER(), | 33 | MISSING_HOLDER(), |
| 32 | 34 | ||
| 33 | /** | 35 | /** |
| 34 | - * Represents the current holder in parsable stack for given YANG construct is missing. | 36 | + * Represents the current holder in parsable stack for given YANG construct |
| 37 | + * is missing. | ||
| 35 | */ | 38 | */ |
| 36 | MISSING_CURRENT_HOLDER(), | 39 | MISSING_CURRENT_HOLDER(), |
| 37 | 40 | ||
| 38 | /** | 41 | /** |
| 39 | - * Represents that the child in parsable stack for given YANG construct is invalid. | 42 | + * Represents that the child in parsable stack for given YANG construct is |
| 43 | + * invalid. | ||
| 40 | */ | 44 | */ |
| 41 | INVALID_CHILD(), | 45 | INVALID_CHILD(), |
| 42 | 46 | ||
| ... | @@ -46,6 +50,11 @@ public enum ListenerErrorType { | ... | @@ -46,6 +50,11 @@ public enum ListenerErrorType { |
| 46 | INVALID_CARDINALITY(), | 50 | INVALID_CARDINALITY(), |
| 47 | 51 | ||
| 48 | /** | 52 | /** |
| 53 | + * Represents that the entry is duplicate. | ||
| 54 | + */ | ||
| 55 | + DUPLICATE_ENTRY(), | ||
| 56 | + | ||
| 57 | + /** | ||
| 49 | * Represents that some of earlier parsed data is not handled correctly. | 58 | * Represents that some of earlier parsed data is not handled correctly. |
| 50 | */ | 59 | */ |
| 51 | UNHANDLED_PARSED_DATA(); | 60 | UNHANDLED_PARSED_DATA(); |
| ... | @@ -69,6 +78,8 @@ public enum ListenerErrorType { | ... | @@ -69,6 +78,8 @@ public enum ListenerErrorType { |
| 69 | return "Invalid child in"; | 78 | return "Invalid child in"; |
| 70 | case INVALID_CARDINALITY: | 79 | case INVALID_CARDINALITY: |
| 71 | return "Invalid cardinality in"; | 80 | return "Invalid cardinality in"; |
| 81 | + case DUPLICATE_ENTRY: | ||
| 82 | + return "Duplicate"; | ||
| 72 | case UNHANDLED_PARSED_DATA: | 83 | case UNHANDLED_PARSED_DATA: |
| 73 | return "Unhandled parsed data at"; | 84 | return "Unhandled parsed data at"; |
| 74 | default: | 85 | default: | ... | ... |
utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/EnumListenerTest.java
0 → 100644
| 1 | +/* | ||
| 2 | + * Copyright 2014-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 static org.hamcrest.MatcherAssert.assertThat; | ||
| 20 | +import static org.hamcrest.core.Is.is; | ||
| 21 | +import org.junit.Test; | ||
| 22 | +import org.onosproject.yangutils.datamodel.YangDataTypes; | ||
| 23 | +import org.onosproject.yangutils.datamodel.YangEnum; | ||
| 24 | +import org.onosproject.yangutils.datamodel.YangEnumeration; | ||
| 25 | +import org.onosproject.yangutils.datamodel.YangLeaf; | ||
| 26 | +import org.onosproject.yangutils.datamodel.YangModule; | ||
| 27 | +import org.onosproject.yangutils.datamodel.YangNode; | ||
| 28 | +import org.onosproject.yangutils.datamodel.YangNodeType; | ||
| 29 | +import org.onosproject.yangutils.parser.exceptions.ParserException; | ||
| 30 | +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; | ||
| 31 | + | ||
| 32 | +import java.io.IOException; | ||
| 33 | +import java.util.ListIterator; | ||
| 34 | +import java.util.Set; | ||
| 35 | + | ||
| 36 | +/** | ||
| 37 | + * Test cases for enum listener. | ||
| 38 | + */ | ||
| 39 | +public class EnumListenerTest { | ||
| 40 | + | ||
| 41 | + private final YangUtilsParserManager manager = new YangUtilsParserManager(); | ||
| 42 | + | ||
| 43 | + /** | ||
| 44 | + * Checks enum statement without value. | ||
| 45 | + */ | ||
| 46 | + @Test | ||
| 47 | + public void processEnumTypeStatement() throws IOException, ParserException { | ||
| 48 | + | ||
| 49 | + YangNode node = manager.getDataModel("src/test/resources/EnumTypeStatement.yang"); | ||
| 50 | + | ||
| 51 | + // Check whether the data model tree returned is of type module. | ||
| 52 | + assertThat((node instanceof YangModule), is(true)); | ||
| 53 | + | ||
| 54 | + // Check whether the node type is set properly to module. | ||
| 55 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
| 56 | + | ||
| 57 | + // Check whether the module name is set correctly. | ||
| 58 | + YangModule yangNode = (YangModule) node; | ||
| 59 | + assertThat(yangNode.getName(), is("Test")); | ||
| 60 | + | ||
| 61 | + ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); | ||
| 62 | + YangLeaf leafInfo = leafIterator.next(); | ||
| 63 | + | ||
| 64 | + assertThat(leafInfo.getLeafName(), is("speed")); | ||
| 65 | + assertThat(leafInfo.getDataType().getDataTypeName(), is("enumeration")); | ||
| 66 | + assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.ENUMERATION)); | ||
| 67 | + assertThat(((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumerationName(), | ||
| 68 | + is("speed")); | ||
| 69 | + | ||
| 70 | + Set<YangEnum> enumSet = ((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumSet(); | ||
| 71 | + for (YangEnum tmp : enumSet) { | ||
| 72 | + if (tmp.getNamedValue().equals("10m")) { | ||
| 73 | + assertThat(tmp.getValue(), is(0)); | ||
| 74 | + } else if (tmp.getNamedValue().equals("100m")) { | ||
| 75 | + assertThat(tmp.getValue(), is(1)); | ||
| 76 | + } else if (tmp.getNamedValue().equals("auto")) { | ||
| 77 | + assertThat(tmp.getValue(), is(2)); | ||
| 78 | + } | ||
| 79 | + } | ||
| 80 | + } | ||
| 81 | +} |
utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ValueListenerTest.java
0 → 100644
| 1 | +/* | ||
| 2 | + * Copyright 2014-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 static org.hamcrest.MatcherAssert.assertThat; | ||
| 20 | +import static org.hamcrest.core.Is.is; | ||
| 21 | +import org.junit.Test; | ||
| 22 | +import org.onosproject.yangutils.datamodel.YangDataTypes; | ||
| 23 | +import org.onosproject.yangutils.datamodel.YangEnum; | ||
| 24 | +import org.onosproject.yangutils.datamodel.YangEnumeration; | ||
| 25 | +import org.onosproject.yangutils.datamodel.YangLeaf; | ||
| 26 | +import org.onosproject.yangutils.datamodel.YangModule; | ||
| 27 | +import org.onosproject.yangutils.datamodel.YangNode; | ||
| 28 | +import org.onosproject.yangutils.datamodel.YangNodeType; | ||
| 29 | +import org.onosproject.yangutils.parser.exceptions.ParserException; | ||
| 30 | +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; | ||
| 31 | + | ||
| 32 | +import java.io.IOException; | ||
| 33 | +import java.util.ListIterator; | ||
| 34 | +import java.util.Set; | ||
| 35 | + | ||
| 36 | +/** | ||
| 37 | + * Test cases for value listener. | ||
| 38 | + */ | ||
| 39 | +public class ValueListenerTest { | ||
| 40 | + | ||
| 41 | + private final YangUtilsParserManager manager = new YangUtilsParserManager(); | ||
| 42 | + | ||
| 43 | + /** | ||
| 44 | + * Checks explicitly configured value. | ||
| 45 | + */ | ||
| 46 | + @Test | ||
| 47 | + public void processValueStatement() throws IOException, ParserException { | ||
| 48 | + | ||
| 49 | + YangNode node = manager.getDataModel("src/test/resources/ValueStatement.yang"); | ||
| 50 | + | ||
| 51 | + // Check whether the data model tree returned is of type module. | ||
| 52 | + assertThat((node instanceof YangModule), is(true)); | ||
| 53 | + | ||
| 54 | + // Check whether the node type is set properly to module. | ||
| 55 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
| 56 | + | ||
| 57 | + // Check whether the module name is set correctly. | ||
| 58 | + YangModule yangNode = (YangModule) node; | ||
| 59 | + assertThat(yangNode.getName(), is("Test")); | ||
| 60 | + | ||
| 61 | + ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); | ||
| 62 | + YangLeaf leafInfo = leafIterator.next(); | ||
| 63 | + | ||
| 64 | + assertThat(leafInfo.getLeafName(), is("speed")); | ||
| 65 | + assertThat(leafInfo.getDataType().getDataTypeName(), is("enumeration")); | ||
| 66 | + assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.ENUMERATION)); | ||
| 67 | + assertThat(((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumerationName(), | ||
| 68 | + is("speed")); | ||
| 69 | + | ||
| 70 | + Set<YangEnum> enumSet = ((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumSet(); | ||
| 71 | + for (YangEnum tmp : enumSet) { | ||
| 72 | + if (tmp.getNamedValue().equals("10m")) { | ||
| 73 | + assertThat(tmp.getValue(), is(10)); | ||
| 74 | + } else if (tmp.getNamedValue().equals("100m")) { | ||
| 75 | + assertThat(tmp.getValue(), is(100)); | ||
| 76 | + } else if (tmp.getNamedValue().equals("auto")) { | ||
| 77 | + assertThat(tmp.getValue(), is(1000)); | ||
| 78 | + } | ||
| 79 | + } | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + /** | ||
| 83 | + * Checks explicit value and auto generated value. | ||
| 84 | + */ | ||
| 85 | + @Test | ||
| 86 | + public void processValueAndAutoStatement() throws IOException, ParserException { | ||
| 87 | + | ||
| 88 | + YangNode node = manager.getDataModel("src/test/resources/ValueAndAutoStatement.yang"); | ||
| 89 | + | ||
| 90 | + // Check whether the data model tree returned is of type module. | ||
| 91 | + assertThat((node instanceof YangModule), is(true)); | ||
| 92 | + | ||
| 93 | + // Check whether the node type is set properly to module. | ||
| 94 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
| 95 | + | ||
| 96 | + // Check whether the module name is set correctly. | ||
| 97 | + YangModule yangNode = (YangModule) node; | ||
| 98 | + assertThat(yangNode.getName(), is("Test")); | ||
| 99 | + | ||
| 100 | + ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); | ||
| 101 | + YangLeaf leafInfo = leafIterator.next(); | ||
| 102 | + | ||
| 103 | + assertThat(leafInfo.getLeafName(), is("speed")); | ||
| 104 | + assertThat(leafInfo.getDataType().getDataTypeName(), is("enumeration")); | ||
| 105 | + assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.ENUMERATION)); | ||
| 106 | + assertThat(((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumerationName(), | ||
| 107 | + is("speed")); | ||
| 108 | + | ||
| 109 | + Set<YangEnum> enumSet = ((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumSet(); | ||
| 110 | + for (YangEnum tmp : enumSet) { | ||
| 111 | + if (tmp.getNamedValue().equals("10m")) { | ||
| 112 | + assertThat(tmp.getValue(), is(10)); | ||
| 113 | + } else if (tmp.getNamedValue().equals("100m")) { | ||
| 114 | + assertThat(tmp.getValue(), is(11)); | ||
| 115 | + } else if (tmp.getNamedValue().equals("auto")) { | ||
| 116 | + assertThat(tmp.getValue(), is(1000)); | ||
| 117 | + } | ||
| 118 | + } | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + /** | ||
| 122 | + * Checks explicit value should not be repeated. | ||
| 123 | + */ | ||
| 124 | + @Test(expected = ParserException.class) | ||
| 125 | + public void processValueDuplication() throws IOException, ParserException { | ||
| 126 | + | ||
| 127 | + YangNode node = manager.getDataModel("src/test/resources/ValueDuplication.yang"); | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + /** | ||
| 131 | + * Checks explicit or auto generated value should not be repeated. | ||
| 132 | + */ | ||
| 133 | + @Test(expected = ParserException.class) | ||
| 134 | + public void processValueExplicitAndAutoDuplication() throws IOException, ParserException { | ||
| 135 | + | ||
| 136 | + YangNode node = manager.getDataModel("src/test/resources/ValueExplicitAndAutoDuplication.yang"); | ||
| 137 | + } | ||
| 138 | +} |
-
Please register or login to post a comment