blob: c77ffea80eeff483c29f72f718e610db912ad9cd [file] [log] [blame]
Gaurav Agrawal22db16d2016-02-12 16:50:55 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal22db16d2016-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
Vidyashree Rama13b4c552016-06-20 15:12:43 +053019import java.util.Date;
VinodKumarS-Huawei423dc9a2016-08-17 22:08:42 +053020
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053021import org.onosproject.yangutils.datamodel.ResolvableType;
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053022import org.onosproject.yangutils.datamodel.YangModule;
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053023import org.onosproject.yangutils.datamodel.YangReferenceResolver;
janani ba3027492016-03-24 12:43:48 +053024import org.onosproject.yangutils.datamodel.YangRevision;
Gaurav Agrawal1fbfae12016-03-29 02:17:23 +053025import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Vidyashree Rama13b4c552016-06-20 15:12:43 +053026import org.onosproject.yangutils.datamodel.utils.Parsable;
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053027import org.onosproject.yangutils.linker.exceptions.LinkerException;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053028import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053029import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053030import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Ramad3221322016-03-04 19:08:35 +053031
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053032import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053033import static org.onosproject.yangutils.datamodel.utils.YangConstructType.MODULE_DATA;
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053034import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
35import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Vidyashree Ramab3670472016-08-06 15:49:56 +053036import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction
37 .constructListenerErrorMessage;
38import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CHILD;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053039import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
40import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
41import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Vidyashree Rama13b4c552016-06-20 15:12:43 +053042import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getCurrentDateForRevision;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053043import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053044import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
45import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053046import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangModuleNode;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053047
48/*
49 * Reference: RFC6020 and YANG ANTLR Grammar
50 *
51 * ABNF grammar as per RFC6020
52 * module-stmt = optsep module-keyword sep identifier-arg-str
53 * optsep
54 * "{" stmtsep
55 * module-header-stmts
56 * linkage-stmts
57 * meta-stmts
58 * revision-stmts
59 * body-stmts
60 * "}" optsep
61 *
62 * ANTLR grammar rule
Vidyashree Ramad3221322016-03-04 19:08:35 +053063 * module_stmt : MODULE_KEYWORD identifier LEFT_CURLY_BRACE module_body* RIGHT_CURLY_BRACE;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053064 */
65
66/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053067 * Represents listener based call back function corresponding to the "module"
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053068 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
69 */
70public final class ModuleListener {
71
72 /**
73 * Creates a new module listener.
74 */
75 private ModuleListener() {
76 }
77
78 /**
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053079 * It is called when parser receives an input matching the grammar rule
80 * (module), perform validations and update the data model tree.
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053081 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053082 * @param listener Listener's object
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053083 * @param ctx context object of the grammar rule
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053084 */
85 public static void processModuleEntry(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) {
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053086
87 // Check if stack is empty.
Vidyashree Ramad3221322016-03-04 19:08:35 +053088 checkStackIsEmpty(listener, INVALID_HOLDER, MODULE_DATA, ctx.identifier().getText(), ENTRY);
89
90 String identifier = getValidIdentifier(ctx.identifier().getText(), MODULE_DATA, ctx);
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053091
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053092 YangModule yangModule = getYangModuleNode(JAVA_GENERATION);
Vidyashree Ramad3221322016-03-04 19:08:35 +053093 yangModule.setName(identifier);
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053094
Vidyashree Rama07021a22016-03-09 20:41:44 +053095 if (ctx.moduleBody().moduleHeaderStatement().yangVersionStatement() == null) {
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053096 yangModule.setVersion((byte) 1);
97 }
98
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053099 listener.getParsedDataStack().push(yangModule);
Gaurav Agrawal22db16d2016-02-12 16:50:55 +0530100 }
101
102 /**
103 * It is called when parser exits from grammar rule (module), it perform
104 * validations and update the data model tree.
105 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530106 * @param listener Listener's object
Gaurav Agrawal58b348e2016-06-07 14:00:26 +0530107 * @param ctx context object of the grammar rule
Gaurav Agrawal22db16d2016-02-12 16:50:55 +0530108 */
109 public static void processModuleExit(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) {
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530110
111 // Check for stack to be non empty.
Vidyashree Ramad3221322016-03-04 19:08:35 +0530112 checkStackIsNotEmpty(listener, MISSING_HOLDER, MODULE_DATA, ctx.identifier().getText(), EXIT);
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530113
Vidyashree Rama13b4c552016-06-20 15:12:43 +0530114 Parsable tmpNode = listener.getParsedDataStack().peek();
115 if (!(tmpNode instanceof YangModule)) {
Gaurav Agrawal800a8e72016-02-19 12:57:13 +0530116 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, MODULE_DATA,
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530117 ctx.identifier().getText(), EXIT));
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530118 }
Vidyashree Rama13b4c552016-06-20 15:12:43 +0530119
120 if (((YangModule) tmpNode).getRevision() == null) {
121 Date currentDate = getCurrentDateForRevision();
122 YangRevision currentRevision = new YangRevision();
123 currentRevision.setRevDate(currentDate);
124 ((YangModule) tmpNode).setRevision(currentRevision);
125 }
126
Vidyashree Ramab3670472016-08-06 15:49:56 +0530127 YangModule module = (YangModule) tmpNode;
128 if (module.getUnresolvedResolutionList(ResolvableType.YANG_COMPILER_ANNOTATION) != null
129 && module.getUnresolvedResolutionList(ResolvableType.YANG_COMPILER_ANNOTATION).size() != 0
130 && module.getChild() != null) {
131 throw new ParserException(constructListenerErrorMessage(INVALID_CHILD, MODULE_DATA,
132 ctx.identifier().getText(), EXIT));
133 }
134
Gaurav Agrawal1fbfae12016-03-29 02:17:23 +0530135 try {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530136 ((YangReferenceResolver) listener.getParsedDataStack()
Vidyashree Rama13b4c552016-06-20 15:12:43 +0530137 .peek()).resolveSelfFileLinking(ResolvableType.YANG_IF_FEATURE);
138 ((YangReferenceResolver) listener.getParsedDataStack()
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530139 .peek()).resolveSelfFileLinking(ResolvableType.YANG_USES);
140 ((YangReferenceResolver) listener.getParsedDataStack()
141 .peek()).resolveSelfFileLinking(ResolvableType.YANG_DERIVED_DATA_TYPE);
janani b0e4e8ae2016-07-13 21:06:41 +0530142 ((YangReferenceResolver) listener.getParsedDataStack()
143 .peek()).resolveSelfFileLinking(ResolvableType.YANG_LEAFREF);
Shankara-Huawei234cd092016-07-14 11:35:34 +0530144 ((YangReferenceResolver) listener.getParsedDataStack()
145 .peek()).resolveSelfFileLinking(ResolvableType.YANG_BASE);
146 ((YangReferenceResolver) listener.getParsedDataStack()
147 .peek()).resolveSelfFileLinking(ResolvableType.YANG_IDENTITYREF);
Gaurav Agrawal1fbfae12016-03-29 02:17:23 +0530148 } catch (DataModelException e) {
Gaurav Agrawal58b348e2016-06-07 14:00:26 +0530149 LinkerException linkerException = new LinkerException(e.getMessage());
150 linkerException.setLine(e.getLineNumber());
151 linkerException.setCharPosition(e.getCharPositionInLine());
152 throw linkerException;
Gaurav Agrawal1fbfae12016-03-29 02:17:23 +0530153 }
Gaurav Agrawal22db16d2016-02-12 16:50:55 +0530154 }
Gaurav Agrawal800a8e72016-02-19 12:57:13 +0530155}