/* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.openflowplugin.applications.notification.supplier.impl; import com.google.common.base.Preconditions; import java.util.Collection; import org.opendaylight.mdsal.binding.api.DataBroker; import org.opendaylight.mdsal.binding.api.DataObjectModification; import org.opendaylight.mdsal.binding.api.DataTreeModification; import org.opendaylight.mdsal.binding.api.NotificationPublishService; import org.opendaylight.openflowplugin.applications.notification.supplier.NotificationSupplierForItemRoot; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.binding.Notification; /** * Class is package protected abstract implementation for all Root Items * Notification Suppliers. * * @param - data tree item Object * @param - Create notification * @param - Delete notification */ public abstract class AbstractNotificationSupplierForItemRoot extends AbstractNotificationSupplierBase implements NotificationSupplierForItemRoot { private final NotificationPublishService notificationProviderService; /** * Default constructor for all Root Item Notification Supplier implementation. * * @param notificationProviderService - notification publisher * @param db - DataBroker for DataTreeChangeListener registration * @param clazz - Statistics Notification Class */ public AbstractNotificationSupplierForItemRoot(final NotificationPublishService notificationProviderService, final DataBroker db, final Class clazz) { super(db, clazz); this.notificationProviderService = Preconditions.checkNotNull(notificationProviderService); } @Override public void onDataTreeChanged(Collection> changes) { Preconditions.checkNotNull(changes, "Changes may not be null!"); for (DataTreeModification change : changes) { final InstanceIdentifier key = change.getRootPath().getRootIdentifier(); final DataObjectModification mod = change.getRootNode(); switch (mod.getModificationType()) { case DELETE: remove(key, mod.getDataBefore()); break; case SUBTREE_MODIFIED: update(key, mod.getDataBefore(), mod.getDataAfter()); break; case WRITE: if (mod.getDataBefore() == null) { add(key, mod.getDataAfter()); } else { update(key, mod.getDataBefore(), mod.getDataAfter()); } break; default: throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType()); } } } public void add(InstanceIdentifier identifier, O add) { putNotification(createNotification(add, identifier)); } public void remove(InstanceIdentifier identifier, O del) { putNotification(deleteNotification(identifier.firstIdentifierOf(clazz))); } public void update(InstanceIdentifier identifier, O before, O after) { //EMPTY NO-OP } private void putNotification(Notification notif) { if (notif != null) { try { notificationProviderService.putNotification(notif); } catch (InterruptedException e) { throw new IllegalStateException("Interrupted while publishing " + notif, e); } } } }