Merge "Add ovsdb/netvirt ui to vpnservice netvirt code."
[netvirt.git] / openstack / sfc-translator / impl / src / main / java / org / opendaylight / netvirt / openstack / sfc / translator / DelegatingDataTreeListener.java
1 /*
2  * Copyright (c) 2013, 2016 Dell, 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.openstack.sfc.translator;
10
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;
23
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;
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 threadFactory = new ThreadFactoryBuilder()
38         .setNameFormat("NeutronSfcListener-%d").build();
39     private final ExecutorService executorService = Executors.newFixedThreadPool(1, threadFactory);
40     private final INeutronSfcDataProcessor<T> dataProcessor;
41     private ListenerRegistration<DelegatingDataTreeListener<T>> listenerRegistration;
42
43     public DelegatingDataTreeListener(DataBroker db, DataTreeIdentifier<T> treeId) {
44         this.dataProcessor = Preconditions.checkNotNull(this, "Data processor can not be null!");
45         registerListener(Preconditions.checkNotNull(db, "Data broker can not be null!"),
46                 Preconditions.checkNotNull(treeId, "Tree identifier can not be null!"));
47     }
48
49     private void registerListener(final DataBroker db, DataTreeIdentifier<T> treeId) {
50         try {
51             LOG.info("Registering Data Change Listener for {}", getClass().getSimpleName());
52             listenerRegistration = db.registerDataTreeChangeListener(treeId, this);
53         } catch (final Exception e) {
54             LOG.warn("{} DataChange listener registration fail!", getClass().getSimpleName(), e);
55             throw new IllegalStateException("DataTreeListener startup fail! System needs restart.", e);
56         }
57     }
58
59     private void processChanges(Collection<DataTreeModification<T>> changes) {
60         LOG.info("onDataTreeChanged: Received Data Tree Changed {}", changes);
61         for (DataTreeModification<T> change : changes) {
62             final InstanceIdentifier<T> key = change.getRootPath().getRootIdentifier();
63             final DataObjectModification<T> mod = change.getRootNode();
64             LOG.info("onDataTreeChanged: Received Data Tree Changed Update of Type={} for Key={}",
65                     mod.getModificationType(), key);
66             switch (mod.getModificationType()) {
67                 case DELETE:
68                     dataProcessor.remove(key, mod.getDataBefore());
69                     break;
70                 case SUBTREE_MODIFIED:
71                     dataProcessor.update(key, mod.getDataBefore(), mod.getDataAfter());
72                     break;
73                 case WRITE:
74                     if (mod.getDataBefore() == null) {
75                         dataProcessor.add(key, mod.getDataAfter());
76                     } else {
77                         dataProcessor.update(key, mod.getDataBefore(), mod.getDataAfter());
78                     }
79                     break;
80                 default:
81                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
82             }
83         }
84     }
85
86     @Override
87     public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<T>> changes) {
88         Preconditions.checkNotNull(changes, "Changes may not be null!");
89         executorService.submit(new Runnable() {
90             @Override
91             public void run() {
92                 processChanges(changes);
93             }
94         });
95     }
96
97     @Override
98     public void close() {
99         if (listenerRegistration != null) {
100             listenerRegistration.close();
101             listenerRegistration = null;
102         }
103         if (executorService != null) {
104             executorService.shutdownNow();
105         }
106     }
107 }