blob: 53c86cba0d9a18b2d3664e6861057b3123364def [file] [log] [blame]
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +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
Gaurav Agrawala04483c2016-02-13 14:23:40 +053019import org.onosproject.yangutils.datamodel.YangModule;
20import org.onosproject.yangutils.datamodel.YangSubModule;
21import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053022import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053023import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053024import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053025
26import static org.onosproject.yangutils.parser.ParsableDataType.CONTACT_DATA;
27import 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;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053032
33/*
34 * Reference: RFC6020 and YANG ANTLR Grammar
35 *
36 * ABNF grammar as per RFC6020
37 * meta-stmts = ;; these stmts can appear in any order
38 * [organization-stmt stmtsep]
39 * [contact-stmt stmtsep]
40 * [description-stmt stmtsep]
41 * [reference-stmt stmtsep]
Gaurav Agrawala04483c2016-02-13 14:23:40 +053042 * contact-stmt = contact-keyword sep string optsep stmtend
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053043 *
44 * ANTLR grammar rule
45 * meta_stmts : organization_stmt? contact_stmt? description_stmt? reference_stmt?
46 * | organization_stmt? contact_stmt? reference_stmt? description_stmt?
47 * | organization_stmt? description_stmt? contact_stmt? reference_stmt?
48 * | organization_stmt? description_stmt? reference_stmt? contact_stmt?
49 * | organization_stmt? reference_stmt? contact_stmt? description_stmt?
50 * | organization_stmt? reference_stmt? description_stmt? contact_stmt?
51 * | contact_stmt? organization_stmt? description_stmt? reference_stmt?
52 * | contact_stmt? organization_stmt? reference_stmt? description_stmt?
53 * | contact_stmt? reference_stmt? organization_stmt? description_stmt?
54 * | contact_stmt? reference_stmt? description_stmt? organization_stmt?
55 * | contact_stmt? description_stmt? reference_stmt? organization_stmt?
56 * | contact_stmt? description_stmt? organization_stmt? reference_stmt?
57 * | reference_stmt? contact_stmt? organization_stmt? description_stmt?
58 * | reference_stmt? contact_stmt? description_stmt? organization_stmt?
59 * | reference_stmt? organization_stmt? contact_stmt? description_stmt?
60 * | reference_stmt? organization_stmt? description_stmt? contact_stmt?
61 * | reference_stmt? description_stmt? organization_stmt? contact_stmt?
62 * | reference_stmt? description_stmt? contact_stmt? organization_stmt?
63 * | description_stmt? reference_stmt? contact_stmt? organization_stmt?
64 * | description_stmt? reference_stmt? organization_stmt? contact_stmt?
65 * | description_stmt? contact_stmt? reference_stmt? organization_stmt?
66 * | description_stmt? contact_stmt? organization_stmt? reference_stmt?
67 * | description_stmt? organization_stmt? contact_stmt? reference_stmt?
68 * | description_stmt? organization_stmt? reference_stmt? contact_stmt?
69 * ;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053070 * contact_stmt : CONTACT_KEYWORD string STMTEND;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053071 */
72
73/**
74 * Implements listener based call back function corresponding to the "contact"
75 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
76 */
77public final class ContactListener {
78
79 /**
80 * Creates a new contact listener.
81 */
82 private ContactListener() {
83 }
84
85 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053086 * It is called when parser receives an input matching the grammar rule
87 * (contact), perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053088 *
89 * @param listener Listener's object.
90 * @param ctx context object of the grammar rule.
91 */
92 public static void processContactEntry(TreeWalkListener listener, GeneratedYangParser.ContactStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053093
94 // Check for stack to be non empty.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053095 checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTACT_DATA, ctx.string().getText(), ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053096
97 // Obtain the node of the stack.
98 Parsable tmpNode = listener.getParsedDataStack().peek();
99 switch (tmpNode.getParsableDataType()) {
100 case MODULE_DATA: {
101 YangModule module = (YangModule) tmpNode;
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530102 module.setContact(ctx.string().getText());
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530103 break;
104 }
105 case SUB_MODULE_DATA: {
106 YangSubModule subModule = (YangSubModule) tmpNode;
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530107 subModule.setContact(ctx.string().getText());
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530108 break;
109 }
110 default:
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530111 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, CONTACT_DATA,
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530112 ctx.string().getText(), ENTRY));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530113 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530114 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530115}