blob: 81223fd94902d090162c468218461a35c6354e26 [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/**
Bharat saraswald9822e92016-04-05 15:13:44 +053048 * Represesnts listener based call back function corresponding to the "key"
Vidyashree Rama92fc5562016-02-12 18:44:12 +053049 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
50 */
51public final class KeyListener {
52
53 /**
54 * Creates a new key listener.
55 */
56 private KeyListener() {
57 }
58
59 /**
60 * It is called when parser receives an input matching the grammar
61 * rule (key), perform validations and updates the data model
62 * tree.
63 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053064 * @param listener listener's object
65 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +053066 */
67 public static void processKeyEntry(TreeWalkListener listener,
68 GeneratedYangParser.KeyStatementContext ctx) {
Vidyashree Rama92fc5562016-02-12 18:44:12 +053069
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053070 // Check for stack to be non empty.
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053071 checkStackIsNotEmpty(listener, MISSING_HOLDER, KEY_DATA, ctx.key().getText(), ENTRY);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053072
73 Parsable tmpData = listener.getParsedDataStack().peek();
74 if (listener.getParsedDataStack().peek() instanceof YangList) {
75 YangList yangList = (YangList) tmpData;
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053076 String tmpKeyValue = removeQuotesAndHandleConcat(ctx.key().getText());
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053077 if (tmpKeyValue.contains(" ")) {
78 String[] keyValues = tmpKeyValue.split(" ");
79 for (String keyValue : keyValues) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053080 try {
81 yangList.addKey(keyValue);
82 } catch (DataModelException e) {
83 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, KEY_DATA,
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053084 ctx.key().getText(), ENTRY, e.getMessage()));
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053085 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053086 }
87 } else {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053088 try {
89 yangList.addKey(tmpKeyValue);
90 } catch (DataModelException e) {
91 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, KEY_DATA,
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053092 ctx.key().getText(), ENTRY, e.getMessage()));
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053093 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053094 }
95 } else {
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053096 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, KEY_DATA, ctx.key().getText(),
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053097 ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053098 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +053099 }
100}