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