da8aaed3db27dc962364af2c45def56aa463eb63
[openflowplugin.git] / applications / notification-supplier / src / main / java / org / opendaylight / openflowplugin / applications / notification / supplier / impl / item / AbstractNotificationSupplierForItem.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.notification.supplier.impl.item;
10
11 import com.google.common.base.Preconditions;
12 import java.util.Collection;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
16 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
17 import org.opendaylight.openflowplugin.applications.notification.supplier.NotificationSupplierForItem;
18 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.AbstractNotificationSupplierBase;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.opendaylight.yangtools.yang.binding.Notification;
22
23 /**
24  * Class is package protected abstract implementation for all Old Root Items
25  * Notification Suppliers.
26  *
27  * @param <O> - data tree item Object
28  * @param <C> - Create notification
29  * @param <U> - Update notification
30  * @param <D> - Delete notification
31  */
32 public abstract class AbstractNotificationSupplierForItem<O extends DataObject, C extends Notification, U extends
33         Notification, D extends Notification> extends AbstractNotificationSupplierBase<O> implements
34         NotificationSupplierForItem<O, C, U, D> {
35
36     private final NotificationProviderService notificationProviderService;
37
38     /**
39      * Default constructor for all item Notification Supplier implementation.
40      *
41      * @param notifProviderService - notification publisher
42      * @param db                   - DataBroker for DataTreeChangeListener registration
43      * @param clazz                - Statistics Notification Class
44      */
45     public AbstractNotificationSupplierForItem(final NotificationProviderService notifProviderService,
46                                                final DataBroker db, final Class<O> clazz) {
47         super(db, clazz);
48         this.notificationProviderService = Preconditions.checkNotNull(notifProviderService);
49     }
50
51     @Override
52     public void onDataTreeChanged(Collection<DataTreeModification<O>> changes) {
53
54         Preconditions.checkNotNull(changes, "Changes may not be null!");
55
56         for (DataTreeModification<O> change : changes) {
57             final InstanceIdentifier<O> key = change.getRootPath().getRootIdentifier();
58             final DataObjectModification<O> mod = change.getRootNode();
59             switch (mod.getModificationType()) {
60                 case DELETE:
61                     remove(key, mod.getDataBefore());
62                     break;
63                 case SUBTREE_MODIFIED:
64                     update(key, mod.getDataBefore(), mod.getDataAfter());
65                     break;
66                 case WRITE:
67                     if (mod.getDataBefore() == null) {
68                         add(key, mod.getDataAfter());
69                     } else {
70                         update(key, mod.getDataBefore(), mod.getDataAfter());
71                     }
72                     break;
73                 default:
74                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
75             }
76         }
77     }
78
79     public void add(InstanceIdentifier<O> identifier, O add) {
80         final C notif = createNotification(add, identifier);
81         if (notif != null) {
82             notificationProviderService.publish(notif);
83         }
84     }
85
86     public void remove(InstanceIdentifier<O> identifier, O del) {
87         final D notif = deleteNotification(identifier.firstIdentifierOf(clazz));
88         if (notif != null) {
89             notificationProviderService.publish(notif);
90         }
91     }
92
93     public void update(InstanceIdentifier<O> identifier, O before, O after) {
94
95         final U notif = updateNotification(after, identifier);
96         if (notif != null) {
97             notificationProviderService.publish(notif);
98         }
99     }
100 }