Switch to MD-SAL APIs
[openflowplugin.git] / applications / notification-supplier / src / main / java / org / opendaylight / openflowplugin / applications / notification / supplier / impl / item / stat / AbstractNotificationSupplierForItemStat.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.stat;
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.NotificationSupplierForItemStat;
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 Statistics
24  * Notification Suppliers.
25  *
26  * @param <O> - Statistics {@link DataObject}
27  * @param <N> - Statistics Notification
28  */
29 public abstract class AbstractNotificationSupplierForItemStat<O extends DataObject, N extends Notification> extends
30         AbstractNotificationSupplierBase<O> implements NotificationSupplierForItemStat<O, N> {
31
32     private final NotificationPublishService notifProviderService;
33
34     /**
35      * Default constructor for all Statistic Notification Supplier implementation.
36      *
37      * @param notifProviderService - notification publisher
38      * @param db                   - DataBroker for DataTreeChangeListener registration
39      * @param clazz                - Statistics Notification Class
40      */
41     public AbstractNotificationSupplierForItemStat(final NotificationPublishService notifProviderService,
42                                                    final DataBroker db, final Class<O> clazz) {
43         super(db, clazz);
44         this.notifProviderService = Preconditions.checkNotNull(notifProviderService);
45     }
46
47     @Override
48     public void onDataTreeChanged(Collection<DataTreeModification<O>> changes) {
49
50         Preconditions.checkNotNull(changes, "Changes may not be null!");
51
52         for (DataTreeModification<O> change : changes) {
53             final InstanceIdentifier<O> key = change.getRootPath().getRootIdentifier();
54             final DataObjectModification<O> mod = change.getRootNode();
55             switch (mod.getModificationType()) {
56                 case DELETE:
57                     remove(key, mod.getDataBefore());
58                     break;
59                 case SUBTREE_MODIFIED:
60                     update(key, mod.getDataBefore(), mod.getDataAfter());
61                     break;
62                 case WRITE:
63                     if (mod.getDataBefore() == null) {
64                         add(key, mod.getDataAfter());
65                     } else {
66                         update(key, mod.getDataBefore(), mod.getDataAfter());
67                     }
68                     break;
69                 default:
70                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
71             }
72         }
73     }
74
75
76     public void add(InstanceIdentifier<O> identifier, O add) {
77         final N notif = createNotification(add, identifier);
78         if (notif != null) {
79             try {
80                 notifProviderService.putNotification(notif);
81             } catch (InterruptedException e) {
82                 throw new IllegalStateException("Interrupted while publishing " + notif, e);
83             }
84         }
85     }
86
87     public void remove(InstanceIdentifier<O> identifier, O del) {
88         //EMPTY NO-OP
89     }
90
91     public void update(InstanceIdentifier<O> identifier, O before, O after) {
92         //EMPTY NO-OP
93     }
94 }
95