blob: 03fc737df41499749e81e1a74de41d2e5f38cd4f [file] [log] [blame]
Vidyashree Ramabc9611f2016-04-12 23:33:33 +05301/*
Gaurav Agrawalcc55d482016-05-03 00:41:48 +05302 * Copyright 2016-present Open Networking Laboratory
Vidyashree Ramabc9611f2016-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 Ramabc9611f2016-04-12 23:33:33 +053019import org.onosproject.yangutils.datamodel.YangDataTypes;
20import org.onosproject.yangutils.datamodel.YangDerivedInfo;
Gaurav Agrawalcc55d482016-05-03 00:41:48 +053021import org.onosproject.yangutils.datamodel.YangRangeRestriction;
22import org.onosproject.yangutils.datamodel.YangStringRestriction;
23import org.onosproject.yangutils.datamodel.YangType;
Vidyashree Ramabc9611f2016-04-12 23:33:33 +053024import org.onosproject.yangutils.parser.Parsable;
25import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
26import org.onosproject.yangutils.parser.exceptions.ParserException;
27import org.onosproject.yangutils.parser.impl.TreeWalkListener;
28import org.onosproject.yangutils.utils.YangConstructType;
Vidyashree Ramabc9611f2016-04-12 23:33:33 +053029
Gaurav Agrawalcc55d482016-05-03 00:41:48 +053030import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED;
Vidyashree Ramabc9611f2016-04-12 23:33:33 +053031import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
Vidyashree Ramabc9611f2016-04-12 23:33:33 +053032import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Vidyashree Ramabc9611f2016-04-12 23:33:33 +053035import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawalcc55d482016-05-03 00:41:48 +053036import static org.onosproject.yangutils.utils.RestrictionResolver.processLengthRestriction;
37import static org.onosproject.yangutils.utils.YangConstructType.LENGTH_DATA;
38import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
Vidyashree Ramabc9611f2016-04-12 23:33:33 +053039
40/*
41 * Reference: RFC6020 and YANG ANTLR Grammar
42 *
43 * ABNF grammar as per RFC6020
44 * length-stmt = length-keyword sep length-arg-str optsep
45 * (";" /
46 * "{" stmtsep
47 * ;; these stmts can appear in any order
48 * [error-message-stmt stmtsep]
49 * [error-app-tag-stmt stmtsep]
50 * [description-stmt stmtsep]
51 * [reference-stmt stmtsep]
52 * "}")
53 *
54 *
55 * ANTLR grammar rule
56 * lengthStatement : LENGTH_KEYWORD length
57 * (STMTEND | LEFT_CURLY_BRACE commonStatements RIGHT_CURLY_BRACE);
58 */
59
60/**
61 * Represents listener based call back function corresponding to the "length"
62 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
63 */
64public final class LengthRestrictionListener {
65
Vidyashree Ramabc9611f2016-04-12 23:33:33 +053066 /**
67 * Creates a new length restriction listener.
68 */
69 private LengthRestrictionListener() {
70 }
71
72 /**
73 * It is called when parser receives an input matching the grammar
74 * rule (length), performs validation and updates the data model
75 * tree.
76 *
77 * @param listener listener's object
Gaurav Agrawalcc55d482016-05-03 00:41:48 +053078 * @param ctx context object of the grammar rule
Vidyashree Ramabc9611f2016-04-12 23:33:33 +053079 */
80 public static void processLengthRestrictionEntry(TreeWalkListener listener,
Gaurav Agrawalcc55d482016-05-03 00:41:48 +053081 GeneratedYangParser.LengthStatementContext ctx) {
Vidyashree Ramabc9611f2016-04-12 23:33:33 +053082
83 // Check for stack to be non empty.
84 checkStackIsNotEmpty(listener, MISSING_HOLDER, LENGTH_DATA, ctx.length().getText(), ENTRY);
85
86 Parsable tmpData = listener.getParsedDataStack().peek();
87 if (tmpData.getYangConstructType() == TYPE_DATA) {
88 YangType type = (YangType) tmpData;
89 setLengthRestriction(type, ctx);
90 } else {
91 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LENGTH_DATA,
92 ctx.length().getText(), ENTRY));
93 }
94 }
95
96 /**
97 * Sets the length restriction to type.
98 *
99 * @param type Yang type for which length restriction to be set
Gaurav Agrawalcc55d482016-05-03 00:41:48 +0530100 * @param ctx context object of the grammar rule
Vidyashree Ramabc9611f2016-04-12 23:33:33 +0530101 */
102 private static void setLengthRestriction(YangType type,
Gaurav Agrawalcc55d482016-05-03 00:41:48 +0530103 GeneratedYangParser.LengthStatementContext ctx) {
Vidyashree Ramabc9611f2016-04-12 23:33:33 +0530104
Gaurav Agrawalcc55d482016-05-03 00:41:48 +0530105 if (type.getDataType() == DERIVED) {
106 ((YangDerivedInfo<YangRangeRestriction>) type.getDataTypeExtendedInfo())
107 .setLengthRestrictionString(ctx.length().getText());
108 ((YangDerivedInfo<YangRangeRestriction>) type.getDataTypeExtendedInfo())
109 .setLineNumber(ctx.getStart().getLine());
110 ((YangDerivedInfo<YangRangeRestriction>) type.getDataTypeExtendedInfo())
111 .setCharPosition(ctx.getStart().getCharPositionInLine());
112 return;
113 }
Vidyashree Ramabc9611f2016-04-12 23:33:33 +0530114
Gaurav Agrawalcc55d482016-05-03 00:41:48 +0530115 if (type.getDataType() != YangDataTypes.STRING) {
Vidyashree Ramabc9611f2016-04-12 23:33:33 +0530116 ParserException parserException = new ParserException("YANG file error : " +
117 YangConstructType.getYangConstructType(LENGTH_DATA) + " name " + ctx.length().getText() +
118 " can be used to restrict the built-in type string or types derived from string.");
119 parserException.setLine(ctx.getStart().getLine());
120 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
121 throw parserException;
122 }
123
Gaurav Agrawalcc55d482016-05-03 00:41:48 +0530124 YangRangeRestriction lengthRestriction = processLengthRestriction(null, ctx.getStart().getLine(),
125 ctx.getStart().getCharPositionInLine(), false, ctx.length().getText());
126
127 YangStringRestriction stringRestriction = (YangStringRestriction) type.getDataTypeExtendedInfo();
Vidyashree Ramabc9611f2016-04-12 23:33:33 +0530128
129 if (stringRestriction == null) {
130 stringRestriction = new YangStringRestriction();
Gaurav Agrawalcc55d482016-05-03 00:41:48 +0530131 type.setDataTypeExtendedInfo(stringRestriction);
Vidyashree Ramabc9611f2016-04-12 23:33:33 +0530132 }
133
134 stringRestriction.setLengthRestriction(lengthRestriction);
135 }
136}