blob: 3ec31c7343cc368e66d91c1d235d5951b49c697d [file] [log] [blame]
Vidyashree Rama07c26bb2016-07-28 17:33:15 +05301/*
2 * Copyright 2016-present 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
19import org.onosproject.yangutils.datamodel.YangAppExtendedName;
20import org.onosproject.yangutils.datamodel.YangCompilerAnnotation;
21import org.onosproject.yangutils.datamodel.utils.Parsable;
22import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
23import org.onosproject.yangutils.parser.exceptions.ParserException;
24import org.onosproject.yangutils.parser.impl.TreeWalkListener;
25
26import static org.onosproject.yangutils.datamodel.utils.YangConstructType.APP_EXTENDED_NAME_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.ListenerUtil.getValidPrefix;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
34
35/*
36 * Reference: RFC6020 and YANG ANTLR Grammar
37 *
38 * ABNF grammar as per RFC6020
39 * app-extended-stmt = prefix:app-extended-name-keyword string ";"
40 *
41 * ANTLR grammar rule
42 * appExtendedStatement : APP_EXTENDED extendedName STMTEND;
43 */
44
45/**
46 * Represents listener based call back function corresponding to the "app-extended-name"
47 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
48 */
49public final class AppExtendedNameListener {
50
51 /**
52 * Creates a new app-extended-name listener.
53 */
54 private AppExtendedNameListener() {
55 }
56
57 /**
58 * Performs validation and updates the data model tree. It is called when parser receives an
59 * input matching the grammar rule(app-extended-name).
60 *
61 * @param listener listener's object
62 * @param ctx context object of the grammar rule
63 */
64 public static void processAppExtendedNameEntry(TreeWalkListener listener,
65 GeneratedYangParser.AppExtendedStatementContext ctx) {
66
67 checkStackIsNotEmpty(listener, MISSING_HOLDER, APP_EXTENDED_NAME_DATA, ctx.extendedName().getText(), ENTRY);
68
69 String prefix = getValidPrefix(ctx.APP_EXTENDED().getText(), APP_EXTENDED_NAME_DATA, ctx);
70 YangAppExtendedName extendedName = new YangAppExtendedName();
71 extendedName.setPrefix(prefix);
72 extendedName.setYangAppExtendedName(removeQuotesAndHandleConcat(ctx.extendedName().getText()));
73
74 Parsable curData = listener.getParsedDataStack().peek();
75 if (curData instanceof YangCompilerAnnotation) {
76 YangCompilerAnnotation compilerAnnotation = ((YangCompilerAnnotation) curData);
77 compilerAnnotation.setYangAppExtendedName(extendedName);
78 } else {
79 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, APP_EXTENDED_NAME_DATA,
80 ctx.extendedName().getText(), ENTRY));
81 }
82 }
83}