blob: 6a70f31ad8555d94b4d212eb9fddce772bebe3c9 [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
19/*
20 * Reference: RFC6020 and YANG ANTLR Grammar
21 *
22 * ABNF grammar as per RFC6020
23 * feature-stmt = feature-keyword sep identifier-arg-str optsep
24 * (";" /
25 * "{" stmtsep
26 * ;; these stmts can appear in any order
27 * *(if-feature-stmt stmtsep)
28 * [status-stmt stmtsep]
29 * [description-stmt stmtsep]
30 * [reference-stmt stmtsep]
31 * "}")
32 *
33 *
34 *
35 * ANTLR grammar rule
36 * featureStatement : FEATURE_KEYWORD string (STMTEND | LEFT_CURLY_BRACE featureBody RIGHT_CURLY_BRACE);
37 */
38
39import org.onosproject.yangutils.datamodel.YangFeature;
40import org.onosproject.yangutils.datamodel.YangFeatureHolder;
41import org.onosproject.yangutils.datamodel.utils.Parsable;
42import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
43import org.onosproject.yangutils.parser.exceptions.ParserException;
44import org.onosproject.yangutils.parser.impl.TreeWalkListener;
45
46import static org.onosproject.yangutils.datamodel.utils.YangConstructType.FEATURE_DATA;
47import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
48import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
49import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
50import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
51import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
52import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
53import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
54import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
55
56/**
57 * Represents listener based call back function corresponding to the "feature"
58 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
59 */
60public final class FeatureListener {
61
62 /**
63 * Creates a new feature listener.
64 */
65 private FeatureListener() {
66 }
67
68 /**
69 * Performs validation and updates the data model tree.It is called when parser receives
70 * an input matching the grammar rule (feature).
71 *
72 * @param listener listener's object
73 * @param ctx context object of the grammar rule
74 */
75 public static void processFeatureEntry(TreeWalkListener listener,
76 GeneratedYangParser.FeatureStatementContext ctx) {
77
78 // Check for stack to be non empty.
79 checkStackIsNotEmpty(listener, MISSING_HOLDER, FEATURE_DATA, ctx.string().getText(), ENTRY);
80
81 String identifier = getValidIdentifier(ctx.string().getText(), FEATURE_DATA, ctx);
82
83 // Obtain the node of the stack.
84 Parsable tmpNode = listener.getParsedDataStack().peek();
85 if (tmpNode instanceof YangFeatureHolder) {
86 YangFeatureHolder featureHolder = (YangFeatureHolder) tmpNode;
87
88 YangFeature feature = new YangFeature();
89 feature.setName(identifier);
90
91 featureHolder.addFeatureList(feature);
92 listener.getParsedDataStack().push(feature);
93 } else {
94 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, FEATURE_DATA,
95 ctx.string().getText(), ENTRY));
96 }
97 }
98
99 /**
100 * Perform validations and updates the data model tree.It is called when parser exits from
101 * grammar rule(feature).
102 *
103 * @param listener listener's object
104 * @param ctx context object of the grammar rule
105 */
106 public static void processFeatureExit(TreeWalkListener listener,
107 GeneratedYangParser.FeatureStatementContext ctx) {
108
109 // Check for stack to be non empty.
110 checkStackIsNotEmpty(listener, MISSING_HOLDER, FEATURE_DATA, ctx.string().getText(), EXIT);
111
112 if (listener.getParsedDataStack().peek() instanceof YangFeature) {
113 listener.getParsedDataStack().pop();
114 } else {
115 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, FEATURE_DATA,
116 ctx.string().getText(), EXIT));
117 }
118 }
119}