2 * Copyright (c) 2013, 2016 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netvirt.netvirt.renderers.neutron;
11 import com.google.common.base.Preconditions;
12 import java.util.Collection;
13 import java.util.concurrent.ExecutorService;
14 import java.util.concurrent.Executors;
15 import java.util.concurrent.ThreadFactory;
16 import javax.annotation.Nonnull;
18 import com.google.common.util.concurrent.ThreadFactoryBuilder;
19 import org.opendaylight.controller.md.sal.binding.api.*;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * Data-tree listener which delegates data processing to a {@link DataProcessor}.
29 public class DelegatingDataTreeListener<T extends DataObject> implements AutoCloseable, ClusteredDataTreeChangeListener<T> {
30 private static final Logger LOG = LoggerFactory.getLogger(DelegatingDataTreeListener.class);
31 protected DataBroker db;
32 private static final ThreadFactory threadFactory = new ThreadFactoryBuilder()
33 .setNameFormat("NV-NeutronDTL-%d").build();
34 private final ExecutorService executorService = Executors.newFixedThreadPool(1, threadFactory);
35 private final DataProcessor<T> dataProcessor;
36 private ListenerRegistration<DelegatingDataTreeListener<T>> listenerRegistration;
38 public DelegatingDataTreeListener(DataProcessor<T> dataProcessor,
39 DataBroker db, DataTreeIdentifier<T> treeId) {
40 this.dataProcessor = Preconditions.checkNotNull(dataProcessor, "Data processor can not be null!");
41 registerListener(Preconditions.checkNotNull(db, "Data broker can not be null!"),
42 Preconditions.checkNotNull(treeId, "Tree identifier can not be null!"));
45 private void registerListener(final DataBroker db, DataTreeIdentifier<T> treeId) {
47 LOG.info("Registering Data Change Listener for {}", getClass().getSimpleName());
49 listenerRegistration = db.registerDataTreeChangeListener(treeId, this);
50 } catch (final Exception e) {
51 LOG.warn("{} DataChange listener registration fail!", getClass().getSimpleName(), e);
52 throw new IllegalStateException("DataTreeListener startup fail! System needs restart.", e);
56 private void processChanges(Collection<DataTreeModification<T>> changes) {
57 for (DataTreeModification<T> change : changes) {
58 final InstanceIdentifier<T> key = change.getRootPath().getRootIdentifier();
59 final DataObjectModification<T> mod = change.getRootNode();
60 switch (mod.getModificationType()) {
62 dataProcessor.remove(key, mod.getDataBefore());
64 case SUBTREE_MODIFIED:
65 dataProcessor.update(key, mod.getDataBefore(), mod.getDataAfter());
68 if (mod.getDataBefore() == null) {
69 dataProcessor.add(key, mod.getDataAfter());
71 dataProcessor.update(key, mod.getDataBefore(), mod.getDataAfter());
75 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
81 public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<T>> changes) {
82 Preconditions.checkNotNull(changes, "Changes may not be null!");
83 executorService.submit(new Runnable() {
86 processChanges(changes);
93 if (listenerRegistration != null) {
94 listenerRegistration.close();
95 listenerRegistration = null;