blob: 9b9a38828292ce216a38f846ac09d33ec0f2f724 [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;
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
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053026import static org.onosproject.yangutils.utils.YangConstructType.VERSION_DATA;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053027import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053032
33/*
34 * Reference: RFC6020 and YANG ANTLR Grammar
35 *
36 * ABNF grammar as per RFC6020
37 * module-header-stmts = ;; these stmts can appear in any order
38 * [yang-version-stmt stmtsep]
39 * namespace-stmt stmtsep
40 * prefix-stmt stmtsep
41 *
42 * submodule-header-stmts =
43 * ;; these stmts can appear in any order
44 * [yang-version-stmt stmtsep]
45 * belongs-to-stmt stmtsep
46 *
47 * yang-version-stmt = yang-version-keyword sep yang-version-arg-str
48 * optsep stmtend
49 *
50 *
51 * ANTLR grammar rule
52 * module_header_statement : yang_version_stmt? namespace_stmt prefix_stmt
53 * | yang_version_stmt? prefix_stmt namespace_stmt
54 * | namespace_stmt yang_version_stmt? prefix_stmt
55 * | namespace_stmt prefix_stmt yang_version_stmt?
56 * | prefix_stmt namespace_stmt yang_version_stmt?
57 * | prefix_stmt yang_version_stmt? namespace_stmt?
58 * ;
59 * submodule_header_statement : yang_version_stmt? belongs_to_stmt
60 * | belongs_to_stmt yang_version_stmt?
61 * ;
62 * yang_version_stmt : YANG_VERSION_KEYWORD INTEGER STMTEND;
63 */
64
65/**
66 * Implements listener based call back function corresponding to the "version"
67 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
68 */
69public final class VersionListener {
70
71 /**
72 * Creates a new version listener.
73 */
74 private VersionListener() {
75 }
76
77 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053078 * It is called when parser receives an input matching the grammar rule
79 * (version), perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053080 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053081 * @param listener Listener's object
82 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053083 */
84 public static void processVersionEntry(TreeWalkListener listener,
85 GeneratedYangParser.YangVersionStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053086
87 // Check for stack to be non empty.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053088 checkStackIsNotEmpty(listener, MISSING_HOLDER, VERSION_DATA, ctx.INTEGER().getText(), ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053089
90 Integer version = Integer.valueOf(ctx.INTEGER().getText());
91 if (!isVersionValid(version)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053092 ParserException parserException = new ParserException("YANG file error: Input version not supported");
Gaurav Agrawala04483c2016-02-13 14:23:40 +053093 parserException.setLine(ctx.INTEGER().getSymbol().getLine());
94 parserException.setCharPosition(ctx.INTEGER().getSymbol().getCharPositionInLine());
95 throw parserException;
96 }
97
98 // Obtain the node of the stack.
99 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530100 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530101 case MODULE_DATA: {
102 YangModule module = (YangModule) tmpNode;
103 module.setVersion((byte) 1);
104 break;
105 }
106 case SUB_MODULE_DATA: {
107 YangSubModule subModule = (YangSubModule) tmpNode;
108 subModule.setVersion((byte) 1);
109 break;
110 }
111 default:
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530112 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, VERSION_DATA,
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530113 ctx.INTEGER().getText(), ENTRY));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530114 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530115 }
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530116
117 /**
118 * Validates whether the value of YANG version.
119 *
120 * @param version input yang version
121 * @return validation result
122 */
123 private static boolean isVersionValid(Integer version) {
124 return version == 1;
125 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530126}