fd35781193f45a62ca8fe0a0bffc1ee4b1967a5a
[netvirt.git] / netvirt / renderers / neutron / src / main / java / org / opendaylight / netvirt / netvirt / renderers / neutron / DelegatingDataTreeListener.java
1 /*
2  * Copyright (c) 2013, 2016 Red Hat, 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.netvirt.renderers.neutron;
10
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;
17
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;
25
26 /**
27  * Data-tree listener which delegates data processing to a {@link DataProcessor}.
28  */
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;
38
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!"));
45     }
46
47     private void registerListener(final DataBroker db, DataTreeIdentifier<T> treeId) {
48         try {
49             LOG.info("Registering Data Change Listener for {}", getClass().getSimpleName());
50             this.db = db;
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);
55         }
56     }
57
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()) {
63                 case DELETE:
64                     dataProcessor.remove(key, mod.getDataBefore());
65                     break;
66                 case SUBTREE_MODIFIED:
67                     dataProcessor.update(key, mod.getDataBefore(), mod.getDataAfter());
68                     break;
69                 case WRITE:
70                     if (mod.getDataBefore() == null) {
71                         dataProcessor.add(key, mod.getDataAfter());
72                     } else {
73                         dataProcessor.update(key, mod.getDataBefore(), mod.getDataAfter());
74                     }
75                     break;
76                 default:
77                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
78             }
79         }
80     }
81
82     @Override
83     public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<T>> changes) {
84         Preconditions.checkNotNull(changes, "Changes may not be null!");
85         executorService.submit(new Runnable() {
86             @Override
87             public void run() {
88                 processChanges(changes);
89             }
90         });
91     }
92
93     @Override
94     public void close() {
95         if (listenerRegistration != null) {
96             listenerRegistration.close();
97             listenerRegistration = null;
98         }
99     }
100 }