blob: 15092a11aa2fa2b2772f3885c45b3e983debce94 [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.YangWhen;
20import org.onosproject.yangutils.datamodel.YangWhenHolder;
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.WHEN_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 * when-stmt = when-keyword sep string optsep
42 * (";" /
43 * "{" stmtsep
44 * ;; these stmts can appear in any order
45 * [description-stmt stmtsep]
46 * [reference-stmt stmtsep]
47 * "}")
48 *
49 * ANTLR grammar rule
50 * whenStatement : WHEN_KEYWORD string (STMTEND | LEFT_CURLY_BRACE ((descriptionStatement? referenceStatement?)
51 * | (referenceStatement? descriptionStatement?)) RIGHT_CURLY_BRACE);
52 */
53
54/**
55 * Represents listener based call back function corresponding to the
56 * "when" rule defined in ANTLR grammar file for corresponding ABNF rule
57 * in RFC 6020.
58 */
59public final class WhenListener {
60
61 /**
62 * Creates a new when listener.
63 */
64 private WhenListener() {
65 }
66
67 /**
68 * Perform validations and updates the data model tree.It is called when parser
69 * receives an input matching the grammar rule (when).
70 *
71 * @param listener listener's object
72 * @param ctx context object of the grammar rule
73 */
74 public static void processWhenEntry(TreeWalkListener listener,
75 GeneratedYangParser.WhenStatementContext ctx) {
76
77 // Check for stack to be non empty.
78 checkStackIsNotEmpty(listener, MISSING_HOLDER, WHEN_DATA, ctx.string().getText(), ENTRY);
79 String condition = removeQuotesAndHandleConcat(ctx.string().getText());
80
81 YangWhenHolder whenHolder;
82
83 // Obtain the node of the stack.
84 Parsable tmpNode = listener.getParsedDataStack().peek();
85 if (tmpNode instanceof YangWhenHolder) {
86 whenHolder = (YangWhenHolder) tmpNode;
87
88 YangWhen when = new YangWhen();
89 when.setCondition(condition);
90
91 whenHolder.setWhen(when);
92 listener.getParsedDataStack().push(when);
93 } else {
94 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER,
95 WHEN_DATA, ctx.string().getText(), ENTRY));
96 }
97 }
98
99 /**
100 * Performs validation and updates the data model tree.It is called when parser
101 * exits from grammar rule (when).
102 *
103 * @param listener listener's object
104 * @param ctx context object of the grammar rule
105 */
106 public static void processWhenExit(TreeWalkListener listener,
107 GeneratedYangParser.WhenStatementContext ctx) {
108
109 // Check for stack to be non empty.
110 checkStackIsNotEmpty(listener, MISSING_HOLDER, WHEN_DATA, ctx.string().getText(), EXIT);
111
112 if (listener.getParsedDataStack().peek() instanceof YangWhen) {
113 listener.getParsedDataStack().pop();
114 } else {
115 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, WHEN_DATA,
116 ctx.string().getText(), EXIT));
117 }
118 }
119}