blob: c284fc295cb3f14959cc1d8497e5db9df11a4e95 [file] [log] [blame]
Vidyashree Rama92fc5562016-02-12 18:44:12 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Rama92fc5562016-02-12 18:44:12 +05303 *
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.datamodel.YangLeafList;
21import org.onosproject.yangutils.parser.Parsable;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053022import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053023import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053024import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama59071f32016-02-20 19:27:56 +053025
Vidyashree Rama59071f32016-02-20 19:27:56 +053026import 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;
Bharat saraswald9822e92016-04-05 15:13:44 +053031import static org.onosproject.yangutils.utils.YangConstructType.UNITS_DATA;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053032
33/*
34 * Reference: RFC6020 and YANG ANTLR Grammar
35 *
36 * ABNF grammar as per RFC6020
37 * units-stmt = units-keyword sep string optsep stmtend
38 *
39 * ANTLR grammar rule
40 * unitsStatement : UNITS_KEYWORD string STMTEND;
41 */
42
43/**
Bharat saraswald9822e92016-04-05 15:13:44 +053044 * Represents listener based call back function corresponding to the "units"
Vidyashree Rama92fc5562016-02-12 18:44:12 +053045 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
46 */
47public final class UnitsListener {
48
49 /**
50 * Creates a new units listener.
51 */
52 private UnitsListener() {
53 }
54
55 /**
56 * It is called when parser receives an input matching the grammar
57 * rule (units), performs validation and updates the data model
58 * tree.
59 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053060 * @param listener listener's object
61 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +053062 */
63 public static void processUnitsEntry(TreeWalkListener listener,
64 GeneratedYangParser.UnitsStatementContext ctx) {
Vidyashree Rama92fc5562016-02-12 18:44:12 +053065
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053066 // Check for stack to be non empty.
Vidyashree Rama59071f32016-02-20 19:27:56 +053067 checkStackIsNotEmpty(listener, MISSING_HOLDER, UNITS_DATA, ctx.string().getText(), ENTRY);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053068
69 Parsable tmpData = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053070 switch (tmpData.getYangConstructType()) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053071 case LEAF_DATA:
72 YangLeaf leaf = (YangLeaf) tmpData;
73 leaf.setUnits(ctx.string().getText());
74 break;
75 case LEAF_LIST_DATA:
76 YangLeafList leafList = (YangLeafList) tmpData;
77 leafList.setUnits(ctx.string().getText());
78 break;
79 case TYPEDEF_DATA:
80 // TODO
81 break;
82 default:
Vidyashree Rama59071f32016-02-20 19:27:56 +053083 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, UNITS_DATA,
84 ctx.string().getText(), ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053085 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +053086 }
87}