blob: c52f3e61521565d6868c865a6eb032538f75c9a3 [file] [log] [blame]
Vidyashree Rama92fc5562016-02-12 18:44:12 +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
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053019import org.onosproject.yangutils.datamodel.YangStatus;
20import org.onosproject.yangutils.datamodel.YangStatusType;
21import org.onosproject.yangutils.parser.Parsable;
22import org.onosproject.yangutils.parser.ParsableDataType;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053023import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053024import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053025import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +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;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053030
31/*
32 * Reference: RFC6020 and YANG ANTLR Grammar
33 *
34 * ABNF grammar as per RFC6020
35 * status-stmt = status-keyword sep status-arg-str stmtend
36 * status-arg-str = < a string that matches the rule
37 * status-arg >
38 * status-arg = current-keyword /
39 * obsolete-keyword /
40 * deprecated-keyword
41 *
42 * ANTLR grammar rule
43 * statusStatement : STATUS_KEYWORD (CURRENT_KEYWORD | OBSOLETE_KEYWORD | DEPRECATED_KEYWORD) STMTEND;
44 */
45
46/**
47 * Implements listener based call back function corresponding to the "status"
48 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
49 */
50public final class StatusListener {
51
52 /**
53 * Creates a new status listener.
54 */
55 private StatusListener() {
56 }
57
58 /**
59 * It is called when parser receives an input matching the grammar
60 * rule (status), performs validation and updates the data model
61 * tree.
62 *
63 * @param listener listener's object.
64 * @param ctx context object of the grammar rule.
65 */
66 public static void processStatusEntry(TreeWalkListener listener,
67 GeneratedYangParser.StatusStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053068 YangStatusType status;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053069
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053070 // Check for stack to be non empty.
71 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
72 ParsableDataType.STATUS_DATA, "", ListenerErrorLocation.ENTRY);
73
74 if (ctx.CURRENT_KEYWORD() != null) {
75 status = YangStatusType.CURRENT.CURRENT;
76 } else if (ctx.DEPRECATED_KEYWORD() != null) {
77 status = YangStatusType.DEPRECATED;
78 } else {
79 status = YangStatusType.OBSOLETE.OBSOLETE;
80 }
81
82 Parsable tmpData = listener.getParsedDataStack().peek();
83 if (tmpData instanceof YangStatus) {
84 YangStatus yangStatus = (YangStatus) tmpData;
85 yangStatus.setStatus(status);
86 } else {
87 throw new ParserException(ListenerErrorMessageConstruction
88 .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
89 ParsableDataType.STATUS_DATA, "", ListenerErrorLocation.ENTRY));
90 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +053091 }
92}