blob: ae9389f6d699981d11bdbcc4125ee9b4173c3064 [file] [log] [blame]
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
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;
22import org.onosproject.yangutils.parser.ParsableDataType;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053023import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053024import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053025import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053026import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
27import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
28import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
29import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053030
31/*
32 * Reference: RFC6020 and YANG ANTLR Grammar
33 *
34 * ABNF grammar as per RFC6020
35 * module-header-stmts = ;; these stmts can appear in any order
36 * [yang-version-stmt stmtsep]
37 * namespace-stmt stmtsep
38 * prefix-stmt stmtsep
39 *
40 * submodule-header-stmts =
41 * ;; these stmts can appear in any order
42 * [yang-version-stmt stmtsep]
43 * belongs-to-stmt stmtsep
44 *
45 * yang-version-stmt = yang-version-keyword sep yang-version-arg-str
46 * optsep stmtend
47 *
48 *
49 * ANTLR grammar rule
50 * module_header_statement : yang_version_stmt? namespace_stmt prefix_stmt
51 * | yang_version_stmt? prefix_stmt namespace_stmt
52 * | namespace_stmt yang_version_stmt? prefix_stmt
53 * | namespace_stmt prefix_stmt yang_version_stmt?
54 * | prefix_stmt namespace_stmt yang_version_stmt?
55 * | prefix_stmt yang_version_stmt? namespace_stmt?
56 * ;
57 * submodule_header_statement : yang_version_stmt? belongs_to_stmt
58 * | belongs_to_stmt yang_version_stmt?
59 * ;
60 * yang_version_stmt : YANG_VERSION_KEYWORD INTEGER STMTEND;
61 */
62
63/**
64 * Implements listener based call back function corresponding to the "version"
65 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
66 */
67public final class VersionListener {
68
69 /**
70 * Creates a new version listener.
71 */
72 private VersionListener() {
73 }
74
75 /**
76 * It is called when parser receives an input matching the grammar
77 * rule (version), perform validations and update the data model
78 * tree.
79 *
80 * @param listener Listener's object.
81 * @param ctx context object of the grammar rule.
82 */
83 public static void processVersionEntry(TreeWalkListener listener,
84 GeneratedYangParser.YangVersionStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053085
86 // Check for stack to be non empty.
87 ListenerValidation
88 .checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER, ParsableDataType.VERSION_DATA,
89 String.valueOf(ctx.INTEGER().getText()), ListenerErrorLocation.ENTRY);
90
91 Integer version = Integer.valueOf(ctx.INTEGER().getText());
92 if (!isVersionValid(version)) {
93 ParserException parserException = new ParserException("Input version not supported");
94 parserException.setLine(ctx.INTEGER().getSymbol().getLine());
95 parserException.setCharPosition(ctx.INTEGER().getSymbol().getCharPositionInLine());
96 throw parserException;
97 }
98
99 // Obtain the node of the stack.
100 Parsable tmpNode = listener.getParsedDataStack().peek();
101 switch (tmpNode.getParsableDataType()) {
102 case MODULE_DATA: {
103 YangModule module = (YangModule) tmpNode;
104 module.setVersion((byte) 1);
105 break;
106 }
107 case SUB_MODULE_DATA: {
108 YangSubModule subModule = (YangSubModule) tmpNode;
109 subModule.setVersion((byte) 1);
110 break;
111 }
112 default:
113 throw new ParserException(ListenerErrorMessageConstruction.
114 constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
115 ParsableDataType.VERSION_DATA, String.valueOf(ctx.INTEGER().getText()),
116 ListenerErrorLocation.ENTRY));
117 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530118 }
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530119
120 /**
121 * Validates whether the value of YANG version.
122 *
123 * @param version input yang version
124 * @return validation result
125 */
126 private static boolean isVersionValid(Integer version) {
127 return version == 1;
128 }
129}