blob: 9d215d2ad526f0733d6949f2dcba5562a1f4ff10 [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.YangList;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053020import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053021import org.onosproject.yangutils.parser.Parsable;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053022import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053023import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053024import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053025
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053026import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053027import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
Bharat saraswald9822e92016-04-05 15:13:44 +053028import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053029import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053030import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Bharat saraswald9822e92016-04-05 15:13:44 +053031import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053033import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Bharat saraswald9822e92016-04-05 15:13:44 +053034import static org.onosproject.yangutils.utils.YangConstructType.KEY_DATA;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053035
36/*
37 * Reference: RFC6020 and YANG ANTLR Grammar
38 *
39 * ABNF grammar as per RFC6020
40 * key-stmt = key-keyword sep key-arg-str stmtend
41 *
42 * ANTLR grammar rule
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053043 * keyStatement : KEY_KEYWORD key STMTEND;
44 * key : string;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053045 */
46
47/**
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053048 * Represesnts listener based call back function corresponding to the "key" rule
49 * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053050 */
51public final class KeyListener {
52
53 /**
54 * Creates a new key listener.
55 */
56 private KeyListener() {
57 }
58
59 /**
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053060 * It is called when parser receives an input matching the grammar rule
61 * (key), perform validations and updates the data model tree.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053062 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053063 * @param listener listener's object
64 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +053065 */
66 public static void processKeyEntry(TreeWalkListener listener,
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053067 GeneratedYangParser.KeyStatementContext ctx) {
Vidyashree Rama92fc5562016-02-12 18:44:12 +053068
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053069 // Check for stack to be non empty.
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053070 checkStackIsNotEmpty(listener, MISSING_HOLDER, KEY_DATA, ctx.key().getText(), ENTRY);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053071
72 Parsable tmpData = listener.getParsedDataStack().peek();
73 if (listener.getParsedDataStack().peek() instanceof YangList) {
74 YangList yangList = (YangList) tmpData;
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053075 String tmpKeyValue = removeQuotesAndHandleConcat(ctx.key().getText());
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053076 if (tmpKeyValue.contains(" ")) {
77 String[] keyValues = tmpKeyValue.split(" ");
78 for (String keyValue : keyValues) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053079 try {
80 yangList.addKey(keyValue);
81 } catch (DataModelException e) {
82 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, KEY_DATA,
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053083 ctx.key().getText(), ENTRY, e.getMessage()));
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053084 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053085 }
86 } else {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053087 try {
88 yangList.addKey(tmpKeyValue);
89 } catch (DataModelException e) {
90 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, KEY_DATA,
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053091 ctx.key().getText(), ENTRY, e.getMessage()));
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053092 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053093 }
94 } else {
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053095 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, KEY_DATA, ctx.key().getText(),
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053096 ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053097 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +053098 }
99}