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