Switch to MD-SAL APIs
[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 package org.opendaylight.openflowplugin.applications.notification.supplier.impl.item;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Collection;
12 import org.opendaylight.mdsal.binding.api.DataBroker;
13 import org.opendaylight.mdsal.binding.api.DataObjectModification;
14 import org.opendaylight.mdsal.binding.api.DataTreeModification;
15 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
16 import org.opendaylight.openflowplugin.applications.notification.supplier.NotificationSupplierForItem;
17 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.AbstractNotificationSupplierBase;
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 public abstract class AbstractNotificationSupplierForItem<O extends DataObject, C extends Notification, U extends
32         Notification, D extends Notification> extends AbstractNotificationSupplierBase<O> implements
33         NotificationSupplierForItem<O, C, U, D> {
34
35     private final NotificationPublishService notificationProviderService;
36
37     /**
38      * Default constructor for all item Notification Supplier implementation.
39      *
40      * @param notifProviderService - notification publisher
41      * @param db                   - DataBroker for DataTreeChangeListener registration
42      * @param clazz                - Statistics Notification Class
43      */
44     public AbstractNotificationSupplierForItem(final NotificationPublishService notifProviderService,
45                                                final DataBroker db, final Class<O> clazz) {
46         super(db, clazz);
47         this.notificationProviderService = Preconditions.checkNotNull(notifProviderService);
48     }
49
50     @Override
51     public void onDataTreeChanged(Collection<DataTreeModification<O>> changes) {
52
53         Preconditions.checkNotNull(changes, "Changes may not be null!");
54
55         for (DataTreeModification<O> change : changes) {
56             final InstanceIdentifier<O> key = change.getRootPath().getRootIdentifier();
57             final DataObjectModification<O> mod = change.getRootNode();
58             switch (mod.getModificationType()) {
59                 case DELETE:
60                     remove(key, mod.getDataBefore());
61                     break;
62                 case SUBTREE_MODIFIED:
63                     update(key, mod.getDataBefore(), mod.getDataAfter());
64                     break;
65                 case WRITE:
66                     if (mod.getDataBefore() == null) {
67                         add(key, mod.getDataAfter());
68                     } else {
69                         update(key, mod.getDataBefore(), mod.getDataAfter());
70                     }
71                     break;
72                 default:
73                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
74             }
75         }
76     }
77
78     public void add(InstanceIdentifier<O> identifier, O add) {
79         putNotification(createNotification(add, identifier));
80     }
81
82     public void remove(InstanceIdentifier<O> identifier, O del) {
83         putNotification(deleteNotification(identifier.firstIdentifierOf(clazz)));
84     }
85
86     public void update(InstanceIdentifier<O> identifier, O before, O after) {
87         putNotification(updateNotification(after, identifier));
88     }
89
90     private void putNotification(Notification notif) {
91         if (notif != null) {
92             try {
93                 notificationProviderService.putNotification(notif);
94             } catch (InterruptedException e) {
95                 throw new IllegalStateException("Interrupted while publishing " + notif, e);
96             }
97         }
98     }
99 }