HONEYCOMB-431: add validation support to Writers
[honeycomb.git] / infra / translate-spi / src / main / java / io / fd / honeycomb / translate / spi / write / WriterCustomizer.java
1 /*
2  * Copyright (c) 2016 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.spi.write;
18
19 import com.google.common.annotations.Beta;
20 import io.fd.honeycomb.translate.write.WriteContext;
21 import io.fd.honeycomb.translate.write.WriteFailedException;
22 import javax.annotation.Nonnull;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25
26 /**
27  * CompositeChildWriter SPI to customize its behavior.
28  *
29  * @param <D> Specific DataObject derived type (Identifiable), that is handled by this customizer.
30  */
31 @Beta
32 public interface WriterCustomizer<D extends DataObject> {
33
34     /**
35      * Handle write operation. C from CRUD.
36      *
37      * @param id Identifier(from root) of data being written
38      * @param dataAfter New data to be written
39      * @param writeContext Write context can be used to store any useful information and then utilized by other customizers
40      *
41      * @throws WriteFailedException if write was unsuccessful
42      */
43     void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
44                                 @Nonnull final D dataAfter,
45                                 @Nonnull final WriteContext writeContext) throws WriteFailedException;
46
47     /**
48      * Handle update operation. U from CRUD.
49      * By default, updates will be broken into delete + create.
50      * Override this if there is a direct support for updates on lower level.
51      *
52      * @param id Identifier(from root) of data being written
53      * @param dataBefore Old data
54      * @param dataAfter New, updated data
55      * @param writeContext Write context can be used to store any useful information and then utilized by other customizers
56      *
57      * @throws WriteFailedException if update was unsuccessful
58      */
59     default void updateCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
60                                          @Nonnull final D dataBefore,
61                                          @Nonnull final D dataAfter,
62                                          @Nonnull final WriteContext writeContext) throws WriteFailedException {
63         throw new UnsupportedOperationException(
64                 "Default implementation of updateCurrentAttributes should not be invoked." +
65                         "Either override this method or do not invoke it directly");
66     }
67
68     /**
69      * Handle delete operation. D from CRUD.
70      *
71      * @param id Identifier(from root) of data being written
72      * @param dataBefore Old data being deleted
73      * @param writeContext Write context can be used to store any useful information and then utilized by other customizers
74      *
75      * @throws WriteFailedException if delete was unsuccessful
76      */
77     void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
78                                  @Nonnull final D dataBefore,
79                                  @Nonnull final WriteContext writeContext) throws WriteFailedException;
80 }