blob: d8f8467020be300cc2c2861aeeaea78e62b23419 [file] [log] [blame]
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +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 * 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;
49import static org.onosproject.yangutils.parser.ParsableDataType.BITS_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 "bits"
64 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
65 */
66public final class BitsListener {
67
68 /**
69 * Creates a new bits listener.
70 */
71 private BitsListener() {
72 }
73
74 /**
75 * It is called when parser enters grammar rule (bits), it perform
76 * validations and updates the data model tree.
77 *
78 * @param listener listener's object.
79 * @param ctx context object of the grammar rule.
80 */
81 public static void processBitsEntry(TreeWalkListener listener,
82 GeneratedYangParser.BitsSpecificationContext ctx) {
83
84 // Check for stack to be non empty.
85 checkStackIsNotEmpty(listener, MISSING_HOLDER, BITS_DATA, "", ENTRY);
86
87 if (listener.getParsedDataStack().peek() instanceof YangType) {
88 YangBits bitsNode = new YangBits();
89 Parsable typeData = listener.getParsedDataStack().pop();
90
91 // Check for stack to be non empty.
92 checkStackIsNotEmpty(listener, MISSING_HOLDER, BITS_DATA, "", ENTRY);
93
94 Parsable tmpData = listener.getParsedDataStack().peek();
95
96 switch (tmpData.getParsableDataType()) {
97 case LEAF_DATA:
98 bitsNode.setBitsName(((YangLeaf) tmpData).getLeafName());
99 break;
100 case LEAF_LIST_DATA:
101 bitsNode.setBitsName(((YangLeafList) tmpData).getLeafName());
102 break;
103 // TODO typedef, union, deviate.
104 default:
105 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
106 ((YangType) typeData).getDataTypeName(), ENTRY));
107 }
108 listener.getParsedDataStack().push(typeData);
109 listener.getParsedDataStack().push(bitsNode);
110 } else {
111 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, BITS_DATA, "", ENTRY));
112 }
113 }
114
115 /**
116 * It is called when parser exits from grammar rule (bits), it perform
117 * validations and update the data model tree.
118 *
119 * @param listener Listener's object.
120 * @param ctx context object of the grammar rule.
121 */
122 public static void processBitsExit(TreeWalkListener listener,
123 GeneratedYangParser.BitsSpecificationContext ctx) {
124
125 // Check for stack to be non empty.
126 checkStackIsNotEmpty(listener, MISSING_HOLDER, BITS_DATA, "", EXIT);
127
128 Parsable tmpBitsNode = listener.getParsedDataStack().peek();
129 if (tmpBitsNode instanceof YangBits) {
130 listener.getParsedDataStack().pop();
131
132 // Check for stack to be non empty.
133 checkStackIsNotEmpty(listener, MISSING_HOLDER, BITS_DATA, "", EXIT);
134
135 Parsable tmpNode = listener.getParsedDataStack().peek();
136 switch (tmpNode.getParsableDataType()) {
137 case TYPE_DATA: {
138 YangType typeNode = (YangType) tmpNode;
139 typeNode.setDataTypeExtendedInfo((YangBits) tmpBitsNode);
140 break;
141 }
142 default:
143 throw new ParserException(
144 constructListenerErrorMessage(INVALID_HOLDER, BITS_DATA, "", EXIT));
145 }
146 } else {
147 throw new ParserException(
148 constructListenerErrorMessage(MISSING_CURRENT_HOLDER, BITS_DATA, "", EXIT));
149 }
150 }
151}