2 * Copyright (c) 2018 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.translate.write;
19 import static com.google.common.base.Preconditions.checkNotNull;
21 import io.fd.honeycomb.translate.ValidationFailedException;
22 import javax.annotation.Nonnull;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 * Thrown when a writer fails to validate data update.
29 public class DataValidationFailedException extends ValidationFailedException {
30 private static final long serialVersionUID = 1;
32 private final InstanceIdentifier<?> failedId;
35 * Constructs an ValidationFailedException given data id, exception detail message and exception cause.
37 * @param failedId instance identifier of the data object that could not be validated
38 * @param cause the cause of validation failure
39 * @param message the exception detail message
41 public DataValidationFailedException(@Nonnull final InstanceIdentifier<?> failedId,
42 @Nonnull final String message,
43 @Nonnull final Throwable cause) {
44 super(message, cause);
45 this.failedId = checkNotNull(failedId, "failedId should not be null");
49 * Constructs an ValidationFailedException given data id.
51 * @param failedId instance identifier of the data object that could not be validated
53 public DataValidationFailedException(@Nonnull final InstanceIdentifier<?> failedId,
54 @Nonnull final String message) {
56 this.failedId = checkNotNull(failedId, "failedId should not be null");
60 * Constructs an ValidationFailedException given data id and exception cause.
62 * @param failedId instance identifier of the data object that could not be validated
63 * @param cause the cause of validated failure
65 public DataValidationFailedException(@Nonnull final InstanceIdentifier<?> failedId,
66 @Nonnull final Throwable cause) {
68 this.failedId = checkNotNull(failedId, "failedId should not be null");
72 * Returns id of the data object that could not be validated.
74 * @return data object instance identifier
77 public InstanceIdentifier<?> getFailedId() {
82 * Create specific validated failed exception.
84 public static class CreateValidationFailedException extends DataValidationFailedException {
85 private static final long serialVersionUID = 1;
87 private final transient DataObject data;
89 public CreateValidationFailedException(@Nonnull final InstanceIdentifier<?> failedId,
90 @Nonnull final DataObject data,
91 @Nonnull final Throwable cause) {
92 super(failedId, getMsg(failedId, data), cause);
93 this.data = checkNotNull(data, "data");
96 private static String getMsg(final @Nonnull InstanceIdentifier<?> failedId,
97 final @Nonnull DataObject data) {
98 return String.format("Failed to validate create request for: %s at: %s.", data, failedId);
101 public DataObject getData() {
107 * Update specific validated failed exception.
109 public static class UpdateValidationFailedException extends DataValidationFailedException {
110 private static final long serialVersionUID = 1;
112 private final transient DataObject dataBefore;
113 private final transient DataObject dataAfter;
115 public UpdateValidationFailedException(@Nonnull final InstanceIdentifier<?> failedId,
116 @Nonnull final DataObject dataBefore,
117 @Nonnull final DataObject dataAfter,
118 @Nonnull final Throwable cause) {
119 super(failedId, getMsg(failedId, dataBefore, dataAfter), cause);
120 this.dataBefore = checkNotNull(dataBefore, "dataBefore");
121 this.dataAfter = checkNotNull(dataAfter, "dataAfter");
124 private static String getMsg(final @Nonnull InstanceIdentifier<?> failedId, final DataObject dataBefore,
125 final @Nonnull DataObject dataAfter) {
127 .format("Failed to validate update request from: %s to: %s, at: %s.", dataBefore, dataAfter, failedId);
130 public DataObject getDataBefore() {
134 public DataObject getDataAfter() {
140 * Delete specific validated failed exception.
142 public static class DeleteValidationFailedException extends DataValidationFailedException {
143 private static final long serialVersionUID = 1;
145 public DeleteValidationFailedException(@Nonnull final InstanceIdentifier<?> failedId, @Nonnull final Throwable cause) {
146 super(failedId, getMsg(failedId), cause);
149 private static String getMsg(@Nonnull final InstanceIdentifier<?> failedId) {
150 return String.format("Failed to validate delete request: %s.", failedId);