Remove JSR305 annotations from tools-api
[serviceutils.git] / tools / api / src / main / java / org / opendaylight / serviceutils / tools / mdsal / listener / DataTreeChangeListenerActions.java
1 /*
2  * Copyright (c) 2018 Ericsson, S.A. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.serviceutils.tools.mdsal.listener;
9
10 import java.util.Collection;
11 import javax.inject.Singleton;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
14 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
15 import org.opendaylight.yangtools.yang.binding.DataObject;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17
18 /**
19  * Interface to be implemented by classes interested in receiving notifications
20  * about data tree changes. It implements a default method to handle the data
21  * tree modifications. Those notifications will be forwarded to the appropriate
22  * methods (add, update, remove) depending on their action type. The listeners
23  * implementing this interface will need to be annotated as {@link Singleton}.
24  *
25  * @param <T> type of the data object the listener is registered to.
26  * @author David Suárez (david.suarez.fuentes@gmail.com)
27  * @deprecated Use {@code listener-api} instead.
28  */
29 @Deprecated
30 interface DataTreeChangeListenerActions<T extends DataObject> {
31
32     /**
33      * Default method invoked upon data tree change, in turn it calls the
34      * appropriate method (add, update, remove) depending on the type of change.
35      *
36      * @param changes          collection of changes
37      * @param dataStoreMetrics data store metrics
38      */
39     @SuppressWarnings("checkstyle:MissingSwitchDefault") // http://errorprone.info/bugpattern/UnnecessaryDefaultInEnumSwitch
40     default void onDataTreeChanged(@NonNull Collection<DataTreeModification<T>> changes,
41                                    DataStoreMetrics dataStoreMetrics) {
42         // This code is also in DataTreeEventCallbackRegistrarImpl and any changes should be applied there as well
43         for (DataTreeModification<T> dataTreeModification : changes) {
44             InstanceIdentifier<T> instanceIdentifier = dataTreeModification.getRootPath().getRootIdentifier();
45             DataObjectModification<T> dataObjectModification = dataTreeModification.getRootNode();
46             T dataBefore = dataObjectModification.getDataBefore();
47             T dataAfter = dataObjectModification.getDataAfter();
48
49             switch (dataObjectModification.getModificationType()) {
50                 case SUBTREE_MODIFIED:
51                     if (dataStoreMetrics != null) {
52                         dataStoreMetrics.incrementUpdated();
53                     }
54                     update(instanceIdentifier, dataBefore, dataAfter);
55                     break;
56                 case DELETE:
57                     if (dataStoreMetrics != null) {
58                         dataStoreMetrics.incrementDeleted();
59                     }
60                     remove(instanceIdentifier, dataBefore);
61                     break;
62                 case WRITE:
63                     if (dataBefore == null) {
64                         if (dataStoreMetrics != null) {
65                             dataStoreMetrics.incrementAdded();
66                         }
67                         add(instanceIdentifier, dataAfter);
68                     } else {
69                         if (dataStoreMetrics != null) {
70                             dataStoreMetrics.incrementUpdated();
71                         }
72                         update(instanceIdentifier, dataBefore, dataAfter);
73                     }
74                     break;
75             }
76         }
77     }
78
79     /**
80      * Invoked when a new data object is added.
81      *
82      * @param instanceIdentifier instance id for this data object
83      * @param newDataObject      newly added object
84      */
85     @SuppressWarnings("InconsistentOverloads") // TODO remove when @Deprecated add() is removed
86     default void add(@NonNull InstanceIdentifier<T> instanceIdentifier, @NonNull T newDataObject) {
87         add(newDataObject);
88     }
89
90     /**
91      * Invoked when a new data object added.
92      *
93      * @param newDataObject newly added object
94      */
95     @Deprecated
96     void add(@NonNull T newDataObject);
97
98     /**
99      * Invoked when the data object has been removed.
100      *
101      * @param instanceIdentifier instance id for this data object
102      * @param removedDataObject  existing object being removed
103      */
104     @SuppressWarnings("InconsistentOverloads") // TODO remove when @Deprecated remove() is removed
105     default void remove(@NonNull InstanceIdentifier<T> instanceIdentifier, @NonNull T removedDataObject) {
106         remove(removedDataObject);
107     }
108
109     /**
110      * Invoked when the data object has been removed.
111      *
112      * @param removedDataObject existing object being removed
113      */
114     @Deprecated
115     void remove(@NonNull T removedDataObject);
116
117     /**
118      * Invoked when there is a change in the data object.
119      *
120      * @param instanceIdentifier instance id for this data object
121      * @param originalDataObject existing object being modified
122      * @param updatedDataObject  modified data object
123      */
124     @SuppressWarnings("InconsistentOverloads") // TODO remove when @Deprecated update() is removed
125     default void update(@NonNull InstanceIdentifier<T> instanceIdentifier, @NonNull T originalDataObject,
126             @NonNull T updatedDataObject) {
127         update(originalDataObject, updatedDataObject);
128     }
129
130     /**
131      * Invoked when there is a change in the data object.
132      *
133      * @param originalDataObject existing object being modified
134      * @param updatedDataObject  modified data object
135      */
136     @Deprecated
137     void update(@NonNull T originalDataObject, @NonNull T updatedDataObject);
138 }