b17c1159d89048a26dc2956fe095d5038bd95c46
[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 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.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
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;
38
39 @Singleton
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";
44
45     private final DataBroker dataBroker;
46     private final NotificationProviderService 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;
53
54     @Inject
55     public FlowCapableTopologyProvider(@Reference final DataBroker dataBroker,
56                                        @Reference final NotificationProviderService 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;
64     }
65
66     /**
67      * Gets called on start of a bundle.
68      */
69     @PostConstruct
70     public void start() {
71         final TopologyKey key = new TopologyKey(new TopologyId(TOPOLOGY_ID));
72         this.topologyPathIID = InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, key);
73
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.");
81     }
82
83     @Override
84     @PreDestroy
85     public void close() {
86         this.transactionChainManager.close();
87         if (this.listenerRegistration != null) {
88             LOG.info("Closing notification listener registration.");
89             this.listenerRegistration.close();
90             this.listenerRegistration = null;
91         }
92
93         if (this.singletonServiceRegistration != null) {
94             LOG.info("Closing clustering singleton service registration.");
95             this.singletonServiceRegistration.close();
96             this.singletonServiceRegistration = null;
97         }
98         LOG.debug("Topology Manager instance is stopped.");
99     }
100
101     @Override
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);
109         }
110     }
111
112     @Override
113     public ListenableFuture<? extends Object> closeServiceInstance() {
114         return Futures.immediateFuture(null);
115     }
116
117     @Nonnull
118     @Override
119     public ServiceGroupIdentifier getIdentifier() {
120         return ServiceGroupIdentifier.create(TOPOLOGY_PROVIDER);
121     }
122
123     private boolean isFlowTopologyExist(final InstanceIdentifier<Topology> path) {
124         try {
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()) {
129                 return true;
130             }
131         } catch (InterruptedException | ExecutionException e) {
132             LOG.warn("OpenFlow topology read operation failed!", e);
133         }
134         return false;
135     }
136 }