blob: f83c49c9bec806ec6b35c0fb374fc746e0beb665 [file] [log] [blame]
Gaurav Agrawal9c512e02016-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;
49import static org.onosproject.yangutils.parser.ParsableDataType.ENUMERATION_DATA;
50import static org.onosproject.yangutils.parser.ParsableDataType.TYPE_DATA;
51import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
52import org.onosproject.yangutils.parser.exceptions.ParserException;
53import org.onosproject.yangutils.parser.impl.TreeWalkListener;
54import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
55import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
56import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
57import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
58import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
59import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
60import 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 */
67public 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}