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 NeutronProvider provider;
32 protected DataBroker db;
33 private static final ThreadFactory threadFactory = new ThreadFactoryBuilder()
34 .setNameFormat("NV-NeutronDTL-%d").build();
35 private final ExecutorService executorService = Executors.newFixedThreadPool(1, threadFactory);
36 private final DataProcessor<T> dataProcessor;
37 private ListenerRegistration<DelegatingDataTreeListener<T>> listenerRegistration;
39 public DelegatingDataTreeListener(NeutronProvider provider, DataProcessor<T> dataProcessor,
40 DataBroker db, DataTreeIdentifier<T> treeId) {
41 this.provider = Preconditions.checkNotNull(provider, "provider can not be null!");
42 this.dataProcessor = Preconditions.checkNotNull(dataProcessor, "Data processor can not be null!");
43 registerListener(Preconditions.checkNotNull(db, "Data broker can not be null!"),
44 Preconditions.checkNotNull(treeId, "Tree identifier can not be null!"));
47 private void registerListener(final DataBroker db, DataTreeIdentifier<T> treeId) {
49 LOG.info("Registering Data Change Listener for {}", getClass().getSimpleName());
51 listenerRegistration = db.registerDataTreeChangeListener(treeId, this);
52 } catch (final Exception e) {
53 LOG.warn("{} DataChange listener registration fail!", getClass().getSimpleName(), e);
54 throw new IllegalStateException("DataTreeListener startup fail! System needs restart.", e);
58 private void processChanges(Collection<DataTreeModification<T>> changes) {
59 for (DataTreeModification<T> change : changes) {
60 final InstanceIdentifier<T> key = change.getRootPath().getRootIdentifier();
61 final DataObjectModification<T> mod = change.getRootNode();
62 switch (mod.getModificationType()) {
64 dataProcessor.remove(key, mod.getDataBefore());
66 case SUBTREE_MODIFIED:
67 dataProcessor.update(key, mod.getDataBefore(), mod.getDataAfter());
70 if (mod.getDataBefore() == null) {
71 dataProcessor.add(key, mod.getDataAfter());
73 dataProcessor.update(key, mod.getDataBefore(), mod.getDataAfter());
77 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
83 public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<T>> changes) {
84 Preconditions.checkNotNull(changes, "Changes may not be null!");
85 executorService.submit(new Runnable() {
88 processChanges(changes);
95 if (listenerRegistration != null) {
96 listenerRegistration.close();
97 listenerRegistration = null;