blob: 21696403c67c285e66e51e4137058c75e146d41d [file] [log] [blame]
Vidyashree Rama36f2fab2016-07-15 14:06:56 +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.YangExtension;
20import org.onosproject.yangutils.datamodel.YangModule;
21import org.onosproject.yangutils.datamodel.YangSubModule;
22import org.onosproject.yangutils.datamodel.utils.Parsable;
23import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
24import org.onosproject.yangutils.parser.exceptions.ParserException;
25import org.onosproject.yangutils.parser.impl.TreeWalkListener;
26
27import static org.onosproject.yangutils.datamodel.utils.YangConstructType.EXTENSION_DATA;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
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 * extension-stmt = extension-keyword sep identifier-arg-str optsep
42 * (";" /
43 * "{" stmtsep
44 * ;; these stmts can appear in any order
45 * [argument-stmt stmtsep]
46 * [status-stmt stmtsep]
47 * [description-stmt stmtsep]
48 * [reference-stmt stmtsep]
49 * "}")
50 *
51 * ANTLR grammar rule
52 * extensionStatement : EXTENSION_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE extensionBody RIGHT_CURLY_BRACE);
53 */
54
55/**
56 * Represents listener based call back function corresponding to the "extension"
57 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
58 */
59public final class ExtensionListener {
60
61 /**
62 * Creates a new extension listener.
63 */
64 private ExtensionListener() {
65 }
66
67 /**
68 * Performs validation and updates the data model tree. It is called when parser
69 * receives an input matching the grammar rule (extension).
70 *
71 * @param listener listener's object
72 * @param ctx context object of the grammar rule
73 */
74 public static void processExtensionEntry(TreeWalkListener listener,
75 GeneratedYangParser.ExtensionStatementContext ctx) {
76
77 // Check for stack to be non empty.
78 checkStackIsNotEmpty(listener, MISSING_HOLDER, EXTENSION_DATA, ctx.identifier().getText(), ENTRY);
79
80 String identifier = getValidIdentifier(ctx.identifier().getText(), EXTENSION_DATA, ctx);
81
82 YangExtension extension = new YangExtension();
83 extension.setName(identifier);
84
85 Parsable curData = listener.getParsedDataStack().peek();
86 switch (curData.getYangConstructType()) {
87 case MODULE_DATA:
88 YangModule module = ((YangModule) curData);
89 module.addExtension(extension);
90 break;
91 case SUB_MODULE_DATA:
92 YangSubModule subModule = ((YangSubModule) curData);
93 subModule.addExtension(extension);
94 break;
95 default:
96 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, EXTENSION_DATA,
97 ctx.identifier().getText(), ENTRY));
98 }
99 listener.getParsedDataStack().push(extension);
100 }
101
102 /**
103 * Performs validation and updates the data model tree. It is called when parser exits
104 * from grammar rule(extension).
105 *
106 * @param listener listener's object
107 * @param ctx context object of the grammar rule
108 */
109 public static void processExtensionExit(TreeWalkListener listener,
110 GeneratedYangParser.ExtensionStatementContext ctx) {
111
112 // Check for stack to be non empty.
113 checkStackIsNotEmpty(listener, MISSING_HOLDER, EXTENSION_DATA, ctx.identifier().getText(), EXIT);
114
115 if (!(listener.getParsedDataStack().peek() instanceof YangExtension)) {
116 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, EXTENSION_DATA,
117 ctx.identifier().getText(), EXIT));
118 }
119 listener.getParsedDataStack().pop();
120 }
121}