blob: b6cbf2051b2f371832d5ab68e5482943dcc8a9b7 [file] [log] [blame]
Vidyashree Ramadeac28b2016-06-20 15:12:43 +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
19import org.onosproject.yangutils.datamodel.YangMust;
20import org.onosproject.yangutils.datamodel.YangMustHolder;
21import org.onosproject.yangutils.datamodel.utils.Parsable;
22import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
23import org.onosproject.yangutils.parser.exceptions.ParserException;
24import org.onosproject.yangutils.parser.impl.TreeWalkListener;
25
26import static org.onosproject.yangutils.datamodel.utils.YangConstructType.MUST_DATA;
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
35
36/*
37 * Reference: RFC6020 and YANG ANTLR Grammar
38 *
39 * ABNF grammar as per RFC6020
40 *
41 * must-stmt = must-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 * mustStatement : MUST_KEYWORD string (STMTEND | LEFT_CURLY_BRACE commonStatements RIGHT_CURLY_BRACE);
53 */
54
55/**
56 * Represents listener based call back function corresponding to the
57 * "must" rule defined in ANTLR grammar file for corresponding ABNF rule
58 * in RFC 6020.
59 */
60public final class MustListener {
61
62 /**
63 * Creates a new must listener.
64 */
65 private MustListener() {
66 }
67
68 /**
69 * Perform validations and updates the data model tree.It is called when parser
70 * receives an input matching the grammar rule (must).
71 *
72 * @param listener listener's object
73 * @param ctx context object of the grammar rule
74 */
75 public static void processMustEntry(TreeWalkListener listener,
76 GeneratedYangParser.MustStatementContext ctx) {
77
78 // Check for stack to be non empty.
79 checkStackIsNotEmpty(listener, MISSING_HOLDER, MUST_DATA, ctx.string().getText(), ENTRY);
80 String constraint = removeQuotesAndHandleConcat(ctx.string().getText());
81
82 // Obtain the node of the stack.
83 Parsable tmpNode = listener.getParsedDataStack().peek();
84 if (tmpNode instanceof YangMustHolder) {
85
86 YangMust must = new YangMust();
87 must.setConstraint(constraint);
88
89 YangMustHolder mustHolder = (YangMustHolder) tmpNode;
90 mustHolder.addMust(must);
91
92 listener.getParsedDataStack().push(must);
93 } else {
94 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, MUST_DATA,
95 ctx.string().getText(), ENTRY));
96 }
97
98 }
99
100 /**
101 * Performs validation and updates the data model tree.It is called when parser
102 * exits from grammar rule (must).
103 *
104 * @param listener listener's object
105 * @param ctx context object of the grammar rule
106 */
107 public static void processMustExit(TreeWalkListener listener,
108 GeneratedYangParser.MustStatementContext ctx) {
109
110 // Check for stack to be non empty.
111 checkStackIsNotEmpty(listener, MISSING_HOLDER, MUST_DATA, ctx.string().getText(), EXIT);
112
113 if (listener.getParsedDataStack().peek() instanceof YangMust) {
114 listener.getParsedDataStack().pop();
115 } else {
116 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, MUST_DATA,
117 ctx.string().getText(), EXIT));
118 }
119 }
120}