blob: 79ce0b9575ea16ae85e5f58402e18456a15414f1 [file] [log] [blame]
Gaurav Agrawalbd804472016-03-25 11:25:36 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawalbd804472016-03-25 11:25:36 +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 * 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/**
Bharat saraswald9822e92016-04-05 15:13:44 +053063 * Represents listener based call back function corresponding to the "union" rule
Gaurav Agrawalbd804472016-03-25 11:25:36 +053064 * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
65 */
66public final class UnionListener {
Bharat saraswald9822e92016-04-05 15:13:44 +053067
Gaurav Agrawalbd804472016-03-25 11:25:36 +053068 /**
69 * Creates a new union listener.
70 */
71 private UnionListener() {
72 }
73
74 /**
75 * It is called when parser enters grammar rule (union), 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 processUnionEntry(TreeWalkListener listener,
82 GeneratedYangParser.UnionSpecificationContext ctx) {
83
84 // Check for stack to be non empty.
85 checkStackIsNotEmpty(listener, MISSING_HOLDER, UNION_DATA, "", ENTRY);
86
87 if (listener.getParsedDataStack().peek() instanceof YangType) {
88 YangUnion unionNode = new YangUnion();
89 Parsable typeData = listener.getParsedDataStack().pop();
90
91 // Check for stack to be non empty.
92 checkStackIsNotEmpty(listener, MISSING_HOLDER, UNION_DATA, "", ENTRY);
93
94 Parsable tmpData = listener.getParsedDataStack().peek();
95
96 switch (tmpData.getYangConstructType()) {
97 case LEAF_DATA:
98 unionNode.setUnionName(((YangLeaf) tmpData).getLeafName());
99 break;
100 case LEAF_LIST_DATA:
101 unionNode.setUnionName(((YangLeafList) tmpData).getLeafName());
102 break;
103 case UNION_DATA:
104 unionNode.setUnionName(((YangUnion) tmpData).getUnionName());
105 break;
106 // TODO typedef, deviate.
107 default:
108 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
109 ((YangType<?>) typeData).getDataTypeName(), ENTRY));
110 }
111 listener.getParsedDataStack().push(typeData);
112 listener.getParsedDataStack().push(unionNode);
113 } else {
114 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, UNION_DATA, "", ENTRY));
115 }
116 }
117
118 /**
119 * It is called when parser exits from grammar rule (union), it perform
120 * validations and update the data model tree.
121 *
122 * @param listener Listener's object
123 * @param ctx context object of the grammar rule
124 */
125 public static void processUnionExit(TreeWalkListener listener,
126 GeneratedYangParser.UnionSpecificationContext ctx) {
127
128 // Check for stack to be non empty.
129 checkStackIsNotEmpty(listener, MISSING_HOLDER, UNION_DATA, "", EXIT);
130
131 Parsable tmpUnionNode = listener.getParsedDataStack().peek();
132 if (tmpUnionNode instanceof YangUnion) {
133 YangUnion unionNode = (YangUnion) tmpUnionNode;
134 listener.getParsedDataStack().pop();
135
136 // Check for stack to be non empty.
137 checkStackIsNotEmpty(listener, MISSING_HOLDER, UNION_DATA, "", EXIT);
138
139 Parsable tmpNode = listener.getParsedDataStack().peek();
140 switch (tmpNode.getYangConstructType()) {
141 case TYPE_DATA: {
142 YangType<YangUnion> typeNode = (YangType<YangUnion>) tmpNode;
143 typeNode.setDataTypeExtendedInfo(unionNode);
144 break;
145 }
146 default:
147 throw new ParserException(
148 constructListenerErrorMessage(INVALID_HOLDER, UNION_DATA, "", EXIT));
149 }
150 } else {
151 throw new ParserException(
152 constructListenerErrorMessage(MISSING_CURRENT_HOLDER, UNION_DATA, "", EXIT));
153 }
154 }
155}