blob: fffa86a6ecf61a0c419c8b3e6525a2bd98c5f6b1 [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.YangDataTypes;
20import org.onosproject.yangutils.datamodel.YangLeaf;
21import org.onosproject.yangutils.datamodel.YangLeafList;
22import org.onosproject.yangutils.datamodel.YangType;
23import org.onosproject.yangutils.parser.Parsable;
24import org.onosproject.yangutils.parser.ParsableDataType;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053025import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053026import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053027import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053028import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
29import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
30import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
31import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053032
33/*
34 * Reference: RFC6020 and YANG ANTLR Grammar
35 *
36 * ABNF grammar as per RFC6020
37 * type-stmt = type-keyword sep identifier-ref-arg-str optsep
38 * (";" /
39 * "{" stmtsep
40 * type-body-stmts
41 * "}")
42 *
43 * ANTLR grammar rule
44 * typeStatement : TYPE_KEYWORD string (STMTEND | LEFT_CURLY_BRACE typeBodyStatements RIGHT_CURLY_BRACE);
45 */
46
47/**
48 * Implements listener based call back function corresponding to the "type"
49 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
50 */
51public final class TypeListener {
52
53 /**
54 * Creates a new type listener.
55 */
56 private TypeListener() {
57 }
58
59 /**
60 * It is called when parser receives an input matching the grammar
61 * rule (type), performs validation and updates the data model
62 * tree.
63 *
64 * @param listener listener's object.
65 * @param ctx context object of the grammar rule.
66 */
67 public static void processTypeEntry(TreeWalkListener listener,
68 GeneratedYangParser.TypeStatementContext ctx) {
Vidyashree Rama92fc5562016-02-12 18:44:12 +053069
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053070 // Check for stack to be non empty.
71 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
72 ParsableDataType.TYPE_DATA, String.valueOf(ctx.string().getText()),
73 ListenerErrorLocation.ENTRY);
74
75 YangType type = new YangType();
76 YangDataTypes yangDataTypes = YangDataTypes.getType(ctx.string().getText());
77 type.setDataTypeName(ctx.string().getText());
78 type.setDataType(yangDataTypes);
79
80 Parsable tmpData = listener.getParsedDataStack().peek();
81 switch (tmpData.getParsableDataType()) {
82 case LEAF_DATA:
83 YangLeaf leaf = (YangLeaf) tmpData;
84 leaf.setDataType(type);
85 break;
86 case LEAF_LIST_DATA:
87 YangLeafList leafList = (YangLeafList) tmpData;
88 leafList.setDataType(type);
89 break;
90 case TYPEDEF_DATA: //TODO
91 break;
92 default:
93 throw new ParserException(ListenerErrorMessageConstruction
94 .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
95 ParsableDataType.TYPE_DATA,
96 String.valueOf(ctx.string().getText()),
97 ListenerErrorLocation.ENTRY));
98 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +053099 }
100}