blob: 26e4907c1351724e5194ef4964c2b8cb1c38467e [file] [log] [blame]
Gaurav Agrawal88897632016-02-12 18:37:50 +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.parserutils;
18
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053019import org.onosproject.yangutils.datamodel.YangContainer;
20import org.onosproject.yangutils.datamodel.YangList;
21import org.onosproject.yangutils.datamodel.YangNode;
22import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053023import org.onosproject.yangutils.parser.ParsableDataType;
24import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal88897632016-02-12 18:37:50 +053025import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053026import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Gaurav Agrawal88897632016-02-12 18:37:50 +053027
28/**
Gaurav Agrawala04483c2016-02-13 14:23:40 +053029 * It's a utility to carry out listener validation.
Gaurav Agrawal88897632016-02-12 18:37:50 +053030 */
31public final class ListenerValidation {
32
33 /**
Gaurav Agrawala04483c2016-02-13 14:23:40 +053034 * Creates a new listener validation.
Gaurav Agrawal88897632016-02-12 18:37:50 +053035 */
36 private ListenerValidation() {
37 }
38
39 /**
Gaurav Agrawala04483c2016-02-13 14:23:40 +053040 * Checks parsed data stack is not empty.
Gaurav Agrawal88897632016-02-12 18:37:50 +053041 *
42 * @param listener Listener's object.
Gaurav Agrawala04483c2016-02-13 14:23:40 +053043 * @param errorType error type needs to be set in error message.
44 * @param parsableDataType type of parsable data in which error occurred.
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053045 * @param parsableDataTypeName name of parsable data type in which error
46 * occurred.
Gaurav Agrawala04483c2016-02-13 14:23:40 +053047 * @param errorLocation location where error occurred.
Gaurav Agrawal88897632016-02-12 18:37:50 +053048 */
Gaurav Agrawala04483c2016-02-13 14:23:40 +053049 public static void checkStackIsNotEmpty(TreeWalkListener listener, ListenerErrorType errorType,
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053050 ParsableDataType parsableDataType, String parsableDataTypeName,
51 ListenerErrorLocation errorLocation) {
Gaurav Agrawal88897632016-02-12 18:37:50 +053052 if (listener.getParsedDataStack().empty()) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053053 /*
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053054 * If stack is empty it indicates error condition, value of
55 * parsableDataTypeName will be null in case there is no name
56 * attached to parsable data type.
Gaurav Agrawala04483c2016-02-13 14:23:40 +053057 */
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053058 String message = constructListenerErrorMessage(errorType, parsableDataType, parsableDataTypeName,
59 errorLocation);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053060 throw new ParserException(message);
Gaurav Agrawal88897632016-02-12 18:37:50 +053061 }
Gaurav Agrawala04483c2016-02-13 14:23:40 +053062 }
63
64 /**
65 * Checks parsed data stack is empty.
66 *
67 * @param listener Listener's object.
68 * @param errorType error type needs to be set in error message.
69 * @param parsableDataType type of parsable data in which error occurred.
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053070 * @param parsableDataTypeName name of parsable data type in which error
71 * occurred.
Gaurav Agrawala04483c2016-02-13 14:23:40 +053072 * @param errorLocation location where error occurred.
73 */
74
75 public static void checkStackIsEmpty(TreeWalkListener listener, ListenerErrorType errorType,
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053076 ParsableDataType parsableDataType, String parsableDataTypeName,
77 ListenerErrorLocation errorLocation) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053078
79 if (!listener.getParsedDataStack().empty()) {
80 /*
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053081 * If stack is empty it indicates error condition, value of
82 * parsableDataTypeName will be null in case there is no name
83 * attached to parsable data type.
Gaurav Agrawala04483c2016-02-13 14:23:40 +053084 */
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053085 String message = constructListenerErrorMessage(errorType, parsableDataType, parsableDataTypeName,
86 errorLocation);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053087 throw new ParserException(message);
88 }
Gaurav Agrawal88897632016-02-12 18:37:50 +053089 }
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053090
91 /**
92 * Returns parent node config value, if top node does not specify a config statement
93 * then default value true is returned.
94 *
95 * @param listener listener's object.
96 * @return true/false parent's config value.
97 */
98 public static boolean getParentNodeConfig(TreeWalkListener listener) {
99 YangNode parentNode;
100 Parsable curData = listener.getParsedDataStack().peek();
101 if (curData instanceof YangNode) {
102 parentNode = ((YangNode) curData).getParent();
103 if (parentNode instanceof YangContainer) {
104 return ((YangContainer) parentNode).isConfig();
105 } else if (parentNode instanceof YangList) {
106 return ((YangList) parentNode).isConfig();
107 }
108 }
109 return true;
110 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530111}