Merge "Bug 5974: He plugin: Don't invalidate session context that is not valid."
[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 com.google.common.base.Optional;
11 import java.util.concurrent.ExecutionException;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
14 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
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.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class FlowCapableTopologyProvider implements AutoCloseable {
30     private static final Logger LOG = LoggerFactory.getLogger(FlowCapableTopologyProvider.class);
31     static final String TOPOLOGY_ID = "flow:1";
32
33     private final DataBroker dataBroker;
34     private final NotificationProviderService notificationService;
35     private final OperationProcessor processor;
36     private ListenerRegistration<NotificationListener> listenerRegistration;
37
38     public FlowCapableTopologyProvider(DataBroker dataBroker, NotificationProviderService notificationService,
39             OperationProcessor processor) {
40         this.dataBroker = dataBroker;
41         this.notificationService = notificationService;
42         this.processor = processor;
43     }
44
45     /**
46      * Gets called on start of a bundle.
47      */
48     public void start() {
49         final TopologyKey key = new TopologyKey(new TopologyId(TOPOLOGY_ID));
50         final InstanceIdentifier<Topology> path = InstanceIdentifier
51                 .create(NetworkTopology.class)
52                 .child(Topology.class, key);
53
54         final FlowCapableTopologyExporter listener = new FlowCapableTopologyExporter(processor, path);
55         this.listenerRegistration = notificationService.registerNotificationListener(listener);
56
57         if(!isFlowTopologyExist(dataBroker, path)){
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
67         LOG.info("FlowCapableTopologyProvider started");
68     }
69
70     @Override
71     public void close() {
72         LOG.info("FlowCapableTopologyProvider stopped.");
73         if (this.listenerRegistration != null) {
74             try {
75                 this.listenerRegistration.close();
76             } catch (Exception e) {
77                 LOG.warn("Failed to close listener registration: {}", e.getMessage());
78                 LOG.debug("Failed to close listener registration.. ", e);
79             }
80             listenerRegistration = null;
81         }
82     }
83
84     private boolean isFlowTopologyExist(final DataBroker dataBroker,
85                                         final InstanceIdentifier<Topology> path) {
86         final ReadTransaction tx = dataBroker.newReadOnlyTransaction();
87         try {
88             Optional<Topology> ofTopology = tx.read(LogicalDatastoreType.OPERATIONAL, path).checkedGet();
89             LOG.debug("OpenFlow topology exist in the operational data store at {}",path);
90             if(ofTopology.isPresent()){
91                 return true;
92             }
93         } catch (ReadFailedException e) {
94             LOG.warn("OpenFlow topology read operation failed!", e);
95         }
96         return false;
97     }
98 }