1b31c35a2e35f3c7969b29fc1e3fb6f0de18a161
[lispflowmapping.git] / mappingservice / neutron / src / main / java / org / opendaylight / lispflowmapping / neutron / DelegatingDataTreeListener.java
1 /*
2  * Copyright (c) 2016 Cisco 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 package org.opendaylight.lispflowmapping.neutron;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Collection;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
14 import org.opendaylight.mdsal.binding.api.DataBroker;
15 import org.opendaylight.mdsal.binding.api.DataObjectModification;
16 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
17 import org.opendaylight.mdsal.binding.api.DataTreeModification;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class DelegatingDataTreeListener<T extends DataObject> implements ClusteredDataTreeChangeListener<T>,
27         AutoCloseable {
28     private static final Logger LOG = LoggerFactory.getLogger(DelegatingDataTreeListener.class);
29     private final DataProcessor<T> dataProcessor;
30     private ListenerRegistration<ClusteredDataTreeChangeListener<T>> dataTreeChangeListenerRegistration;
31
32     public DelegatingDataTreeListener(DataProcessor<T> dataProcessor, DataBroker dataBroker, DataTreeIdentifier<T>
33             dataTreeIdentifier) {
34         Preconditions.checkNotNull(dataBroker, "Can not instantiate Listener! Broker is null!");
35         Preconditions.checkNotNull(dataTreeIdentifier, "DataTreeIndentifier can not be null!");
36         this.dataProcessor = dataProcessor;
37         dataTreeChangeListenerRegistration = dataBroker.registerDataTreeChangeListener(dataTreeIdentifier ,this);
38     }
39
40     public static <T extends DataObject> DelegatingDataTreeListener initiateListener(
41             Class<T> dataObject, ILispNeutronService lispNeutronService, DataBroker dataBroker) {
42
43         if (dataObject == Network.class) {
44             return new NetworkListener(new NetworkDataProcessor(lispNeutronService), dataBroker);
45         } else if (dataObject == Subnet.class) {
46             return new SubnetListener(new SubnetDataProcessor(lispNeutronService), dataBroker);
47         } else if (dataObject == Port.class) {
48             return new PortListener(new PortDataProcessor(lispNeutronService), dataBroker);
49         }
50         LOG.debug(dataObject.getName() + " listener can not be instantiated.");
51         return null;
52     }
53
54     @Override
55     public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<T>> changes) {
56         for (DataTreeModification<T> change : changes) {
57             DataObjectModification<T> mod = change.getRootNode();
58
59             if (mod.getModificationType() == DataObjectModification.ModificationType.WRITE) {
60                 T object = mod.getDataAfter();
61                 dataProcessor.create(object);
62                 LOG.info(object.toString() + " created.");
63             } else if (mod.getModificationType() == DataObjectModification.ModificationType.DELETE) {
64                 T object = mod.getDataBefore();
65                 dataProcessor.delete(object);
66                 LOG.info(object.toString() + " removed.");
67             } else {
68                 T object = mod.getDataAfter();
69                 dataProcessor.update(object);
70                 LOG.info(object.toString() + " updated.");
71             }
72         }
73     }
74
75     @Override
76     public void close() throws Exception {
77         if (dataTreeChangeListenerRegistration != null) {
78             dataTreeChangeListenerRegistration.close();
79             dataTreeChangeListenerRegistration = null;
80
81             LOG.info(this.toString() + " closed");
82         }
83     }
84 }