* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.restconf.common.errors;
import com.google.common.base.Preconditions;
import java.util.List;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response.Status;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
import org.opendaylight.yangtools.yang.common.OperationFailedException;
throw new RestconfDocumentedException(message, cause, cause.getErrorList());
}
+ /**
+ * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
+ * this method does nothing.
+ *
+ * @param expression Expression to be evaluated
+ * @param errorType The enumerated type indicating the layer where the error occurred.
+ * @param errorTag The enumerated tag representing a more specific error cause.
+ * @param format Format string, according to {@link String#format(String, Object...)}.
+ * @param args Format string arguments, according to {@link String#format(String, Object...)}
+ * @throws RestconfDocumentedException if the expression evaluates to true.
+ */
+ public static void throwIf(final boolean expression, final ErrorType errorType, final ErrorTag errorTag,
+ final @NonNull String format, final Object... args) {
+ if (expression) {
+ throw new RestconfDocumentedException(String.format(format, args), errorType, errorTag);
+ }
+ }
+
+ /**
+ * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
+ * this method does nothing.
+ *
+ * @param expression Expression to be evaluated
+ * @param message error message
+ * @param errorType The enumerated type indicating the layer where the error occurred.
+ * @param errorTag The enumerated tag representing a more specific error cause.
+ * @throws RestconfDocumentedException if the expression evaluates to true.
+ */
+ public static void throwIf(final boolean expression, final @NonNull String message,
+ final ErrorType errorType, final ErrorTag errorTag) {
+ if (expression) {
+ throw new RestconfDocumentedException(message, errorType, errorTag);
+ }
+ }
+
+ /**
+ * Throw an instance of this exception if an object is null. If the object is non-null, it will
+ * be returned as the result of this method.
+ *
+ * @param obj Object reference to be checked
+ * @param errorType The enumerated type indicating the layer where the error occurred.
+ * @param errorTag The enumerated tag representing a more specific error cause.
+ * @param format Format string, according to {@link String#format(String, Object...)}.
+ * @param args Format string arguments, according to {@link String#format(String, Object...)}
+ * @throws RestconfDocumentedException if the expression evaluates to true.
+ */
+ public static <T> @NonNull T throwIfNull(final @Nullable T obj, final ErrorType errorType, final ErrorTag errorTag,
+ final @NonNull String format, final Object... args) {
+ if (obj == null) {
+ throw new RestconfDocumentedException(String.format(format, args), errorType, errorTag);
+ }
+ return obj;
+ }
+
private static List<RestconfError> convertToRestconfErrors(final Collection<? extends RpcError> rpcErrors) {
final List<RestconfError> errorList = new ArrayList<>();
if (rpcErrors != null) {
throw new UnsupportedOperationException("Utility class");
}
- /**
- * Method returns {@link RestconfDocumentedException} for a false condition.
- *
- * @param condition - condition for rise {@link RestconfDocumentedException}
- * @param type - input {@link ErrorType} for create {@link RestconfDocumentedException}
- * @param tag - input {@link ErrorTag} for create {@link RestconfDocumentedException}
- * @param message - input error message for create {@link RestconfDocumentedException}
- */
- public static void checkDocumentedError(final boolean condition, final ErrorType type,
- final ErrorTag tag, final String message) {
- if (!condition) {
- throw new RestconfDocumentedException(message, type, tag);
- }
- }
-
/**
* Method returns {@link RestconfDocumentedException} if value is NULL or same input value.
* {@link ErrorType} is relevant for server application layer
}
- RestconfValidationUtils.checkDocumentedError(componentIter.hasNext(),
- ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, "Module name must be supplied.");
+ RestconfDocumentedException.throwIf(!componentIter.hasNext(), "Module name must be supplied.",
+ ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
final String moduleName = componentIter.next();
- RestconfValidationUtils.checkDocumentedError(componentIter.hasNext(),
- ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, "Revision date must be supplied.");
+ RestconfDocumentedException.throwIf(!componentIter.hasNext(), "Revision date must be supplied.",
+ ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
final String revisionString = componentIter.next();
return getExportUsingNameAndRevision(schemaContext, moduleName, revisionString,
salContext.getYangTextSourceProvider());
import org.opendaylight.restconf.common.patch.PatchContext;
import org.opendaylight.restconf.common.patch.PatchStatusContext;
import org.opendaylight.restconf.common.util.DataChangeScope;
-import org.opendaylight.restconf.common.validation.RestconfValidationUtils;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
import org.opendaylight.yangtools.yang.common.Empty;
final Map<QName, Object> mutableCopyUriKeyValues = Maps.newHashMap(uriKeyValues);
for (final QName keyDefinition : keyDefinitions) {
- final Object uriKeyValue = mutableCopyUriKeyValues.remove(keyDefinition);
- // should be caught during parsing URI to InstanceIdentifier
- RestconfValidationUtils.checkDocumentedError(uriKeyValue != null, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING,
- "Missing key " + keyDefinition + " in URI.");
+ final Object uriKeyValue = RestconfDocumentedException.throwIfNull(
+ // should be caught during parsing URI to InstanceIdentifier
+ mutableCopyUriKeyValues.remove(keyDefinition), ErrorType.PROTOCOL, ErrorTag.DATA_MISSING,
+ "Missing key %s in URI.", keyDefinition);
final Object dataKeyValue = payload.getIdentifier().getKeyValues().get(keyDefinition);
import org.opendaylight.restconf.common.errors.RestconfError;
import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
-import org.opendaylight.restconf.common.validation.RestconfValidationUtils;
import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
import org.opendaylight.restconf.nb.rfc8040.references.SchemaContextRef;
import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper;
final List<QName> keyDefinitions) {
final Map<QName, Object> mutableCopyUriKeyValues = Maps.newHashMap(uriKeyValues);
for (final QName keyDefinition : keyDefinitions) {
- final Object uriKeyValue = mutableCopyUriKeyValues.remove(keyDefinition);
- RestconfValidationUtils.checkDocumentedError(uriKeyValue != null, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING,
- "Missing key " + keyDefinition + " in URI.");
+ final Object uriKeyValue = RestconfDocumentedException.throwIfNull(
+ mutableCopyUriKeyValues.remove(keyDefinition), ErrorType.PROTOCOL, ErrorTag.DATA_MISSING,
+ "Missing key %s in URI.", keyDefinition);
final Object dataKeyValue = payload.getIdentifier().getKeyValues().get(keyDefinition);
import org.opendaylight.restconf.common.errors.RestconfError;
import org.opendaylight.restconf.common.util.RestUtil;
import org.opendaylight.restconf.common.util.RestconfSchemaUtil;
-import org.opendaylight.restconf.common.validation.RestconfValidationUtils;
import org.opendaylight.restconf.nb.rfc8040.codecs.RestCodec;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
}
// check if next value is parsable
- RestconfValidationUtils.checkDocumentedError(IDENTIFIER_PREDICATE.matches(currentChar()),
- RestconfError.ErrorType.PROTOCOL,RestconfError.ErrorTag.MALFORMED_MESSAGE, "");
+ RestconfDocumentedException.throwIf(!IDENTIFIER_PREDICATE.matches(currentChar()), "",
+ RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.MALFORMED_MESSAGE);
// parse value
final QName key = keys.next();
}
// there should be no more missing keys
- RestconfValidationUtils.checkDocumentedError(
- !keys.hasNext(),
- RestconfError.ErrorType.PROTOCOL,
- RestconfError.ErrorTag.MISSING_ATTRIBUTE,
- "Key value missing for: " + qname
- );
+ RestconfDocumentedException.throwIf(keys.hasNext(),
+ RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.MISSING_ATTRIBUTE,
+ "Key value missing for: %s", qname);
}
path.add(new YangInstanceIdentifier.NodeIdentifierWithPredicates(qname, values.build()));
final String value = nextIdentifierFromNextSequence(IDENTIFIER_PREDICATE);
// exception if value attribute is missing
- RestconfValidationUtils.checkDocumentedError(
- !value.isEmpty(),
+ RestconfDocumentedException.throwIf(
+ value.isEmpty(),
RestconfError.ErrorType.PROTOCOL,
RestconfError.ErrorTag.MISSING_ATTRIBUTE,
- "Value missing for: " + qname
- );
+ "Value missing for: %s", qname);
final DataSchemaNode dataSchemaNode = current.getDataSchemaNode();
final Object valueByType = prepareValueByType(dataSchemaNode, findAndParsePercentEncoded(value));
path.add(new YangInstanceIdentifier.NodeWithValue<>(qname, valueByType));
}
private static <T extends DataNodeContainer & SchemaNode & ActionNodeContainer> QName getQNameOfDataSchemaNode(
- final T parent, String nodeName) {
+ final T parent, final String nodeName) {
final Optional<ActionDefinition> actionDef = findActionDefinition(parent, nodeName);
final SchemaNode node;
if (actionDef.isPresent()) {
import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
-import org.opendaylight.restconf.common.validation.RestconfValidationUtils;
import org.opendaylight.restconf.nb.rfc8040.utils.parser.builder.ParserBuilderConstants;
import org.opendaylight.yangtools.yang.common.Revision;
* @return {@link Date}
*/
public static Revision validateAndGetRevision(final Iterator<String> revisionDate) {
- RestconfValidationUtils.checkDocumentedError(revisionDate.hasNext(), ErrorType.PROTOCOL,
- ErrorTag.INVALID_VALUE, "Revision date must be supplied.");
+ RestconfDocumentedException.throwIf(!revisionDate.hasNext(), "Revision date must be supplied.",
+ ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
try {
return Revision.of(revisionDate.next());
} catch (final DateTimeParseException e) {
* @return {@link String}
*/
public static String validateAndGetModulName(final Iterator<String> moduleName) {
- RestconfValidationUtils.checkDocumentedError(
- moduleName.hasNext(),
- ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
- "Module name must be supplied."
- );
-
+ RestconfDocumentedException.throwIf(!moduleName.hasNext(), "Module name must be supplied.", ErrorType.PROTOCOL,
+ ErrorTag.INVALID_VALUE);
final String name = moduleName.next();
- RestconfValidationUtils.checkDocumentedError(
- !name.isEmpty() && ParserBuilderConstants.Deserializer.IDENTIFIER_FIRST_CHAR.matches(name.charAt(0)),
- ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
- "Identifier must start with character from set 'a-zA-Z_"
- );
-
- RestconfValidationUtils.checkDocumentedError(
- !name.toUpperCase(Locale.ROOT).startsWith("XML"),
- ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
- "Identifier must NOT start with XML ignore case."
- );
-
- RestconfValidationUtils.checkDocumentedError(
- ParserBuilderConstants.Deserializer.IDENTIFIER.matchesAllOf(name.substring(1)),
- ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
- "Supplied name has not expected identifier format."
- );
+ RestconfDocumentedException.throwIf(
+ name.isEmpty() || !ParserBuilderConstants.Deserializer.IDENTIFIER_FIRST_CHAR.matches(name.charAt(0)),
+ "Identifier must start with character from set 'a-zA-Z_", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
+ RestconfDocumentedException.throwIf(name.toUpperCase(Locale.ROOT).startsWith("XML"),
+ "Identifier must NOT start with XML ignore case.", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
+ RestconfDocumentedException.throwIf(
+ !ParserBuilderConstants.Deserializer.IDENTIFIER.matchesAllOf(name.substring(1)),
+ "Supplied name has not expected identifier format.", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
return name;
}