blob: 2e118cef049192c6926ccf826f9636978566eab8 [file] [log] [blame]
Vidyashree Rama92fc5562016-02-12 18:44:12 +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
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053019import org.onosproject.yangutils.datamodel.YangLeaf;
20import org.onosproject.yangutils.parser.Parsable;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053021import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053022import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053023import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama59071f32016-02-20 19:27:56 +053024
25import static org.onosproject.yangutils.parser.ParsableDataType.MANDATORY_DATA;
26import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053031
32/*
33 * Reference: RFC6020 and YANG ANTLR Grammar
34 *
35 * ABNF grammar as per RFC6020
36 * mandatory-stmt = mandatory-keyword sep
37 * mandatory-arg-str stmtend
38 *
39 * mandatory-arg-str = < a string that matches the rule
40 * mandatory-arg >
41 *
42 * mandatory-arg = true-keyword / false-keyword
43 *
44 * ANTLR grammar rule
45 * mandatoryStatement : MANDATORY_KEYWORD (TRUE_KEYWORD | FALSE_KEYWORD) STMTEND;
46 */
47
48/**
49 * Implements listener based call back function corresponding to the "mandatory"
50 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
51 */
52public final class MandatoryListener {
53
54 /**
55 * Creates a new mandatory listener.
56 */
57 private MandatoryListener() {
58 }
59
60 /**
61 * It is called when parser receives an input matching the grammar
62 * rule (mandatory), performs validation and updates the data model
63 * tree.
64 *
65 * @param listener listener's object.
66 * @param ctx context object of the grammar rule.
67 */
68 public static void processMandatoryEntry(TreeWalkListener listener,
69 GeneratedYangParser.MandatoryStatementContext ctx) {
Vidyashree Rama92fc5562016-02-12 18:44:12 +053070
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053071 // Check for stack to be non empty.
Vidyashree Rama59071f32016-02-20 19:27:56 +053072 checkStackIsNotEmpty(listener, MISSING_HOLDER, MANDATORY_DATA, "", ENTRY);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053073
74 Parsable tmpNode = listener.getParsedDataStack().peek();
75 switch (tmpNode.getParsableDataType()) {
76 case LEAF_DATA:
77 YangLeaf leaf = (YangLeaf) tmpNode;
78 if (ctx.TRUE_KEYWORD() != null) {
79 leaf.setMandatory(true);
80 } else {
81 leaf.setMandatory(false);
82 }
83 break;
84 case CHOICE_DATA: // TODO
85 break;
86 default:
Vidyashree Rama59071f32016-02-20 19:27:56 +053087 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, MANDATORY_DATA, "", ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053088 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +053089 }
90}