2 * Copyright (c) 2018 Ericsson, S.A. and others. All rights reserved.
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
8 package org.opendaylight.serviceutils.tools.mdsal.listener;
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;
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}.
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.
30 interface DataTreeChangeListenerActions<T extends DataObject> {
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.
36 * @param changes collection of changes
37 * @param dataStoreMetrics data store metrics
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();
49 switch (dataObjectModification.getModificationType()) {
50 case SUBTREE_MODIFIED:
51 if (dataStoreMetrics != null) {
52 dataStoreMetrics.incrementUpdated();
54 update(instanceIdentifier, dataBefore, dataAfter);
57 if (dataStoreMetrics != null) {
58 dataStoreMetrics.incrementDeleted();
60 remove(instanceIdentifier, dataBefore);
63 if (dataBefore == null) {
64 if (dataStoreMetrics != null) {
65 dataStoreMetrics.incrementAdded();
67 add(instanceIdentifier, dataAfter);
69 if (dataStoreMetrics != null) {
70 dataStoreMetrics.incrementUpdated();
72 update(instanceIdentifier, dataBefore, dataAfter);
80 * Invoked when a new data object is added.
82 * @param instanceIdentifier instance id for this data object
83 * @param newDataObject newly added object
85 @SuppressWarnings("InconsistentOverloads") // TODO remove when @Deprecated add() is removed
86 default void add(@NonNull InstanceIdentifier<T> instanceIdentifier, @NonNull T newDataObject) {
91 * Invoked when a new data object added.
93 * @param newDataObject newly added object
96 void add(@NonNull T newDataObject);
99 * Invoked when the data object has been removed.
101 * @param instanceIdentifier instance id for this data object
102 * @param removedDataObject existing object being removed
104 @SuppressWarnings("InconsistentOverloads") // TODO remove when @Deprecated remove() is removed
105 default void remove(@NonNull InstanceIdentifier<T> instanceIdentifier, @NonNull T removedDataObject) {
106 remove(removedDataObject);
110 * Invoked when the data object has been removed.
112 * @param removedDataObject existing object being removed
115 void remove(@NonNull T removedDataObject);
118 * Invoked when there is a change in the data object.
120 * @param instanceIdentifier instance id for this data object
121 * @param originalDataObject existing object being modified
122 * @param updatedDataObject modified data object
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);
131 * Invoked when there is a change in the data object.
133 * @param originalDataObject existing object being modified
134 * @param updatedDataObject modified data object
137 void update(@NonNull T originalDataObject, @NonNull T updatedDataObject);