Hotfix bug 1556 - FRM: NPE thrown in notification
[controller.git] / opendaylight / md-sal / topology-manager / src / main / java / org / opendaylight / md / controller / 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.md.controller.topology.manager;
9
10 import java.util.concurrent.ExecutionException;
11
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.sal.binding.api.AbstractBindingAwareProvider;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
17 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.binding.NotificationListener;
26 import org.osgi.framework.BundleContext;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class FlowCapableTopologyProvider extends AbstractBindingAwareProvider implements AutoCloseable {
31     private final static Logger LOG = LoggerFactory.getLogger(FlowCapableTopologyProvider.class);
32     private ListenerRegistration<NotificationListener> listenerRegistration;
33     private Thread thread;
34
35     /**
36      * Gets called on start of a bundle.
37      *
38      * @param session
39      */
40     @Override
41     public synchronized void onSessionInitiated(final ProviderContext session) {
42         final DataBroker dataBroker = session.getSALService(DataBroker.class);
43         final NotificationProviderService notificationService = session.getSALService(NotificationProviderService.class);
44
45         final String name = "flow:1";
46         final TopologyKey key = new TopologyKey(new TopologyId(name));
47         final InstanceIdentifier<Topology> path = InstanceIdentifier
48                 .create(NetworkTopology.class)
49                 .child(Topology.class, key);
50
51         final OperationProcessor processor = new OperationProcessor(dataBroker);
52         final FlowCapableTopologyExporter listener = new FlowCapableTopologyExporter(processor, path);
53         this.listenerRegistration = notificationService.registerNotificationListener(listener);
54
55         final ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
56         tx.put(LogicalDatastoreType.OPERATIONAL, path, new TopologyBuilder().setKey(key).build(), true);
57         try {
58             tx.submit().get();
59         } catch (InterruptedException | ExecutionException e) {
60             LOG.warn("Initial topology export failed, continuing anyway", e);
61         }
62
63         thread = new Thread(processor);
64         thread.setDaemon(true);
65         thread.setName("FlowCapableTopologyExporter-" + name);
66         thread.start();
67     }
68
69     @Override
70     public synchronized void close() throws InterruptedException {
71         LOG.info("FlowCapableTopologyProvider stopped.");
72         if (this.listenerRegistration != null) {
73             try {
74                 this.listenerRegistration.close();
75             } catch (Exception e) {
76                 LOG.error("Failed to close listener registration", e);
77             }
78             listenerRegistration = null;
79         }
80         if (thread != null) {
81             thread.interrupt();
82             thread.join();
83             thread = null;
84         }
85     }
86
87     /**
88      * Gets called during stop bundle
89      *
90      * @param context The execution context of the bundle being stopped.
91      */
92     @Override
93     public void stopImpl(final BundleContext context) {
94         try {
95             this.close();
96         } catch (InterruptedException e) {
97             LOG.error("Failed to stop provider", e);
98         }
99     }
100 }