Mark methods as static
[openflowplugin.git] / applications / topology-manager / src / main / java / org / opendaylight / openflowplugin / applications / topology / manager / FlowCapableTopologyProvider.java
1 /*
2  * Copyright (c) 2013 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.openflowplugin.applications.topology.manager;
9
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;
28
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";
36
37     /**
38      * Gets called on start of a bundle.
39      *
40      * @param session
41      */
42     @Override
43     public synchronized void onSessionInitiated(final ProviderContext session) {
44         final DataBroker dataBroker = session.getSALService(DataBroker.class);
45         final NotificationProviderService notificationService = session.getSALService(NotificationProviderService.class);
46
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);
51
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);
57
58         final ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
59         tx.put(LogicalDatastoreType.OPERATIONAL, path, new TopologyBuilder().setKey(key).build(), true);
60         try {
61             tx.submit().get();
62         } catch (InterruptedException | ExecutionException e) {
63             LOG.warn("Initial topology export failed, continuing anyway", e);
64         }
65
66         thread = new Thread(processor);
67         thread.setDaemon(true);
68         thread.setName("FlowCapableTopologyExporter-" + TOPOLOGY_ID);
69         thread.start();
70     }
71
72     @Override
73     public synchronized void close() throws InterruptedException {
74         LOG.info("FlowCapableTopologyProvider stopped.");
75         if (this.listenerRegistration != null) {
76             try {
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);
81             }
82             listenerRegistration = null;
83         }
84         unregisterListener(terminationPointChangeListener);
85         unregisterListener(nodeChangeListener);
86         if (thread != null) {
87             thread.interrupt();
88             thread.join();
89             thread = null;
90         }
91     }
92
93     private static void unregisterListener(final AutoCloseable listenerToClose) {
94         if (listenerToClose != null) {
95             try {
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);
100             }
101         }
102     }
103
104     /**
105      * Gets called during stop bundle
106      *
107      * @param context The execution context of the bundle being stopped.
108      */
109     @Override
110     public void stopImpl(final BundleContext context) {
111         try {
112             this.close();
113         } catch (InterruptedException e) {
114             LOG.warn("Failed to stop provider: {}", e.getMessage());
115             LOG.debug("Failed to stop provider.. ", e);
116         }
117     }
118 }