blob: 3c1fd5a6ce86ec1c57ba31408fd3513908651e92 [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.YangDataTypes;
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053020import org.onosproject.yangutils.datamodel.YangDerivedInfo;
21import org.onosproject.yangutils.datamodel.YangPatternRestriction;
22import org.onosproject.yangutils.datamodel.YangStringRestriction;
23import org.onosproject.yangutils.datamodel.YangType;
Vidyashree Ramaa2f73982016-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;
29
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053030import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053035import static org.onosproject.yangutils.utils.YangConstructType.PATTERN_DATA;
36import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053037
38/*
39 * Reference: RFC6020 and YANG ANTLR Grammar
40 *
41 * ABNF grammar as per RFC6020
42 * pattern-stmt = pattern-keyword sep string optsep
43 * (";" /
44 * "{" stmtsep
45 * ;; these stmts can appear in any order
46 * [error-message-stmt stmtsep]
47 * [error-app-tag-stmt stmtsep]
48 * [description-stmt stmtsep]
49 * [reference-stmt stmtsep]
50 * "}")
51 *
52 * ANTLR grammar rule
53 * patternStatement : PATTERN_KEYWORD string (STMTEND | LEFT_CURLY_BRACE commonStatements RIGHT_CURLY_BRACE);
54 */
55
56/**
57 * Represents listener based call back function corresponding to the "pattern"
58 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
59 */
60public final class PatternRestrictionListener {
61
62 private static final String EMPTY_STRING = "";
63
64 /**
65 * Creates a new pattern restriction listener.
66 */
67 private PatternRestrictionListener() {
68 }
69
70 /**
71 * It is called when parser receives an input matching the grammar
72 * rule (pattern), performs validation and updates the data model
73 * tree.
74 *
75 * @param listener listener's object
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053076 * @param ctx context object of the grammar rule
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053077 */
78 public static void processPatternRestrictionEntry(TreeWalkListener listener,
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053079 GeneratedYangParser.PatternStatementContext ctx) {
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053080
81 // Check for stack to be non empty.
82 checkStackIsNotEmpty(listener, MISSING_HOLDER, PATTERN_DATA, ctx.string().getText(), ENTRY);
83
84 Parsable tmpData = listener.getParsedDataStack().peek();
85 if (tmpData.getYangConstructType() == TYPE_DATA) {
86 YangType type = (YangType) tmpData;
87 setPatternRestriction(type, ctx);
88 } else {
89 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, PATTERN_DATA,
90 ctx.string().getText(), ENTRY));
91 }
92 }
93
94 /**
95 * Sets the pattern restriction to type.
96 *
97 * @param type Yang type for which pattern restriction to be set
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053098 * @param ctx context object of the grammar rule
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053099 */
100 private static void setPatternRestriction(YangType type,
101 GeneratedYangParser.PatternStatementContext ctx) {
102
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530103 if (type.getDataType() != YangDataTypes.STRING && type.getDataType() != YangDataTypes.DERIVED) {
104
105 ParserException parserException = new ParserException("YANG file error : " +
106 YangConstructType.getYangConstructType(PATTERN_DATA) + " name " + ctx.string().getText() +
107 " can be used to restrict the built-in type string or types derived from string.");
108 parserException.setLine(ctx.getStart().getLine());
109 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
110 throw parserException;
111 }
112
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530113 String patternArgument = ctx.string().getText().replace("\"", EMPTY_STRING);
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530114
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530115 if (type.getDataType() == YangDataTypes.STRING) {
116 YangStringRestriction stringRestriction = (YangStringRestriction) type.getDataTypeExtendedInfo();
117 if (stringRestriction == null) {
118 stringRestriction = new YangStringRestriction();
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530119 type.setDataTypeExtendedInfo(stringRestriction);
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530120 stringRestriction.addPattern(patternArgument);
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530121 } else {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530122 stringRestriction.addPattern(patternArgument);
123 }
124 } else {
125 YangPatternRestriction patternRestriction = (YangPatternRestriction) ((YangDerivedInfo<?>) type
126 .getDataTypeExtendedInfo()).getPatternRestriction();
127 if (patternRestriction == null) {
128 patternRestriction = new YangPatternRestriction();
129 ((YangDerivedInfo<?>) type.getDataTypeExtendedInfo()).setPatternRestriction(patternRestriction);
130 patternRestriction.addPattern(patternArgument);
131 } else {
132 ((YangDerivedInfo<?>) type.getDataTypeExtendedInfo()).setPatternRestriction(patternRestriction);
133 patternRestriction.addPattern(patternArgument);
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530134 }
135 }
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530136 }
137}