HONEYCOMB-431: add validation support to Writers
[honeycomb.git] / infra / translate-api / src / main / java / io / fd / honeycomb / translate / write / DataValidationFailedException.java
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
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
17 package io.fd.honeycomb.translate.write;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20
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;
25
26 /**
27  * Thrown when a writer fails to validate data update.
28  */
29 public class DataValidationFailedException extends ValidationFailedException {
30     private static final long serialVersionUID = 1;
31
32     private final InstanceIdentifier<?> failedId;
33
34     /**
35      * Constructs an ValidationFailedException given data id, exception detail message and exception cause.
36      *
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
40      */
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");
46     }
47
48     /**
49      * Constructs an ValidationFailedException given data id.
50      *
51      * @param failedId instance identifier of the data object that could not be validated
52      */
53     public DataValidationFailedException(@Nonnull final InstanceIdentifier<?> failedId,
54                                          @Nonnull final String message) {
55         super(message);
56         this.failedId = checkNotNull(failedId, "failedId should not be null");
57     }
58
59     /**
60      * Constructs an ValidationFailedException given data id and exception cause.
61      *
62      * @param failedId instance identifier of the data object that could not be validated
63      * @param cause    the cause of validated failure
64      */
65     public DataValidationFailedException(@Nonnull final InstanceIdentifier<?> failedId,
66                                          @Nonnull final Throwable cause) {
67         super(cause);
68         this.failedId = checkNotNull(failedId, "failedId should not be null");
69     }
70
71     /**
72      * Returns id of the data object that could not be validated.
73      *
74      * @return data object instance identifier
75      */
76     @Nonnull
77     public InstanceIdentifier<?> getFailedId() {
78         return failedId;
79     }
80
81     /**
82      * Create specific validated failed exception.
83      */
84     public static class CreateValidationFailedException extends DataValidationFailedException {
85         private static final long serialVersionUID = 1;
86
87         private final transient DataObject data;
88
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");
94         }
95
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);
99         }
100
101         public DataObject getData() {
102             return data;
103         }
104     }
105
106     /**
107      * Update specific validated failed exception.
108      */
109     public static class UpdateValidationFailedException extends DataValidationFailedException {
110         private static final long serialVersionUID = 1;
111
112         private final transient DataObject dataBefore;
113         private final transient DataObject dataAfter;
114
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");
122         }
123
124         private static String getMsg(final @Nonnull InstanceIdentifier<?> failedId, final DataObject dataBefore,
125                                      final @Nonnull DataObject dataAfter) {
126             return String
127                 .format("Failed to validate update request from: %s to: %s, at: %s.", dataBefore, dataAfter, failedId);
128         }
129
130         public DataObject getDataBefore() {
131             return dataBefore;
132         }
133
134         public DataObject getDataAfter() {
135             return dataAfter;
136         }
137     }
138
139     /**
140      * Delete specific validated failed exception.
141      */
142     public static class DeleteValidationFailedException extends DataValidationFailedException {
143         private static final long serialVersionUID = 1;
144
145         public DeleteValidationFailedException(@Nonnull final InstanceIdentifier<?> failedId, @Nonnull final Throwable cause) {
146             super(failedId, getMsg(failedId), cause);
147         }
148
149         private static String getMsg(@Nonnull final InstanceIdentifier<?> failedId) {
150             return String.format("Failed to validate delete request: %s.", failedId);
151         }
152     }
153 }