Merge "Exception while topology-manager writes flow:1 root node to operational datast...
[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 com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.concurrent.ExecutionException;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
18 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
21 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
22 import org.opendaylight.openflowplugin.common.txchain.TransactionChainManager;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.binding.NotificationListener;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class FlowCapableTopologyProvider implements ClusterSingletonService, AutoCloseable {
35     private static final Logger LOG = LoggerFactory.getLogger(FlowCapableTopologyProvider.class);
36     private static final String TOPOLOGY_PROVIDER = "ofp-topology-manager";
37     static final String TOPOLOGY_ID = "flow:1";
38
39     private final DataBroker dataBroker;
40     private final NotificationProviderService notificationService;
41     private final OperationProcessor processor;
42     private final ClusterSingletonServiceProvider clusterSingletonServiceProvider;
43     private InstanceIdentifier<Topology> topologyPathIID;
44     private TransactionChainManager transactionChainManager;
45     private ListenerRegistration<NotificationListener> listenerRegistration;
46     private ClusterSingletonServiceRegistration singletonServiceRegistration;
47
48     public FlowCapableTopologyProvider(final DataBroker dataBroker,
49                                        final NotificationProviderService notificationService,
50                                        final OperationProcessor processor,
51                                        final ClusterSingletonServiceProvider clusterSingletonServiceProvider) {
52         this.dataBroker = dataBroker;
53         this.notificationService = notificationService;
54         this.processor = processor;
55         this.clusterSingletonServiceProvider = clusterSingletonServiceProvider;
56     }
57
58     /**
59      * Gets called on start of a bundle.
60      */
61     public void start() {
62         final TopologyKey key = new TopologyKey(new TopologyId(TOPOLOGY_ID));
63         this.topologyPathIID = InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, key);
64
65         final FlowCapableTopologyExporter listener = new FlowCapableTopologyExporter(processor, topologyPathIID);
66         this.listenerRegistration = notificationService.registerNotificationListener(listener);
67         this.transactionChainManager = new TransactionChainManager(dataBroker, TOPOLOGY_PROVIDER);
68         this.transactionChainManager.activateTransactionManager();
69         this.transactionChainManager.initialSubmitWriteTransaction();
70         this.singletonServiceRegistration = this.clusterSingletonServiceProvider.registerClusterSingletonService(this);
71         LOG.info("Topology Manager service started.");
72     }
73
74     @Override
75     public void close() {
76         this.transactionChainManager.close();
77         if (this.listenerRegistration != null) {
78             LOG.info("Closing notification listener registration.");
79             this.listenerRegistration.close();
80             this.listenerRegistration = null;
81         }
82
83         if (this.singletonServiceRegistration != null) {
84             LOG.info("Closing clustering singleton service registration.");
85             this.singletonServiceRegistration.close();
86             this.singletonServiceRegistration = null;
87         }
88         LOG.debug("Topology Manager instance is stopped.");
89     }
90
91     @Override
92     public void instantiateServiceInstance() {
93         LOG.debug("Topology Manager instance is elected as an active instance.");
94         if (!isFlowTopologyExist(topologyPathIID)) {
95             transactionChainManager.writeToTransaction(LogicalDatastoreType.OPERATIONAL, topologyPathIID,
96                     new TopologyBuilder().withKey(new TopologyKey(new TopologyId(TOPOLOGY_ID))).build(), true);
97             transactionChainManager.submitTransaction();
98             LOG.info("Topology node {} is successfully written to the operational datastore.", TOPOLOGY_ID);
99         }
100     }
101
102     @Override
103     public ListenableFuture<? extends Object> closeServiceInstance() {
104         return Futures.immediateFuture(null);
105     }
106
107     @Nonnull
108     @Override
109     public ServiceGroupIdentifier getIdentifier() {
110         return ServiceGroupIdentifier.create(TOPOLOGY_PROVIDER);
111     }
112
113     private boolean isFlowTopologyExist(final InstanceIdentifier<Topology> path) {
114         try {
115             Optional<Topology> ofTopology = this.transactionChainManager
116                     .readFromTransaction(LogicalDatastoreType.OPERATIONAL, path).get();
117             LOG.debug("OpenFlow topology exist in the operational data store at {}", path);
118             if (ofTopology.isPresent()) {
119                 return true;
120             }
121         } catch (InterruptedException | ExecutionException e) {
122             LOG.warn("OpenFlow topology read operation failed!", e);
123         }
124         return false;
125     }
126 }