blob: 68d2b434324b59fa2612826a77672bf225c704a0 [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.YangContainer;
20import org.onosproject.yangutils.datamodel.YangLeaf;
21import org.onosproject.yangutils.datamodel.YangLeafList;
22import org.onosproject.yangutils.datamodel.YangList;
23import org.onosproject.yangutils.parser.Parsable;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053024import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053025import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053026import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama59071f32016-02-20 19:27:56 +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 Agrawal8e8770a2016-02-27 03:57:50 +053032import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053033
34/*
35 * Reference: RFC6020 and YANG ANTLR Grammar
36 *
37 * ABNF grammar as per RFC6020
38 * config-stmt = config-keyword sep
39 * config-arg-str stmtend
40 * config-arg-str = < a string that matches the rule
41 * config-arg >
42 * config-arg = true-keyword / false-keyword
43 *
44 * ANTLR grammar rule
45 * configStatement : CONFIG_KEYWORD (TRUE_KEYWORD | FALSE_KEYWORD) STMTEND;
46 */
47
48/**
49 * Implements listener based call back function corresponding to the "config"
50 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
51 */
52public final class ConfigListener {
53
54 /**
55 * Creates a new config listener.
56 */
57 private ConfigListener() {
58 }
59
60 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053061 * It is called when parser receives an input matching the grammar rule
62 * (config), performs validation and updates the data model tree.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053063 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053064 * @param listener listener's object
65 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +053066 */
67 public static void processConfigEntry(TreeWalkListener listener,
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053068 GeneratedYangParser.ConfigStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053069 boolean isConfig = false;
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, CONFIG_DATA, "", ENTRY);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053073
74 if (ctx.TRUE_KEYWORD() != null) {
75 isConfig = true;
76 }
77
78 Parsable tmpData = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053079 switch (tmpData.getYangConstructType()) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053080 case LEAF_DATA:
81 YangLeaf leaf = (YangLeaf) tmpData;
82 leaf.setConfig(isConfig);
83 break;
84 case CONTAINER_DATA:
85 YangContainer container = (YangContainer) tmpData;
86 container.setConfig(isConfig);
87 break;
88 case LEAF_LIST_DATA:
89 YangLeafList leafList = (YangLeafList) tmpData;
90 leafList.setConfig(isConfig);
91 break;
92 case LIST_DATA:
93 YangList yangList = (YangList) tmpData;
94 yangList.setConfig(isConfig);
95 break;
96 case CHOICE_DATA: // TODO
97 break;
98 default:
Vidyashree Rama59071f32016-02-20 19:27:56 +053099 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, CONFIG_DATA, "", ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530100 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530101 }
102}