Switch to MD-SAL APIs
[openflowplugin.git] / applications / notification-supplier / src / main / java / org / opendaylight / openflowplugin / applications / notification / supplier / impl / AbstractNotificationSupplierForItemRoot.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;
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.NotificationSupplierForItemRoot;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.opendaylight.yangtools.yang.binding.Notification;
20
21 /**
22  * Class is package protected abstract implementation for all Root Items
23  * Notification Suppliers.
24  *
25  * @param <O> - data tree item Object
26  * @param <C> - Create notification
27  * @param <D> - Delete notification
28  */
29 public abstract class AbstractNotificationSupplierForItemRoot<O extends DataObject, C extends Notification, D extends
30         Notification> extends AbstractNotificationSupplierBase<O> implements NotificationSupplierForItemRoot<O, C, D> {
31
32     private final NotificationPublishService notificationProviderService;
33
34     /**
35      * Default constructor for all Root Item Notification Supplier implementation.
36      *
37      * @param notificationProviderService - notification publisher
38      * @param db                          - DataBroker for DataTreeChangeListener registration
39      * @param clazz                       - Statistics Notification Class
40      */
41     public AbstractNotificationSupplierForItemRoot(final NotificationPublishService notificationProviderService,
42                                                    final DataBroker db, final Class<O> clazz) {
43         super(db, clazz);
44         this.notificationProviderService = Preconditions.checkNotNull(notificationProviderService);
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         putNotification(createNotification(add, identifier));
78     }
79
80     public void remove(InstanceIdentifier<O> identifier, O del) {
81         putNotification(deleteNotification(identifier.firstIdentifierOf(clazz)));
82     }
83
84     public void update(InstanceIdentifier<O> identifier, O before, O after) {
85         //EMPTY NO-OP
86     }
87
88     private void putNotification(Notification notif) {
89         if (notif != null) {
90             try {
91                 notificationProviderService.putNotification(notif);
92             } catch (InterruptedException e) {
93                 throw new IllegalStateException("Interrupted while publishing " + notif, e);
94             }
95         }
96     }
97 }
98