Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 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 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 19 | import java.net.URI; |
| 20 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangModule; |
| 22 | import org.onosproject.yangutils.datamodel.YangNameSpace; |
| 23 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 27 | |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.utils.YangConstructType.NAMESPACE_DATA; |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 34 | |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 35 | /* |
| 36 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 37 | * |
| 38 | * ABNF grammar as per RFC6020 |
| 39 | * module-header-stmts = ;; these stmts can appear in any order |
| 40 | * [yang-version-stmt stmtsep] |
| 41 | * namespace-stmt stmtsep |
| 42 | * prefix-stmt stmtsep |
| 43 | * |
| 44 | * namespace-stmt = namespace-keyword sep uri-str optsep stmtend |
| 45 | * |
| 46 | * ANTLR grammar rule |
| 47 | * module_header_statement : yang_version_stmt? namespace_stmt prefix_stmt |
| 48 | * | yang_version_stmt? prefix_stmt namespace_stmt |
| 49 | * | namespace_stmt yang_version_stmt? prefix_stmt |
| 50 | * | namespace_stmt prefix_stmt yang_version_stmt? |
| 51 | * | prefix_stmt namespace_stmt yang_version_stmt? |
| 52 | * | prefix_stmt yang_version_stmt? namespace_stmt |
| 53 | * ; |
| 54 | * namespace_stmt : NAMESPACE_KEYWORD string STMTEND; |
| 55 | */ |
| 56 | |
| 57 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 58 | * Represents listener based call back function corresponding to the "namespace" |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 59 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 60 | */ |
| 61 | public final class NamespaceListener { |
| 62 | |
| 63 | /** |
| 64 | * Creates a new namespace listener. |
| 65 | */ |
| 66 | private NamespaceListener() { |
| 67 | } |
| 68 | |
| 69 | /** |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 70 | * It is called when parser receives an input matching the grammar rule |
| 71 | * (namespace), perform validations and update the data model tree. |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 72 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 73 | * @param listener Listener's object |
| 74 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 75 | */ |
| 76 | public static void processNamespaceEntry(TreeWalkListener listener, |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 77 | GeneratedYangParser.NamespaceStatementContext ctx) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 78 | |
| 79 | // Check for stack to be non empty. |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 80 | checkStackIsNotEmpty(listener, MISSING_HOLDER, NAMESPACE_DATA, ctx.string().getText(), ENTRY); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 81 | |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 82 | if (!validateUriValue(ctx.string().getText())) { |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 83 | ParserException parserException = new ParserException("YANG file error: Invalid namespace URI"); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 84 | parserException.setLine(ctx.string().STRING(0).getSymbol().getLine()); |
| 85 | parserException.setCharPosition(ctx.string().STRING(0).getSymbol().getCharPositionInLine()); |
| 86 | throw parserException; |
| 87 | } |
| 88 | |
| 89 | // Obtain the node of the stack. |
| 90 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 91 | switch (tmpNode.getYangConstructType()) { |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 92 | case MODULE_DATA: { |
| 93 | YangModule module = (YangModule) tmpNode; |
| 94 | YangNameSpace uri = new YangNameSpace(); |
| 95 | uri.setUri(ctx.string().getText()); |
| 96 | module.setNameSpace(uri); |
| 97 | break; |
| 98 | } |
| 99 | default: |
| 100 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, NAMESPACE_DATA, |
| 101 | ctx.string().getText(), ENTRY)); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 102 | } |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 103 | } |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * Validate input URI. |
| 107 | * |
| 108 | * @param uri input namespace URI |
| 109 | * @return validation result |
| 110 | */ |
| 111 | private static boolean validateUriValue(String uri) { |
| 112 | uri = uri.replace("\"", ""); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 113 | try { |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 114 | URI.create(uri); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 115 | } catch (Exception e1) { |
| 116 | return false; |
| 117 | } |
| 118 | return true; |
| 119 | } |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 120 | } |