blob: b429e6df9cd287081b18e8fb6e93617037658c48 [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-Huawei8f164222016-08-31 15:47:30 +053020
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053021import org.onosproject.yangutils.datamodel.ResolvableType;
22import org.onosproject.yangutils.datamodel.YangReferenceResolver;
janani ba3027492016-03-24 12:43:48 +053023import org.onosproject.yangutils.datamodel.YangRevision;
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053024import org.onosproject.yangutils.datamodel.YangSubModule;
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;
Vidyashree Ramab3670472016-08-06 15:49:56 +053033import static org.onosproject.yangutils.datamodel.utils.YangConstructType.MODULE_DATA;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053034import static org.onosproject.yangutils.datamodel.utils.YangConstructType.SUB_MODULE_DATA;
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053035import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Vidyashree Ramab3670472016-08-06 15:49:56 +053037import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction
38 .constructListenerErrorMessage;
39import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CHILD;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053040import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
41import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
42import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Vidyashree Rama13b4c552016-06-20 15:12:43 +053043import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getCurrentDateForRevision;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053044import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053045import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
46import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053047import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangSubModuleNode;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053048
49/*
50 * Reference: RFC6020 and YANG ANTLR Grammar
51 *
52 * ABNF grammar as per RFC6020
53 * submodule-stmt = optsep submodule-keyword sep identifier-arg-str
54 * optsep
55 * "{" stmtsep
56 * submodule-header-stmts
57 * linkage-stmts
58 * meta-stmts
59 * revision-stmts
60 * body-stmts
61 * "}" optsep
62 *
63 * ANTLR grammar rule
Vidyashree Ramad3221322016-03-04 19:08:35 +053064 * submodule_stmt : SUBMODULE_KEYWORD identifier LEFT_CURLY_BRACE submodule_body* RIGHT_CURLY_BRACE;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053065 * submodule_body : submodule_header_statement linkage_stmts meta_stmts revision_stmts body_stmts;
66 */
67
68/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053069 * Represents listener based call back function corresponding to the "submodule"
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053070 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
71 */
72public final class SubModuleListener {
73
74 /**
75 * Creates a new sub module listener.
76 */
77 private SubModuleListener() {
78 }
79
80 /**
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053081 * It is called when parser receives an input matching the grammar rule (sub
82 * module), perform validations and update the data model tree.
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053083 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053084 * @param listener Listener's object
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053085 * @param ctx context object of the grammar rule
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053086 */
87 public static void processSubModuleEntry(TreeWalkListener listener,
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053088 GeneratedYangParser.SubModuleStatementContext ctx) {
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053089
90 // Check if stack is empty.
Vidyashree Ramad3221322016-03-04 19:08:35 +053091 checkStackIsEmpty(listener, INVALID_HOLDER, SUB_MODULE_DATA, ctx.identifier().getText(),
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053092 ENTRY);
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053093
Vidyashree Ramad3221322016-03-04 19:08:35 +053094 String identifier = getValidIdentifier(ctx.identifier().getText(), SUB_MODULE_DATA, ctx);
95
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053096 YangSubModule yangSubModule = getYangSubModuleNode(JAVA_GENERATION);
Vidyashree Ramad3221322016-03-04 19:08:35 +053097 yangSubModule.setName(identifier);
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053098
Bharat saraswale3175d32016-08-31 17:50:11 +053099 yangSubModule.setLineNumber(ctx.getStart().getLine());
100 yangSubModule.setCharPosition(ctx.getStart().getCharPositionInLine());
101 yangSubModule.setFileName(listener.getFileName());
Vidyashree Rama07021a22016-03-09 20:41:44 +0530102 if (ctx.submoduleBody().submoduleHeaderStatement().yangVersionStatement() == null) {
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530103 yangSubModule.setVersion((byte) 1);
104 }
105
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530106 listener.getParsedDataStack().push(yangSubModule);
Gaurav Agrawal22db16d2016-02-12 16:50:55 +0530107 }
108
109 /**
110 * It is called when parser exits from grammar rule (submodule), it perform
111 * validations and update the data model tree.
112 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530113 * @param listener Listener's object
Gaurav Agrawal58b348e2016-06-07 14:00:26 +0530114 * @param ctx context object of the grammar rule
Gaurav Agrawal22db16d2016-02-12 16:50:55 +0530115 */
116 public static void processSubModuleExit(TreeWalkListener listener,
Gaurav Agrawal58b348e2016-06-07 14:00:26 +0530117 GeneratedYangParser.SubModuleStatementContext ctx) {
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530118
119 // Check for stack to be non empty.
Vidyashree Ramad3221322016-03-04 19:08:35 +0530120 checkStackIsNotEmpty(listener, MISSING_HOLDER, SUB_MODULE_DATA, ctx.identifier().getText(),
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530121 EXIT);
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530122
Vidyashree Rama13b4c552016-06-20 15:12:43 +0530123 Parsable tmpNode = listener.getParsedDataStack().peek();
124 if (!(tmpNode instanceof YangSubModule)) {
Gaurav Agrawal800a8e72016-02-19 12:57:13 +0530125 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, SUB_MODULE_DATA,
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530126 ctx.identifier().getText(), EXIT));
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530127 }
Vidyashree Rama13b4c552016-06-20 15:12:43 +0530128
129 if (((YangSubModule) tmpNode).getRevision() == null) {
130 Date currentDate = getCurrentDateForRevision();
131 YangRevision currentRevision = new YangRevision();
132 currentRevision.setRevDate(currentDate);
133 ((YangSubModule) tmpNode).setRevision(currentRevision);
134 }
135
Vidyashree Ramab3670472016-08-06 15:49:56 +0530136 YangSubModule subModule = (YangSubModule) tmpNode;
137 if (subModule.getUnresolvedResolutionList(ResolvableType.YANG_COMPILER_ANNOTATION) != null
138 && subModule.getUnresolvedResolutionList(ResolvableType.YANG_COMPILER_ANNOTATION).size() != 0
139 && subModule.getChild() != null) {
140 throw new ParserException(constructListenerErrorMessage(INVALID_CHILD, MODULE_DATA,
141 ctx.identifier().getText(), EXIT));
142 }
143
Gaurav Agrawal1fbfae12016-03-29 02:17:23 +0530144 try {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530145 ((YangReferenceResolver) listener.getParsedDataStack().peek())
Vidyashree Rama13b4c552016-06-20 15:12:43 +0530146 .resolveSelfFileLinking(ResolvableType.YANG_IF_FEATURE);
147 ((YangReferenceResolver) listener.getParsedDataStack().peek())
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530148 .resolveSelfFileLinking(ResolvableType.YANG_USES);
149 ((YangReferenceResolver) listener.getParsedDataStack().peek())
150 .resolveSelfFileLinking(ResolvableType.YANG_DERIVED_DATA_TYPE);
janani b0e4e8ae2016-07-13 21:06:41 +0530151 ((YangReferenceResolver) listener.getParsedDataStack().peek())
152 .resolveSelfFileLinking(ResolvableType.YANG_LEAFREF);
Shankara-Huawei234cd092016-07-14 11:35:34 +0530153 ((YangReferenceResolver) listener.getParsedDataStack().peek())
154 .resolveSelfFileLinking(ResolvableType.YANG_BASE);
155 ((YangReferenceResolver) listener.getParsedDataStack().peek())
156 .resolveSelfFileLinking(ResolvableType.YANG_IDENTITYREF);
Gaurav Agrawal1fbfae12016-03-29 02:17:23 +0530157 } catch (DataModelException e) {
Gaurav Agrawal58b348e2016-06-07 14:00:26 +0530158 LinkerException linkerException = new LinkerException(e.getMessage());
159 linkerException.setLine(e.getLineNumber());
160 linkerException.setCharPosition(e.getCharPositionInLine());
Bharat saraswale3175d32016-08-31 17:50:11 +0530161 linkerException.setFileName(listener.getFileName());
Gaurav Agrawal58b348e2016-06-07 14:00:26 +0530162 throw linkerException;
Gaurav Agrawal1fbfae12016-03-29 02:17:23 +0530163 }
Gaurav Agrawal22db16d2016-02-12 16:50:55 +0530164 }
Gaurav Agrawal800a8e72016-02-19 12:57:13 +0530165}