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.Nonnull;
15 import javax.annotation.PostConstruct;
16 import javax.annotation.PreDestroy;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.apache.aries.blueprint.annotation.service.Reference;
20 import org.opendaylight.mdsal.binding.api.DataBroker;
21 import org.opendaylight.mdsal.binding.api.NotificationService;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
24 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
25 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
26 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
27 import org.opendaylight.openflowplugin.common.txchain.TransactionChainManager;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.binding.NotificationListener;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 public class FlowCapableTopologyProvider implements ClusterSingletonService, AutoCloseable {
41 private static final Logger LOG = LoggerFactory.getLogger(FlowCapableTopologyProvider.class);
42 private static final String TOPOLOGY_PROVIDER = "ofp-topology-manager";
43 static final String TOPOLOGY_ID = "flow:1";
45 private final DataBroker dataBroker;
46 private final NotificationService notificationService;
47 private final OperationProcessor processor;
48 private final ClusterSingletonServiceProvider clusterSingletonServiceProvider;
49 private InstanceIdentifier<Topology> topologyPathIID;
50 private TransactionChainManager transactionChainManager;
51 private ListenerRegistration<NotificationListener> listenerRegistration;
52 private ClusterSingletonServiceRegistration singletonServiceRegistration;
55 public FlowCapableTopologyProvider(@Reference final DataBroker dataBroker,
56 @Reference final NotificationService notificationService,
57 final OperationProcessor processor,
58 @Reference final ClusterSingletonServiceProvider
59 clusterSingletonServiceProvider) {
60 this.dataBroker = dataBroker;
61 this.notificationService = notificationService;
62 this.processor = processor;
63 this.clusterSingletonServiceProvider = clusterSingletonServiceProvider;
67 * Gets called on start of a bundle.
71 final TopologyKey key = new TopologyKey(new TopologyId(TOPOLOGY_ID));
72 this.topologyPathIID = InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, key);
74 final FlowCapableTopologyExporter listener = new FlowCapableTopologyExporter(processor, topologyPathIID);
75 this.listenerRegistration = notificationService.registerNotificationListener(listener);
76 this.transactionChainManager = new TransactionChainManager(dataBroker, TOPOLOGY_PROVIDER);
77 this.transactionChainManager.activateTransactionManager();
78 this.transactionChainManager.initialSubmitWriteTransaction();
79 this.singletonServiceRegistration = this.clusterSingletonServiceProvider.registerClusterSingletonService(this);
80 LOG.info("Topology Manager service started.");
86 this.transactionChainManager.close();
87 if (this.listenerRegistration != null) {
88 LOG.info("Closing notification listener registration.");
89 this.listenerRegistration.close();
90 this.listenerRegistration = null;
93 if (this.singletonServiceRegistration != null) {
94 LOG.info("Closing clustering singleton service registration.");
95 this.singletonServiceRegistration.close();
96 this.singletonServiceRegistration = null;
98 LOG.debug("Topology Manager instance is stopped.");
102 public void instantiateServiceInstance() {
103 LOG.debug("Topology Manager instance is elected as an active instance.");
104 if (!isFlowTopologyExist(topologyPathIID)) {
105 transactionChainManager.writeToTransaction(LogicalDatastoreType.OPERATIONAL, topologyPathIID,
106 new TopologyBuilder().withKey(new TopologyKey(new TopologyId(TOPOLOGY_ID))).build(), true);
107 transactionChainManager.submitTransaction();
108 LOG.info("Topology node {} is successfully written to the operational datastore.", TOPOLOGY_ID);
113 public ListenableFuture<? extends Object> closeServiceInstance() {
114 return Futures.immediateFuture(null);
119 public ServiceGroupIdentifier getIdentifier() {
120 return ServiceGroupIdentifier.create(TOPOLOGY_PROVIDER);
123 private boolean isFlowTopologyExist(final InstanceIdentifier<Topology> path) {
125 Optional<Topology> ofTopology = this.transactionChainManager
126 .readFromTransaction(LogicalDatastoreType.OPERATIONAL, path).get();
127 LOG.debug("OpenFlow topology exist in the operational data store at {}", path);
128 if (ofTopology.isPresent()) {
131 } catch (InterruptedException | ExecutionException e) {
132 LOG.warn("OpenFlow topology read operation failed!", e);