2 * Copyright (c) 2013, 2016 Dell, 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.openstack.sfc.translator;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.ThreadFactoryBuilder;
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.DataTreeChangeListener;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
17 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
18 import org.opendaylight.yangtools.concepts.ListenerRegistration;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import javax.annotation.Nonnull;
25 import java.util.Collection;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
28 import java.util.concurrent.ThreadFactory;
31 * Data-tree listener which delegates data processing to a {@link INeutronSfcDataProcessor}.
33 public class DelegatingDataTreeListener<T extends DataObject> implements AutoCloseable, DataTreeChangeListener<T> {
34 private static final Logger LOG = LoggerFactory.getLogger(DelegatingDataTreeListener.class);
35 private static final ThreadFactory threadFactory = new ThreadFactoryBuilder()
36 .setNameFormat("NeutronSfcListener-%d").build();
37 private final ExecutorService executorService = Executors.newFixedThreadPool(1, threadFactory);
38 private final INeutronSfcDataProcessor<T> dataProcessor;
39 private ListenerRegistration<DelegatingDataTreeListener<T>> listenerRegistration;
41 public DelegatingDataTreeListener(INeutronSfcDataProcessor<T> dataProcessor,
42 DataBroker db, DataTreeIdentifier<T> treeId) {
43 this.dataProcessor = Preconditions.checkNotNull(dataProcessor, "Data processor can not be null!");
44 registerListener(Preconditions.checkNotNull(db, "Data broker can not be null!"),
45 Preconditions.checkNotNull(treeId, "Tree identifier can not be null!"));
48 private void registerListener(final DataBroker db, DataTreeIdentifier<T> treeId) {
50 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 LOG.info("onDataTreeChanged: Received Data Tree Changed {}", changes);
60 for (DataTreeModification<T> change : changes) {
61 final InstanceIdentifier<T> key = change.getRootPath().getRootIdentifier();
62 final DataObjectModification<T> mod = change.getRootNode();
63 LOG.info("onDataTreeChanged: Received Data Tree Changed Update of Type={} for Key={}",
64 mod.getModificationType(), key);
65 switch (mod.getModificationType()) {
67 dataProcessor.remove(key, mod.getDataBefore());
69 case SUBTREE_MODIFIED:
70 dataProcessor.update(key, mod.getDataBefore(), mod.getDataAfter());
73 if (mod.getDataBefore() == null) {
74 dataProcessor.add(key, mod.getDataAfter());
76 dataProcessor.update(key, mod.getDataBefore(), mod.getDataAfter());
80 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
86 public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<T>> changes) {
87 Preconditions.checkNotNull(changes, "Changes may not be null!");
88 executorService.submit(new Runnable() {
91 processChanges(changes);
98 if (listenerRegistration != null) {
99 listenerRegistration.close();
100 listenerRegistration = null;
102 if (executorService != null) {
103 executorService.shutdownNow();