blob: d460634d0198c42573589aef00d9f0cc627417d3 [file] [log] [blame]
Gaurav Agrawal91c69612016-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 Ramaf67c4ba2016-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 Agrawal2737d2a2016-02-13 14:23:40 +053023import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal91c69612016-02-12 18:37:50 +053024import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053025import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053026import org.onosproject.yangutils.utils.YangConstructType;
27import static org.onosproject.yangutils.utils.YangConstructType.getYangConstructType;
28
29import java.util.List;
Gaurav Agrawal91c69612016-02-12 18:37:50 +053030
31/**
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053032 * It's a utility to carry out listener validation.
Gaurav Agrawal91c69612016-02-12 18:37:50 +053033 */
34public final class ListenerValidation {
35
36 /**
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053037 * Creates a new listener validation.
Gaurav Agrawal91c69612016-02-12 18:37:50 +053038 */
39 private ListenerValidation() {
40 }
41
42 /**
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053043 * Checks parsed data stack is not empty.
Gaurav Agrawal91c69612016-02-12 18:37:50 +053044 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053045 * @param listener Listener's object
46 * @param errorType error type needs to be set in error message
47 * @param yangConstructType type of parsable data in which error occurred
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053048 * @param parsableDataTypeName name of parsable data type in which error
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053049 * occurred
50 * @param errorLocation location where error occurred
Gaurav Agrawal91c69612016-02-12 18:37:50 +053051 */
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053052 public static void checkStackIsNotEmpty(TreeWalkListener listener, ListenerErrorType errorType,
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053053 YangConstructType yangConstructType, String parsableDataTypeName,
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053054 ListenerErrorLocation errorLocation) {
Gaurav Agrawal91c69612016-02-12 18:37:50 +053055 if (listener.getParsedDataStack().empty()) {
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053056 /*
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053057 * If stack is empty it indicates error condition, value of
58 * parsableDataTypeName will be null in case there is no name
59 * attached to parsable data type.
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053060 */
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053061 String message = constructListenerErrorMessage(errorType, yangConstructType, parsableDataTypeName,
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053062 errorLocation);
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053063 throw new ParserException(message);
Gaurav Agrawal91c69612016-02-12 18:37:50 +053064 }
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053065 }
66
67 /**
68 * Checks parsed data stack is empty.
69 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053070 * @param listener Listener's object
71 * @param errorType error type needs to be set in error message
72 * @param yangConstructType type of parsable data in which error occurred
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053073 * @param parsableDataTypeName name of parsable data type in which error
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053074 * occurred
75 * @param errorLocation location where error occurred
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053076 */
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053077 public static void checkStackIsEmpty(TreeWalkListener listener, ListenerErrorType errorType,
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053078 YangConstructType yangConstructType, String parsableDataTypeName,
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053079 ListenerErrorLocation errorLocation) {
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053080
81 if (!listener.getParsedDataStack().empty()) {
82 /*
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053083 * If stack is empty it indicates error condition, value of
84 * parsableDataTypeName will be null in case there is no name
85 * attached to parsable data type.
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053086 */
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053087 String message = constructListenerErrorMessage(errorType, yangConstructType, parsableDataTypeName,
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053088 errorLocation);
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053089 throw new ParserException(message);
90 }
Gaurav Agrawal91c69612016-02-12 18:37:50 +053091 }
Vidyashree Ramaf67c4ba2016-02-24 12:28:22 +053092
93 /**
94 * Returns parent node config value, if top node does not specify a config statement
95 * then default value true is returned.
96 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053097 * @param listener listener's object
98 * @return true/false parent's config value
Vidyashree Ramaf67c4ba2016-02-24 12:28:22 +053099 */
100 public static boolean getParentNodeConfig(TreeWalkListener listener) {
101 YangNode parentNode;
102 Parsable curData = listener.getParsedDataStack().peek();
103 if (curData instanceof YangNode) {
104 parentNode = ((YangNode) curData).getParent();
105 if (parentNode instanceof YangContainer) {
106 return ((YangContainer) parentNode).isConfig();
107 } else if (parentNode instanceof YangList) {
108 return ((YangList) parentNode).isConfig();
109 }
110 }
111 return true;
112 }
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530113
114 /**
115 * Checks if a rule occurrences is as per the expected YANG grammar's
116 * cardinality.
117 *
118 * @param childContext child's context
119 * @param yangChildConstruct child construct for whom cardinality is to be
120 * validated
121 * @param yangParentConstruct parent construct
122 * @param parentName parent name
123 * @throws ParserException exception if cardinality check fails
124 */
125 public static void validateCardinality(List<?> childContext, YangConstructType yangChildConstruct,
126 YangConstructType yangParentConstruct, String parentName)
127 throws ParserException {
128
129 if (!childContext.isEmpty() && childContext.size() != 1) {
130 ParserException parserException = new ParserException("YANG file error: Invalid cardinality of "
131 + getYangConstructType(yangChildConstruct) + " in " + getYangConstructType(yangParentConstruct)
132 + " \"" + parentName + "\".");
133 throw parserException;
134 }
135 }
136
137 /**
138 * Checks if a rule occurrences is exactly 1.
139 *
140 * @param childContext child's context
141 * @param yangChildConstruct child construct for whom cardinality is to be
142 * validated
143 * @param yangParentConstruct parent construct
144 * @param parentName parent name
145 * @throws ParserException exception if cardinality check fails
146 */
147 public static void validateCardinalityEqualsOne(List<?> childContext, YangConstructType yangChildConstruct,
148 YangConstructType yangParentConstruct, String parentName)
149 throws ParserException {
150
151 if (childContext.isEmpty() || childContext.size() != 1) {
152 ParserException parserException = new ParserException("YANG file error: Invalid cardinality of "
153 + getYangConstructType(yangChildConstruct) + " in " + getYangConstructType(yangParentConstruct)
154 + " \"" + parentName + "\".");
155 throw parserException;
156 }
157 }
158
159 /**
160 * Checks if a rule occurrences is minimum 1.
161 *
162 * @param childContext child's context
163 * @param yangChildConstruct child construct for whom cardinality is to be
164 * validated
165 * @param yangParentConstruct parent construct
166 * @param parentName parent name
167 * @throws ParserException exception if cardinality check fails
168 */
169 public static void validateCardinalityNonNull(List<?> childContext, YangConstructType yangChildConstruct,
170 YangConstructType yangParentConstruct, String parentName)
171 throws ParserException {
172
173 if (childContext.isEmpty()) {
174 ParserException parserException = new ParserException("YANG file error: Invalid cardinality of "
175 + getYangConstructType(yangChildConstruct) + " in " + getYangConstructType(yangParentConstruct)
176 + " \"" + parentName + "\".");
177 throw parserException;
178 }
179 }
Gaurav Agrawal800a8e72016-02-19 12:57:13 +0530180}