2 * Copyright (c) 2013 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.openflowplugin.applications.topology.manager;
10 import java.util.concurrent.ExecutionException;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.controller.sal.binding.api.AbstractBindingAwareProvider;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
16 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.binding.NotificationListener;
25 import org.osgi.framework.BundleContext;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public class FlowCapableTopologyProvider extends AbstractBindingAwareProvider implements AutoCloseable {
30 private static final Logger LOG = LoggerFactory.getLogger(FlowCapableTopologyProvider.class);
31 private ListenerRegistration<NotificationListener> listenerRegistration;
32 private Thread thread;
33 private TerminationPointChangeListenerImpl terminationPointChangeListener;
34 private NodeChangeListenerImpl nodeChangeListener;
35 static final String TOPOLOGY_ID = "flow:1";
38 * Gets called on start of a bundle.
43 public synchronized void onSessionInitiated(final ProviderContext session) {
44 final DataBroker dataBroker = session.getSALService(DataBroker.class);
45 final NotificationProviderService notificationService = session.getSALService(NotificationProviderService.class);
47 final TopologyKey key = new TopologyKey(new TopologyId(TOPOLOGY_ID));
48 final InstanceIdentifier<Topology> path = InstanceIdentifier
49 .create(NetworkTopology.class)
50 .child(Topology.class, key);
52 final OperationProcessor processor = new OperationProcessor(dataBroker);
53 final FlowCapableTopologyExporter listener = new FlowCapableTopologyExporter(processor, path);
54 this.listenerRegistration = notificationService.registerNotificationListener(listener);
55 this.terminationPointChangeListener = new TerminationPointChangeListenerImpl(dataBroker, processor);
56 nodeChangeListener = new NodeChangeListenerImpl(dataBroker, processor);
58 final ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
59 tx.put(LogicalDatastoreType.OPERATIONAL, path, new TopologyBuilder().setKey(key).build(), true);
62 } catch (InterruptedException | ExecutionException e) {
63 LOG.warn("Initial topology export failed, continuing anyway", e);
66 thread = new Thread(processor);
67 thread.setDaemon(true);
68 thread.setName("FlowCapableTopologyExporter-" + TOPOLOGY_ID);
73 public synchronized void close() throws InterruptedException {
74 LOG.info("FlowCapableTopologyProvider stopped.");
75 if (this.listenerRegistration != null) {
77 this.listenerRegistration.close();
78 } catch (Exception e) {
79 LOG.warn("Failed to close listener registration: {}", e.getMessage());
80 LOG.debug("Failed to close listener registration.. ", e);
82 listenerRegistration = null;
84 unregisterListener(terminationPointChangeListener);
85 unregisterListener(nodeChangeListener);
93 private static void unregisterListener(final AutoCloseable listenerToClose) {
94 if (listenerToClose != null) {
96 listenerToClose.close();
97 } catch (Exception e) {
98 LOG.warn("Failed to close listener registration: {}", e.getMessage());
99 LOG.debug("Failed to close listener registration.. ", e);
105 * Gets called during stop bundle
107 * @param context The execution context of the bundle being stopped.
110 public void stopImpl(final BundleContext context) {
113 } catch (InterruptedException e) {
114 LOG.warn("Failed to stop provider: {}", e.getMessage());
115 LOG.debug("Failed to stop provider.. ", e);