blob: ea4d20fcea317a2179595f70838d8006d1726321 [file] [log] [blame]
Vidyashree Ramafe971cb2016-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 Ramab24ca902016-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 Ramafe971cb2016-02-12 18:44:12 +053024import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Ramab24ca902016-02-13 21:47:58 +053025import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053026import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Ramae300f702016-02-20 19:27:56 +053027import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
Vidyashree Ramae68bb192016-03-15 10:18:25 +053028import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidBooleanValue;
Vidyashree Ramae300f702016-02-20 19:27:56 +053029import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053033import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA;
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053034
35/*
36 * Reference: RFC6020 and YANG ANTLR Grammar
37 *
38 * ABNF grammar as per RFC6020
39 * config-stmt = config-keyword sep
40 * config-arg-str stmtend
41 * config-arg-str = < a string that matches the rule
42 * config-arg >
43 * config-arg = true-keyword / false-keyword
44 *
45 * ANTLR grammar rule
Vidyashree Ramae68bb192016-03-15 10:18:25 +053046 * configStatement : CONFIG_KEYWORD config STMTEND;
47 * config : string;
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053048 */
49
50/**
51 * Implements listener based call back function corresponding to the "config"
52 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
53 */
54public final class ConfigListener {
55
56 /**
57 * Creates a new config listener.
58 */
59 private ConfigListener() {
60 }
61
62 /**
Vinod Kumar Sc26bf192016-02-23 22:36:57 +053063 * It is called when parser receives an input matching the grammar rule
64 * (config), performs validation and updates the data model tree.
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053065 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053066 * @param listener listener's object
67 * @param ctx context object of the grammar rule
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053068 */
69 public static void processConfigEntry(TreeWalkListener listener,
Vinod Kumar Sc26bf192016-02-23 22:36:57 +053070 GeneratedYangParser.ConfigStatementContext ctx) {
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053071
Vidyashree Ramab24ca902016-02-13 21:47:58 +053072 // Check for stack to be non empty.
Vidyashree Ramae300f702016-02-20 19:27:56 +053073 checkStackIsNotEmpty(listener, MISSING_HOLDER, CONFIG_DATA, "", ENTRY);
Vidyashree Ramab24ca902016-02-13 21:47:58 +053074
Vidyashree Ramae68bb192016-03-15 10:18:25 +053075 boolean isConfig = getValidBooleanValue(ctx.config().getText(), CONFIG_DATA, ctx);
Vidyashree Ramab24ca902016-02-13 21:47:58 +053076
77 Parsable tmpData = listener.getParsedDataStack().peek();
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053078 switch (tmpData.getYangConstructType()) {
Vidyashree Ramab24ca902016-02-13 21:47:58 +053079 case LEAF_DATA:
80 YangLeaf leaf = (YangLeaf) tmpData;
81 leaf.setConfig(isConfig);
82 break;
83 case CONTAINER_DATA:
84 YangContainer container = (YangContainer) tmpData;
85 container.setConfig(isConfig);
86 break;
87 case LEAF_LIST_DATA:
88 YangLeafList leafList = (YangLeafList) tmpData;
89 leafList.setConfig(isConfig);
90 break;
91 case LIST_DATA:
92 YangList yangList = (YangList) tmpData;
93 yangList.setConfig(isConfig);
94 break;
95 case CHOICE_DATA: // TODO
96 break;
97 default:
Vidyashree Ramae300f702016-02-20 19:27:56 +053098 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, CONFIG_DATA, "", ENTRY));
Vidyashree Ramab24ca902016-02-13 21:47:58 +053099 }
Vidyashree Ramafe971cb2016-02-12 18:44:12 +0530100 }
101}