2 * Copyright (c) 2014 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.ovsdb.southbound;
10 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
12 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
13 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
14 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
15 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
16 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
17 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
18 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
19 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
24 import org.opendaylight.ovsdb.lib.OvsdbConnection;
25 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvoker;
26 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvokerImpl;
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.NetworkTopologyBuilder;
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.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import com.google.common.base.Optional;
37 import com.google.common.util.concurrent.CheckedFuture;
39 public class SouthboundProvider implements BindingAwareProvider, AutoCloseable {
41 private static final Logger LOG = LoggerFactory.getLogger(SouthboundProvider.class);
42 private static final String ENTITY_TYPE = "ovsdb-southbound-provider";
44 public static DataBroker getDb() {
48 private static DataBroker db;
49 private OvsdbConnectionManager cm;
50 private TransactionInvoker txInvoker;
51 private OvsdbDataChangeListener ovsdbDataChangeListener;
52 private EntityOwnershipService entityOwnershipService;
53 private EntityOwnershipCandidateRegistration registration;
54 private SouthboundPluginInstanceEntityOwnershipListener providerOwnershipChangeListener;
55 private OvsdbConnection ovsdbConnection;
58 public SouthboundProvider(
59 EntityOwnershipService entityOwnershipServiceDependency,
60 OvsdbConnection ovsdbConnection) {
61 this.entityOwnershipService = entityOwnershipServiceDependency;
63 this.ovsdbConnection = ovsdbConnection;
64 LOG.info("SouthboundProvider ovsdbConnectionService: {}", ovsdbConnection);
68 public void onSessionInitiated(ProviderContext session) {
69 LOG.info("SouthboundProvider Session Initiated");
70 db = session.getSALService(DataBroker.class);
71 this.txInvoker = new TransactionInvokerImpl(db);
72 cm = new OvsdbConnectionManager(db,txInvoker,entityOwnershipService, ovsdbConnection);
73 ovsdbDataChangeListener = new OvsdbDataChangeListener(db,cm);
75 //Register listener for entityOnwership changes
76 providerOwnershipChangeListener =
77 new SouthboundPluginInstanceEntityOwnershipListener(this,this.entityOwnershipService);
78 entityOwnershipService.registerListener(ENTITY_TYPE,providerOwnershipChangeListener);
80 //register instance entity to get the ownership of the provider
81 Entity instanceEntity = new Entity(ENTITY_TYPE, ENTITY_TYPE);
83 Optional<EntityOwnershipState> ownershipStateOpt = entityOwnershipService.getOwnershipState(instanceEntity);
84 registration = entityOwnershipService.registerCandidate(instanceEntity);
85 if (ownershipStateOpt.isPresent()) {
86 EntityOwnershipState ownershipState = ownershipStateOpt.get();
87 if (ownershipState.hasOwner() && !ownershipState.isOwner()) {
88 ovsdbConnection.registerConnectionListener(cm);
89 ovsdbConnection.startOvsdbManager(SouthboundConstants.DEFAULT_OVSDB_PORT);
90 LOG.info("*This* instance of OVSDB southbound provider is set as a SLAVE instance");
93 } catch (CandidateAlreadyRegisteredException e) {
94 LOG.warn("OVSDB Southbound Provider instance entity {} was already "
95 + "registered for {} ownership", instanceEntity, e);
100 public void close() throws Exception {
101 LOG.info("SouthboundProvider Closed");
103 ovsdbDataChangeListener.close();
104 registration.close();
105 providerOwnershipChangeListener.close();
108 private void initializeOvsdbTopology(LogicalDatastoreType type) {
109 InstanceIdentifier<Topology> path = InstanceIdentifier
110 .create(NetworkTopology.class)
111 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID));
112 initializeTopology(type);
113 ReadWriteTransaction transaction = db.newReadWriteTransaction();
114 CheckedFuture<Optional<Topology>, ReadFailedException> ovsdbTp = transaction.read(type, path);
116 if (!ovsdbTp.get().isPresent()) {
117 TopologyBuilder tpb = new TopologyBuilder();
118 tpb.setTopologyId(SouthboundConstants.OVSDB_TOPOLOGY_ID);
119 transaction.put(type, path, tpb.build());
120 transaction.submit();
122 transaction.cancel();
124 } catch (Exception e) {
125 LOG.error("Error initializing ovsdb topology", e);
129 private void initializeTopology(LogicalDatastoreType type) {
130 ReadWriteTransaction transaction = db.newReadWriteTransaction();
131 InstanceIdentifier<NetworkTopology> path = InstanceIdentifier.create(NetworkTopology.class);
132 CheckedFuture<Optional<NetworkTopology>, ReadFailedException> topology = transaction.read(type,path);
134 if (!topology.get().isPresent()) {
135 NetworkTopologyBuilder ntb = new NetworkTopologyBuilder();
136 transaction.put(type,path,ntb.build());
137 transaction.submit();
139 transaction.cancel();
141 } catch (Exception e) {
142 LOG.error("Error initializing ovsdb topology {}",e);
146 public void handleOwnershipChange(EntityOwnershipChange ownershipChange) {
147 if (ownershipChange.isOwner()) {
148 LOG.info("*This* instance of OVSDB southbound provider is set as a MASTER instance");
149 LOG.info("Initialize OVSDB topology {} in operational and config data store if not already present"
150 ,SouthboundConstants.OVSDB_TOPOLOGY_ID);
151 initializeOvsdbTopology(LogicalDatastoreType.OPERATIONAL);
152 initializeOvsdbTopology(LogicalDatastoreType.CONFIGURATION);
154 LOG.info("*This* instance of OVSDB southbound provider is set as a SLAVE instance");
156 ovsdbConnection.registerConnectionListener(cm);
157 ovsdbConnection.startOvsdbManager(SouthboundConstants.DEFAULT_OVSDB_PORT);
160 private class SouthboundPluginInstanceEntityOwnershipListener implements EntityOwnershipListener {
161 private SouthboundProvider sp;
162 private EntityOwnershipListenerRegistration listenerRegistration;
164 SouthboundPluginInstanceEntityOwnershipListener(SouthboundProvider sp,
165 EntityOwnershipService entityOwnershipService) {
167 listenerRegistration = entityOwnershipService.registerListener(ENTITY_TYPE, this);
170 public void close() {
171 this.listenerRegistration.close();
174 public void ownershipChanged(EntityOwnershipChange ownershipChange) {
175 sp.handleOwnershipChange(ownershipChange);