blob: 32bd3aa128cb8d5b72bbaf89ed82c33e2f220b68 [file] [log] [blame]
Vidyashree Rama92fc5562016-02-12 18:44:12 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Rama92fc5562016-02-12 18:44:12 +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
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053019import org.onosproject.yangutils.datamodel.YangDesc;
20import org.onosproject.yangutils.parser.Parsable;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053021import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053022import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053023import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama59071f32016-02-20 19:27:56 +053024
Vidyashree Rama59071f32016-02-20 19:27:56 +053025import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
26import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Bharat saraswald9822e92016-04-05 15:13:44 +053030import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053031
32/*
33 * Reference: RFC6020 and YANG ANTLR Grammar
34 *
35 * ABNF grammar as per RFC6020
36 * description-stmt = description-keyword sep string optsep stmtend
37 *
38 * ANTLR grammar rule
39 * descriptionStatement : DESCRIPTION_KEYWORD string STMTEND;
40 */
41
42/**
Bharat saraswald9822e92016-04-05 15:13:44 +053043 * Represents listener based call back function corresponding to the "description"
Vidyashree Rama92fc5562016-02-12 18:44:12 +053044 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
45 */
46public final class DescriptionListener {
47
48 /**
49 * Creates a new description listener.
50 */
51 private DescriptionListener() {
52 }
53
54 /**
55 * It is called when parser receives an input matching the grammar
56 * rule (description), perform validations and updates the data model
57 * tree.
58 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053059 * @param listener listener's object
60 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +053061 */
62 public static void processDescriptionEntry(TreeWalkListener listener,
63 GeneratedYangParser.DescriptionStatementContext ctx) {
Vidyashree Rama92fc5562016-02-12 18:44:12 +053064
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053065 // Check for stack to be non empty.
Vidyashree Rama59071f32016-02-20 19:27:56 +053066 checkStackIsNotEmpty(listener, MISSING_HOLDER, DESCRIPTION_DATA, ctx.string().getText(), ENTRY);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053067
68 Parsable tmpData = listener.getParsedDataStack().peek();
69 if (tmpData instanceof YangDesc) {
70 YangDesc description = (YangDesc) tmpData;
71 description.setDescription(ctx.string().getText());
72 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +053073 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, DESCRIPTION_DATA,
74 ctx.string().getText(), ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053075 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +053076 }
77}