blob: 7260c1c38268df4de318dd837c270f68dba0bc77 [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.ORGANIZATION_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 * 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/**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053075 * Implements listener based call back function corresponding to the
76 * "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 *
91 * @param listener Listener's object.
92 * @param ctx context object of the grammar rule.
93 */
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 Agrawal02b05d22016-02-19 12:57:13 +053098 checkStackIsNotEmpty(listener, MISSING_HOLDER, ORGANIZATION_DATA, String.valueOf(ctx.string().getText()),
99 ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530100
101 // Obtain the node of the stack.
102 Parsable tmpNode = listener.getParsedDataStack().peek();
103 switch (tmpNode.getParsableDataType()) {
104 case MODULE_DATA: {
105 YangModule module = (YangModule) tmpNode;
106 module.setOrganization(String.valueOf(ctx.string().getText()));
107 break;
108 }
109 case SUB_MODULE_DATA: {
110 YangSubModule subModule = (YangSubModule) tmpNode;
111 subModule.setOrganization(String.valueOf(ctx.string().getText()));
112 break;
113 }
114 default:
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530115 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, ORGANIZATION_DATA,
116 String.valueOf(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}