Committed by
Gerrit Code Review
YANG rpc, input and output listener
Change-Id: Idd9847175c61d9f033cf80213b46e9c9c949849c
Showing
16 changed files
with
971 additions
and
13 deletions
| ... | @@ -26,6 +26,9 @@ import org.onosproject.yangutils.datamodel.YangSubModule; | ... | @@ -26,6 +26,9 @@ import org.onosproject.yangutils.datamodel.YangSubModule; |
| 26 | import org.onosproject.yangutils.datamodel.YangTypeDef; | 26 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
| 27 | import org.onosproject.yangutils.datamodel.YangUses; | 27 | import org.onosproject.yangutils.datamodel.YangUses; |
| 28 | import org.onosproject.yangutils.datamodel.YangNotification; | 28 | import org.onosproject.yangutils.datamodel.YangNotification; |
| 29 | +import org.onosproject.yangutils.datamodel.YangRpc; | ||
| 30 | +import org.onosproject.yangutils.datamodel.YangInput; | ||
| 31 | +import org.onosproject.yangutils.datamodel.YangOutput; | ||
| 29 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugment; | 32 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugment; |
| 30 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaCase; | 33 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaCase; |
| 31 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaChoice; | 34 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaChoice; |
| ... | @@ -37,6 +40,9 @@ import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule; | ... | @@ -37,6 +40,9 @@ import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule; |
| 37 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeDef; | 40 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeDef; |
| 38 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaUses; | 41 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaUses; |
| 39 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaNotification; | 42 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaNotification; |
| 43 | +import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaRpc; | ||
| 44 | +import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaInput; | ||
| 45 | +import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaOutput; | ||
| 40 | 46 | ||
| 41 | /** | 47 | /** |
| 42 | * Factory to create data model objects based on the target file type. | 48 | * Factory to create data model objects based on the target file type. |
| ... | @@ -246,4 +252,58 @@ public final class YangDataModelFactory { | ... | @@ -246,4 +252,58 @@ public final class YangDataModelFactory { |
| 246 | } | 252 | } |
| 247 | } | 253 | } |
| 248 | } | 254 | } |
| 255 | + | ||
| 256 | + /** | ||
| 257 | + * Based on the target language generate the inherited data model node. | ||
| 258 | + * | ||
| 259 | + * @param targetLanguage target language in which YANG mapping needs to be | ||
| 260 | + * generated | ||
| 261 | + * @return the corresponding inherited node based on the target language | ||
| 262 | + */ | ||
| 263 | + public static YangRpc getYangRpcNode(GeneratedLanguage targetLanguage) { | ||
| 264 | + switch (targetLanguage) { | ||
| 265 | + case JAVA_GENERATION: { | ||
| 266 | + return new YangJavaRpc(); | ||
| 267 | + } | ||
| 268 | + default: { | ||
| 269 | + throw new RuntimeException("Only YANG to Java is supported."); | ||
| 270 | + } | ||
| 271 | + } | ||
| 272 | + } | ||
| 273 | + | ||
| 274 | + /** | ||
| 275 | + * Based on the target language generate the inherited data model node. | ||
| 276 | + * | ||
| 277 | + * @param targetLanguage target language in which YANG mapping needs to be | ||
| 278 | + * generated | ||
| 279 | + * @return the corresponding inherited node based on the target language | ||
| 280 | + */ | ||
| 281 | + public static YangInput getYangInputNode(GeneratedLanguage targetLanguage) { | ||
| 282 | + switch (targetLanguage) { | ||
| 283 | + case JAVA_GENERATION: { | ||
| 284 | + return new YangJavaInput(); | ||
| 285 | + } | ||
| 286 | + default: { | ||
| 287 | + throw new RuntimeException("Only YANG to Java is supported."); | ||
| 288 | + } | ||
| 289 | + } | ||
| 290 | + } | ||
| 291 | + | ||
| 292 | + /** | ||
| 293 | + * Based on the target language generate the inherited data model node. | ||
| 294 | + * | ||
| 295 | + * @param targetLanguage target language in which YANG mapping needs to be | ||
| 296 | + * generated | ||
| 297 | + * @return the corresponding inherited node based on the target language | ||
| 298 | + */ | ||
| 299 | + public static YangOutput getYangOutputNode(GeneratedLanguage targetLanguage) { | ||
| 300 | + switch (targetLanguage) { | ||
| 301 | + case JAVA_GENERATION: { | ||
| 302 | + return new YangJavaOutput(); | ||
| 303 | + } | ||
| 304 | + default: { | ||
| 305 | + throw new RuntimeException("Only YANG to Java is supported."); | ||
| 306 | + } | ||
| 307 | + } | ||
| 308 | + } | ||
| 249 | } | 309 | } | ... | ... |
This diff is collapsed. Click to expand it.
| ... | @@ -42,6 +42,7 @@ import org.onosproject.yangutils.parser.impl.listeners.EnumerationListener; | ... | @@ -42,6 +42,7 @@ import org.onosproject.yangutils.parser.impl.listeners.EnumerationListener; |
| 42 | import org.onosproject.yangutils.parser.impl.listeners.GroupingListener; | 42 | import org.onosproject.yangutils.parser.impl.listeners.GroupingListener; |
| 43 | import org.onosproject.yangutils.parser.impl.listeners.ImportListener; | 43 | import org.onosproject.yangutils.parser.impl.listeners.ImportListener; |
| 44 | import org.onosproject.yangutils.parser.impl.listeners.IncludeListener; | 44 | import org.onosproject.yangutils.parser.impl.listeners.IncludeListener; |
| 45 | +import org.onosproject.yangutils.parser.impl.listeners.InputListener; | ||
| 45 | import org.onosproject.yangutils.parser.impl.listeners.KeyListener; | 46 | import org.onosproject.yangutils.parser.impl.listeners.KeyListener; |
| 46 | import org.onosproject.yangutils.parser.impl.listeners.LeafListListener; | 47 | import org.onosproject.yangutils.parser.impl.listeners.LeafListListener; |
| 47 | import org.onosproject.yangutils.parser.impl.listeners.LeafListener; | 48 | import org.onosproject.yangutils.parser.impl.listeners.LeafListener; |
| ... | @@ -53,12 +54,14 @@ import org.onosproject.yangutils.parser.impl.listeners.ModuleListener; | ... | @@ -53,12 +54,14 @@ import org.onosproject.yangutils.parser.impl.listeners.ModuleListener; |
| 53 | import org.onosproject.yangutils.parser.impl.listeners.NotificationListener; | 54 | import org.onosproject.yangutils.parser.impl.listeners.NotificationListener; |
| 54 | import org.onosproject.yangutils.parser.impl.listeners.NamespaceListener; | 55 | import org.onosproject.yangutils.parser.impl.listeners.NamespaceListener; |
| 55 | import org.onosproject.yangutils.parser.impl.listeners.OrganizationListener; | 56 | import org.onosproject.yangutils.parser.impl.listeners.OrganizationListener; |
| 57 | +import org.onosproject.yangutils.parser.impl.listeners.OutputListener; | ||
| 56 | import org.onosproject.yangutils.parser.impl.listeners.PositionListener; | 58 | import org.onosproject.yangutils.parser.impl.listeners.PositionListener; |
| 57 | import org.onosproject.yangutils.parser.impl.listeners.PrefixListener; | 59 | import org.onosproject.yangutils.parser.impl.listeners.PrefixListener; |
| 58 | import org.onosproject.yangutils.parser.impl.listeners.PresenceListener; | 60 | import org.onosproject.yangutils.parser.impl.listeners.PresenceListener; |
| 59 | import org.onosproject.yangutils.parser.impl.listeners.ReferenceListener; | 61 | import org.onosproject.yangutils.parser.impl.listeners.ReferenceListener; |
| 60 | import org.onosproject.yangutils.parser.impl.listeners.RevisionDateListener; | 62 | import org.onosproject.yangutils.parser.impl.listeners.RevisionDateListener; |
| 61 | import org.onosproject.yangutils.parser.impl.listeners.RevisionListener; | 63 | import org.onosproject.yangutils.parser.impl.listeners.RevisionListener; |
| 64 | +import org.onosproject.yangutils.parser.impl.listeners.RpcListener; | ||
| 62 | import org.onosproject.yangutils.parser.impl.listeners.ShortCaseListener; | 65 | import org.onosproject.yangutils.parser.impl.listeners.ShortCaseListener; |
| 63 | import org.onosproject.yangutils.parser.impl.listeners.StatusListener; | 66 | import org.onosproject.yangutils.parser.impl.listeners.StatusListener; |
| 64 | import org.onosproject.yangutils.parser.impl.listeners.SubModuleListener; | 67 | import org.onosproject.yangutils.parser.impl.listeners.SubModuleListener; |
| ... | @@ -1062,32 +1065,48 @@ public class TreeWalkListener implements GeneratedYangListener { | ... | @@ -1062,32 +1065,48 @@ public class TreeWalkListener implements GeneratedYangListener { |
| 1062 | 1065 | ||
| 1063 | @Override | 1066 | @Override |
| 1064 | public void enterRpcStatement(GeneratedYangParser.RpcStatementContext ctx) { | 1067 | public void enterRpcStatement(GeneratedYangParser.RpcStatementContext ctx) { |
| 1065 | - // TODO: implement the method. | 1068 | + RpcListener.processRpcEntry(this, ctx); |
| 1066 | } | 1069 | } |
| 1067 | 1070 | ||
| 1068 | @Override | 1071 | @Override |
| 1069 | public void exitRpcStatement(GeneratedYangParser.RpcStatementContext ctx) { | 1072 | public void exitRpcStatement(GeneratedYangParser.RpcStatementContext ctx) { |
| 1070 | - // TODO: implement the method. | 1073 | + RpcListener.processRpcExit(this, ctx); |
| 1071 | } | 1074 | } |
| 1072 | 1075 | ||
| 1073 | @Override | 1076 | @Override |
| 1074 | public void enterInputStatement(GeneratedYangParser.InputStatementContext ctx) { | 1077 | public void enterInputStatement(GeneratedYangParser.InputStatementContext ctx) { |
| 1075 | - // TODO: implement the method. | 1078 | + InputListener.processInputEntry(this, ctx); |
| 1076 | } | 1079 | } |
| 1077 | 1080 | ||
| 1078 | @Override | 1081 | @Override |
| 1079 | public void exitInputStatement(GeneratedYangParser.InputStatementContext ctx) { | 1082 | public void exitInputStatement(GeneratedYangParser.InputStatementContext ctx) { |
| 1080 | - // TODO: implement the method. | 1083 | + InputListener.processInputExit(this, ctx); |
| 1084 | + } | ||
| 1085 | + | ||
| 1086 | + @Override | ||
| 1087 | + public void enterInputStatementBody(GeneratedYangParser.InputStatementBodyContext ctx) { | ||
| 1088 | + } | ||
| 1089 | + | ||
| 1090 | + @Override | ||
| 1091 | + public void exitInputStatementBody(GeneratedYangParser.InputStatementBodyContext ctx) { | ||
| 1081 | } | 1092 | } |
| 1082 | 1093 | ||
| 1083 | @Override | 1094 | @Override |
| 1084 | public void enterOutputStatement(GeneratedYangParser.OutputStatementContext ctx) { | 1095 | public void enterOutputStatement(GeneratedYangParser.OutputStatementContext ctx) { |
| 1085 | - // TODO: implement the method. | 1096 | + OutputListener.processOutputEntry(this, ctx); |
| 1086 | } | 1097 | } |
| 1087 | 1098 | ||
| 1088 | @Override | 1099 | @Override |
| 1089 | public void exitOutputStatement(GeneratedYangParser.OutputStatementContext ctx) { | 1100 | public void exitOutputStatement(GeneratedYangParser.OutputStatementContext ctx) { |
| 1090 | - // TODO: implement the method. | 1101 | + OutputListener.processOutputExit(this, ctx); |
| 1102 | + } | ||
| 1103 | + | ||
| 1104 | + @Override | ||
| 1105 | + public void enterOutputStatementBody(GeneratedYangParser.OutputStatementBodyContext ctx) { | ||
| 1106 | + } | ||
| 1107 | + | ||
| 1108 | + @Override | ||
| 1109 | + public void exitOutputStatementBody(GeneratedYangParser.OutputStatementBodyContext ctx) { | ||
| 1091 | } | 1110 | } |
| 1092 | 1111 | ||
| 1093 | @Override | 1112 | @Override | ... | ... |
utils/yangutils/src/main/java/org/onosproject/yangutils/parser/impl/listeners/InputListener.java
0 → 100644
| 1 | +/* | ||
| 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 | + | ||
| 17 | +package org.onosproject.yangutils.parser.impl.listeners; | ||
| 18 | + | ||
| 19 | +import org.onosproject.yangutils.datamodel.YangInput; | ||
| 20 | +import org.onosproject.yangutils.datamodel.YangNode; | ||
| 21 | +import org.onosproject.yangutils.datamodel.YangRpc; | ||
| 22 | +import org.onosproject.yangutils.datamodel.exceptions.DataModelException; | ||
| 23 | +import org.onosproject.yangutils.parser.Parsable; | ||
| 24 | +import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; | ||
| 25 | +import org.onosproject.yangutils.parser.exceptions.ParserException; | ||
| 26 | +import org.onosproject.yangutils.parser.impl.TreeWalkListener; | ||
| 27 | + | ||
| 28 | +import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION; | ||
| 29 | +import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangInputNode; | ||
| 30 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; | ||
| 31 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; | ||
| 32 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; | ||
| 33 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; | ||
| 34 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; | ||
| 35 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; | ||
| 36 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; | ||
| 37 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; | ||
| 38 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; | ||
| 39 | +import static org.onosproject.yangutils.utils.YangConstructType.INPUT_DATA; | ||
| 40 | + | ||
| 41 | +/* | ||
| 42 | + * Reference: RFC6020 and YANG ANTLR Grammar | ||
| 43 | + * | ||
| 44 | + * ABNF grammar as per RFC6020 | ||
| 45 | + * | ||
| 46 | + * input-stmt = input-keyword optsep | ||
| 47 | + * "{" stmtsep | ||
| 48 | + * ;; these stmts can appear in any order | ||
| 49 | + * *((typedef-stmt / | ||
| 50 | + * grouping-stmt) stmtsep) | ||
| 51 | + * 1*(data-def-stmt stmtsep) | ||
| 52 | + * "}" | ||
| 53 | + * | ||
| 54 | + * inputStatement : INPUT_KEYWORD LEFT_CURLY_BRACE inputStatementBody RIGHT_CURLY_BRACE; | ||
| 55 | + | ||
| 56 | + * inputStatementBody : typedefStatement* dataDefStatement+ | ||
| 57 | + * | dataDefStatement+ typedefStatement* | ||
| 58 | + * | groupingStatement* dataDefStatement+ | ||
| 59 | + * | dataDefStatement+ groupingStatement*; | ||
| 60 | + */ | ||
| 61 | + | ||
| 62 | +/** | ||
| 63 | + * Implements listener based call back function corresponding to the "input" | ||
| 64 | + * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. | ||
| 65 | + */ | ||
| 66 | +public final class InputListener { | ||
| 67 | + | ||
| 68 | + private static final String INPUT_KEYWORD = "Input"; | ||
| 69 | + | ||
| 70 | + /** | ||
| 71 | + * Creates a new input listener. | ||
| 72 | + */ | ||
| 73 | + private InputListener() { | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + /** | ||
| 77 | + * It is called when parser receives an input matching the grammar rule | ||
| 78 | + * (input), performs validation and updates the data model tree. | ||
| 79 | + * | ||
| 80 | + * @param listener listener's object | ||
| 81 | + * @param ctx context object of the grammar rule | ||
| 82 | + */ | ||
| 83 | + public static void processInputEntry(TreeWalkListener listener, | ||
| 84 | + GeneratedYangParser.InputStatementContext ctx) { | ||
| 85 | + | ||
| 86 | + // Check for stack to be non empty. | ||
| 87 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, INPUT_DATA, "", ENTRY); | ||
| 88 | + | ||
| 89 | + Parsable curData = listener.getParsedDataStack().peek(); | ||
| 90 | + if (curData instanceof YangRpc) { | ||
| 91 | + | ||
| 92 | + YangInput yangInput = getYangInputNode(JAVA_GENERATION); | ||
| 93 | + yangInput.setName(((YangRpc) curData).getName() + INPUT_KEYWORD); | ||
| 94 | + YangNode curNode = (YangNode) curData; | ||
| 95 | + try { | ||
| 96 | + curNode.addChild(yangInput); | ||
| 97 | + } catch (DataModelException e) { | ||
| 98 | + throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, | ||
| 99 | + INPUT_DATA, "", ENTRY, e.getMessage())); | ||
| 100 | + } | ||
| 101 | + listener.getParsedDataStack().push(yangInput); | ||
| 102 | + } else { | ||
| 103 | + throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, INPUT_DATA, | ||
| 104 | + "", ENTRY)); | ||
| 105 | + } | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + /** | ||
| 109 | + * It is called when parser exits from grammar rule (input), it perform | ||
| 110 | + * validations and updates the data model tree. | ||
| 111 | + * | ||
| 112 | + * @param listener listener's object | ||
| 113 | + * @param ctx context object of the grammar rule | ||
| 114 | + */ | ||
| 115 | + public static void processInputExit(TreeWalkListener listener, | ||
| 116 | + GeneratedYangParser.InputStatementContext ctx) { | ||
| 117 | + | ||
| 118 | + // Check for stack to be non empty. | ||
| 119 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, INPUT_DATA, "", EXIT); | ||
| 120 | + | ||
| 121 | + if (!(listener.getParsedDataStack().peek() instanceof YangInput)) { | ||
| 122 | + throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, INPUT_DATA, | ||
| 123 | + "", EXIT)); | ||
| 124 | + } | ||
| 125 | + listener.getParsedDataStack().pop(); | ||
| 126 | + } | ||
| 127 | +} |
utils/yangutils/src/main/java/org/onosproject/yangutils/parser/impl/listeners/OutputListener.java
0 → 100644
| 1 | +/* | ||
| 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 | + | ||
| 17 | +package org.onosproject.yangutils.parser.impl.listeners; | ||
| 18 | + | ||
| 19 | +import org.onosproject.yangutils.datamodel.YangNode; | ||
| 20 | +import org.onosproject.yangutils.datamodel.YangOutput; | ||
| 21 | +import org.onosproject.yangutils.datamodel.YangRpc; | ||
| 22 | +import org.onosproject.yangutils.datamodel.exceptions.DataModelException; | ||
| 23 | +import org.onosproject.yangutils.parser.Parsable; | ||
| 24 | +import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; | ||
| 25 | +import org.onosproject.yangutils.parser.exceptions.ParserException; | ||
| 26 | +import org.onosproject.yangutils.parser.impl.TreeWalkListener; | ||
| 27 | + | ||
| 28 | +import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION; | ||
| 29 | +import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangOutputNode; | ||
| 30 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; | ||
| 31 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; | ||
| 32 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; | ||
| 33 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; | ||
| 34 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.*; | ||
| 35 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; | ||
| 36 | +import static org.onosproject.yangutils.utils.YangConstructType.OUTPUT_DATA; | ||
| 37 | + | ||
| 38 | +/* | ||
| 39 | + * Reference: RFC6020 and YANG ANTLR Grammar | ||
| 40 | + * | ||
| 41 | + * ABNF grammar as per RFC6020 | ||
| 42 | + * | ||
| 43 | + * output-stmt = output-keyword optsep | ||
| 44 | + * "{" stmtsep | ||
| 45 | + * ;; these stmts can appear in any order | ||
| 46 | + * *((typedef-stmt / | ||
| 47 | + * grouping-stmt) stmtsep) | ||
| 48 | + * 1*(data-def-stmt stmtsep) | ||
| 49 | + * "}" | ||
| 50 | + * | ||
| 51 | + * outputStatement : OUTPUT_KEYWORD LEFT_CURLY_BRACE outputStatementBody RIGHT_CURLY_BRACE; | ||
| 52 | + | ||
| 53 | + * outputStatementBody : typedefStatement* dataDefStatement+ | ||
| 54 | + * | dataDefStatement+ typedefStatement* | ||
| 55 | + * | groupingStatement* dataDefStatement+ | ||
| 56 | + * | dataDefStatement+ groupingStatement*; | ||
| 57 | + */ | ||
| 58 | + | ||
| 59 | +/** | ||
| 60 | + * Implements listener based call back function corresponding to the "output" | ||
| 61 | + * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. | ||
| 62 | + */ | ||
| 63 | +public final class OutputListener { | ||
| 64 | + | ||
| 65 | + private static final String OUTPUT_KEYWORD = "Output"; | ||
| 66 | + | ||
| 67 | + /** | ||
| 68 | + * Creates a new output listener. | ||
| 69 | + */ | ||
| 70 | + private OutputListener() { | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + /** | ||
| 74 | + * It is called when parser receives an input matching the grammar rule | ||
| 75 | + * (output), performs validation and updates the data model tree. | ||
| 76 | + * | ||
| 77 | + * @param listener listener's object | ||
| 78 | + * @param ctx context object of the grammar rule | ||
| 79 | + */ | ||
| 80 | + public static void processOutputEntry(TreeWalkListener listener, | ||
| 81 | + GeneratedYangParser.OutputStatementContext ctx) { | ||
| 82 | + | ||
| 83 | + // Check for stack to be non empty. | ||
| 84 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, OUTPUT_DATA, "", ENTRY); | ||
| 85 | + | ||
| 86 | + Parsable curData = listener.getParsedDataStack().peek(); | ||
| 87 | + if (curData instanceof YangRpc) { | ||
| 88 | + | ||
| 89 | + YangOutput yangOutput = getYangOutputNode(JAVA_GENERATION); | ||
| 90 | + yangOutput.setName(((YangRpc) curData).getName() + OUTPUT_KEYWORD); | ||
| 91 | + YangNode curNode = (YangNode) curData; | ||
| 92 | + try { | ||
| 93 | + curNode.addChild(yangOutput); | ||
| 94 | + } catch (DataModelException e) { | ||
| 95 | + throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, | ||
| 96 | + OUTPUT_DATA, "", ENTRY, e.getMessage())); | ||
| 97 | + } | ||
| 98 | + listener.getParsedDataStack().push(yangOutput); | ||
| 99 | + } else { | ||
| 100 | + throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, OUTPUT_DATA, | ||
| 101 | + "", ENTRY)); | ||
| 102 | + } | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + /** | ||
| 106 | + * It is called when parser exits from grammar rule (output), it perform | ||
| 107 | + * validations and updates the data model tree. | ||
| 108 | + * | ||
| 109 | + * @param listener listener's object | ||
| 110 | + * @param ctx context object of the grammar rule | ||
| 111 | + */ | ||
| 112 | + public static void processOutputExit(TreeWalkListener listener, | ||
| 113 | + GeneratedYangParser.OutputStatementContext ctx) { | ||
| 114 | + | ||
| 115 | + // Check for stack to be non empty. | ||
| 116 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, OUTPUT_DATA, "", EXIT); | ||
| 117 | + | ||
| 118 | + if (!(listener.getParsedDataStack().peek() instanceof YangOutput)) { | ||
| 119 | + throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, OUTPUT_DATA, | ||
| 120 | + "", EXIT)); | ||
| 121 | + } | ||
| 122 | + listener.getParsedDataStack().pop(); | ||
| 123 | + } | ||
| 124 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
utils/yangutils/src/main/java/org/onosproject/yangutils/parser/impl/listeners/RpcListener.java
0 → 100644
| 1 | +/* | ||
| 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 | + | ||
| 17 | +package org.onosproject.yangutils.parser.impl.listeners; | ||
| 18 | + | ||
| 19 | +import org.onosproject.yangutils.datamodel.YangNode; | ||
| 20 | +import org.onosproject.yangutils.datamodel.YangRpc; | ||
| 21 | +import org.onosproject.yangutils.datamodel.YangModule; | ||
| 22 | +import org.onosproject.yangutils.datamodel.YangSubModule; | ||
| 23 | +import org.onosproject.yangutils.datamodel.exceptions.DataModelException; | ||
| 24 | +import org.onosproject.yangutils.parser.Parsable; | ||
| 25 | +import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; | ||
| 26 | +import org.onosproject.yangutils.parser.exceptions.ParserException; | ||
| 27 | +import org.onosproject.yangutils.parser.impl.TreeWalkListener; | ||
| 28 | + | ||
| 29 | +import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION; | ||
| 30 | +import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangRpcNode; | ||
| 31 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil; | ||
| 32 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; | ||
| 33 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; | ||
| 34 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; | ||
| 35 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; | ||
| 36 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.*; | ||
| 37 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier; | ||
| 38 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; | ||
| 39 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne; | ||
| 40 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateMutuallyExclusiveChilds; | ||
| 41 | +import static org.onosproject.yangutils.utils.YangConstructType.RPC_DATA; | ||
| 42 | +import static org.onosproject.yangutils.utils.YangConstructType.INPUT_DATA; | ||
| 43 | +import static org.onosproject.yangutils.utils.YangConstructType.OUTPUT_DATA; | ||
| 44 | +import static org.onosproject.yangutils.utils.YangConstructType.TYPEDEF_DATA; | ||
| 45 | +import static org.onosproject.yangutils.utils.YangConstructType.GROUPING_DATA; | ||
| 46 | +import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA; | ||
| 47 | +import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA; | ||
| 48 | +import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA; | ||
| 49 | + | ||
| 50 | +/* | ||
| 51 | + * Reference: RFC6020 and YANG ANTLR Grammar | ||
| 52 | + * | ||
| 53 | + * ABNF grammar as per RFC6020 | ||
| 54 | + * rpc-stmt = rpc-keyword sep identifier-arg-str optsep | ||
| 55 | + * (";" / | ||
| 56 | + * "{" stmtsep | ||
| 57 | + * ;; these stmts can appear in any order | ||
| 58 | + * *(if-feature-stmt stmtsep) | ||
| 59 | + * [status-stmt stmtsep] | ||
| 60 | + * [description-stmt stmtsep] | ||
| 61 | + * [reference-stmt stmtsep] | ||
| 62 | + * *((typedef-stmt / | ||
| 63 | + * grouping-stmt) stmtsep) | ||
| 64 | + * [input-stmt stmtsep] | ||
| 65 | + * [output-stmt stmtsep] | ||
| 66 | + * "}") | ||
| 67 | + * | ||
| 68 | + * ANTLR grammar rule | ||
| 69 | + * rpcStatement : RPC_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE (ifFeatureStatement | statusStatement | ||
| 70 | + * | descriptionStatement | referenceStatement | typedefStatement | groupingStatement | inputStatement | ||
| 71 | + * | outputStatement)* RIGHT_CURLY_BRACE); | ||
| 72 | + */ | ||
| 73 | + | ||
| 74 | +/** | ||
| 75 | + * Implements listener based call back function corresponding to the "rpc" | ||
| 76 | + * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. | ||
| 77 | + */ | ||
| 78 | +public final class RpcListener { | ||
| 79 | + | ||
| 80 | + /** | ||
| 81 | + * Creates a new rpc listener. | ||
| 82 | + */ | ||
| 83 | + private RpcListener() { | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + /** | ||
| 87 | + * It is called when parser receives an input matching the grammar rule | ||
| 88 | + * (rpc), performs validation and updates the data model tree. | ||
| 89 | + * | ||
| 90 | + * @param listener listener's object | ||
| 91 | + * @param ctx context object of the grammar rule | ||
| 92 | + */ | ||
| 93 | + public static void processRpcEntry(TreeWalkListener listener, | ||
| 94 | + GeneratedYangParser.RpcStatementContext ctx) { | ||
| 95 | + | ||
| 96 | + // Check for stack to be non empty. | ||
| 97 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, RPC_DATA, ctx.identifier().getText(), ENTRY); | ||
| 98 | + | ||
| 99 | + String identifier = getValidIdentifier(ctx.identifier().getText(), RPC_DATA, ctx); | ||
| 100 | + | ||
| 101 | + // Validate sub statement cardinality. | ||
| 102 | + validateSubStatementsCardinality(ctx); | ||
| 103 | + | ||
| 104 | + // Check for identifier collision | ||
| 105 | + int line = ctx.getStart().getLine(); | ||
| 106 | + int charPositionInLine = ctx.getStart().getCharPositionInLine(); | ||
| 107 | + detectCollidingChildUtil(listener, line, charPositionInLine, identifier, RPC_DATA); | ||
| 108 | + | ||
| 109 | + Parsable curData = listener.getParsedDataStack().peek(); | ||
| 110 | + if (curData instanceof YangModule || curData instanceof YangSubModule) { | ||
| 111 | + | ||
| 112 | + YangNode curNode = (YangNode) curData; | ||
| 113 | + YangRpc yangRpc = getYangRpcNode(JAVA_GENERATION); | ||
| 114 | + yangRpc.setName(identifier); | ||
| 115 | + try { | ||
| 116 | + curNode.addChild(yangRpc); | ||
| 117 | + } catch (DataModelException e) { | ||
| 118 | + throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, | ||
| 119 | + RPC_DATA, ctx.identifier().getText(), ENTRY, e.getMessage())); | ||
| 120 | + } | ||
| 121 | + listener.getParsedDataStack().push(yangRpc); | ||
| 122 | + } else { | ||
| 123 | + throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, RPC_DATA, | ||
| 124 | + ctx.identifier().getText(), ENTRY)); | ||
| 125 | + } | ||
| 126 | + } | ||
| 127 | + | ||
| 128 | + /** | ||
| 129 | + * It is called when parser exits from grammar rule (rpc), it perform | ||
| 130 | + * validations and updates the data model tree. | ||
| 131 | + * | ||
| 132 | + * @param listener listener's object | ||
| 133 | + * @param ctx context object of the grammar rule | ||
| 134 | + */ | ||
| 135 | + public static void processRpcExit(TreeWalkListener listener, | ||
| 136 | + GeneratedYangParser.RpcStatementContext ctx) { | ||
| 137 | + | ||
| 138 | + //Check for stack to be non empty. | ||
| 139 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, RPC_DATA, ctx.identifier().getText(), EXIT); | ||
| 140 | + | ||
| 141 | + if (!(listener.getParsedDataStack().peek() instanceof YangRpc)) { | ||
| 142 | + throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, RPC_DATA, | ||
| 143 | + ctx.identifier().getText(), EXIT)); | ||
| 144 | + } | ||
| 145 | + listener.getParsedDataStack().pop(); | ||
| 146 | + } | ||
| 147 | + | ||
| 148 | + /** | ||
| 149 | + * Validates the cardinality of rpc sub-statements as per grammar. | ||
| 150 | + * | ||
| 151 | + * @param ctx context object of the grammar rule | ||
| 152 | + */ | ||
| 153 | + private static void validateSubStatementsCardinality(GeneratedYangParser.RpcStatementContext ctx) { | ||
| 154 | + | ||
| 155 | + validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, RPC_DATA, ctx.identifier().getText()); | ||
| 156 | + validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, RPC_DATA, ctx.identifier().getText()); | ||
| 157 | + validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, RPC_DATA, ctx.identifier().getText()); | ||
| 158 | + validateCardinalityMaxOne(ctx.inputStatement(), INPUT_DATA, RPC_DATA, ctx.identifier().getText()); | ||
| 159 | + validateCardinalityMaxOne(ctx.outputStatement(), OUTPUT_DATA, RPC_DATA, ctx.identifier().getText()); | ||
| 160 | + validateMutuallyExclusiveChilds(ctx.typedefStatement(), TYPEDEF_DATA, ctx.groupingStatement(), GROUPING_DATA, | ||
| 161 | + RPC_DATA, ctx.identifier().getText()); | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | +} |
utils/yangutils/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/YangJavaRpc.java
0 → 100644
| 1 | +/* | ||
| 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 | + | ||
| 17 | +package org.onosproject.yangutils.translator.tojava.javamodel; | ||
| 18 | + | ||
| 19 | +import java.io.IOException; | ||
| 20 | +import org.onosproject.yangutils.datamodel.YangRpc; | ||
| 21 | +import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; | ||
| 22 | + | ||
| 23 | +/** | ||
| 24 | + * Rpc information extended to support java code generation. | ||
| 25 | + */ | ||
| 26 | +public class YangJavaRpc extends YangRpc implements JavaCodeGenerator { | ||
| 27 | + | ||
| 28 | + /** | ||
| 29 | + * Creates an instance of java Rpc. | ||
| 30 | + */ | ||
| 31 | + public YangJavaRpc() { | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + /** | ||
| 35 | + * Prepare the information for java code generation corresponding to YANG | ||
| 36 | + * rpc info. | ||
| 37 | + * | ||
| 38 | + * @param codeGenDir code generation directory | ||
| 39 | + * @throws IOException IO operation fail | ||
| 40 | + */ | ||
| 41 | + @Override | ||
| 42 | + public void generateCodeEntry(String codeGenDir) throws IOException { | ||
| 43 | + // TODO | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + /** | ||
| 47 | + * Create a java file using the YANG rpc info. | ||
| 48 | + * | ||
| 49 | + * @throws IOException IO operation fail | ||
| 50 | + */ | ||
| 51 | + @Override | ||
| 52 | + public void generateCodeExit() throws IOException { | ||
| 53 | + // TODO | ||
| 54 | + } | ||
| 55 | +} |
| ... | @@ -1085,7 +1085,6 @@ package org.onosproject.yangutils.parser.antlrgencode; | ... | @@ -1085,7 +1085,6 @@ package org.onosproject.yangutils.parser.antlrgencode; |
| 1085 | * [input-stmt stmtsep] | 1085 | * [input-stmt stmtsep] |
| 1086 | * [output-stmt stmtsep] | 1086 | * [output-stmt stmtsep] |
| 1087 | * "}") | 1087 | * "}") |
| 1088 | - * TODO : 0..1 occurance to be checked in listener | ||
| 1089 | */ | 1088 | */ |
| 1090 | rpcStatement : RPC_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE (ifFeatureStatement | statusStatement | descriptionStatement | 1089 | rpcStatement : RPC_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE (ifFeatureStatement | statusStatement | descriptionStatement |
| 1091 | | referenceStatement | typedefStatement | groupingStatement | inputStatement | outputStatement)* RIGHT_CURLY_BRACE); | 1090 | | referenceStatement | typedefStatement | groupingStatement | inputStatement | outputStatement)* RIGHT_CURLY_BRACE); |
| ... | @@ -1099,9 +1098,12 @@ package org.onosproject.yangutils.parser.antlrgencode; | ... | @@ -1099,9 +1098,12 @@ package org.onosproject.yangutils.parser.antlrgencode; |
| 1099 | * 1*(data-def-stmt stmtsep) | 1098 | * 1*(data-def-stmt stmtsep) |
| 1100 | * "}" | 1099 | * "}" |
| 1101 | */ | 1100 | */ |
| 1102 | - inputStatement : INPUT_KEYWORD LEFT_CURLY_BRACE | 1101 | + inputStatement : INPUT_KEYWORD LEFT_CURLY_BRACE inputStatementBody RIGHT_CURLY_BRACE; |
| 1103 | - ((typedefStatement | groupingStatement)* | dataDefStatement+) | 1102 | + |
| 1104 | - | (dataDefStatement+ | (typedefStatement | groupingStatement)*)RIGHT_CURLY_BRACE; | 1103 | + inputStatementBody : typedefStatement* dataDefStatement+ |
| 1104 | + | dataDefStatement+ typedefStatement* | ||
| 1105 | + | groupingStatement* dataDefStatement+ | ||
| 1106 | + | dataDefStatement+ groupingStatement*; | ||
| 1105 | 1107 | ||
| 1106 | /** | 1108 | /** |
| 1107 | * output-stmt = output-keyword optsep | 1109 | * output-stmt = output-keyword optsep |
| ... | @@ -1112,9 +1114,12 @@ package org.onosproject.yangutils.parser.antlrgencode; | ... | @@ -1112,9 +1114,12 @@ package org.onosproject.yangutils.parser.antlrgencode; |
| 1112 | * 1*(data-def-stmt stmtsep) | 1114 | * 1*(data-def-stmt stmtsep) |
| 1113 | * "}" | 1115 | * "}" |
| 1114 | */ | 1116 | */ |
| 1115 | - outputStatement : OUTPUT_KEYWORD LEFT_CURLY_BRACE | 1117 | + outputStatement : OUTPUT_KEYWORD LEFT_CURLY_BRACE outputStatementBody RIGHT_CURLY_BRACE; |
| 1116 | - ((typedefStatement | groupingStatement)* | dataDefStatement+) | 1118 | + |
| 1117 | - | (dataDefStatement+ | (typedefStatement | groupingStatement)*)RIGHT_CURLY_BRACE; | 1119 | + outputStatementBody : typedefStatement* dataDefStatement+ |
| 1120 | + | dataDefStatement+ typedefStatement* | ||
| 1121 | + | groupingStatement* dataDefStatement+ | ||
| 1122 | + | dataDefStatement+ groupingStatement*; | ||
| 1118 | 1123 | ||
| 1119 | /** | 1124 | /** |
| 1120 | * notification-stmt = notification-keyword sep | 1125 | * notification-stmt = notification-keyword sep | ... | ... |
utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/InputListenerTest.java
0 → 100644
| 1 | +/* | ||
| 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 | + | ||
| 17 | +package org.onosproject.yangutils.parser.impl.listeners; | ||
| 18 | + | ||
| 19 | +import java.io.IOException; | ||
| 20 | +import java.util.ListIterator; | ||
| 21 | + | ||
| 22 | +import org.junit.Test; | ||
| 23 | +import org.onosproject.yangutils.datamodel.YangModule; | ||
| 24 | +import org.onosproject.yangutils.datamodel.YangNode; | ||
| 25 | +import org.onosproject.yangutils.datamodel.YangNodeType; | ||
| 26 | +import org.onosproject.yangutils.datamodel.YangRpc; | ||
| 27 | +import org.onosproject.yangutils.datamodel.YangInput; | ||
| 28 | +import org.onosproject.yangutils.datamodel.YangLeaf; | ||
| 29 | +import org.onosproject.yangutils.datamodel.YangList; | ||
| 30 | +import org.onosproject.yangutils.datamodel.YangContainer; | ||
| 31 | +import org.onosproject.yangutils.datamodel.YangTypeDef; | ||
| 32 | +import org.onosproject.yangutils.datamodel.YangStatusType; | ||
| 33 | +import org.onosproject.yangutils.datamodel.YangDataTypes; | ||
| 34 | +import org.onosproject.yangutils.parser.exceptions.ParserException; | ||
| 35 | +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; | ||
| 36 | + | ||
| 37 | +import static org.hamcrest.core.Is.is; | ||
| 38 | +import static org.junit.Assert.assertThat; | ||
| 39 | + | ||
| 40 | +/** | ||
| 41 | + * Test cases for testing Input listener functionality. | ||
| 42 | + */ | ||
| 43 | +public class InputListenerTest { | ||
| 44 | + | ||
| 45 | + private final YangUtilsParserManager manager = new YangUtilsParserManager(); | ||
| 46 | + | ||
| 47 | + /** | ||
| 48 | + * Checks input statements with data definition statements as sub-statements. | ||
| 49 | + */ | ||
| 50 | + @Test | ||
| 51 | + public void processInputStatementWithDataDefinition() throws IOException, ParserException { | ||
| 52 | + | ||
| 53 | + YangNode node = manager.getDataModel("src/test/resources/InputStatementWithDataDefinition.yang"); | ||
| 54 | + | ||
| 55 | + assertThat((node instanceof YangModule), is(true)); | ||
| 56 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
| 57 | + YangModule yangNode = (YangModule) node; | ||
| 58 | + assertThat(yangNode.getName(), is("rock")); | ||
| 59 | + | ||
| 60 | + YangRpc yangRpc = (YangRpc) yangNode.getChild(); | ||
| 61 | + assertThat(yangRpc.getName(), is("activate-software-image")); | ||
| 62 | + | ||
| 63 | + YangInput yangInput = (YangInput) yangRpc.getChild(); | ||
| 64 | + assertThat(yangInput.getName(), is("activate-software-imageInput")); | ||
| 65 | + ListIterator<YangLeaf> leafIterator = yangInput.getListOfLeaf().listIterator(); | ||
| 66 | + YangLeaf leafInfo = leafIterator.next(); | ||
| 67 | + | ||
| 68 | + assertThat(leafInfo.getLeafName(), is("image-name")); | ||
| 69 | + assertThat(leafInfo.getDataType().getDataTypeName(), is("string")); | ||
| 70 | + | ||
| 71 | + YangList yangList = (YangList) yangInput.getChild(); | ||
| 72 | + assertThat(yangList.getName(), is("ospf")); | ||
| 73 | + assertThat(yangList.getKeyList().contains("invalid-interval"), is(true)); | ||
| 74 | + assertThat(yangList.isConfig(), is(true)); | ||
| 75 | + assertThat(yangList.getMaxElements(), is(10)); | ||
| 76 | + assertThat(yangList.getMinElements(), is(3)); | ||
| 77 | + leafIterator = yangList.getListOfLeaf().listIterator(); | ||
| 78 | + leafInfo = leafIterator.next(); | ||
| 79 | + assertThat(leafInfo.getLeafName(), is("invalid-interval")); | ||
| 80 | + assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); | ||
| 81 | + | ||
| 82 | + YangContainer yangContainer = (YangContainer) yangList.getNextSibling(); | ||
| 83 | + assertThat(yangContainer.getName(), is("isis")); | ||
| 84 | + | ||
| 85 | + leafIterator = yangContainer.getListOfLeaf().listIterator(); | ||
| 86 | + leafInfo = leafIterator.next(); | ||
| 87 | + assertThat(leafInfo.getLeafName(), is("invalid-interval")); | ||
| 88 | + assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + /** | ||
| 92 | + * Checks input statements with type-def statement as sub-statements. | ||
| 93 | + */ | ||
| 94 | + @Test | ||
| 95 | + public void processInputStatementWithTypedef() throws IOException, ParserException { | ||
| 96 | + | ||
| 97 | + YangNode node = manager.getDataModel("src/test/resources/InputStatementWithTypedef.yang"); | ||
| 98 | + YangModule yangNode = (YangModule) node; | ||
| 99 | + assertThat(yangNode.getName(), is("rock")); | ||
| 100 | + | ||
| 101 | + YangRpc yangRpc = (YangRpc) yangNode.getChild(); | ||
| 102 | + assertThat(yangRpc.getName(), is("activate-software-image")); | ||
| 103 | + | ||
| 104 | + YangInput yangInput = (YangInput) yangRpc.getChild(); | ||
| 105 | + assertThat(yangInput.getName(), is("activate-software-imageInput")); | ||
| 106 | + YangTypeDef typeDef = (YangTypeDef) yangInput.getChild(); | ||
| 107 | + assertThat(typeDef.getName(), is("my-type")); | ||
| 108 | + assertThat(typeDef.getStatus(), is(YangStatusType.DEPRECATED)); | ||
| 109 | + assertThat(typeDef.getDataType().getDataType(), is(YangDataTypes.INT32)); | ||
| 110 | + } | ||
| 111 | +} |
| 1 | +/* | ||
| 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 | + | ||
| 17 | +package org.onosproject.yangutils.parser.impl.listeners; | ||
| 18 | + | ||
| 19 | +import java.io.IOException; | ||
| 20 | +import java.util.ListIterator; | ||
| 21 | +import org.junit.Test; | ||
| 22 | + | ||
| 23 | +import org.onosproject.yangutils.datamodel.YangModule; | ||
| 24 | +import org.onosproject.yangutils.datamodel.YangNode; | ||
| 25 | +import org.onosproject.yangutils.datamodel.YangNodeType; | ||
| 26 | +import org.onosproject.yangutils.datamodel.YangRpc; | ||
| 27 | +import org.onosproject.yangutils.datamodel.YangOutput; | ||
| 28 | +import org.onosproject.yangutils.datamodel.YangLeaf; | ||
| 29 | +import org.onosproject.yangutils.datamodel.YangList; | ||
| 30 | +import org.onosproject.yangutils.datamodel.YangContainer; | ||
| 31 | +import org.onosproject.yangutils.datamodel.YangTypeDef; | ||
| 32 | +import org.onosproject.yangutils.datamodel.YangStatusType; | ||
| 33 | +import org.onosproject.yangutils.datamodel.YangDataTypes; | ||
| 34 | +import org.onosproject.yangutils.parser.exceptions.ParserException; | ||
| 35 | +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; | ||
| 36 | + | ||
| 37 | +import static org.hamcrest.core.Is.is; | ||
| 38 | +import static org.junit.Assert.assertThat; | ||
| 39 | + | ||
| 40 | +/** | ||
| 41 | + * Test cases for testing output listener functionality. | ||
| 42 | + */ | ||
| 43 | +public class OutputListenerTest { | ||
| 44 | + | ||
| 45 | + private final YangUtilsParserManager manager = new YangUtilsParserManager(); | ||
| 46 | + | ||
| 47 | + /** | ||
| 48 | + * Checks output statements with data definition statements as sub-statements. | ||
| 49 | + */ | ||
| 50 | + @Test | ||
| 51 | + public void processOutputStatementWithDataDefinition() throws IOException, ParserException { | ||
| 52 | + | ||
| 53 | + YangNode node = manager.getDataModel("src/test/resources/OutputStatementWithDataDefinition.yang"); | ||
| 54 | + | ||
| 55 | + assertThat((node instanceof YangModule), is(true)); | ||
| 56 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
| 57 | + YangModule yangNode = (YangModule) node; | ||
| 58 | + assertThat(yangNode.getName(), is("rock")); | ||
| 59 | + | ||
| 60 | + YangRpc yangRpc = (YangRpc) yangNode.getChild(); | ||
| 61 | + assertThat(yangRpc.getName(), is("activate-software-image")); | ||
| 62 | + | ||
| 63 | + YangOutput yangOutput = (YangOutput) yangRpc.getChild(); | ||
| 64 | + assertThat(yangOutput.getName(), is("activate-software-imageOutput")); | ||
| 65 | + ListIterator<YangLeaf> leafIterator = yangOutput.getListOfLeaf().listIterator(); | ||
| 66 | + YangLeaf leafInfo = leafIterator.next(); | ||
| 67 | + | ||
| 68 | + assertThat(leafInfo.getLeafName(), is("image-name")); | ||
| 69 | + assertThat(leafInfo.getDataType().getDataTypeName(), is("string")); | ||
| 70 | + | ||
| 71 | + YangList yangList = (YangList) yangOutput.getChild(); | ||
| 72 | + assertThat(yangList.getName(), is("ospf")); | ||
| 73 | + assertThat(yangList.getKeyList().contains("invalid-interval"), is(true)); | ||
| 74 | + assertThat(yangList.isConfig(), is(true)); | ||
| 75 | + assertThat(yangList.getMaxElements(), is(10)); | ||
| 76 | + assertThat(yangList.getMinElements(), is(3)); | ||
| 77 | + leafIterator = yangList.getListOfLeaf().listIterator(); | ||
| 78 | + leafInfo = leafIterator.next(); | ||
| 79 | + assertThat(leafInfo.getLeafName(), is("invalid-interval")); | ||
| 80 | + assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); | ||
| 81 | + | ||
| 82 | + YangContainer yangContainer = (YangContainer) yangList.getNextSibling(); | ||
| 83 | + assertThat(yangContainer.getName(), is("isis")); | ||
| 84 | + | ||
| 85 | + leafIterator = yangContainer.getListOfLeaf().listIterator(); | ||
| 86 | + leafInfo = leafIterator.next(); | ||
| 87 | + assertThat(leafInfo.getLeafName(), is("invalid-interval")); | ||
| 88 | + assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + /** | ||
| 92 | + * Checks output statements with type-def statement as sub-statements. | ||
| 93 | + */ | ||
| 94 | + @Test | ||
| 95 | + public void processOutputStatementWithTypedef() throws IOException, ParserException { | ||
| 96 | + | ||
| 97 | + YangNode node = manager.getDataModel("src/test/resources/OutputStatementWithTypedef.yang"); | ||
| 98 | + YangModule yangNode = (YangModule) node; | ||
| 99 | + assertThat(yangNode.getName(), is("rock")); | ||
| 100 | + | ||
| 101 | + YangRpc yangRpc = (YangRpc) yangNode.getChild(); | ||
| 102 | + assertThat(yangRpc.getName(), is("activate-software-image")); | ||
| 103 | + | ||
| 104 | + YangOutput yangOutput = (YangOutput) yangRpc.getChild(); | ||
| 105 | + assertThat(yangOutput.getName(), is("activate-software-imageOutput")); | ||
| 106 | + YangTypeDef typeDef = (YangTypeDef) yangOutput.getChild(); | ||
| 107 | + assertThat(typeDef.getName(), is("my-type")); | ||
| 108 | + assertThat(typeDef.getStatus(), is(YangStatusType.DEPRECATED)); | ||
| 109 | + assertThat(typeDef.getName(), is("my-type")); | ||
| 110 | + assertThat(typeDef.getStatus(), is(YangStatusType.DEPRECATED)); | ||
| 111 | + assertThat(typeDef.getDataType().getDataType(), is(YangDataTypes.INT32)); | ||
| 112 | + } | ||
| 113 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/RpcListenerTest.java
0 → 100644
| 1 | +/* | ||
| 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 | + | ||
| 17 | +package org.onosproject.yangutils.parser.impl.listeners; | ||
| 18 | + | ||
| 19 | +import java.io.IOException; | ||
| 20 | + | ||
| 21 | +import org.junit.Test; | ||
| 22 | +import org.onosproject.yangutils.datamodel.YangNode; | ||
| 23 | +import org.onosproject.yangutils.datamodel.YangModule; | ||
| 24 | +import org.onosproject.yangutils.datamodel.YangRpc; | ||
| 25 | +import org.onosproject.yangutils.datamodel.YangTypeDef; | ||
| 26 | +import org.onosproject.yangutils.datamodel.YangStatusType; | ||
| 27 | +import org.onosproject.yangutils.datamodel.YangNodeType; | ||
| 28 | +import org.onosproject.yangutils.datamodel.YangDataTypes; | ||
| 29 | +import org.onosproject.yangutils.parser.exceptions.ParserException; | ||
| 30 | +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; | ||
| 31 | + | ||
| 32 | +import static org.hamcrest.core.Is.is; | ||
| 33 | +import static org.junit.Assert.assertThat; | ||
| 34 | + | ||
| 35 | +/** | ||
| 36 | + * Test cases for testing Rpc listener functionality. | ||
| 37 | + */ | ||
| 38 | +public class RpcListenerTest { | ||
| 39 | + | ||
| 40 | + private final YangUtilsParserManager manager = new YangUtilsParserManager(); | ||
| 41 | + | ||
| 42 | + /** | ||
| 43 | + * Checks valid rpc statements. | ||
| 44 | + */ | ||
| 45 | + @Test | ||
| 46 | + public void processValidRpcStatement() throws IOException, ParserException { | ||
| 47 | + | ||
| 48 | + YangNode node = manager.getDataModel("src/test/resources/ValidRpcStatement.yang"); | ||
| 49 | + | ||
| 50 | + assertThat((node instanceof YangModule), is(true)); | ||
| 51 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
| 52 | + YangModule yangNode = (YangModule) node; | ||
| 53 | + assertThat(yangNode.getName(), is("rock")); | ||
| 54 | + | ||
| 55 | + YangRpc yangRpc = (YangRpc) yangNode.getChild(); | ||
| 56 | + assertThat(yangRpc.getName(), is("rock-the-house")); | ||
| 57 | + assertThat(yangRpc.getDescription(), is("\"description\"")); | ||
| 58 | + assertThat(yangRpc.getReference(), is("\"reference\"")); | ||
| 59 | + assertThat(yangRpc.getStatus(), is(YangStatusType.CURRENT)); | ||
| 60 | + | ||
| 61 | + YangTypeDef typeDef = (YangTypeDef) yangRpc.getChild(); | ||
| 62 | + assertThat(typeDef.getName(), is("my-type")); | ||
| 63 | + assertThat(typeDef.getStatus(), is(YangStatusType.DEPRECATED)); | ||
| 64 | + assertThat(typeDef.getDataType().getDataType(), is(YangDataTypes.INT32)); | ||
| 65 | + } | ||
| 66 | +} |
| 1 | +module rock { | ||
| 2 | + namespace "http://example.net/rock"; | ||
| 3 | + prefix "rock"; | ||
| 4 | + | ||
| 5 | + rpc activate-software-image { | ||
| 6 | + description "description"; | ||
| 7 | + input { | ||
| 8 | + leaf image-name { | ||
| 9 | + type string; | ||
| 10 | + } | ||
| 11 | + list ospf { | ||
| 12 | + key "invalid-interval"; | ||
| 13 | + config true; | ||
| 14 | + max-elements 10; | ||
| 15 | + min-elements 3; | ||
| 16 | + leaf invalid-interval { | ||
| 17 | + type uint16; | ||
| 18 | + } | ||
| 19 | + } | ||
| 20 | + container isis { | ||
| 21 | + config true; | ||
| 22 | + leaf invalid-interval { | ||
| 23 | + type uint16; | ||
| 24 | + } | ||
| 25 | + } | ||
| 26 | + } | ||
| 27 | + } | ||
| 28 | +} |
| 1 | +module rock { | ||
| 2 | + namespace "http://example.net/rock"; | ||
| 3 | + prefix "rock"; | ||
| 4 | + | ||
| 5 | + rpc activate-software-image { | ||
| 6 | + description "description"; | ||
| 7 | + output { | ||
| 8 | + leaf image-name { | ||
| 9 | + type string; | ||
| 10 | + } | ||
| 11 | + list ospf { | ||
| 12 | + key "invalid-interval"; | ||
| 13 | + config true; | ||
| 14 | + max-elements 10; | ||
| 15 | + min-elements 3; | ||
| 16 | + leaf invalid-interval { | ||
| 17 | + type uint16; | ||
| 18 | + } | ||
| 19 | + } | ||
| 20 | + container isis { | ||
| 21 | + config true; | ||
| 22 | + leaf invalid-interval { | ||
| 23 | + type uint16; | ||
| 24 | + } | ||
| 25 | + } | ||
| 26 | + } | ||
| 27 | + } | ||
| 28 | +} |
| 1 | +module rock { | ||
| 2 | + namespace "http://example.net/rock"; | ||
| 3 | + prefix "rock"; | ||
| 4 | + | ||
| 5 | + rpc rock-the-house { | ||
| 6 | + description "description"; | ||
| 7 | + status current; | ||
| 8 | + reference "reference"; | ||
| 9 | + typedef my-type { | ||
| 10 | + status deprecated; | ||
| 11 | + type int32; | ||
| 12 | + } | ||
| 13 | + input { | ||
| 14 | + leaf zip-code { | ||
| 15 | + type string; | ||
| 16 | + } | ||
| 17 | + } | ||
| 18 | + output { | ||
| 19 | + leaf status { | ||
| 20 | + type string; | ||
| 21 | + } | ||
| 22 | + } | ||
| 23 | + } | ||
| 24 | +} |
-
Please register or login to post a comment