blob: 268d53500c4323155dd7abf8e39f0dcd8357b35e [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.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
26import static org.onosproject.yangutils.parser.ParsableDataType.KEY_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.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053034
35/*
36 * Reference: RFC6020 and YANG ANTLR Grammar
37 *
38 * ABNF grammar as per RFC6020
39 * key-stmt = key-keyword sep key-arg-str stmtend
40 *
41 * ANTLR grammar rule
42 * keyStatement : KEY_KEYWORD string STMTEND;
43 */
44
45/**
46 * Implements listener based call back function corresponding to the "key"
47 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
48 */
49public final class KeyListener {
50
51 /**
52 * Creates a new key listener.
53 */
54 private KeyListener() {
55 }
56
57 /**
58 * It is called when parser receives an input matching the grammar
59 * rule (key), perform validations and updates the data model
60 * tree.
61 *
62 * @param listener listener's object.
63 * @param ctx context object of the grammar rule.
64 */
65 public static void processKeyEntry(TreeWalkListener listener,
66 GeneratedYangParser.KeyStatementContext ctx) {
Vidyashree Rama92fc5562016-02-12 18:44:12 +053067
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053068 // Check for stack to be non empty.
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053069 checkStackIsNotEmpty(listener, MISSING_HOLDER, KEY_DATA, ctx.string().getText(), ENTRY);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053070
71 Parsable tmpData = listener.getParsedDataStack().peek();
72 if (listener.getParsedDataStack().peek() instanceof YangList) {
73 YangList yangList = (YangList) tmpData;
74 String tmpKeyValue = ctx.string().getText().replace("\"", "");
75 if (tmpKeyValue.contains(" ")) {
76 String[] keyValues = tmpKeyValue.split(" ");
77 for (String keyValue : keyValues) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053078 try {
79 yangList.addKey(keyValue);
80 } catch (DataModelException e) {
81 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, KEY_DATA,
82 ctx.string().getText(), ENTRY, e.getMessage()));
83 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053084 }
85 } else {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053086 try {
87 yangList.addKey(tmpKeyValue);
88 } catch (DataModelException e) {
89 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, KEY_DATA,
90 ctx.string().getText(), ENTRY, e.getMessage()));
91 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053092 }
93 } else {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053094 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, KEY_DATA, ctx.string().getText(),
95 ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053096 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +053097 }
98}