blob: b00024232acd3f34d8ba8ee82727f8eef26241cf [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;
Gaurav Agrawalbd804472016-03-25 11:25:36 +053025import org.onosproject.yangutils.datamodel.YangUnion;
26import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053027import org.onosproject.yangutils.parser.Parsable;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053028import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053029import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053030import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vinod Kumar S71cba682016-02-25 15:52:16 +053031
Vidyashree Rama59071f32016-02-20 19:27:56 +053032import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053033import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Vidyashree Rama59071f32016-02-20 19:27:56 +053034import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
35import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053036import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
Vidyashree Rama59071f32016-02-20 19:27:56 +053037import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
38import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053039import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
Vidyashree Rama59071f32016-02-20 19:27:56 +053040
Vidyashree Rama92fc5562016-02-12 18:44:12 +053041/*
42 * Reference: RFC6020 and YANG ANTLR Grammar
43 *
44 * ABNF grammar as per RFC6020
45 * type-stmt = type-keyword sep identifier-ref-arg-str optsep
46 * (";" /
47 * "{" stmtsep
48 * type-body-stmts
49 * "}")
50 *
51 * ANTLR grammar rule
52 * typeStatement : TYPE_KEYWORD string (STMTEND | LEFT_CURLY_BRACE typeBodyStatements RIGHT_CURLY_BRACE);
53 */
54
55/**
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053056 * Implements listener based call back function corresponding to the "type" rule
57 * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053058 */
59public final class TypeListener {
60
61 /**
62 * Creates a new type listener.
63 */
64 private TypeListener() {
65 }
66
67 /**
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053068 * It is called when parser receives an input matching the grammar rule
69 * (type), performs validation and updates the data model tree.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053070 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053071 * @param listener listener's object
72 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +053073 */
74 public static void processTypeEntry(TreeWalkListener listener,
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053075 GeneratedYangParser.TypeStatementContext ctx) {
Vidyashree Rama92fc5562016-02-12 18:44:12 +053076
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053077 // Check for stack to be non empty.
Vidyashree Rama59071f32016-02-20 19:27:56 +053078 checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPE_DATA, ctx.string().getText(), ENTRY);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053079
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053080 YangDataTypes yangDataTypes = YangDataTypes.getType(ctx.string().getText());
Vinod Kumar S71cba682016-02-25 15:52:16 +053081 YangType<?> type = new YangType();
82
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053083 type.setDataTypeName(ctx.string().getText());
84 type.setDataType(yangDataTypes);
85
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053086 listener.getParsedDataStack().push(type);
87 }
88
89 /**
90 * It is called when parser exits from grammar rule (type), it perform
91 * validations and update the data model tree.
92 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053093 * @param listener Listener's object
94 * @param ctx context object of the grammar rule
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053095 */
96 public static void processTypeExit(TreeWalkListener listener,
97 GeneratedYangParser.TypeStatementContext ctx) {
98
99 // Check for stack to be non empty.
100 checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT);
101
102 Parsable type = listener.getParsedDataStack().pop();
103 if (!(type instanceof YangType)) {
104 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
105 ctx.string().getText(), EXIT));
106 }
107
108 // Check for stack to be non empty.
109 checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT);
110
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530111 Parsable tmpData = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530112 switch (tmpData.getYangConstructType()) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530113 case LEAF_DATA:
114 YangLeaf leaf = (YangLeaf) tmpData;
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530115 leaf.setDataType((YangType<?>) type);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530116 break;
117 case LEAF_LIST_DATA:
118 YangLeafList leafList = (YangLeafList) tmpData;
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530119 leafList.setDataType((YangType<?>) type);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530120 break;
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530121 case UNION_DATA:
122 YangUnion unionNode = (YangUnion) tmpData;
123 try {
124 unionNode.addToTypeList((YangType<?>) type);
125 } catch (DataModelException e) {
126 ParserException parserException = new ParserException(e.getMessage());
127 parserException.setLine(ctx.getStart().getLine());
128 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
129 throw parserException;
130 }
131 break;
Vinod Kumar S71cba682016-02-25 15:52:16 +0530132 case TYPEDEF_DATA:
133
134 /* Prepare the base type info and set in derived type */
135 YangTypeDef typeDef = (YangTypeDef) tmpData;
136 YangType<YangDerivedType> derivedType = typeDef.getDerivedType();
137 if (derivedType == null) {
138 //TODO: set the error info correctly, to depict missing info
139 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
140 ctx.string().getText(), ENTRY));
141 }
142
143 YangDerivedType derivedTypeInfo = new YangDerivedType();
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530144 if (((YangType<?>) type).getDataType() != YangDataTypes.DERIVED) {
145 derivedTypeInfo.setEffectiveYangBuiltInType(((YangType<?>) type).getDataType());
Vinod Kumar S71cba682016-02-25 15:52:16 +0530146 } else {
147 /*
148 * It will be resolved in the validate data model at exit.
149 * Nothing needs to be done.
150 */
151 }
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530152 derivedTypeInfo.setBaseType((YangType<?>) type);
Vinod Kumar S71cba682016-02-25 15:52:16 +0530153 derivedType.setDataTypeExtendedInfo(derivedTypeInfo);
154
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530155 break;
Vinod Kumar S71cba682016-02-25 15:52:16 +0530156 //TODO: union, deviate replacement statement.case TYPEDEF_DATA: //TODO
157
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530158 default:
Vidyashree Rama59071f32016-02-20 19:27:56 +0530159 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530160 ctx.string().getText(), EXIT));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530161 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530162 }
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530163}