blob: e846b42a31ea948312c93d7f6e394639336c99da [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.datamodel.YangLeafList;
21import org.onosproject.yangutils.parser.Parsable;
22import org.onosproject.yangutils.parser.ParsableDataType;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053023import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053024import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053025import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053026import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
27import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
28import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
29import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053030
31/*
32 * Reference: RFC6020 and YANG ANTLR Grammar
33 *
34 * ABNF grammar as per RFC6020
35 * units-stmt = units-keyword sep string optsep stmtend
36 *
37 * ANTLR grammar rule
38 * unitsStatement : UNITS_KEYWORD string STMTEND;
39 */
40
41/**
42 * Implements listener based call back function corresponding to the "units"
43 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
44 */
45public final class UnitsListener {
46
47 /**
48 * Creates a new units listener.
49 */
50 private UnitsListener() {
51 }
52
53 /**
54 * It is called when parser receives an input matching the grammar
55 * rule (units), performs validation and updates the data model
56 * tree.
57 *
58 * @param listener listener's object.
59 * @param ctx context object of the grammar rule.
60 */
61 public static void processUnitsEntry(TreeWalkListener listener,
62 GeneratedYangParser.UnitsStatementContext ctx) {
Vidyashree Rama92fc5562016-02-12 18:44:12 +053063
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053064 // Check for stack to be non empty.
65 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
66 ParsableDataType.UNITS_DATA, String.valueOf(ctx.string().getText()),
67 ListenerErrorLocation.ENTRY);
68
69 Parsable tmpData = listener.getParsedDataStack().peek();
70 switch (tmpData.getParsableDataType()) {
71 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:
83 throw new ParserException(ListenerErrorMessageConstruction
84 .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
85 ParsableDataType.UNITS_DATA,
86 String.valueOf(ctx.string().getText()),
87 ListenerErrorLocation.ENTRY));
88 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +053089 }
90}