Add blueprint wiring for neutron renderer
[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 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;
37
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!"));
43     }
44
45     private void registerListener(final DataBroker db, DataTreeIdentifier<T> treeId) {
46         try {
47             LOG.info("Registering Data Change Listener for {}", getClass().getSimpleName());
48             this.db = db;
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);
53         }
54     }
55
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()) {
61                 case DELETE:
62                     dataProcessor.remove(key, mod.getDataBefore());
63                     break;
64                 case SUBTREE_MODIFIED:
65                     dataProcessor.update(key, mod.getDataBefore(), mod.getDataAfter());
66                     break;
67                 case WRITE:
68                     if (mod.getDataBefore() == null) {
69                         dataProcessor.add(key, mod.getDataAfter());
70                     } else {
71                         dataProcessor.update(key, mod.getDataBefore(), mod.getDataAfter());
72                     }
73                     break;
74                 default:
75                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
76             }
77         }
78     }
79
80     @Override
81     public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<T>> changes) {
82         Preconditions.checkNotNull(changes, "Changes may not be null!");
83         executorService.submit(new Runnable() {
84             @Override
85             public void run() {
86                 processChanges(changes);
87             }
88         });
89     }
90
91     @Override
92     public void close() {
93         if (listenerRegistration != null) {
94             listenerRegistration.close();
95             listenerRegistration = null;
96         }
97     }
98 }