blob: 0501806f492f3320ae4fcdf0f47fd205138b54d2 [file] [log] [blame]
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +05303 *
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 * bits-specification = 1*(bit-stmt stmtsep)
34 *
35 * ANTLR grammar rule
36 *
37 * typeBodyStatements : numericalRestrictions | stringRestrictions | enumSpecification
38 * | leafrefSpecification | identityrefSpecification | instanceIdentifierSpecification
39 * | bitsSpecification | unionSpecification;
40 *
41 * bitsSpecification : bitStatement+;
42 */
43
44import org.onosproject.yangutils.datamodel.YangBits;
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 Agrawal0cfdeed2016-02-26 02:47:39 +053049import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
50import org.onosproject.yangutils.parser.exceptions.ParserException;
51import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053052
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +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 Sc4216002016-03-03 19:55:30 +053060import static org.onosproject.yangutils.utils.YangConstructType.BITS_DATA;
61import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053062
63/**
Bharat saraswald9822e92016-04-05 15:13:44 +053064 * Represents listener based call back function corresponding to the "bits" rule
Vinod Kumar Sc4216002016-03-03 19:55:30 +053065 * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053066 */
67public final class BitsListener {
68
69 /**
70 * Creates a new bits listener.
71 */
72 private BitsListener() {
73 }
74
75 /**
76 * It is called when parser enters grammar rule (bits), it perform
77 * validations and updates the data model tree.
78 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053079 * @param listener listener's object
80 * @param ctx context object of the grammar rule
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053081 */
82 public static void processBitsEntry(TreeWalkListener listener,
Vinod Kumar Sc4216002016-03-03 19:55:30 +053083 GeneratedYangParser.BitsSpecificationContext ctx) {
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053084
85 // Check for stack to be non empty.
86 checkStackIsNotEmpty(listener, MISSING_HOLDER, BITS_DATA, "", ENTRY);
87
88 if (listener.getParsedDataStack().peek() instanceof YangType) {
89 YangBits bitsNode = new YangBits();
90 Parsable typeData = listener.getParsedDataStack().pop();
91
92 // Check for stack to be non empty.
93 checkStackIsNotEmpty(listener, MISSING_HOLDER, BITS_DATA, "", ENTRY);
94
95 Parsable tmpData = listener.getParsedDataStack().peek();
96
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053097 switch (tmpData.getYangConstructType()) {
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053098 case LEAF_DATA:
99 bitsNode.setBitsName(((YangLeaf) tmpData).getLeafName());
100 break;
101 case LEAF_LIST_DATA:
102 bitsNode.setBitsName(((YangLeafList) tmpData).getLeafName());
103 break;
104 // TODO typedef, union, deviate.
105 default:
106 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530107 ((YangType<?>) typeData).getDataTypeName(), ENTRY));
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530108 }
109 listener.getParsedDataStack().push(typeData);
110 listener.getParsedDataStack().push(bitsNode);
111 } else {
112 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, BITS_DATA, "", ENTRY));
113 }
114 }
115
116 /**
117 * It is called when parser exits from grammar rule (bits), it perform
118 * validations and update the data model tree.
119 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530120 * @param listener Listener's object
121 * @param ctx context object of the grammar rule
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530122 */
123 public static void processBitsExit(TreeWalkListener listener,
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530124 GeneratedYangParser.BitsSpecificationContext ctx) {
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530125
126 // Check for stack to be non empty.
127 checkStackIsNotEmpty(listener, MISSING_HOLDER, BITS_DATA, "", EXIT);
128
129 Parsable tmpBitsNode = listener.getParsedDataStack().peek();
130 if (tmpBitsNode instanceof YangBits) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530131 YangBits bitsNode = (YangBits) tmpBitsNode;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530132 listener.getParsedDataStack().pop();
133
134 // Check for stack to be non empty.
135 checkStackIsNotEmpty(listener, MISSING_HOLDER, BITS_DATA, "", EXIT);
136
137 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530138 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530139 case TYPE_DATA: {
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530140 YangType<YangBits> typeNode = (YangType<YangBits>) tmpNode;
141 typeNode.setDataTypeExtendedInfo(bitsNode);
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530142 break;
143 }
144 default:
145 throw new ParserException(
146 constructListenerErrorMessage(INVALID_HOLDER, BITS_DATA, "", EXIT));
147 }
148 } else {
149 throw new ParserException(
150 constructListenerErrorMessage(MISSING_CURRENT_HOLDER, BITS_DATA, "", EXIT));
151 }
152 }
153}