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