increase code quality in tools api/testutils by using infrautils' parent
[serviceutils.git] / api / src / main / java / org / opendaylight / genius / 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.genius.tools.mdsal.listener;
9
10 import java.util.Collection;
11 import javax.annotation.Nonnull;
12 import javax.inject.Singleton;
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  */
28 interface DataTreeChangeListenerActions<T extends DataObject> {
29
30     /**
31      * Default method invoked upon data tree change, in turn it calls the
32      * appropriate method (add, update, remove) depending on the type of change.
33      *
34      * @param changes          collection of changes
35      * @param dataStoreMetrics data store metrics
36      */
37     @SuppressWarnings("checkstyle:MissingSwitchDefault") // http://errorprone.info/bugpattern/UnnecessaryDefaultInEnumSwitch
38     default void onDataTreeChanged(@Nonnull Collection<DataTreeModification<T>> changes,
39                                    DataStoreMetrics dataStoreMetrics) {
40         // This code is also in DataTreeEventCallbackRegistrarImpl and any changes should be applied there as well
41         for (DataTreeModification<T> dataTreeModification : changes) {
42             InstanceIdentifier<T> instanceIdentifier = dataTreeModification.getRootPath().getRootIdentifier();
43             DataObjectModification<T> dataObjectModification = dataTreeModification.getRootNode();
44             T dataBefore = dataObjectModification.getDataBefore();
45             T dataAfter = dataObjectModification.getDataAfter();
46
47             switch (dataObjectModification.getModificationType()) {
48                 case SUBTREE_MODIFIED:
49                     if (dataStoreMetrics != null) {
50                         dataStoreMetrics.incrementUpdated();
51                     }
52                     update(instanceIdentifier, dataBefore, dataAfter);
53                     break;
54                 case DELETE:
55                     if (dataStoreMetrics != null) {
56                         dataStoreMetrics.incrementDeleted();
57                     }
58                     remove(instanceIdentifier, dataBefore);
59                     break;
60                 case WRITE:
61                     if (dataBefore == null) {
62                         if (dataStoreMetrics != null) {
63                             dataStoreMetrics.incrementAdded();
64                         }
65                         add(instanceIdentifier, dataAfter);
66                     } else {
67                         if (dataStoreMetrics != null) {
68                             dataStoreMetrics.incrementUpdated();
69                         }
70                         update(instanceIdentifier, dataBefore, dataAfter);
71                     }
72                     break;
73             }
74         }
75     }
76
77     /**
78      * Invoked when a new data object is added.
79      *
80      * @param instanceIdentifier instance id for this data object
81      * @param newDataObject      newly added object
82      */
83     @SuppressWarnings("InconsistentOverloads") // TODO remove when @Deprecated add() is removed
84     default void add(@Nonnull InstanceIdentifier<T> instanceIdentifier, @Nonnull T newDataObject) {
85         add(newDataObject);
86     }
87
88     /**
89      * Invoked when a new data object added.
90      *
91      * @param newDataObject newly added object
92      */
93     @Deprecated
94     void add(@Nonnull T newDataObject);
95
96     /**
97      * Invoked when the data object has been removed.
98      *
99      * @param instanceIdentifier instance id for this data object
100      * @param removedDataObject  existing object being removed
101      */
102     @SuppressWarnings("InconsistentOverloads") // TODO remove when @Deprecated remove() is removed
103     default void remove(@Nonnull InstanceIdentifier<T> instanceIdentifier, @Nonnull T removedDataObject) {
104         remove(removedDataObject);
105     }
106
107     /**
108      * Invoked when the data object has been removed.
109      *
110      * @param removedDataObject existing object being removed
111      */
112     @Deprecated
113     void remove(@Nonnull T removedDataObject);
114
115     /**
116      * Invoked when there is a change in the data object.
117      *
118      * @param instanceIdentifier instance id for this data object
119      * @param originalDataObject existing object being modified
120      * @param updatedDataObject  modified data object
121      */
122     @SuppressWarnings("InconsistentOverloads") // TODO remove when @Deprecated update() is removed
123     default void update(@Nonnull InstanceIdentifier<T> instanceIdentifier, @Nonnull T originalDataObject,
124                         @Nonnull T updatedDataObject) {
125         update(originalDataObject, updatedDataObject);
126     }
127
128     /**
129      * Invoked when there is a change in the data object.
130      *
131      * @param originalDataObject existing object being modified
132      * @param updatedDataObject  modified data object
133      */
134     @Deprecated
135     void update(@Nonnull T originalDataObject, @Nonnull T updatedDataObject);
136 }