blob: 0dab0f1361a3dc27fd0d2f7cc5221008c4a7c7a3 [file] [log] [blame]
Gaurav Agrawal4ce3acf2016-02-25 04:37:05 +05301/*
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
17package 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
44import org.onosproject.yangutils.datamodel.YangEnumeration;
45import org.onosproject.yangutils.datamodel.YangLeaf;
46import org.onosproject.yangutils.datamodel.YangLeafList;
47import org.onosproject.yangutils.datamodel.YangType;
48import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal4ce3acf2016-02-25 04:37:05 +053049import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
50import org.onosproject.yangutils.parser.exceptions.ParserException;
51import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vinod Kumar S08710982016-03-03 19:55:30 +053052
Gaurav Agrawal4ce3acf2016-02-25 04:37:05 +053053import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
54import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
55import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
56import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
57import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
58import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
59import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Vinod Kumar S08710982016-03-03 19:55:30 +053060import static org.onosproject.yangutils.utils.YangConstructType.ENUMERATION_DATA;
61import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
Gaurav Agrawal4ce3acf2016-02-25 04:37:05 +053062
63/**
64 * Implements listener based call back function corresponding to the
65 * "enumeration" rule defined in ANTLR grammar file for corresponding ABNF rule
66 * in RFC 6020.
67 */
68public final class EnumerationListener {
69
70 /**
71 * Creates a new enumeration listener.
72 */
73 private EnumerationListener() {
74 }
75
76 /**
77 * It is called when parser enters grammar rule (enumeration), it perform
78 * validations and updates the data model tree.
79 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053080 * @param listener listener's object
81 * @param ctx context object of the grammar rule
Gaurav Agrawal4ce3acf2016-02-25 04:37:05 +053082 */
83 public static void processEnumerationEntry(TreeWalkListener listener,
84 GeneratedYangParser.EnumSpecificationContext ctx) {
85
86 // Check for stack to be non empty.
87 checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", ENTRY);
88
89 if (listener.getParsedDataStack().peek() instanceof YangType) {
90 YangEnumeration enumerationNode = new YangEnumeration();
91 Parsable typeData = listener.getParsedDataStack().pop();
92
93 // Check for stack to be non empty.
94 checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", ENTRY);
95
96 Parsable tmpData = listener.getParsedDataStack().peek();
97
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053098 switch (tmpData.getYangConstructType()) {
Gaurav Agrawal4ce3acf2016-02-25 04:37:05 +053099 case LEAF_DATA:
100 enumerationNode.setEnumerationName(((YangLeaf) tmpData).getLeafName());
101 break;
102 case LEAF_LIST_DATA:
103 enumerationNode.setEnumerationName(((YangLeafList) tmpData).getLeafName());
104 break;
105 // TODO typedef, union, deviate.
106 default:
107 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
Vinod Kumar S08710982016-03-03 19:55:30 +0530108 ((YangType<?>) typeData).getDataTypeName(), ENTRY));
Gaurav Agrawal4ce3acf2016-02-25 04:37:05 +0530109 }
110 listener.getParsedDataStack().push(typeData);
111 listener.getParsedDataStack().push(enumerationNode);
112 } else {
113 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, ENUMERATION_DATA, "", ENTRY));
114 }
115 }
116
117 /**
118 * It is called when parser exits from grammar rule (enumeration), it
119 * perform validations and update the data model tree.
120 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530121 * @param listener Listener's object
122 * @param ctx context object of the grammar rule
Gaurav Agrawal4ce3acf2016-02-25 04:37:05 +0530123 */
124 public static void processEnumerationExit(TreeWalkListener listener,
125 GeneratedYangParser.EnumSpecificationContext ctx) {
126
127 // Check for stack to be non empty.
128 checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", EXIT);
129
130 Parsable tmpEnumerationNode = listener.getParsedDataStack().peek();
131 if (tmpEnumerationNode instanceof YangEnumeration) {
Vinod Kumar S08710982016-03-03 19:55:30 +0530132 YangEnumeration enumerationNode = (YangEnumeration) tmpEnumerationNode;
Gaurav Agrawal4ce3acf2016-02-25 04:37:05 +0530133 listener.getParsedDataStack().pop();
134
135 // Check for stack to be non empty.
136 checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", EXIT);
137
138 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530139 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawal4ce3acf2016-02-25 04:37:05 +0530140 case TYPE_DATA: {
Vinod Kumar S08710982016-03-03 19:55:30 +0530141 YangType<YangEnumeration> typeNode = (YangType<YangEnumeration>) tmpNode;
142 typeNode.setDataTypeExtendedInfo(enumerationNode);
Gaurav Agrawal4ce3acf2016-02-25 04:37:05 +0530143 break;
144 }
145 default:
146 throw new ParserException(
147 constructListenerErrorMessage(INVALID_HOLDER, ENUMERATION_DATA, "", EXIT));
148 }
149 } else {
150 throw new ParserException(
151 constructListenerErrorMessage(MISSING_CURRENT_HOLDER, ENUMERATION_DATA, "", EXIT));
152 }
153 }
154}