blob: b6722b5641c9e0e410b28388cab96ebfcec83fe9 [file] [log] [blame]
rama-huawei6c728a92016-07-11 14:48:12 +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.YangAppErrorHolder;
20import org.onosproject.yangutils.datamodel.YangAppErrorInfo;
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.ERROR_MESSAGE_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.removeQuotesAndHandleConcat;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
33
34/*
35 * Reference: RFC6020 and YANG ANTLR Grammar
36 *
37 * ABNF grammar as per RFC6020
38 *
39 * error-message-stmt = error-message-keyword sep string stmtend
40 *
41 * ANTLR grammar rule
42 * errorMessageStatement : ERROR_MESSAGE_KEYWORD string STMTEND;
43 */
44
45/**
46 * Represents listener based call back function corresponding to the
47 * app error message defined in ANTLR grammar file for corresponding ABNF rule
48 * in RFC 6020.
49 */
50public final class ErrorMessageListener {
51
52 /**
53 * Creates a new must listener.
54 */
55 private ErrorMessageListener() {
56 }
57
58 /**
59 * Performs validations and updates the data model tree. It is called when parser
60 * receives an input matching the grammar rule (app error message).
61 *
62 * @param listener listener's object
63 * @param ctx context object of the grammar rule
64 */
65 public static void processErrorMessageEntry(TreeWalkListener listener,
66 GeneratedYangParser.ErrorMessageStatementContext ctx) {
67
68 // Check for stack to be non empty.
69 checkStackIsNotEmpty(listener, MISSING_HOLDER, ERROR_MESSAGE_DATA, ctx.string().getText(), ENTRY);
70 String errorMessage = removeQuotesAndHandleConcat(ctx.string().getText());
71
72 // Obtain the node of the stack.
73 Parsable tmpNode = listener.getParsedDataStack().peek();
74 if (tmpNode instanceof YangAppErrorHolder) {
75 YangAppErrorInfo yangAppErrorInfo = ((YangAppErrorHolder) tmpNode).getAppErrorInfo();
76 yangAppErrorInfo.setErrorMessage(errorMessage);
77 } else {
78 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, ERROR_MESSAGE_DATA,
79 ctx.string().getText(), ENTRY));
80 }
81 }
82}