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