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