2 * Copyright (c) 2014, 2018 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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.util.Collection;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.atomic.AtomicBoolean;
16 import javax.annotation.PostConstruct;
17 import javax.annotation.PreDestroy;
18 import javax.inject.Inject;
19 import javax.inject.Singleton;
20 import org.apache.aries.blueprint.annotation.service.Reference;
21 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
24 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
25 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
28 import org.opendaylight.infrautils.diagstatus.DiagStatusService;
29 import org.opendaylight.infrautils.diagstatus.ServiceState;
30 import org.opendaylight.infrautils.ready.SystemReadyMonitor;
31 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
32 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
33 import org.opendaylight.mdsal.eos.binding.api.Entity;
34 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipCandidateRegistration;
35 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipChange;
36 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
37 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListenerRegistration;
38 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
39 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
40 import org.opendaylight.ovsdb.lib.OvsdbConnection;
41 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvoker;
42 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvokerImpl;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
47 import org.opendaylight.yangtools.concepts.ListenerRegistration;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 public class SouthboundProvider implements ClusteredDataTreeChangeListener<Topology>, AutoCloseable {
55 private static final Logger LOG = LoggerFactory.getLogger(SouthboundProvider.class);
57 private static final String ENTITY_TYPE = "ovsdb-southbound-provider";
59 public static DataBroker getDb() {
63 // FIXME: get rid of this static
64 @SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
65 private static DataBroker db;
66 private OvsdbConnectionManager cm;
67 private TransactionInvoker txInvoker;
68 private OvsdbDataTreeChangeListener ovsdbDataTreeChangeListener;
69 private final EntityOwnershipService entityOwnershipService;
70 private EntityOwnershipCandidateRegistration registration;
71 private SouthboundPluginInstanceEntityOwnershipListener providerOwnershipChangeListener;
72 private final OvsdbConnection ovsdbConnection;
73 private final InstanceIdentifierCodec instanceIdentifierCodec;
74 private final SystemReadyMonitor systemReadyMonitor;
76 private final AtomicBoolean registered = new AtomicBoolean(false);
77 private ListenerRegistration<SouthboundProvider> operTopologyRegistration;
78 private final OvsdbDiagStatusProvider ovsdbStatusProvider;
81 public SouthboundProvider(@Reference final DataBroker dataBroker,
82 @Reference final EntityOwnershipService entityOwnershipServiceDependency,
83 @Reference final OvsdbConnection ovsdbConnection,
84 @Reference final DOMSchemaService schemaService,
85 @Reference final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer,
86 @Reference final SystemReadyMonitor systemReadyMonitor,
87 @Reference final DiagStatusService diagStatusService) {
89 this.entityOwnershipService = entityOwnershipServiceDependency;
91 this.ovsdbConnection = ovsdbConnection;
92 this.ovsdbStatusProvider = new OvsdbDiagStatusProvider(diagStatusService);
93 this.instanceIdentifierCodec = new InstanceIdentifierCodec(schemaService,
94 bindingNormalizedNodeSerializer);
95 this.systemReadyMonitor = systemReadyMonitor;
96 LOG.info("SouthboundProvider ovsdbConnectionService Initialized");
100 * Used by blueprint when starting the container.
104 LOG.info("SouthboundProvider Session Initiated");
105 ovsdbStatusProvider.reportStatus(ServiceState.STARTING, "OVSDB initialization in progress");
106 this.txInvoker = new TransactionInvokerImpl(db);
107 cm = new OvsdbConnectionManager(db, txInvoker, entityOwnershipService, ovsdbConnection,
108 instanceIdentifierCodec);
109 ovsdbDataTreeChangeListener = new OvsdbDataTreeChangeListener(db, cm, instanceIdentifierCodec);
111 //Register listener for entityOnwership changes
112 providerOwnershipChangeListener =
113 new SouthboundPluginInstanceEntityOwnershipListener(this,this.entityOwnershipService);
115 //register instance entity to get the ownership of the provider
116 Entity instanceEntity = new Entity(ENTITY_TYPE, ENTITY_TYPE);
118 registration = entityOwnershipService.registerCandidate(instanceEntity);
119 } catch (CandidateAlreadyRegisteredException e) {
120 LOG.warn("OVSDB Southbound Provider instance entity {} was already "
121 + "registered for ownership", instanceEntity, e);
123 InstanceIdentifier<Topology> path = InstanceIdentifier
124 .create(NetworkTopology.class)
125 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID));
126 DataTreeIdentifier<Topology> treeId =
127 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, path);
129 LOG.trace("Registering listener for path {}", treeId);
130 operTopologyRegistration = db.registerDataTreeChangeListener(treeId, this);
135 public void close() {
136 LOG.info("SouthboundProvider Closed");
139 } catch (InterruptedException e) {
140 ovsdbStatusProvider.reportStatus(ServiceState.ERROR, "OVSDB service shutdown error");
141 LOG.debug("SouthboundProvider failed to close TransactionInvoker.");
144 ovsdbDataTreeChangeListener.close();
145 registration.close();
146 providerOwnershipChangeListener.close();
147 if (operTopologyRegistration != null) {
148 operTopologyRegistration.close();
149 operTopologyRegistration = null;
151 ovsdbStatusProvider.reportStatus(ServiceState.UNREGISTERED, "OVSDB Service stopped");
154 private void initializeOvsdbTopology(LogicalDatastoreType type) {
155 InstanceIdentifier<Topology> path = InstanceIdentifier
156 .create(NetworkTopology.class)
157 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID));
158 ReadWriteTransaction transaction = db.newReadWriteTransaction();
159 CheckedFuture<Optional<Topology>, ReadFailedException> ovsdbTp = transaction.read(type, path);
161 if (!ovsdbTp.get().isPresent()) {
162 TopologyBuilder tpb = new TopologyBuilder();
163 tpb.setTopologyId(SouthboundConstants.OVSDB_TOPOLOGY_ID);
164 transaction.put(type, path, tpb.build(), true);
165 transaction.submit();
167 transaction.cancel();
169 } catch (InterruptedException | ExecutionException e) {
170 LOG.error("Error initializing ovsdb topology", e);
174 public void handleOwnershipChange(EntityOwnershipChange ownershipChange) {
175 if (ownershipChange.getState().isOwner()) {
176 LOG.info("*This* instance of OVSDB southbound provider is set as a MASTER instance");
177 LOG.info("Initialize OVSDB topology {} in operational and config data store if not already present",
178 SouthboundConstants.OVSDB_TOPOLOGY_ID);
179 initializeOvsdbTopology(LogicalDatastoreType.OPERATIONAL);
180 initializeOvsdbTopology(LogicalDatastoreType.CONFIGURATION);
182 LOG.info("*This* instance of OVSDB southbound provider is set as a SLAVE instance");
187 public void onDataTreeChanged(Collection<DataTreeModification<Topology>> collection) {
188 if (!registered.getAndSet(true)) {
189 LOG.info("Starting the ovsdb port");
190 ovsdbConnection.registerConnectionListener(cm);
191 LOG.info("Registering deferred system ready listener to start OVSDB Manager later");
192 systemReadyMonitor.registerListener(() -> {
193 ovsdbConnection.startOvsdbManager();
194 LOG.info("Started OVSDB Manager (in system ready listener)");
196 //mdsal registration/deregistration in mdsal update callback should be avoided
198 if (operTopologyRegistration != null) {
199 operTopologyRegistration.close();
200 operTopologyRegistration = null;
203 ovsdbStatusProvider.reportStatus(ServiceState.OPERATIONAL, "OVSDB initialization complete");
207 private static class SouthboundPluginInstanceEntityOwnershipListener implements EntityOwnershipListener {
208 private final SouthboundProvider sp;
209 private final EntityOwnershipListenerRegistration listenerRegistration;
211 SouthboundPluginInstanceEntityOwnershipListener(SouthboundProvider sp,
212 EntityOwnershipService entityOwnershipService) {
214 listenerRegistration = entityOwnershipService.registerListener(ENTITY_TYPE, this);
217 public void close() {
218 this.listenerRegistration.close();
222 public void ownershipChanged(EntityOwnershipChange ownershipChange) {
223 sp.handleOwnershipChange(ownershipChange);
227 public void setSkipMonitoringManagerStatus(boolean flag) {
228 LOG.debug("skipManagerStatus set to {}", flag);
230 SouthboundConstants.SKIP_COLUMN_FROM_TABLE.get("Manager").add("status");
232 SouthboundConstants.SKIP_COLUMN_FROM_TABLE.get("Manager").remove("status");