blob: ef302bedcae7874634eea9af17f35980c66b4a3e [file] [log] [blame]
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +05301/*
2 * Copyright 2016 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
Gaurav Agrawala04483c2016-02-13 14:23:40 +053019import org.onosproject.yangutils.datamodel.YangModule;
janani bcc9ac302016-03-24 12:43:48 +053020import org.onosproject.yangutils.datamodel.YangRevision;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053021import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053022import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053023import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama468f8282016-03-04 19:08:35 +053024
Vinod Kumar S38046502016-03-23 15:30:27 +053025import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
26import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangModuleNode;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053027import 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;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053030import 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;
Vinod Kumar S38046502016-03-23 15:30:27 +053033import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
janani bcc9ac302016-03-24 12:43:48 +053034import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.setCurrentDateForRevision;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053035import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053037import static org.onosproject.yangutils.utils.YangConstructType.MODULE_DATA;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053038
39/*
40 * Reference: RFC6020 and YANG ANTLR Grammar
41 *
42 * ABNF grammar as per RFC6020
43 * module-stmt = optsep module-keyword sep identifier-arg-str
44 * optsep
45 * "{" stmtsep
46 * module-header-stmts
47 * linkage-stmts
48 * meta-stmts
49 * revision-stmts
50 * body-stmts
51 * "}" optsep
52 *
53 * ANTLR grammar rule
Vidyashree Rama468f8282016-03-04 19:08:35 +053054 * module_stmt : MODULE_KEYWORD identifier LEFT_CURLY_BRACE module_body* RIGHT_CURLY_BRACE;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053055 */
56
57/**
58 * Implements listener based call back function corresponding to the "module"
59 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
60 */
61public final class ModuleListener {
62
63 /**
64 * Creates a new module listener.
65 */
66 private ModuleListener() {
67 }
68
69 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053070 * It is called when parser receives an input matching the grammar rule
71 * (module), perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053072 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053073 * @param listener Listener's object
74 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053075 */
76 public static void processModuleEntry(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053077
78 // Check if stack is empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +053079 checkStackIsEmpty(listener, INVALID_HOLDER, MODULE_DATA, ctx.identifier().getText(), ENTRY);
80
81 String identifier = getValidIdentifier(ctx.identifier().getText(), MODULE_DATA, ctx);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053082
Vinod Kumar S38046502016-03-23 15:30:27 +053083 YangModule yangModule = getYangModuleNode(JAVA_GENERATION);
Vidyashree Rama468f8282016-03-04 19:08:35 +053084 yangModule.setName(identifier);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053085
Vidyashree Ramabcd7fba2016-03-09 20:41:44 +053086 if (ctx.moduleBody().moduleHeaderStatement().yangVersionStatement() == null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053087 yangModule.setVersion((byte) 1);
88 }
89
janani bcc9ac302016-03-24 12:43:48 +053090 if (ctx.moduleBody().revisionStatements().revisionStatement().isEmpty()) {
91 String currentDate = setCurrentDateForRevision();
92 YangRevision currentRevision = new YangRevision();
93 currentRevision.setRevDate(currentDate);
94 yangModule.setRevision(currentRevision);
95 }
96
Gaurav Agrawala04483c2016-02-13 14:23:40 +053097 listener.getParsedDataStack().push(yangModule);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053098 }
99
100 /**
101 * It is called when parser exits from grammar rule (module), it perform
102 * validations and update the data model tree.
103 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530104 * @param listener Listener's object
105 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530106 */
107 public static void processModuleExit(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530108
109 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530110 checkStackIsNotEmpty(listener, MISSING_HOLDER, MODULE_DATA, ctx.identifier().getText(), EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530111
112 if (!(listener.getParsedDataStack().peek() instanceof YangModule)) {
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530113 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, MODULE_DATA,
Vinod Kumar S38046502016-03-23 15:30:27 +0530114 ctx.identifier().getText(), EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530115 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530116 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530117}