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.genius.tools.mdsal.listener;
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;
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)
28 interface DataTreeChangeListenerActions<T extends DataObject> {
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.
34 * @param changes collection of changes
35 * @param dataStoreMetrics data store metrics
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();
47 switch (dataObjectModification.getModificationType()) {
48 case SUBTREE_MODIFIED:
49 if (dataStoreMetrics != null) {
50 dataStoreMetrics.incrementUpdated();
52 update(instanceIdentifier, dataBefore, dataAfter);
55 if (dataStoreMetrics != null) {
56 dataStoreMetrics.incrementDeleted();
58 remove(instanceIdentifier, dataBefore);
61 if (dataBefore == null) {
62 if (dataStoreMetrics != null) {
63 dataStoreMetrics.incrementAdded();
65 add(instanceIdentifier, dataAfter);
67 if (dataStoreMetrics != null) {
68 dataStoreMetrics.incrementUpdated();
70 update(instanceIdentifier, dataBefore, dataAfter);
78 * Invoked when a new data object is added.
80 * @param instanceIdentifier instance id for this data object
81 * @param newDataObject newly added object
83 @SuppressWarnings("InconsistentOverloads") // TODO remove when @Deprecated add() is removed
84 default void add(@Nonnull InstanceIdentifier<T> instanceIdentifier, @Nonnull T newDataObject) {
89 * Invoked when a new data object added.
91 * @param newDataObject newly added object
94 void add(@Nonnull T newDataObject);
97 * Invoked when the data object has been removed.
99 * @param instanceIdentifier instance id for this data object
100 * @param removedDataObject existing object being removed
102 @SuppressWarnings("InconsistentOverloads") // TODO remove when @Deprecated remove() is removed
103 default void remove(@Nonnull InstanceIdentifier<T> instanceIdentifier, @Nonnull T removedDataObject) {
104 remove(removedDataObject);
108 * Invoked when the data object has been removed.
110 * @param removedDataObject existing object being removed
113 void remove(@Nonnull T removedDataObject);
116 * Invoked when there is a change in the data object.
118 * @param instanceIdentifier instance id for this data object
119 * @param originalDataObject existing object being modified
120 * @param updatedDataObject modified data object
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);
129 * Invoked when there is a change in the data object.
131 * @param originalDataObject existing object being modified
132 * @param updatedDataObject modified data object
135 void update(@Nonnull T originalDataObject, @Nonnull T updatedDataObject);