blob: 5d6a7442878a044a0213349a4f855c8f57ba7f89 [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;
Vinod Kumar S71cba682016-02-25 15:52:16 +053020import org.onosproject.yangutils.datamodel.YangDerivedType;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053021import org.onosproject.yangutils.datamodel.YangLeaf;
22import org.onosproject.yangutils.datamodel.YangLeafList;
23import org.onosproject.yangutils.datamodel.YangType;
Vinod Kumar S71cba682016-02-25 15:52:16 +053024import org.onosproject.yangutils.datamodel.YangTypeDef;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053025import org.onosproject.yangutils.parser.Parsable;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053026import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053027import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053028import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vinod Kumar S71cba682016-02-25 15:52:16 +053029
Vidyashree Rama59071f32016-02-20 19:27:56 +053030import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053031import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Vidyashree Rama59071f32016-02-20 19:27:56 +053032import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053034import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
Vidyashree Rama59071f32016-02-20 19:27:56 +053035import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053037import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
Vidyashree Rama59071f32016-02-20 19:27:56 +053038
Vidyashree Rama92fc5562016-02-12 18:44:12 +053039/*
40 * Reference: RFC6020 and YANG ANTLR Grammar
41 *
42 * ABNF grammar as per RFC6020
43 * type-stmt = type-keyword sep identifier-ref-arg-str optsep
44 * (";" /
45 * "{" stmtsep
46 * type-body-stmts
47 * "}")
48 *
49 * ANTLR grammar rule
50 * typeStatement : TYPE_KEYWORD string (STMTEND | LEFT_CURLY_BRACE typeBodyStatements RIGHT_CURLY_BRACE);
51 */
52
53/**
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053054 * Implements listener based call back function corresponding to the "type" rule
55 * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053056 */
57public final class TypeListener {
58
59 /**
60 * Creates a new type listener.
61 */
62 private TypeListener() {
63 }
64
65 /**
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053066 * It is called when parser receives an input matching the grammar rule
67 * (type), performs validation and updates the data model tree.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053068 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053069 * @param listener listener's object
70 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +053071 */
72 public static void processTypeEntry(TreeWalkListener listener,
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053073 GeneratedYangParser.TypeStatementContext ctx) {
Vidyashree Rama92fc5562016-02-12 18:44:12 +053074
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053075 // Check for stack to be non empty.
Vidyashree Rama59071f32016-02-20 19:27:56 +053076 checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPE_DATA, ctx.string().getText(), ENTRY);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053077
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053078 YangDataTypes yangDataTypes = YangDataTypes.getType(ctx.string().getText());
Vinod Kumar S71cba682016-02-25 15:52:16 +053079 YangType<?> type = new YangType();
80
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053081 type.setDataTypeName(ctx.string().getText());
82 type.setDataType(yangDataTypes);
83
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053084 listener.getParsedDataStack().push(type);
85 }
86
87 /**
88 * It is called when parser exits from grammar rule (type), it perform
89 * validations and update the data model tree.
90 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053091 * @param listener Listener's object
92 * @param ctx context object of the grammar rule
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053093 */
94 public static void processTypeExit(TreeWalkListener listener,
95 GeneratedYangParser.TypeStatementContext ctx) {
96
97 // Check for stack to be non empty.
98 checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT);
99
100 Parsable type = listener.getParsedDataStack().pop();
101 if (!(type instanceof YangType)) {
102 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
103 ctx.string().getText(), EXIT));
104 }
105
106 // Check for stack to be non empty.
107 checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT);
108
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530109 Parsable tmpData = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530110 switch (tmpData.getYangConstructType()) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530111 case LEAF_DATA:
112 YangLeaf leaf = (YangLeaf) tmpData;
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530113 leaf.setDataType((YangType<?>) type);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530114 break;
115 case LEAF_LIST_DATA:
116 YangLeafList leafList = (YangLeafList) tmpData;
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530117 leafList.setDataType((YangType<?>) type);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530118 break;
Vinod Kumar S71cba682016-02-25 15:52:16 +0530119 case TYPEDEF_DATA:
120
121 /* Prepare the base type info and set in derived type */
122 YangTypeDef typeDef = (YangTypeDef) tmpData;
123 YangType<YangDerivedType> derivedType = typeDef.getDerivedType();
124 if (derivedType == null) {
125 //TODO: set the error info correctly, to depict missing info
126 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
127 ctx.string().getText(), ENTRY));
128 }
129
130 YangDerivedType derivedTypeInfo = new YangDerivedType();
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530131 if (((YangType<?>) type).getDataType() != YangDataTypes.DERIVED) {
132 derivedTypeInfo.setEffectiveYangBuiltInType(((YangType<?>) type).getDataType());
Vinod Kumar S71cba682016-02-25 15:52:16 +0530133 } else {
134 /*
135 * It will be resolved in the validate data model at exit.
136 * Nothing needs to be done.
137 */
138 }
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530139 derivedTypeInfo.setBaseType((YangType<?>) type);
Vinod Kumar S71cba682016-02-25 15:52:16 +0530140 derivedType.setDataTypeExtendedInfo(derivedTypeInfo);
141
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530142 break;
Vinod Kumar S71cba682016-02-25 15:52:16 +0530143 //TODO: union, deviate replacement statement.case TYPEDEF_DATA: //TODO
144
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530145 default:
Vidyashree Rama59071f32016-02-20 19:27:56 +0530146 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530147 ctx.string().getText(), EXIT));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530148 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530149 }
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530150}