HONEYCOMB-431: add validation support to Writers
[honeycomb.git] / infra / translate-api / src / main / java / io / fd / honeycomb / translate / write / Writer.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.write;
18
19 import com.google.common.annotations.Beta;
20 import io.fd.honeycomb.translate.SubtreeManager;
21 import javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25
26 /**
27  * Base writer, responsible for translation between DataObjects and any other side. Handling all update operations(create,
28  * update, delete)
29  *
30  * @param <D> Specific DataObject derived type, that is handled by this writer
31  */
32 @Beta
33 public interface Writer<D extends DataObject> extends SubtreeManager<D> {
34
35     /**
36      * Validates data modification.
37      *
38      * @param id Identifier of data being validated
39      * @param dataBefore Old data
40      * @param dataAfter New, updated data
41      * @param ctx Write context enabling writer to get information about candidate data as well as current data
42      */
43     default void validate(@Nonnull final InstanceIdentifier<? extends DataObject> id,
44                           @Nullable final DataObject dataBefore,
45                           @Nullable final DataObject dataAfter,
46                           @Nonnull final WriteContext ctx) throws DataValidationFailedException {
47     }
48
49     /**
50      * Process modifications and translate them as create/update/delete operations to lower level
51      *
52      * @param id         Identifier of data being written
53      * @param dataBefore Old data
54      * @param dataAfter  New, updated data
55      * @param ctx        Write context enabling writer to get information about candidate data as well as current data
56      * @throws WriteFailedException if update failed
57      */
58     void processModification(@Nonnull final InstanceIdentifier<? extends DataObject> id,
59                              @Nullable final DataObject dataBefore,
60                              @Nullable final DataObject dataAfter,
61                              @Nonnull final WriteContext ctx) throws WriteFailedException;
62
63     /**
64      * Indicates whether there is direct support for updating nodes handled by writer,
65      * or they must be broken up to individual deletes and creates.
66      */
67     boolean supportsDirectUpdate();
68
69     /**
70      * Returns true if node identified by this identifier can be processes by this writer
71      *
72      * @param instanceIdentifier identifier to be checked
73      */
74     default boolean canProcess(@Nonnull final InstanceIdentifier<? extends DataObject> instanceIdentifier) {
75         return getManagedDataObjectType().equals(instanceIdentifier);
76     }
77 }