blob: 62dfb70aaef786ea77e984a1033a326621233fa7 [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;
20import org.onosproject.yangutils.datamodel.YangSubModule;
21import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053022import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053023import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053024import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053025
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053026import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidVersion;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053027import static org.onosproject.yangutils.utils.YangConstructType.VERSION_DATA;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053028import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053033
34/*
35 * Reference: RFC6020 and YANG ANTLR Grammar
36 *
37 * ABNF grammar as per RFC6020
38 * module-header-stmts = ;; these stmts can appear in any order
39 * [yang-version-stmt stmtsep]
40 * namespace-stmt stmtsep
41 * prefix-stmt stmtsep
42 *
43 * submodule-header-stmts =
44 * ;; these stmts can appear in any order
45 * [yang-version-stmt stmtsep]
46 * belongs-to-stmt stmtsep
47 *
48 * yang-version-stmt = yang-version-keyword sep yang-version-arg-str
49 * optsep stmtend
50 *
51 *
52 * ANTLR grammar rule
53 * module_header_statement : yang_version_stmt? namespace_stmt prefix_stmt
54 * | yang_version_stmt? prefix_stmt namespace_stmt
55 * | namespace_stmt yang_version_stmt? prefix_stmt
56 * | namespace_stmt prefix_stmt yang_version_stmt?
57 * | prefix_stmt namespace_stmt yang_version_stmt?
58 * | prefix_stmt yang_version_stmt? namespace_stmt?
59 * ;
60 * submodule_header_statement : yang_version_stmt? belongs_to_stmt
61 * | belongs_to_stmt yang_version_stmt?
62 * ;
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053063 * yang_version_stmt : YANG_VERSION_KEYWORD version STMTEND;
64 * version : string;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053065 */
66
67/**
Bharat saraswald9822e92016-04-05 15:13:44 +053068 * Represents listener based call back function corresponding to the "version"
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053069 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
70 */
71public final class VersionListener {
72
73 /**
74 * Creates a new version listener.
75 */
76 private VersionListener() {
77 }
78
79 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053080 * It is called when parser receives an input matching the grammar rule
81 * (version), perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053082 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053083 * @param listener Listener's object
84 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053085 */
86 public static void processVersionEntry(TreeWalkListener listener,
87 GeneratedYangParser.YangVersionStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053088
89 // Check for stack to be non empty.
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053090 checkStackIsNotEmpty(listener, MISSING_HOLDER, VERSION_DATA, ctx.version().getText(), ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053091
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053092 byte version = getValidVersion(ctx);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053093
94 // Obtain the node of the stack.
95 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053096 switch (tmpNode.getYangConstructType()) {
Bharat saraswald9822e92016-04-05 15:13:44 +053097 case MODULE_DATA: {
98 YangModule module = (YangModule) tmpNode;
99 module.setVersion(version);
100 break;
101 }
102 case SUB_MODULE_DATA: {
103 YangSubModule subModule = (YangSubModule) tmpNode;
104 subModule.setVersion(version);
105 break;
106 }
107 default:
108 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, VERSION_DATA,
109 ctx.version().getText(), ENTRY));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530110 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530111 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530112}