blob: afa9acd0171ce6e3c63bef9f992ab9d45a5d445e [file] [log] [blame]
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +05303 *
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 Agrawald9d6cc82016-03-29 02:17:23 +053021import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053022import org.onosproject.yangutils.linker.ResolvableType;
23import org.onosproject.yangutils.linker.YangReferenceResolver;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053024import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053025import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053026import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama468f8282016-03-04 19:08:35 +053027
Vinod Kumar S38046502016-03-23 15:30:27 +053028import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
29import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangModuleNode;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053030import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053032import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction
33 .constructListenerErrorMessage;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053034import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
35import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Vinod Kumar S38046502016-03-23 15:30:27 +053037import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
janani bcc9ac302016-03-24 12:43:48 +053038import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.setCurrentDateForRevision;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053039import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
40import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053041import static org.onosproject.yangutils.utils.YangConstructType.MODULE_DATA;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053042
43/*
44 * Reference: RFC6020 and YANG ANTLR Grammar
45 *
46 * ABNF grammar as per RFC6020
47 * module-stmt = optsep module-keyword sep identifier-arg-str
48 * optsep
49 * "{" stmtsep
50 * module-header-stmts
51 * linkage-stmts
52 * meta-stmts
53 * revision-stmts
54 * body-stmts
55 * "}" optsep
56 *
57 * ANTLR grammar rule
Vidyashree Rama468f8282016-03-04 19:08:35 +053058 * module_stmt : MODULE_KEYWORD identifier LEFT_CURLY_BRACE module_body* RIGHT_CURLY_BRACE;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053059 */
60
61/**
Bharat saraswald9822e92016-04-05 15:13:44 +053062 * Represents listener based call back function corresponding to the "module"
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053063 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
64 */
65public final class ModuleListener {
66
67 /**
68 * Creates a new module listener.
69 */
70 private ModuleListener() {
71 }
72
73 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053074 * It is called when parser receives an input matching the grammar rule
75 * (module), perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053076 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053077 * @param listener Listener's object
78 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053079 */
80 public static void processModuleEntry(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053081
82 // Check if stack is empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +053083 checkStackIsEmpty(listener, INVALID_HOLDER, MODULE_DATA, ctx.identifier().getText(), ENTRY);
84
85 String identifier = getValidIdentifier(ctx.identifier().getText(), MODULE_DATA, ctx);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053086
Vinod Kumar S38046502016-03-23 15:30:27 +053087 YangModule yangModule = getYangModuleNode(JAVA_GENERATION);
Vidyashree Rama468f8282016-03-04 19:08:35 +053088 yangModule.setName(identifier);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053089
Vidyashree Ramabcd7fba2016-03-09 20:41:44 +053090 if (ctx.moduleBody().moduleHeaderStatement().yangVersionStatement() == null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053091 yangModule.setVersion((byte) 1);
92 }
93
janani bcc9ac302016-03-24 12:43:48 +053094 if (ctx.moduleBody().revisionStatements().revisionStatement().isEmpty()) {
95 String currentDate = setCurrentDateForRevision();
96 YangRevision currentRevision = new YangRevision();
97 currentRevision.setRevDate(currentDate);
98 yangModule.setRevision(currentRevision);
99 }
100
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530101 listener.getParsedDataStack().push(yangModule);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530102 }
103
104 /**
105 * It is called when parser exits from grammar rule (module), it perform
106 * validations and update the data model tree.
107 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530108 * @param listener Listener's object
109 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530110 */
111 public static void processModuleExit(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530112
113 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530114 checkStackIsNotEmpty(listener, MISSING_HOLDER, MODULE_DATA, ctx.identifier().getText(), EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530115
116 if (!(listener.getParsedDataStack().peek() instanceof YangModule)) {
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530117 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, MODULE_DATA,
Vinod Kumar S38046502016-03-23 15:30:27 +0530118 ctx.identifier().getText(), EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530119 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530120 try {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530121 ((YangReferenceResolver) listener.getParsedDataStack()
122 .peek()).resolveSelfFileLinking(ResolvableType.YANG_USES);
123 ((YangReferenceResolver) listener.getParsedDataStack()
124 .peek()).resolveSelfFileLinking(ResolvableType.YANG_DERIVED_DATA_TYPE);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530125 } catch (DataModelException e) {
126 ParserException parserException = new ParserException(e.getMessage());
127 parserException.setLine(e.getLineNumber());
128 parserException.setCharPosition(e.getCharPositionInLine());
129 throw parserException;
130 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530131 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530132}