blob: b2223b8e818275d3e3c0b099d3a5cc72b65f7c4c [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
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053019import java.util.Date;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053020import org.onosproject.yangutils.datamodel.ResolvableType;
21import org.onosproject.yangutils.datamodel.YangReferenceResolver;
janani bcc9ac302016-03-24 12:43:48 +053022import org.onosproject.yangutils.datamodel.YangRevision;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053023import org.onosproject.yangutils.datamodel.YangSubModule;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053024import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053025import org.onosproject.yangutils.datamodel.utils.Parsable;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053026import org.onosproject.yangutils.linker.exceptions.LinkerException;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053027import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053028import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053029import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama468f8282016-03-04 19:08:35 +053030
Vinod Kumar S38046502016-03-23 15:30:27 +053031import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
Bharat saraswal96dfef02016-06-16 00:29:12 +053032import static org.onosproject.yangutils.datamodel.utils.YangConstructType.SUB_MODULE_DATA;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053033import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053035import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053036import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
37import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
38import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053039import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getCurrentDateForRevision;
Vinod Kumar S38046502016-03-23 15:30:27 +053040import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053041import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
42import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Bharat saraswal96dfef02016-06-16 00:29:12 +053043import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangSubModuleNode;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053044
45/*
46 * Reference: RFC6020 and YANG ANTLR Grammar
47 *
48 * ABNF grammar as per RFC6020
49 * submodule-stmt = optsep submodule-keyword sep identifier-arg-str
50 * optsep
51 * "{" stmtsep
52 * submodule-header-stmts
53 * linkage-stmts
54 * meta-stmts
55 * revision-stmts
56 * body-stmts
57 * "}" optsep
58 *
59 * ANTLR grammar rule
Vidyashree Rama468f8282016-03-04 19:08:35 +053060 * submodule_stmt : SUBMODULE_KEYWORD identifier LEFT_CURLY_BRACE submodule_body* RIGHT_CURLY_BRACE;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053061 * submodule_body : submodule_header_statement linkage_stmts meta_stmts revision_stmts body_stmts;
62 */
63
64/**
Bharat saraswald9822e92016-04-05 15:13:44 +053065 * Represents listener based call back function corresponding to the "submodule"
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053066 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
67 */
68public final class SubModuleListener {
69
70 /**
71 * Creates a new sub module listener.
72 */
73 private SubModuleListener() {
74 }
75
76 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053077 * It is called when parser receives an input matching the grammar rule (sub
78 * module), perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053079 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053080 * @param listener Listener's object
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053081 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053082 */
83 public static void processSubModuleEntry(TreeWalkListener listener,
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053084 GeneratedYangParser.SubModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053085
86 // Check if stack is empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +053087 checkStackIsEmpty(listener, INVALID_HOLDER, SUB_MODULE_DATA, ctx.identifier().getText(),
Vinod Kumar S38046502016-03-23 15:30:27 +053088 ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053089
Vidyashree Rama468f8282016-03-04 19:08:35 +053090 String identifier = getValidIdentifier(ctx.identifier().getText(), SUB_MODULE_DATA, ctx);
91
Vinod Kumar S38046502016-03-23 15:30:27 +053092 YangSubModule yangSubModule = getYangSubModuleNode(JAVA_GENERATION);
Vidyashree Rama468f8282016-03-04 19:08:35 +053093 yangSubModule.setName(identifier);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053094
Vidyashree Ramabcd7fba2016-03-09 20:41:44 +053095 if (ctx.submoduleBody().submoduleHeaderStatement().yangVersionStatement() == null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053096 yangSubModule.setVersion((byte) 1);
97 }
98
Gaurav Agrawala04483c2016-02-13 14:23:40 +053099 listener.getParsedDataStack().push(yangSubModule);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530100 }
101
102 /**
103 * It is called when parser exits from grammar rule (submodule), it perform
104 * validations and update the data model tree.
105 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530106 * @param listener Listener's object
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530107 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530108 */
109 public static void processSubModuleExit(TreeWalkListener listener,
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530110 GeneratedYangParser.SubModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530111
112 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530113 checkStackIsNotEmpty(listener, MISSING_HOLDER, SUB_MODULE_DATA, ctx.identifier().getText(),
Vinod Kumar S38046502016-03-23 15:30:27 +0530114 EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530115
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530116 Parsable tmpNode = listener.getParsedDataStack().peek();
117 if (!(tmpNode instanceof YangSubModule)) {
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530118 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, SUB_MODULE_DATA,
Vinod Kumar S38046502016-03-23 15:30:27 +0530119 ctx.identifier().getText(), EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530120 }
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530121
122 if (((YangSubModule) tmpNode).getRevision() == null) {
123 Date currentDate = getCurrentDateForRevision();
124 YangRevision currentRevision = new YangRevision();
125 currentRevision.setRevDate(currentDate);
126 ((YangSubModule) tmpNode).setRevision(currentRevision);
127 }
128
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530129 try {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530130 ((YangReferenceResolver) listener.getParsedDataStack().peek())
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530131 .resolveSelfFileLinking(ResolvableType.YANG_IF_FEATURE);
132 ((YangReferenceResolver) listener.getParsedDataStack().peek())
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530133 .resolveSelfFileLinking(ResolvableType.YANG_USES);
134 ((YangReferenceResolver) listener.getParsedDataStack().peek())
135 .resolveSelfFileLinking(ResolvableType.YANG_DERIVED_DATA_TYPE);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530136 } catch (DataModelException e) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530137 LinkerException linkerException = new LinkerException(e.getMessage());
138 linkerException.setLine(e.getLineNumber());
139 linkerException.setCharPosition(e.getCharPositionInLine());
140 throw linkerException;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530141 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530142 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530143}