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