blob: e6628b31b89bdb7451498633af26af021dc276d3 [file] [log] [blame]
Vidyashree Rama07c26bb2016-07-28 17:33:15 +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.YangAppDataStructure;
20import org.onosproject.yangutils.datamodel.YangCompilerAnnotation;
21import org.onosproject.yangutils.datamodel.YangDataStructure;
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.YangDataStructure.getType;
28import static org.onosproject.yangutils.datamodel.utils.YangConstructType.APP_DATA_STRUCTURE;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
35import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidPrefix;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
37
38/*
39 * Reference: RFC6020 and YANG ANTLR Grammar
40 *
41 * ABNF grammar as per RFC6020
42 * app-data-structure-stmt = prefix:app-data-structure-keyword string
43 * (";" /
44 * "{"
45 * [data-structure-key-stmt stmtsep]
46 * "}")
47 *
48 * ANTLR grammar rule
49 * appDataStructureStatement : APP_DATA_STRUCTURE appDataStructure (STMTEND | (LEFT_CURLY_BRACE
50 * dataStructureKeyStatement? RIGHT_CURLY_BRACE));
51 */
52
53/**
54 * Represents listener based call back function corresponding to the "app-data-structure"
55 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
56 */
57public final class AppDataStructureListener {
58
59 /**
60 * Creates a new app-data-structure listener.
61 */
62 private AppDataStructureListener() {
63 }
64
65 /**
66 * Performs validation and updates the data model tree. It is called when parser receives an
67 * input matching the grammar rule(app-data-structure).
68 *
69 * @param listener listener's object
70 * @param ctx context object of the grammar rule
71 */
72 public static void processAppDataStructureEntry(TreeWalkListener listener,
73 GeneratedYangParser.AppDataStructureStatementContext ctx) {
74
75 checkStackIsNotEmpty(listener, MISSING_HOLDER, APP_DATA_STRUCTURE, "", ENTRY);
76
77 String prefix = getValidPrefix(ctx.APP_DATA_STRUCTURE().getText(), APP_DATA_STRUCTURE, ctx);
78 YangDataStructure dataStructure = getType(ctx.appDataStructure().getText());
79
80 YangAppDataStructure appDataStructure = new YangAppDataStructure();
81 appDataStructure.setPrefix(prefix);
82 appDataStructure.setDataStructure(dataStructure);
83
84 Parsable curData = listener.getParsedDataStack().peek();
85 if (curData instanceof YangCompilerAnnotation) {
86 YangCompilerAnnotation compilerAnnotation = ((YangCompilerAnnotation) curData);
87 compilerAnnotation.setYangAppDataStructure(appDataStructure);
88 listener.getParsedDataStack().push(appDataStructure);
89 } else {
90 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, APP_DATA_STRUCTURE,
91 "", ENTRY));
92 }
93 }
94
95 /**
96 * Performs validation and updates the data model tree. It is called when parser
97 * exits from grammar rule (app-data-structure).
98 *
99 * @param listener listener's object
100 * @param ctx context object of the grammar rule
101 */
102 public static void processAppDataStructureExit(TreeWalkListener listener,
103 GeneratedYangParser.AppDataStructureStatementContext ctx) {
104
105 checkStackIsNotEmpty(listener, MISSING_HOLDER, APP_DATA_STRUCTURE, "", EXIT);
106 if (!(listener.getParsedDataStack().peek() instanceof YangAppDataStructure)) {
107 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, APP_DATA_STRUCTURE,
108 "", EXIT));
109 }
110 listener.getParsedDataStack().pop();
111 }
112}