blob: edae08e67c09fa7ded54b10357042a48a93fc0e2 [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;
22import org.onosproject.yangutils.parser.ParsableDataType;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053023import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053024import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053025import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawala04483c2016-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 Agrawal2cbb9502016-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 Agrawala04483c2016-02-13 14:23:40 +053040 * organization-stmt = organization-keyword sep string
41 * optsep stmtend
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053042 *
43 * ANTLR grammar rule
44 * meta_stmts : organization_stmt? contact_stmt? description_stmt? reference_stmt?
45 * | organization_stmt? contact_stmt? reference_stmt? description_stmt?
46 * | organization_stmt? description_stmt? contact_stmt? reference_stmt?
47 * | organization_stmt? description_stmt? reference_stmt? contact_stmt?
48 * | organization_stmt? reference_stmt? contact_stmt? description_stmt?
49 * | organization_stmt? reference_stmt? description_stmt? contact_stmt?
50 * | contact_stmt? organization_stmt? description_stmt? reference_stmt?
51 * | contact_stmt? organization_stmt? reference_stmt? description_stmt?
52 * | contact_stmt? reference_stmt? organization_stmt? description_stmt?
53 * | contact_stmt? reference_stmt? description_stmt? organization_stmt?
54 * | contact_stmt? description_stmt? reference_stmt? organization_stmt?
55 * | contact_stmt? description_stmt? organization_stmt? reference_stmt?
56 * | reference_stmt? contact_stmt? organization_stmt? description_stmt?
57 * | reference_stmt? contact_stmt? description_stmt? organization_stmt?
58 * | reference_stmt? organization_stmt? contact_stmt? description_stmt?
59 * | reference_stmt? organization_stmt? description_stmt? contact_stmt?
60 * | reference_stmt? description_stmt? organization_stmt? contact_stmt?
61 * | reference_stmt? description_stmt? contact_stmt? organization_stmt?
62 * | description_stmt? reference_stmt? contact_stmt? organization_stmt?
63 * | description_stmt? reference_stmt? organization_stmt? contact_stmt?
64 * | description_stmt? contact_stmt? reference_stmt? organization_stmt?
65 * | description_stmt? contact_stmt? organization_stmt? reference_stmt?
66 * | description_stmt? organization_stmt? contact_stmt? reference_stmt?
67 * | description_stmt? organization_stmt? reference_stmt? contact_stmt?
68 * ;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053069 * organization_stmt : ORGANIZATION_KEYWORD string STMTEND;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053070 */
71
72/**
73 * Implements listener based call back function corresponding to the "organization"
74 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
75 */
76public final class OrganizationListener {
77
78 /**
79 * Creates a new organization listener.
80 */
81 private OrganizationListener() {
82 }
83
84 /**
85 * It is called when parser receives an input matching the grammar
86 * rule (organization), perform validations and update the data model
87 * tree.
88 *
89 * @param listener Listener's object.
90 * @param ctx context object of the grammar rule.
91 */
92 public static void processOrganizationEntry(TreeWalkListener listener,
93 GeneratedYangParser.OrganizationStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053094
95 // Check for stack to be non empty.
96 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
97 ParsableDataType.ORGANIZATION_DATA,
98 String.valueOf(ctx.string().getText()), ListenerErrorLocation.ENTRY);
99
100 // Obtain the node of the stack.
101 Parsable tmpNode = listener.getParsedDataStack().peek();
102 switch (tmpNode.getParsableDataType()) {
103 case MODULE_DATA: {
104 YangModule module = (YangModule) tmpNode;
105 module.setOrganization(String.valueOf(ctx.string().getText()));
106 break;
107 }
108 case SUB_MODULE_DATA: {
109 YangSubModule subModule = (YangSubModule) tmpNode;
110 subModule.setOrganization(String.valueOf(ctx.string().getText()));
111 break;
112 }
113 default:
114 throw new ParserException(
115 ListenerErrorMessageConstruction
116 .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
117 ParsableDataType.ORGANIZATION_DATA,
118 String.valueOf(ctx.string().getText()),
119 ListenerErrorLocation.ENTRY));
120 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530121 }
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530122}