blob: 19a1e6466cbc9f4c161137f8634f9e0a56ccf0d7 [file] [log] [blame]
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal2cbb9502016-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
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
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053026import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Bharat saraswald9822e92016-04-05 15:13:44 +053031import static org.onosproject.yangutils.utils.YangConstructType.ORGANIZATION_DATA;
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 * organization-stmt = organization-keyword sep string
43 * optsep stmtend
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053044 *
45 * ANTLR grammar rule
46 * meta_stmts : organization_stmt? contact_stmt? description_stmt? reference_stmt?
47 * | organization_stmt? contact_stmt? reference_stmt? description_stmt?
48 * | organization_stmt? description_stmt? contact_stmt? reference_stmt?
49 * | organization_stmt? description_stmt? reference_stmt? contact_stmt?
50 * | organization_stmt? reference_stmt? contact_stmt? description_stmt?
51 * | organization_stmt? reference_stmt? description_stmt? contact_stmt?
52 * | contact_stmt? organization_stmt? description_stmt? reference_stmt?
53 * | contact_stmt? organization_stmt? reference_stmt? description_stmt?
54 * | contact_stmt? reference_stmt? organization_stmt? description_stmt?
55 * | contact_stmt? reference_stmt? description_stmt? organization_stmt?
56 * | contact_stmt? description_stmt? reference_stmt? organization_stmt?
57 * | contact_stmt? description_stmt? organization_stmt? reference_stmt?
58 * | reference_stmt? contact_stmt? organization_stmt? description_stmt?
59 * | reference_stmt? contact_stmt? description_stmt? organization_stmt?
60 * | reference_stmt? organization_stmt? contact_stmt? description_stmt?
61 * | reference_stmt? organization_stmt? description_stmt? contact_stmt?
62 * | reference_stmt? description_stmt? organization_stmt? contact_stmt?
63 * | reference_stmt? description_stmt? contact_stmt? organization_stmt?
64 * | description_stmt? reference_stmt? contact_stmt? organization_stmt?
65 * | description_stmt? reference_stmt? organization_stmt? contact_stmt?
66 * | description_stmt? contact_stmt? reference_stmt? organization_stmt?
67 * | description_stmt? contact_stmt? organization_stmt? reference_stmt?
68 * | description_stmt? organization_stmt? contact_stmt? reference_stmt?
69 * | description_stmt? organization_stmt? reference_stmt? contact_stmt?
70 * ;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053071 * organization_stmt : ORGANIZATION_KEYWORD string STMTEND;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053072 */
73
74/**
Bharat saraswald9822e92016-04-05 15:13:44 +053075 * Represents listener based call back function corresponding to the
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053076 * "organization" rule defined in ANTLR grammar file for corresponding ABNF rule
77 * in RFC 6020.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053078 */
79public final class OrganizationListener {
80
81 /**
82 * Creates a new organization listener.
83 */
84 private OrganizationListener() {
85 }
86
87 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053088 * It is called when parser receives an input matching the grammar rule
89 * (organization), perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053090 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053091 * @param listener Listener's object
92 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053093 */
94 public static void processOrganizationEntry(TreeWalkListener listener,
95 GeneratedYangParser.OrganizationStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053096
97 // Check for stack to be non empty.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053098 checkStackIsNotEmpty(listener, MISSING_HOLDER, ORGANIZATION_DATA, ctx.string().getText(),
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053099 ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530100
101 // Obtain the node of the stack.
102 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530103 switch (tmpNode.getYangConstructType()) {
Bharat saraswald9822e92016-04-05 15:13:44 +0530104 case MODULE_DATA: {
105 YangModule module = (YangModule) tmpNode;
106 module.setOrganization(ctx.string().getText());
107 break;
108 }
109 case SUB_MODULE_DATA: {
110 YangSubModule subModule = (YangSubModule) tmpNode;
111 subModule.setOrganization(ctx.string().getText());
112 break;
113 }
114 default:
115 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, ORGANIZATION_DATA,
116 ctx.string().getText(), ENTRY));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530117 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530118 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530119}