blob: a983f797f1b3feb9e06ac5d464b7ed59d371749b [file] [log] [blame]
Gaurav Agrawalbd804472016-03-25 11:25:36 +05301/*
2 * Copyright 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 * union-specification = 1*(type-stmt stmtsep)
34 *
35 * ANTLR grammar rule
36 * typeBodyStatements : numericalRestrictions | stringRestrictions | enumSpecification
37 * | leafrefSpecification | identityrefSpecification | instanceIdentifierSpecification
38 * | bitsSpecification | unionSpecification;
39 *
40 * unionSpecification : typeStatement+;
41 */
42
43import org.onosproject.yangutils.datamodel.YangLeaf;
44import org.onosproject.yangutils.datamodel.YangLeafList;
45import org.onosproject.yangutils.datamodel.YangType;
46import org.onosproject.yangutils.datamodel.YangUnion;
47import org.onosproject.yangutils.parser.Parsable;
48import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
49import org.onosproject.yangutils.parser.exceptions.ParserException;
50import org.onosproject.yangutils.parser.impl.TreeWalkListener;
51
52import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
53import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
54import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
55import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
56import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
57import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
58import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
59import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
60import static org.onosproject.yangutils.utils.YangConstructType.UNION_DATA;
61
62/**
63 * Implements listener based call back function corresponding to the "union" rule
64 * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
65 */
66public final class UnionListener {
67 /**
68 * Creates a new union listener.
69 */
70 private UnionListener() {
71 }
72
73 /**
74 * It is called when parser enters grammar rule (union), it perform
75 * validations and updates the data model tree.
76 *
77 * @param listener listener's object
78 * @param ctx context object of the grammar rule
79 */
80 public static void processUnionEntry(TreeWalkListener listener,
81 GeneratedYangParser.UnionSpecificationContext ctx) {
82
83 // Check for stack to be non empty.
84 checkStackIsNotEmpty(listener, MISSING_HOLDER, UNION_DATA, "", ENTRY);
85
86 if (listener.getParsedDataStack().peek() instanceof YangType) {
87 YangUnion unionNode = new YangUnion();
88 Parsable typeData = listener.getParsedDataStack().pop();
89
90 // Check for stack to be non empty.
91 checkStackIsNotEmpty(listener, MISSING_HOLDER, UNION_DATA, "", ENTRY);
92
93 Parsable tmpData = listener.getParsedDataStack().peek();
94
95 switch (tmpData.getYangConstructType()) {
96 case LEAF_DATA:
97 unionNode.setUnionName(((YangLeaf) tmpData).getLeafName());
98 break;
99 case LEAF_LIST_DATA:
100 unionNode.setUnionName(((YangLeafList) tmpData).getLeafName());
101 break;
102 case UNION_DATA:
103 unionNode.setUnionName(((YangUnion) tmpData).getUnionName());
104 break;
105 // TODO typedef, deviate.
106 default:
107 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
108 ((YangType<?>) typeData).getDataTypeName(), ENTRY));
109 }
110 listener.getParsedDataStack().push(typeData);
111 listener.getParsedDataStack().push(unionNode);
112 } else {
113 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, UNION_DATA, "", ENTRY));
114 }
115 }
116
117 /**
118 * It is called when parser exits from grammar rule (union), it perform
119 * validations and update the data model tree.
120 *
121 * @param listener Listener's object
122 * @param ctx context object of the grammar rule
123 */
124 public static void processUnionExit(TreeWalkListener listener,
125 GeneratedYangParser.UnionSpecificationContext ctx) {
126
127 // Check for stack to be non empty.
128 checkStackIsNotEmpty(listener, MISSING_HOLDER, UNION_DATA, "", EXIT);
129
130 Parsable tmpUnionNode = listener.getParsedDataStack().peek();
131 if (tmpUnionNode instanceof YangUnion) {
132 YangUnion unionNode = (YangUnion) tmpUnionNode;
133 listener.getParsedDataStack().pop();
134
135 // Check for stack to be non empty.
136 checkStackIsNotEmpty(listener, MISSING_HOLDER, UNION_DATA, "", EXIT);
137
138 Parsable tmpNode = listener.getParsedDataStack().peek();
139 switch (tmpNode.getYangConstructType()) {
140 case TYPE_DATA: {
141 YangType<YangUnion> typeNode = (YangType<YangUnion>) tmpNode;
142 typeNode.setDataTypeExtendedInfo(unionNode);
143 break;
144 }
145 default:
146 throw new ParserException(
147 constructListenerErrorMessage(INVALID_HOLDER, UNION_DATA, "", EXIT));
148 }
149 } else {
150 throw new ParserException(
151 constructListenerErrorMessage(MISSING_CURRENT_HOLDER, UNION_DATA, "", EXIT));
152 }
153 }
154}