2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.lispflowmapping.neutron;
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;
26 public class DelegatingDataTreeListener<T extends DataObject> implements ClusteredDataTreeChangeListener<T>,
28 private static final Logger LOG = LoggerFactory.getLogger(DelegatingDataTreeListener.class);
29 private final DataProcessor<T> dataProcessor;
30 private ListenerRegistration<ClusteredDataTreeChangeListener<T>> dataTreeChangeListenerRegistration;
32 public DelegatingDataTreeListener(DataProcessor<T> dataProcessor, DataBroker dataBroker, DataTreeIdentifier<T>
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);
40 public static <T extends DataObject> DelegatingDataTreeListener initiateListener(
41 Class<T> dataObject, ILispNeutronService lispNeutronService, DataBroker dataBroker) {
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);
50 LOG.debug(dataObject.getName() + " listener can not be instantiated.");
55 public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<T>> changes) {
56 for (DataTreeModification<T> change : changes) {
57 DataObjectModification<T> mod = change.getRootNode();
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.");
68 T object = mod.getDataAfter();
69 dataProcessor.update(object);
70 LOG.info(object.toString() + " updated.");
76 public void close() throws Exception {
77 if (dataTreeChangeListenerRegistration != null) {
78 dataTreeChangeListenerRegistration.close();
79 dataTreeChangeListenerRegistration = null;
81 LOG.info(this.toString() + " closed");