BUG-4117: add support for flow old Notif
[openflowplugin.git] / applications / old-notification-supplier / src / main / java / org / opendaylight / openflowplugin / applications / old / notification / supplier / impl / item / AbstractNotifSupplierForItem.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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
9 package org.opendaylight.openflowplugin.applications.old.notification.supplier.impl.item;
10
11 import com.google.common.base.Preconditions;
12 import java.util.Map.Entry;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
15 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
16 import org.opendaylight.openflowplugin.applications.old.notification.supplier.OldNotifSupplierForItem;
17 import org.opendaylight.openflowplugin.applications.old.notification.supplier.impl.AbstractNotifSupplierBase;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 import org.opendaylight.yangtools.yang.binding.Notification;
21
22 /**
23  * Class is package protected abstract implementation for all Old Root Items
24  * Notification Suppliers
25  *
26  * @param <O> - data tree item Object
27  * @param <C> - Create notification
28  * @param <U> - Update notification
29  * @param <D> - Delete notification
30  */
31 abstract class AbstractNotifSupplierForItem<O extends DataObject,
32                                             C extends Notification,
33                                             U extends Notification,
34                                             D extends Notification>
35                     extends AbstractNotifSupplierBase<O>
36                     implements OldNotifSupplierForItem<O, C, U, D> {
37
38     private final NotificationProviderService notifProviderService;
39
40     /**
41      * Default constructor for all item Notification Supplier implementation
42      *
43      * @param notifProviderService - notification publisher
44      * @param db - DataBroker for DataChangeEvent registration
45      * @param clazz - Statistics Notification Class
46      */
47     public AbstractNotifSupplierForItem(final NotificationProviderService notifProviderService, final DataBroker db,
48             final Class<O> clazz) {
49         super(db, clazz);
50         this.notifProviderService = Preconditions.checkNotNull(notifProviderService);
51     }
52
53     @Override
54     public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
55         Preconditions.checkArgument(change != null, "ChangeEvent can not be null!");
56         if (change.getCreatedData() != null && !(change.getCreatedData().isEmpty())) {
57             for (final Entry<InstanceIdentifier<?>, DataObject> createDataObj : change.getCreatedData().entrySet()) {
58                 if (clazz.isAssignableFrom(createDataObj.getKey().getTargetType())) {
59                     final InstanceIdentifier<O> ii = createDataObj.getKey().firstIdentifierOf(clazz);
60                     final C notif = createNotification((O) createDataObj.getValue(), ii);
61                     if (notif != null) {
62                         notifProviderService.publish(notif);
63                     }
64                 }
65             }
66         }
67
68         if (change.getUpdatedData() != null && !(change.getUpdatedData().isEmpty())) {
69             for (final Entry<InstanceIdentifier<?>, DataObject> updateDataObj : change.getUpdatedData().entrySet()) {
70                 if (clazz.isAssignableFrom(updateDataObj.getKey().getTargetType())) {
71                     final InstanceIdentifier<O> ii = updateDataObj.getKey().firstIdentifierOf(clazz);
72                     final U notif = updateNotification((O) updateDataObj.getValue(), ii);
73                     if (notif != null) {
74                         notifProviderService.publish(notif);
75                     }
76                 }
77             }
78         }
79
80         if (change.getRemovedPaths() != null && !(change.getRemovedPaths().isEmpty())) {
81             for (final InstanceIdentifier<?> deleteDataPath : change.getRemovedPaths()) {
82                 if (clazz.isAssignableFrom(deleteDataPath.getTargetType())) {
83                     final D notif = deleteNotification(deleteDataPath.firstIdentifierOf(clazz));
84                     if (notif != null) {
85                         notifProviderService.publish(notif);
86                     }
87                 }
88             }
89         }
90     }
91 }