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