Update comments and imports after DataChangeListener changes
[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
9 package org.opendaylight.openflowplugin.applications.notification.supplier.impl;
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.NotificationSupplierForItemRoot;
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 Root Items
24  * Notification Suppliers
25  *
26  * @param <O> - data tree item Object
27  * @param <C> - Create notification
28  * @param <D> - Delete notification
29  */
30 abstract class AbstractNotificationSupplierForItemRoot<O extends DataObject,
31                                                C extends Notification,
32                                                D extends Notification>
33                     extends AbstractNotificationSupplierBase<O>
34                     implements NotificationSupplierForItemRoot<O, C, D> {
35
36     private final NotificationProviderService notificationProviderService;
37
38     /**
39      * Default constructor for all Root Item Notification Supplier implementation
40      *
41      * @param notificationProviderService - notification publisher
42      * @param db - DataBroker for DataTreeChangeListener registration
43      * @param clazz - Statistics Notification Class
44      */
45     public AbstractNotificationSupplierForItemRoot(final NotificationProviderService notificationProviderService,
46                                                    final DataBroker db,
47                                                    final Class<O> clazz) {
48         super(db, clazz);
49         this.notificationProviderService = Preconditions.checkNotNull(notificationProviderService);
50     }
51
52     @Override
53     public void onDataTreeChanged(Collection<DataTreeModification<O>> changes) {
54
55         Preconditions.checkNotNull(changes, "Changes may not be null!");
56
57         for (DataTreeModification<O> change : changes) {
58             final InstanceIdentifier<O> key = change.getRootPath().getRootIdentifier();
59             final DataObjectModification<O> mod = change.getRootNode();
60             switch (mod.getModificationType()) {
61                 case DELETE:
62                     remove(key, mod.getDataBefore());
63                     break;
64                 case SUBTREE_MODIFIED:
65                     update(key, mod.getDataBefore(), mod.getDataAfter());
66                     break;
67                 case WRITE:
68                     if (mod.getDataBefore() == null) {
69                         add(key, mod.getDataAfter());
70                     } else {
71                         update(key, mod.getDataBefore(), mod.getDataAfter());
72                     }
73                     break;
74                 default:
75                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
76             }
77         }
78     }
79
80
81     public void add(InstanceIdentifier<O> identifier , O add ){
82
83         final C notif = createNotification(add, identifier);
84         if (notif != null) {
85             notificationProviderService.publish(notif);
86         }
87     }
88
89     public void remove(InstanceIdentifier<O> identifier , O del){
90         final D notif = deleteNotification(identifier.firstIdentifierOf(clazz));
91         if (notif != null) {
92             notificationProviderService.publish(notif);
93         }
94     }
95
96     public void update(InstanceIdentifier<O> identifier , O before, O after){
97         //EMPTY NO-OP
98     }
99
100 }
101