/* * 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.groupbasedpolicy.util; import static com.google.common.base.Preconditions.checkNotNull; import java.util.Collection; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.binding.api.DataObjectModification; import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType; import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener; import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier; import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; /** * The purpose of this class is to eliminate boilerplate code used in most of * {@link DataTreeChangeListener} implementations. * * @param */ public abstract class DataTreeChangeHandler implements DataTreeChangeListener, AutoCloseable { protected final DataBroker dataProvider; protected final ListenerRegistration> registeredListener; /** * Registers {@link DataTreeChangeListener} for {@code pointOfInterest} by using * {@code dataProvider} * * @param dataProvider cannot be {@code null} * @param pointOfInterest cannot be {@code null} * @throws NullPointerException if at least one paramter is {@code null} */ protected DataTreeChangeHandler(DataBroker dataProvider, DataTreeIdentifier pointOfInterest) { this.dataProvider = checkNotNull(dataProvider); registeredListener = dataProvider.registerDataTreeChangeListener(checkNotNull(pointOfInterest), this); } @Override public void onDataTreeChanged(Collection> changes) { for (DataTreeModification change : changes) { DataObjectModification rootNode = change.getRootNode(); InstanceIdentifier rootIdentifier = change.getRootPath().getRootIdentifier(); switch (rootNode.getModificationType()) { case WRITE: onWrite(rootNode, rootIdentifier); break; case DELETE: onDelete(rootNode, rootIdentifier); break; case SUBTREE_MODIFIED: onSubtreeModified(rootNode, rootIdentifier); break; } } } /** * Handles case where {@link DataObjectModification#getModificationType()} is * {@link ModificationType#WRITE}.
* Parameters of this method are never {@code null}. * * @param rootNode represents {@link DataObjectModification} as result of * {@link DataTreeModification#getRootNode()} * @param rootIdentifier represents {@link InstanceIdentifier} obtained from result of * {@link DataTreeModification#getRootPath()} */ protected abstract void onWrite(DataObjectModification rootNode, InstanceIdentifier rootIdentifier); /** * Handles case where {@link DataObjectModification#getModificationType()} is * {@link ModificationType#DELETE}.
* Parameters of this method are never {@code null}. * * @param rootNode represents {@link DataObjectModification} as result of * {@link DataTreeModification#getRootNode()} * @param rootIdentifier represents {@link InstanceIdentifier} obtained from result of * {@link DataTreeModification#getRootPath()} */ protected abstract void onDelete(DataObjectModification rootNode, InstanceIdentifier rootIdentifier); /** * Handles case where {@link DataObjectModification#getModificationType()} is * {@link ModificationType#SUBTREE_MODIFIED}.
* Parameters of this method are never {@code null}. * * @param rootNode represents {@link DataObjectModification} as result of * {@link DataTreeModification#getRootNode()} * @param rootIdentifier represents {@link InstanceIdentifier} obtained from result of * {@link DataTreeModification#getRootPath()} */ protected abstract void onSubtreeModified(DataObjectModification rootNode, InstanceIdentifier rootIdentifier); @Override public void close() throws Exception { registeredListener.close(); } }