blob: bf8c6b060f39c988d9ed4dc5da698f8ad51bb1d2 [file] [log] [blame]
Vidyashree Ramaa2f73982016-04-12 23:33:33 +05301/*
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +05302 * Copyright 2016-present Open Networking Laboratory
Vidyashree Ramaa2f73982016-04-12 23:33:33 +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
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053019import org.onosproject.yangutils.datamodel.YangDerivedInfo;
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053020import org.onosproject.yangutils.datamodel.YangRangeRestriction;
21import org.onosproject.yangutils.datamodel.YangStringRestriction;
22import org.onosproject.yangutils.datamodel.YangType;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053023import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053024import org.onosproject.yangutils.datamodel.utils.Parsable;
25import org.onosproject.yangutils.datamodel.utils.YangConstructType;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053026import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
27import org.onosproject.yangutils.parser.exceptions.ParserException;
28import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053029
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053030import static org.onosproject.yangutils.datamodel.YangDataTypes.BINARY;
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053031import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053032import static org.onosproject.yangutils.datamodel.YangDataTypes.STRING;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053033import static org.onosproject.yangutils.datamodel.utils.RestrictionResolver.processLengthRestriction;
Bharat saraswal96dfef02016-06-16 00:29:12 +053034import static org.onosproject.yangutils.datamodel.utils.YangConstructType.LENGTH_DATA;
35import static org.onosproject.yangutils.datamodel.utils.YangConstructType.TYPE_DATA;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053036import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
Vidyashree Rama1db15562016-05-17 16:16:15 +053037import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053038import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
39import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Vidyashree Rama1db15562016-05-17 16:16:15 +053040import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053041import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053042import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053043
44/*
45 * Reference: RFC6020 and YANG ANTLR Grammar
46 *
47 * ABNF grammar as per RFC6020
48 * length-stmt = length-keyword sep length-arg-str optsep
49 * (";" /
50 * "{" stmtsep
51 * ;; these stmts can appear in any order
52 * [error-message-stmt stmtsep]
53 * [error-app-tag-stmt stmtsep]
54 * [description-stmt stmtsep]
55 * [reference-stmt stmtsep]
56 * "}")
57 *
58 *
59 * ANTLR grammar rule
60 * lengthStatement : LENGTH_KEYWORD length
61 * (STMTEND | LEFT_CURLY_BRACE commonStatements RIGHT_CURLY_BRACE);
62 */
63
64/**
65 * Represents listener based call back function corresponding to the "length"
66 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
67 */
68public final class LengthRestrictionListener {
69
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053070 /**
71 * Creates a new length restriction listener.
72 */
73 private LengthRestrictionListener() {
74 }
75
76 /**
77 * It is called when parser receives an input matching the grammar
78 * rule (length), performs validation and updates the data model
79 * tree.
80 *
81 * @param listener listener's object
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053082 * @param ctx context object of the grammar rule
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053083 */
84 public static void processLengthRestrictionEntry(TreeWalkListener listener,
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053085 GeneratedYangParser.LengthStatementContext ctx) {
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053086
87 // Check for stack to be non empty.
88 checkStackIsNotEmpty(listener, MISSING_HOLDER, LENGTH_DATA, ctx.length().getText(), ENTRY);
89
90 Parsable tmpData = listener.getParsedDataStack().peek();
91 if (tmpData.getYangConstructType() == TYPE_DATA) {
92 YangType type = (YangType) tmpData;
Vidyashree Rama1db15562016-05-17 16:16:15 +053093 setLengthRestriction(listener, type, ctx);
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053094 } else {
95 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LENGTH_DATA,
96 ctx.length().getText(), ENTRY));
97 }
98 }
99
100 /**
101 * Sets the length restriction to type.
102 *
Vidyashree Rama1db15562016-05-17 16:16:15 +0530103 * @param listener listener's object
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530104 * @param type Yang type for which length restriction to be set
105 * @param ctx context object of the grammar rule
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530106 */
Vidyashree Rama1db15562016-05-17 16:16:15 +0530107 private static void setLengthRestriction(TreeWalkListener listener, YangType type,
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530108 GeneratedYangParser.LengthStatementContext ctx) {
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530109
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530110 if (type.getDataType() == DERIVED) {
111 ((YangDerivedInfo<YangRangeRestriction>) type.getDataTypeExtendedInfo())
112 .setLengthRestrictionString(ctx.length().getText());
113 ((YangDerivedInfo<YangRangeRestriction>) type.getDataTypeExtendedInfo())
114 .setLineNumber(ctx.getStart().getLine());
115 ((YangDerivedInfo<YangRangeRestriction>) type.getDataTypeExtendedInfo())
116 .setCharPosition(ctx.getStart().getCharPositionInLine());
117 return;
118 }
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530119
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530120 if (type.getDataType() != STRING && type.getDataType() != BINARY) {
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530121 ParserException parserException = new ParserException("YANG file error : " +
122 YangConstructType.getYangConstructType(LENGTH_DATA) + " name " + ctx.length().getText() +
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530123 " can be used to restrict the built-in type string/binary or types derived from string/binary.");
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530124 parserException.setLine(ctx.getStart().getLine());
125 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
126 throw parserException;
127 }
128
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530129 YangRangeRestriction lengthRestriction = null;
130 try {
131 lengthRestriction = processLengthRestriction(null, ctx.getStart().getLine(),
132 ctx.getStart().getCharPositionInLine(), false, ctx.length().getText());
133 } catch (DataModelException e) {
134 ParserException parserException = new ParserException(e.getMessage());
135 parserException.setCharPosition(e.getCharPositionInLine());
136 parserException.setLine(e.getLineNumber());
137 throw parserException;
138 }
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530139
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530140 if (type.getDataType() == STRING) {
141 YangStringRestriction stringRestriction = (YangStringRestriction) type.getDataTypeExtendedInfo();
142 if (stringRestriction == null) {
143 stringRestriction = new YangStringRestriction();
144 type.setDataTypeExtendedInfo(stringRestriction);
145 }
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530146
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530147 stringRestriction.setLengthRestriction(lengthRestriction);
148 } else {
149 type.setDataTypeExtendedInfo(lengthRestriction);
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530150 }
151
Vidyashree Rama1db15562016-05-17 16:16:15 +0530152 listener.getParsedDataStack().push(lengthRestriction);
153 }
154
155 /**
156 * Performs validation and updates the data model tree.
157 * It is called when parser exits from grammar rule (length).
158 *
159 * @param listener listener's object
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530160 * @param ctx context object of the grammar rule
Vidyashree Rama1db15562016-05-17 16:16:15 +0530161 */
162 public static void processLengthRestrictionExit(TreeWalkListener listener,
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530163 GeneratedYangParser.LengthStatementContext ctx) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530164
165 // Check for stack to be non empty.
166 checkStackIsNotEmpty(listener, MISSING_HOLDER, LENGTH_DATA, ctx.length().getText(), EXIT);
167
168 Parsable tmpData = listener.getParsedDataStack().peek();
169 if (tmpData instanceof YangRangeRestriction) {
170 listener.getParsedDataStack().pop();
171 } else if (tmpData instanceof YangType
172 && ((YangType) tmpData).getDataType() == DERIVED) {
173 // TODO : need to handle in linker
174 } else {
175 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, LENGTH_DATA,
176 ctx.length().getText(), EXIT));
177 }
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530178 }
179}