blob: 21c7533700c9d385f4927adc7bc24104f7106fe6 [file] [log] [blame]
janani be18b5342016-07-13 21:06:41 +05301/*
2 * Copyright 2016-present 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
19import org.onosproject.yangutils.datamodel.YangLeafRef;
20import org.onosproject.yangutils.datamodel.YangType;
21import org.onosproject.yangutils.datamodel.utils.Parsable;
22import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
23import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
24import org.onosproject.yangutils.parser.exceptions.ParserException;
25import org.onosproject.yangutils.parser.impl.TreeWalkListener;
26
27import static org.onosproject.yangutils.datamodel.utils.YangConstructType.REQUIRE_INSTANCE_DATA;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidBooleanValue;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
34
35/*
36 * Reference: RFC6020 and YANG ANTLR Grammar
37 *
38 * ABNF grammar as per RFC6020
39 * require-instance-stmt = require-instance-keyword sep
40 * require-instance-arg-str stmtend
41 *
42 * require-instance-arg-str = < a string that matches the rule
43 * require-instance-arg >
44 *
45 * require-instance-arg = true-keyword / false-keyword
46 *
47 * ANTLR grammar rule
48 *
49 * requireInstanceStatement : REQUIRE_INSTANCE_KEYWORD requireInstance STMTEND;
50 *
51 * requireInstance : string;
52 */
53
54/**
55 * Represents listener based call back function corresponding to the
56 * "require-instance" rule defined in ANTLR grammar file for corresponding ABNF rule
57 * in RFC 6020.
58 */
59public final class RequireInstanceListener {
60
61 /**
62 * Creates a new require instance listener.
63 */
64 private RequireInstanceListener() {
65 }
66
67 /**
68 * It is called when parser receives an input matching the grammar rule
69 * (require-instance), performs validation and updates the data model tree.
70 *
71 * @param listener listener's object
72 * @param ctx context object of the grammar rule
73 */
74 public static void processRequireInstanceEntry(TreeWalkListener listener,
75 GeneratedYangParser.RequireInstanceStatementContext ctx) {
76
77 // Check for stack to be non empty.
78 checkStackIsNotEmpty(listener, MISSING_HOLDER, REQUIRE_INSTANCE_DATA, "", ENTRY);
79
80 Parsable curData = listener.getParsedDataStack().peek();
81
82 // Gets the status of require instance
83 boolean isRequireInstance = getValidBooleanValue(ctx.requireInstance().getText(), REQUIRE_INSTANCE_DATA, ctx);
84
85 // Checks the holder of require-instance as leafref or type, else throws error.
86 if (curData instanceof YangLeafRef) {
87
88 // Sets the require-instance status to leafref.
89 ((YangLeafRef) curData).setRequireInstance(isRequireInstance);
90 } else if (curData instanceof YangType) {
91
92 // Checks type should be instance-identifier, else throw error.
93 if (((YangType) curData).getDataType() == YangDataTypes.INSTANCE_IDENTIFIER) {
94
95 // Sets the require-instance status to instance-identifier type.
96 ((YangType) curData).setDataTypeExtendedInfo(isRequireInstance);
97 } else {
98 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, REQUIRE_INSTANCE_DATA,
99 ctx.getText(), ENTRY));
100 }
101 } else {
102 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, REQUIRE_INSTANCE_DATA,
103 ctx.getText(), ENTRY));
104 }
105 }
106}