blob: 48174b58122db2cb1d904d3ce7e49a4777595054 [file] [log] [blame]
Gaurav Agrawal22db16d2016-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 Agrawal2737d2a2016-02-13 14:23:40 +053019import org.onosproject.yangutils.datamodel.YangModule;
20import org.onosproject.yangutils.datamodel.YangSubModule;
21import org.onosproject.yangutils.parser.Parsable;
22import org.onosproject.yangutils.parser.ParsableDataType;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053023import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053024import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053025import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053026import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
27import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
28import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
29import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053030
31/*
32 * Reference: RFC6020 and YANG ANTLR Grammar
33 *
34 * ABNF grammar as per RFC6020
35 * meta-stmts = ;; these stmts can appear in any order
36 * [organization-stmt stmtsep]
37 * [contact-stmt stmtsep]
38 * [description-stmt stmtsep]
39 * [reference-stmt stmtsep]
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053040 * contact-stmt = contact-keyword sep string optsep stmtend
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053041 *
42 * ANTLR grammar rule
43 * meta_stmts : organization_stmt? contact_stmt? description_stmt? reference_stmt?
44 * | organization_stmt? contact_stmt? reference_stmt? description_stmt?
45 * | organization_stmt? description_stmt? contact_stmt? reference_stmt?
46 * | organization_stmt? description_stmt? reference_stmt? contact_stmt?
47 * | organization_stmt? reference_stmt? contact_stmt? description_stmt?
48 * | organization_stmt? reference_stmt? description_stmt? contact_stmt?
49 * | contact_stmt? organization_stmt? description_stmt? reference_stmt?
50 * | contact_stmt? organization_stmt? reference_stmt? description_stmt?
51 * | contact_stmt? reference_stmt? organization_stmt? description_stmt?
52 * | contact_stmt? reference_stmt? description_stmt? organization_stmt?
53 * | contact_stmt? description_stmt? reference_stmt? organization_stmt?
54 * | contact_stmt? description_stmt? organization_stmt? reference_stmt?
55 * | reference_stmt? contact_stmt? organization_stmt? description_stmt?
56 * | reference_stmt? contact_stmt? description_stmt? organization_stmt?
57 * | reference_stmt? organization_stmt? contact_stmt? description_stmt?
58 * | reference_stmt? organization_stmt? description_stmt? contact_stmt?
59 * | reference_stmt? description_stmt? organization_stmt? contact_stmt?
60 * | reference_stmt? description_stmt? contact_stmt? organization_stmt?
61 * | description_stmt? reference_stmt? contact_stmt? organization_stmt?
62 * | description_stmt? reference_stmt? organization_stmt? contact_stmt?
63 * | description_stmt? contact_stmt? reference_stmt? organization_stmt?
64 * | description_stmt? contact_stmt? organization_stmt? reference_stmt?
65 * | description_stmt? organization_stmt? contact_stmt? reference_stmt?
66 * | description_stmt? organization_stmt? reference_stmt? contact_stmt?
67 * ;
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053068 * contact_stmt : CONTACT_KEYWORD string STMTEND;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053069 */
70
71/**
72 * Implements listener based call back function corresponding to the "contact"
73 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
74 */
75public final class ContactListener {
76
77 /**
78 * Creates a new contact listener.
79 */
80 private ContactListener() {
81 }
82
83 /**
84 * It is called when parser receives an input matching the grammar
85 * rule (contact), perform validations and update the data model
86 * tree.
87 *
88 * @param listener Listener's object.
89 * @param ctx context object of the grammar rule.
90 */
91 public static void processContactEntry(TreeWalkListener listener, GeneratedYangParser.ContactStatementContext ctx) {
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053092
93 // Check for stack to be non empty.
94 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
95 ParsableDataType.CONTACT_DATA,
96 String.valueOf(ctx.string().getText()), ListenerErrorLocation.ENTRY);
97
98 // Obtain the node of the stack.
99 Parsable tmpNode = listener.getParsedDataStack().peek();
100 switch (tmpNode.getParsableDataType()) {
101 case MODULE_DATA: {
102 YangModule module = (YangModule) tmpNode;
103 module.setContact(String.valueOf(ctx.string().getText()));
104 break;
105 }
106 case SUB_MODULE_DATA: {
107 YangSubModule subModule = (YangSubModule) tmpNode;
108 subModule.setContact(String.valueOf(ctx.string().getText()));
109 break;
110 }
111 default:
112 throw new ParserException(
113 ListenerErrorMessageConstruction
114 .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
115 ParsableDataType.CONTACT_DATA,
116 String.valueOf(ctx.string().getText()),
117 ListenerErrorLocation.ENTRY));
118 }
Gaurav Agrawal22db16d2016-02-12 16:50:55 +0530119 }
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530120}