blob: 45f58c885a7152239697f12d65501cd501dcd8b0 [file] [log] [blame]
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +05301/*
2 * Copyright 2016-present 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 * decimal64-specification = fraction-digits-stmt [range-stmt stmtsep]
34 *
35 * fraction-digits-stmt = fraction-digits-keyword sep
36 * fraction-digits-arg-str stmtend
37 *
38 * fraction-digits-arg-str = < a string that matches the rule
39 * fraction-digits-arg >
40 *
41 * fraction-digits-arg = ("1" ["0" / "1" / "2" / "3" / "4" /
42 * "5" / "6" / "7" / "8"])
43 * / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9"
44 *
45 * range-stmt = range-keyword sep range-arg-str optsep
46 * (";" /
47 * "{" stmtsep
48 * ;; these stmts can appear in any order
49 * [error-message-stmt stmtsep]
50 * [error-app-tag-stmt stmtsep]
51 * [description-stmt stmtsep]
52 * [reference-stmt stmtsep]
53 * "}")
54 * ANTLR grammar rule
55 *
56 * typeBodyStatements : numericalRestrictions | decimal64Specification | stringRestrictions | enumSpecification
57 * | leafrefSpecification | identityrefSpecification | instanceIdentifierSpecification
58 * | bitsSpecification | unionSpecification;
59 *
60 * decimal64Specification : fractionDigitStatement rangeStatement?;
61 *
62 * fractionDigitStatement : FRACTION_DIGITS_KEYWORD fraction STMTEND;
63 *
64 * fraction : string;
65 */
66
67import org.onosproject.yangutils.datamodel.YangDecimal64;
68import org.onosproject.yangutils.datamodel.YangRangeRestriction;
69import org.onosproject.yangutils.datamodel.YangType;
70import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
71import org.onosproject.yangutils.datamodel.utils.Parsable;
72import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
73import org.onosproject.yangutils.parser.exceptions.ParserException;
74import org.onosproject.yangutils.parser.impl.TreeWalkListener;
75
76import static org.onosproject.yangutils.datamodel.utils.YangConstructType.DECIMAL64_DATA;
77import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
78import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
79import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
80import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
81import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
82import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
83
84/**
85 * Represents listener based call back function corresponding to the "decimal64" rule
86 * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
87 */
88public final class Decimal64Listener {
89
90 /**
91 * Creates a new Decimal64 listener.
92 */
93 private Decimal64Listener() {
94 }
95
96 /**
97 * It is called when parser enters grammar rule (decimal64), it perform
98 * validations and updates the data model tree.
99 *
100 * @param listener listener's object
101 * @param ctx context object of the grammar rule
102 */
103 public static void processDecimal64Entry(TreeWalkListener listener,
104 GeneratedYangParser.Decimal64SpecificationContext ctx) {
105
106 // Check for stack to be non empty.
107 checkStackIsNotEmpty(listener, MISSING_HOLDER, DECIMAL64_DATA, "", ENTRY);
108
109 Parsable tmpNode = listener.getParsedDataStack().peek();
110 if (tmpNode instanceof YangType) {
111 YangType<YangDecimal64<YangRangeRestriction>> typeNode =
112 (YangType<YangDecimal64<YangRangeRestriction>>) tmpNode;
113 YangDecimal64 decimal64Node = new YangDecimal64();
114 typeNode.setDataTypeExtendedInfo(decimal64Node);
115 } else {
116 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, DECIMAL64_DATA, "", ENTRY));
117 }
118 }
119
120 /**
121 * Performs validation and updates the data model tree.
122 * It is called when parser exits from grammar rule (decimal64).
123 *
124 * @param listener listener's object
125 * @param ctx context object of the grammar rule
126 */
127 public static void processDecimal64Exit(TreeWalkListener listener,
128 GeneratedYangParser.Decimal64SpecificationContext ctx) {
129
130 // Check for stack to be non empty.
131 checkStackIsNotEmpty(listener, MISSING_HOLDER, DECIMAL64_DATA, "", EXIT);
132
133 Parsable tmpNode = listener.getParsedDataStack().peek();
134 if (tmpNode instanceof YangType) {
135 YangType<YangDecimal64<YangRangeRestriction>> typeNode =
136 (YangType<YangDecimal64<YangRangeRestriction>>) tmpNode;
137 YangDecimal64<YangRangeRestriction> decimal64Node = typeNode.getDataTypeExtendedInfo();
138 try {
139 decimal64Node.validateRange();
140 } catch (DataModelException e) {
141 throw new ParserException(e);
142 }
143 } else {
144 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, DECIMAL64_DATA, "", EXIT));
145 }
146 }
147}