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 com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.Optional;
13 import java.util.concurrent.ExecutionException;
14 import javax.annotation.PostConstruct;
15 import javax.annotation.PreDestroy;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.opendaylight.mdsal.binding.api.DataBroker;
19 import org.opendaylight.mdsal.binding.api.NotificationService;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
22 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
23 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
24 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
25 import org.opendaylight.openflowplugin.common.txchain.TransactionChainManager;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
31 import org.opendaylight.yangtools.concepts.Registration;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 public class FlowCapableTopologyProvider implements ClusterSingletonService, AutoCloseable {
38 private static final Logger LOG = LoggerFactory.getLogger(FlowCapableTopologyProvider.class);
39 private static final String TOPOLOGY_PROVIDER = "ofp-topology-manager";
40 static final String TOPOLOGY_ID = "flow:1";
42 private final DataBroker dataBroker;
43 private final NotificationService notificationService;
44 private final OperationProcessor processor;
45 private final ClusterSingletonServiceProvider clusterSingletonServiceProvider;
46 private InstanceIdentifier<Topology> topologyPathIID;
47 private TransactionChainManager transactionChainManager;
48 private Registration listenerRegistration;
49 private ClusterSingletonServiceRegistration singletonServiceRegistration;
52 public FlowCapableTopologyProvider(final DataBroker dataBroker,
53 final NotificationService notificationService,
54 final OperationProcessor processor,
55 final ClusterSingletonServiceProvider clusterSingletonServiceProvider) {
56 this.dataBroker = dataBroker;
57 this.notificationService = notificationService;
58 this.processor = processor;
59 this.clusterSingletonServiceProvider = clusterSingletonServiceProvider;
63 * Gets called on start of a bundle.
67 final TopologyKey key = new TopologyKey(new TopologyId(TOPOLOGY_ID));
68 topologyPathIID = InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, key);
70 listenerRegistration = notificationService.registerCompositeListener(
71 new FlowCapableTopologyExporter(processor, topologyPathIID).toListener());
72 transactionChainManager = new TransactionChainManager(dataBroker, TOPOLOGY_PROVIDER);
73 transactionChainManager.activateTransactionManager();
74 transactionChainManager.initialSubmitWriteTransaction();
75 singletonServiceRegistration = clusterSingletonServiceProvider.registerClusterSingletonService(this);
76 LOG.info("Topology Manager service started.");
82 transactionChainManager.close();
83 if (listenerRegistration != null) {
84 LOG.info("Closing notification listener registration.");
85 listenerRegistration.close();
86 listenerRegistration = null;
89 if (singletonServiceRegistration != null) {
90 LOG.info("Closing clustering singleton service registration.");
91 singletonServiceRegistration.close();
92 singletonServiceRegistration = null;
94 LOG.debug("Topology Manager instance is stopped.");
98 public void instantiateServiceInstance() {
99 LOG.debug("Topology Manager instance is elected as an active instance.");
100 if (!isFlowTopologyExist(topologyPathIID)) {
101 transactionChainManager.writeToTransaction(LogicalDatastoreType.OPERATIONAL, topologyPathIID,
102 new TopologyBuilder().withKey(new TopologyKey(new TopologyId(TOPOLOGY_ID))).build(), true);
103 transactionChainManager.submitTransaction();
104 LOG.info("Topology node {} is successfully written to the operational datastore.", TOPOLOGY_ID);
109 public ListenableFuture<? extends Object> closeServiceInstance() {
110 return Futures.immediateFuture(null);
114 public ServiceGroupIdentifier getIdentifier() {
115 return ServiceGroupIdentifier.create(TOPOLOGY_PROVIDER);
118 private boolean isFlowTopologyExist(final InstanceIdentifier<Topology> path) {
120 Optional<Topology> ofTopology = transactionChainManager
121 .readFromTransaction(LogicalDatastoreType.OPERATIONAL, path).get();
122 LOG.debug("OpenFlow topology exist in the operational data store at {}", path);
123 if (ofTopology.isPresent()) {
126 } catch (InterruptedException | ExecutionException e) {
127 LOG.warn("OpenFlow topology read operation failed!", e);