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 com.google.common.base.Optional;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import java.util.concurrent.ExecutionException;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
16 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
17 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
18 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
19 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
20 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
21 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
25 import org.opendaylight.controller.sal.core.api.model.SchemaService;
26 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
27 import org.opendaylight.ovsdb.lib.OvsdbConnection;
28 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvoker;
29 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvokerImpl;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopologyBuilder;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class SouthboundProvider implements 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 OvsdbDataTreeChangeListener ovsdbDataTreeChangeListener;
52 private EntityOwnershipService entityOwnershipService;
53 private EntityOwnershipCandidateRegistration registration;
54 private SouthboundPluginInstanceEntityOwnershipListener providerOwnershipChangeListener;
55 private OvsdbConnection ovsdbConnection;
57 public SouthboundProvider(final DataBroker dataBroker,
58 final EntityOwnershipService entityOwnershipServiceDependency,
59 final OvsdbConnection ovsdbConnection,
60 final SchemaService schemaService,
61 final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer) {
63 this.entityOwnershipService = entityOwnershipServiceDependency;
65 this.ovsdbConnection = ovsdbConnection;
67 SouthboundUtil.setInstanceIdentifierCodec(new InstanceIdentifierCodec(schemaService,
68 bindingNormalizedNodeSerializer));
69 LOG.info("SouthboundProvider ovsdbConnectionService Initialized");
73 * Used by blueprint when starting the container.
76 LOG.info("SouthboundProvider Session Initiated");
77 this.txInvoker = new TransactionInvokerImpl(db);
78 cm = new OvsdbConnectionManager(db,txInvoker,entityOwnershipService, ovsdbConnection);
79 ovsdbDataTreeChangeListener = new OvsdbDataTreeChangeListener(db, cm);
81 //Register listener for entityOnwership changes
82 providerOwnershipChangeListener =
83 new SouthboundPluginInstanceEntityOwnershipListener(this,this.entityOwnershipService);
85 //register instance entity to get the ownership of the provider
86 Entity instanceEntity = new Entity(ENTITY_TYPE, ENTITY_TYPE);
88 Optional<EntityOwnershipState> ownershipStateOpt = entityOwnershipService.getOwnershipState(instanceEntity);
89 registration = entityOwnershipService.registerCandidate(instanceEntity);
90 if (ownershipStateOpt.isPresent()) {
91 EntityOwnershipState ownershipState = ownershipStateOpt.get();
92 if (ownershipState.hasOwner() && !ownershipState.isOwner()) {
93 ovsdbConnection.registerConnectionListener(cm);
94 ovsdbConnection.startOvsdbManager(SouthboundConstants.DEFAULT_OVSDB_PORT);
95 LOG.info("*This* instance of OVSDB southbound provider is set as a SLAVE instance");
98 } catch (CandidateAlreadyRegisteredException e) {
99 LOG.warn("OVSDB Southbound Provider instance entity {} was already "
100 + "registered for ownership", instanceEntity, e);
105 public void close() {
106 LOG.info("SouthboundProvider Closed");
108 ovsdbDataTreeChangeListener.close();
109 registration.close();
110 providerOwnershipChangeListener.close();
113 private void initializeOvsdbTopology(LogicalDatastoreType type) {
114 InstanceIdentifier<Topology> path = InstanceIdentifier
115 .create(NetworkTopology.class)
116 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID));
117 initializeTopology(type);
118 ReadWriteTransaction transaction = db.newReadWriteTransaction();
119 CheckedFuture<Optional<Topology>, ReadFailedException> ovsdbTp = transaction.read(type, path);
121 if (!ovsdbTp.get().isPresent()) {
122 TopologyBuilder tpb = new TopologyBuilder();
123 tpb.setTopologyId(SouthboundConstants.OVSDB_TOPOLOGY_ID);
124 transaction.put(type, path, tpb.build());
125 transaction.submit();
127 transaction.cancel();
129 } catch (InterruptedException | ExecutionException e) {
130 LOG.error("Error initializing ovsdb topology", e);
134 private void initializeTopology(LogicalDatastoreType type) {
135 ReadWriteTransaction transaction = db.newReadWriteTransaction();
136 InstanceIdentifier<NetworkTopology> path = InstanceIdentifier.create(NetworkTopology.class);
137 CheckedFuture<Optional<NetworkTopology>, ReadFailedException> topology = transaction.read(type,path);
139 if (!topology.get().isPresent()) {
140 NetworkTopologyBuilder ntb = new NetworkTopologyBuilder();
141 transaction.put(type,path,ntb.build());
142 transaction.submit();
144 transaction.cancel();
146 } catch (InterruptedException | ExecutionException e) {
147 LOG.error("Error initializing ovsdb topology", e);
151 public void handleOwnershipChange(EntityOwnershipChange ownershipChange) {
152 if (ownershipChange.isOwner()) {
153 LOG.info("*This* instance of OVSDB southbound provider is set as a MASTER instance");
154 LOG.info("Initialize OVSDB topology {} in operational and config data store if not already present",
155 SouthboundConstants.OVSDB_TOPOLOGY_ID);
156 initializeOvsdbTopology(LogicalDatastoreType.OPERATIONAL);
157 initializeOvsdbTopology(LogicalDatastoreType.CONFIGURATION);
159 LOG.info("*This* instance of OVSDB southbound provider is set as a SLAVE instance");
161 ovsdbConnection.registerConnectionListener(cm);
162 ovsdbConnection.startOvsdbManager(SouthboundConstants.DEFAULT_OVSDB_PORT);
165 private class SouthboundPluginInstanceEntityOwnershipListener implements EntityOwnershipListener {
166 private SouthboundProvider sp;
167 private EntityOwnershipListenerRegistration listenerRegistration;
169 SouthboundPluginInstanceEntityOwnershipListener(SouthboundProvider sp,
170 EntityOwnershipService entityOwnershipService) {
172 listenerRegistration = entityOwnershipService.registerListener(ENTITY_TYPE, this);
175 public void close() {
176 this.listenerRegistration.close();
180 public void ownershipChanged(EntityOwnershipChange ownershipChange) {
181 sp.handleOwnershipChange(ownershipChange);