blob: 6bfaf5097d5eec012685c228105798e9349615cc [file] [log] [blame]
Gaurav Agrawal22db16d2016-02-12 16:50:55 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal22db16d2016-02-12 16:50:55 +05303 *
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
Vinod Kumar S08710982016-03-03 19:55:30 +053019import java.net.URI;
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053020import org.onosproject.yangutils.datamodel.YangModule;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053021import org.onosproject.yangutils.datamodel.utils.Parsable;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053022import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053023import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053024import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053025
Bharat saraswalc2d3be12016-06-16 00:29:12 +053026import static org.onosproject.yangutils.datamodel.utils.YangConstructType.NAMESPACE_DATA;
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053027import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +053031import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053032import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
33
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053034/*
35 * Reference: RFC6020 and YANG ANTLR Grammar
36 *
37 * ABNF grammar as per RFC6020
38 * module-header-stmts = ;; these stmts can appear in any order
39 * [yang-version-stmt stmtsep]
40 * namespace-stmt stmtsep
41 * prefix-stmt stmtsep
42 *
43 * namespace-stmt = namespace-keyword sep uri-str optsep stmtend
44 *
45 * ANTLR grammar rule
46 * module_header_statement : yang_version_stmt? namespace_stmt prefix_stmt
47 * | yang_version_stmt? prefix_stmt namespace_stmt
48 * | namespace_stmt yang_version_stmt? prefix_stmt
49 * | namespace_stmt prefix_stmt yang_version_stmt?
50 * | prefix_stmt namespace_stmt yang_version_stmt?
51 * | prefix_stmt yang_version_stmt? namespace_stmt
52 * ;
53 * namespace_stmt : NAMESPACE_KEYWORD string STMTEND;
54 */
55
56/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053057 * Represents listener based call back function corresponding to the "namespace"
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053058 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
59 */
60public final class NamespaceListener {
61
62 /**
63 * Creates a new namespace listener.
64 */
65 private NamespaceListener() {
66 }
67
68 /**
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053069 * It is called when parser receives an input matching the grammar rule
70 * (namespace), perform validations and update the data model tree.
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053071 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053072 * @param listener Listener's object
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +053073 * @param ctx context object of the grammar rule
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053074 */
75 public static void processNamespaceEntry(TreeWalkListener listener,
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +053076 GeneratedYangParser.NamespaceStatementContext ctx) {
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053077
78 // Check for stack to be non empty.
Gaurav Agrawal0520c7c2016-02-21 02:56:46 +053079 checkStackIsNotEmpty(listener, MISSING_HOLDER, NAMESPACE_DATA, ctx.string().getText(), ENTRY);
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053080
Gaurav Agrawal0520c7c2016-02-21 02:56:46 +053081 if (!validateUriValue(ctx.string().getText())) {
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053082 ParserException parserException = new ParserException("YANG file error: Invalid namespace URI");
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053083 parserException.setLine(ctx.string().STRING(0).getSymbol().getLine());
84 parserException.setCharPosition(ctx.string().STRING(0).getSymbol().getCharPositionInLine());
85 throw parserException;
86 }
87
88 // Obtain the node of the stack.
89 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053090 switch (tmpNode.getYangConstructType()) {
Vinod Kumar S08710982016-03-03 19:55:30 +053091 case MODULE_DATA: {
92 YangModule module = (YangModule) tmpNode;
Gaurav Agrawal493e0a82016-09-28 18:30:08 +053093 module.setModuleNamespace(removeQuotesAndHandleConcat(ctx.string().getText()));
Vinod Kumar S08710982016-03-03 19:55:30 +053094 break;
95 }
96 default:
97 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, NAMESPACE_DATA,
98 ctx.string().getText(), ENTRY));
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053099 }
Gaurav Agrawal22db16d2016-02-12 16:50:55 +0530100 }
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530101
102 /**
103 * Validate input URI.
104 *
105 * @param uri input namespace URI
106 * @return validation result
107 */
108 private static boolean validateUriValue(String uri) {
109 uri = uri.replace("\"", "");
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530110 try {
Vinod Kumar S08710982016-03-03 19:55:30 +0530111 URI.create(uri);
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530112 } catch (Exception e1) {
113 return false;
114 }
115 return true;
116 }
Gaurav Agrawal800a8e72016-02-19 12:57:13 +0530117}