blob: 65fc50b7c18c329a9bf06f21ef35613136061feb [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.YangAppDataStructure;
20import org.onosproject.yangutils.datamodel.utils.Parsable;
21import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
22import org.onosproject.yangutils.parser.exceptions.ParserException;
23import org.onosproject.yangutils.parser.impl.TreeWalkListener;
24
25import static org.onosproject.yangutils.datamodel.utils.YangConstructType.KEY_DATA;
26import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
32import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
33
34/*
35 * Reference: RFC6020 and YANG ANTLR Grammar
36 *
37 * ABNF grammar as per RFC6020
38 * data-structure-key-stmt = prefix:key-keyword string ";"
39 *
40 * ANTLR grammar rule
41 * dataStructureKeyStatement : DATA_STRUCTURE_KEY string STMTEND;
42 */
43
44/**
45 * Represents listener based call back function corresponding to the "key"
46 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
47 */
48public final class DataStructureKeyListener {
49
50 /**
51 * Creates a new data-structure-key listener.
52 */
53 private DataStructureKeyListener() {
54 }
55
56 /**
57 * Performs validation and updates the data model tree. It is called when parser receives an
58 * input matching the grammar rule(key).
59 *
60 * @param listener listener's object
61 * @param ctx context object of the grammar rule
62 */
63 public static void processDataStructureKeyEntry(TreeWalkListener listener,
64 GeneratedYangParser.DataStructureKeyStatementContext ctx) {
65
66 // Check for stack to be non empty.
67 checkStackIsNotEmpty(listener, MISSING_HOLDER, KEY_DATA, ctx.string().getText(), ENTRY);
68
69 Parsable tmpData = listener.getParsedDataStack().peek();
70 if (listener.getParsedDataStack().peek() instanceof YangAppDataStructure) {
71 YangAppDataStructure dataStructure = (YangAppDataStructure) tmpData;
72 String tmpKeyValue = removeQuotesAndHandleConcat(ctx.string().getText());
73 if (tmpKeyValue.contains(SPACE)) {
74 String[] keyValues = tmpKeyValue.split(SPACE);
75 for (String keyValue : keyValues) {
76 dataStructure.addKey(keyValue);
77 }
78 } else {
79 dataStructure.addKey(tmpKeyValue);
80 }
81 } else {
82 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, KEY_DATA, ctx.string().getText(),
83 ENTRY));
84 }
85 }
86}
87