blob: b06ecb56678d4f2fcc8637c0c62ccb16e9ccee87 [file] [log] [blame]
Vidyashree Ramaa2f73982016-04-12 23:33:33 +05301/*
2 * Copyright 2016 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
19import org.onosproject.yangutils.datamodel.YangDerivedInfo;
20import org.onosproject.yangutils.datamodel.YangType;
21import org.onosproject.yangutils.datamodel.YangStringRestriction;
22import org.onosproject.yangutils.datamodel.YangDataTypes;
23import org.onosproject.yangutils.parser.Parsable;
24import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
25import org.onosproject.yangutils.parser.exceptions.ParserException;
26import org.onosproject.yangutils.parser.impl.TreeWalkListener;
27import org.onosproject.yangutils.utils.YangConstructType;
28
29import static org.onosproject.yangutils.utils.YangConstructType.PATTERN_DATA;
30import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
32import 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;
35import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
36
37/*
38 * Reference: RFC6020 and YANG ANTLR Grammar
39 *
40 * ABNF grammar as per RFC6020
41 * pattern-stmt = pattern-keyword sep string optsep
42 * (";" /
43 * "{" stmtsep
44 * ;; these stmts can appear in any order
45 * [error-message-stmt stmtsep]
46 * [error-app-tag-stmt stmtsep]
47 * [description-stmt stmtsep]
48 * [reference-stmt stmtsep]
49 * "}")
50 *
51 * ANTLR grammar rule
52 * patternStatement : PATTERN_KEYWORD string (STMTEND | LEFT_CURLY_BRACE commonStatements RIGHT_CURLY_BRACE);
53 */
54
55/**
56 * Represents listener based call back function corresponding to the "pattern"
57 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
58 */
59public final class PatternRestrictionListener {
60
61 private static final String EMPTY_STRING = "";
62
63 /**
64 * Creates a new pattern restriction listener.
65 */
66 private PatternRestrictionListener() {
67 }
68
69 /**
70 * It is called when parser receives an input matching the grammar
71 * rule (pattern), performs validation and updates the data model
72 * tree.
73 *
74 * @param listener listener's object
75 * @param ctx context object of the grammar rule
76 */
77 public static void processPatternRestrictionEntry(TreeWalkListener listener,
78 GeneratedYangParser.PatternStatementContext ctx) {
79
80 // Check for stack to be non empty.
81 checkStackIsNotEmpty(listener, MISSING_HOLDER, PATTERN_DATA, ctx.string().getText(), ENTRY);
82
83 Parsable tmpData = listener.getParsedDataStack().peek();
84 if (tmpData.getYangConstructType() == TYPE_DATA) {
85 YangType type = (YangType) tmpData;
86 setPatternRestriction(type, ctx);
87 } else {
88 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, PATTERN_DATA,
89 ctx.string().getText(), ENTRY));
90 }
91 }
92
93 /**
94 * Sets the pattern restriction to type.
95 *
96 * @param type Yang type for which pattern restriction to be set
97 * @param ctx context object of the grammar rule
98 */
99 private static void setPatternRestriction(YangType type,
100 GeneratedYangParser.PatternStatementContext ctx) {
101
102 YangStringRestriction stringRestriction;
103
104 if (type.getDataType() != YangDataTypes.STRING && type.getDataType() != YangDataTypes.DERIVED) {
105
106 ParserException parserException = new ParserException("YANG file error : " +
107 YangConstructType.getYangConstructType(PATTERN_DATA) + " name " + ctx.string().getText() +
108 " can be used to restrict the built-in type string or types derived from string.");
109 parserException.setLine(ctx.getStart().getLine());
110 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
111 throw parserException;
112 }
113
114 if (type.getDataType() == YangDataTypes.STRING) {
115 stringRestriction = (YangStringRestriction) type.getDataTypeExtendedInfo();
116 } else {
117 stringRestriction = (YangStringRestriction) ((YangDerivedInfo<?>) type
118 .getDataTypeExtendedInfo()).getExtendedInfo();
119 }
120
121 if (stringRestriction == null) {
122 stringRestriction = new YangStringRestriction();
123 if (type.getDataType() == YangDataTypes.STRING) {
124 type.setDataTypeExtendedInfo(stringRestriction);
125 } else {
126 ((YangDerivedInfo<YangStringRestriction>) type.getDataTypeExtendedInfo())
127 .setExtendedInfo(stringRestriction);
128 }
129 }
130
131 String patternArgument = ctx.string().getText().replace("\"", EMPTY_STRING);
132 stringRestriction.addPattern(patternArgument);
133 }
134}