003605e1d00666d5b672a196979b5930a542e9f5
[netvirt.git] / sfc / translator / src / main / java / org / opendaylight / netvirt / sfc / translator / DelegatingDataTreeListener.java
1 /*
2  * Copyright (c) 2016, 2017 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netvirt.sfc.translator;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.ThreadFactoryBuilder;
13 import java.util.Collection;
14 import java.util.concurrent.ExecutorService;
15 import java.util.concurrent.Executors;
16 import java.util.concurrent.ThreadFactory;
17 import javax.annotation.Nonnull;
18 import javax.annotation.Nullable;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
22 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
23 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Data-tree listener which delegates data processing to a {@link INeutronSfcDataProcessor}.
32  */
33 public abstract class DelegatingDataTreeListener<T extends DataObject> implements AutoCloseable,
34         DataTreeChangeListener<T>,
35         INeutronSfcDataProcessor<T> {
36     private static final Logger LOG = LoggerFactory.getLogger(DelegatingDataTreeListener.class);
37     private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder()
38         .setNameFormat("NeutronSfcListener-%d").build();
39     private final ExecutorService executorService = Executors.newFixedThreadPool(1, THREAD_FACTORY);
40     private final INeutronSfcDataProcessor<T> dataProcessor;
41     @Nullable
42     private ListenerRegistration<DelegatingDataTreeListener<T>> listenerRegistration;
43
44     public DelegatingDataTreeListener(DataBroker db, DataTreeIdentifier<T> treeId) {
45         this.dataProcessor = Preconditions.checkNotNull(this, "Data processor can not be null!");
46         registerListener(Preconditions.checkNotNull(db, "Data broker can not be null!"),
47                 Preconditions.checkNotNull(treeId, "Tree identifier can not be null!"));
48     }
49
50     // TODO Clean up the exception handling
51     @SuppressWarnings("checkstyle:IllegalCatch")
52     private void registerListener(final DataBroker db, DataTreeIdentifier<T> treeId) {
53         try {
54             LOG.info("Registering Data Change Listener for {}", getClass().getSimpleName());
55             listenerRegistration = db.registerDataTreeChangeListener(treeId, this);
56         } catch (final Exception e) {
57             LOG.warn("{} DataChange listener registration fail!", getClass().getSimpleName(), e);
58             throw new IllegalStateException("DataTreeListener startup fail! System needs restart.", e);
59         }
60     }
61
62     private void processChanges(Collection<DataTreeModification<T>> changes) {
63         LOG.info("onDataTreeChanged: Received Data Tree Changed {}", changes);
64         for (DataTreeModification<T> change : changes) {
65             final InstanceIdentifier<T> key = change.getRootPath().getRootIdentifier();
66             final DataObjectModification<T> mod = change.getRootNode();
67             LOG.info("onDataTreeChanged: Received Data Tree Changed Update of Type={} for Key={}",
68                     mod.getModificationType(), key);
69             switch (mod.getModificationType()) {
70                 case DELETE:
71                     dataProcessor.remove(mod.getDataBefore());
72                     break;
73                 case SUBTREE_MODIFIED:
74                     dataProcessor.update(mod.getDataBefore(), mod.getDataAfter());
75                     break;
76                 case WRITE:
77                     if (mod.getDataBefore() == null) {
78                         dataProcessor.add(mod.getDataAfter());
79                     } else {
80                         dataProcessor.update(mod.getDataBefore(), mod.getDataAfter());
81                     }
82                     break;
83                 default:
84                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
85             }
86         }
87     }
88
89     @Override
90     public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<T>> changes) {
91         Preconditions.checkNotNull(changes, "Changes may not be null!");
92         executorService.execute(() -> processChanges(changes));
93     }
94
95     @Override
96     public void close() {
97         if (listenerRegistration != null) {
98             listenerRegistration.close();
99             listenerRegistration = null;
100         }
101         executorService.shutdownNow();
102     }
103 }