blob: 784b2d571747fe0c7c098e190e8f7526ee0b5caf [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
Vinod Kumar S08710982016-03-03 19:55:30 +053019import java.util.List;
20
Vidyashree Ramaf67c4ba2016-02-24 12:28:22 +053021import org.onosproject.yangutils.datamodel.YangContainer;
22import org.onosproject.yangutils.datamodel.YangList;
23import org.onosproject.yangutils.datamodel.YangNode;
24import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053025import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal91c69612016-02-12 18:37:50 +053026import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053027import org.onosproject.yangutils.utils.YangConstructType;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053028
Vinod Kumar S08710982016-03-03 19:55:30 +053029import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
30import static org.onosproject.yangutils.utils.YangConstructType.getYangConstructType;
Gaurav Agrawal91c69612016-02-12 18:37:50 +053031
32/**
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053033 * It's a utility to carry out listener validation.
Gaurav Agrawal91c69612016-02-12 18:37:50 +053034 */
35public final class ListenerValidation {
36
37 /**
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053038 * Creates a new listener validation.
Gaurav Agrawal91c69612016-02-12 18:37:50 +053039 */
40 private ListenerValidation() {
41 }
42
43 /**
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053044 * Checks parsed data stack is not empty.
Gaurav Agrawal91c69612016-02-12 18:37:50 +053045 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053046 * @param listener Listener's object
47 * @param errorType error type needs to be set in error message
48 * @param yangConstructType type of parsable data in which error occurred
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053049 * @param parsableDataTypeName name of parsable data type in which error
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053050 * occurred
51 * @param errorLocation location where error occurred
Gaurav Agrawal91c69612016-02-12 18:37:50 +053052 */
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053053 public static void checkStackIsNotEmpty(TreeWalkListener listener, ListenerErrorType errorType,
Vinod Kumar S08710982016-03-03 19:55:30 +053054 YangConstructType yangConstructType, String parsableDataTypeName,
55 ListenerErrorLocation errorLocation) {
Gaurav Agrawal91c69612016-02-12 18:37:50 +053056 if (listener.getParsedDataStack().empty()) {
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053057 /*
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053058 * If stack is empty it indicates error condition, value of
59 * parsableDataTypeName will be null in case there is no name
60 * attached to parsable data type.
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053061 */
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053062 String message = constructListenerErrorMessage(errorType, yangConstructType, parsableDataTypeName,
Vinod Kumar S08710982016-03-03 19:55:30 +053063 errorLocation);
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053064 throw new ParserException(message);
Gaurav Agrawal91c69612016-02-12 18:37:50 +053065 }
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053066 }
67
68 /**
69 * Checks parsed data stack is empty.
70 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053071 * @param listener Listener's object
72 * @param errorType error type needs to be set in error message
73 * @param yangConstructType type of parsable data in which error occurred
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053074 * @param parsableDataTypeName name of parsable data type in which error
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053075 * occurred
76 * @param errorLocation location where error occurred
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053077 */
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053078 public static void checkStackIsEmpty(TreeWalkListener listener, ListenerErrorType errorType,
Vinod Kumar S08710982016-03-03 19:55:30 +053079 YangConstructType yangConstructType, String parsableDataTypeName,
80 ListenerErrorLocation errorLocation) {
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053081
82 if (!listener.getParsedDataStack().empty()) {
83 /*
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053084 * If stack is empty it indicates error condition, value of
85 * parsableDataTypeName will be null in case there is no name
86 * attached to parsable data type.
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053087 */
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053088 String message = constructListenerErrorMessage(errorType, yangConstructType, parsableDataTypeName,
Vinod Kumar S08710982016-03-03 19:55:30 +053089 errorLocation);
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053090 throw new ParserException(message);
91 }
Gaurav Agrawal91c69612016-02-12 18:37:50 +053092 }
Vidyashree Ramaf67c4ba2016-02-24 12:28:22 +053093
94 /**
Vinod Kumar S08710982016-03-03 19:55:30 +053095 * Returns parent node config value, if top node does not specify a config
96 * statement then default value true is returned.
Vidyashree Ramaf67c4ba2016-02-24 12:28:22 +053097 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053098 * @param listener listener's object
99 * @return true/false parent's config value
Vidyashree Ramaf67c4ba2016-02-24 12:28:22 +0530100 */
101 public static boolean getParentNodeConfig(TreeWalkListener listener) {
102 YangNode parentNode;
103 Parsable curData = listener.getParsedDataStack().peek();
104 if (curData instanceof YangNode) {
105 parentNode = ((YangNode) curData).getParent();
106 if (parentNode instanceof YangContainer) {
107 return ((YangContainer) parentNode).isConfig();
108 } else if (parentNode instanceof YangList) {
109 return ((YangList) parentNode).isConfig();
110 }
111 }
112 return true;
113 }
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530114
115 /**
116 * Checks if a rule occurrences is as per the expected YANG grammar's
117 * cardinality.
118 *
119 * @param childContext child's context
120 * @param yangChildConstruct child construct for whom cardinality is to be
Vinod Kumar S08710982016-03-03 19:55:30 +0530121 * validated
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530122 * @param yangParentConstruct parent construct
123 * @param parentName parent name
124 * @throws ParserException exception if cardinality check fails
125 */
126 public static void validateCardinality(List<?> childContext, YangConstructType yangChildConstruct,
Vinod Kumar S08710982016-03-03 19:55:30 +0530127 YangConstructType yangParentConstruct, String parentName)
128 throws ParserException {
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530129
130 if (!childContext.isEmpty() && childContext.size() != 1) {
131 ParserException parserException = new ParserException("YANG file error: Invalid cardinality of "
132 + getYangConstructType(yangChildConstruct) + " in " + getYangConstructType(yangParentConstruct)
133 + " \"" + parentName + "\".");
134 throw parserException;
135 }
136 }
137
138 /**
139 * Checks if a rule occurrences is exactly 1.
140 *
141 * @param childContext child's context
142 * @param yangChildConstruct child construct for whom cardinality is to be
Vinod Kumar S08710982016-03-03 19:55:30 +0530143 * validated
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530144 * @param yangParentConstruct parent construct
145 * @param parentName parent name
146 * @throws ParserException exception if cardinality check fails
147 */
148 public static void validateCardinalityEqualsOne(List<?> childContext, YangConstructType yangChildConstruct,
Vinod Kumar S08710982016-03-03 19:55:30 +0530149 YangConstructType yangParentConstruct, String parentName) throws ParserException {
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530150
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
Vinod Kumar S08710982016-03-03 19:55:30 +0530164 * validated
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530165 * @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,
Vinod Kumar S08710982016-03-03 19:55:30 +0530170 YangConstructType yangParentConstruct, String parentName) throws ParserException {
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530171
172 if (childContext.isEmpty()) {
173 ParserException parserException = new ParserException("YANG file error: Invalid cardinality of "
174 + getYangConstructType(yangChildConstruct) + " in " + getYangConstructType(yangParentConstruct)
175 + " \"" + parentName + "\".");
176 throw parserException;
177 }
178 }
Gaurav Agrawal800a8e72016-02-19 12:57:13 +0530179}