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.annotations.VisibleForTesting;
11 import com.google.common.util.concurrent.FluentFuture;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.Optional;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.atomic.AtomicBoolean;
19 import javax.annotation.PostConstruct;
20 import javax.annotation.PreDestroy;
21 import javax.inject.Inject;
22 import javax.inject.Singleton;
23 import org.apache.aries.blueprint.annotation.service.Reference;
25 import org.opendaylight.infrautils.diagstatus.DiagStatusService;
26 import org.opendaylight.infrautils.diagstatus.ServiceState;
27 import org.opendaylight.infrautils.ready.SystemReadyMonitor;
28 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
29 import org.opendaylight.mdsal.binding.api.DataBroker;
30 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
31 import org.opendaylight.mdsal.binding.api.DataTreeModification;
32 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
33 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
34 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
35 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
36 import org.opendaylight.mdsal.eos.binding.api.Entity;
37 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipCandidateRegistration;
38 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipChange;
39 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
40 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListenerRegistration;
41 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
42 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
43 import org.opendaylight.ovsdb.lib.OvsdbConnection;
44 import org.opendaylight.ovsdb.southbound.reconciliation.OvsdbUpgradeStateListener;
45 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvoker;
46 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvokerImpl;
47 import org.opendaylight.serviceutils.upgrade.UpgradeState;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
52 import org.opendaylight.yangtools.concepts.ListenerRegistration;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
58 public class SouthboundProvider implements ClusteredDataTreeChangeListener<Topology>, AutoCloseable {
60 private static final Logger LOG = LoggerFactory.getLogger(SouthboundProvider.class);
62 private static final String ENTITY_TYPE = "ovsdb-southbound-provider";
64 public static DataBroker getDb() {
68 // FIXME: get rid of this static
69 @SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
70 private static DataBroker db;
71 private OvsdbConnectionManager cm;
72 private TransactionInvoker txInvoker;
73 private OvsdbDataTreeChangeListener ovsdbDataTreeChangeListener;
74 private OvsdbOperGlobalListener ovsdbOperGlobalListener;
75 private final EntityOwnershipService entityOwnershipService;
76 private EntityOwnershipCandidateRegistration registration;
77 private SouthboundPluginInstanceEntityOwnershipListener providerOwnershipChangeListener;
78 private final OvsdbConnection ovsdbConnection;
79 private final InstanceIdentifierCodec instanceIdentifierCodec;
80 private final SystemReadyMonitor systemReadyMonitor;
81 private final UpgradeState upgradeState;
82 private final AtomicBoolean registered = new AtomicBoolean(false);
83 private ListenerRegistration<SouthboundProvider> operTopologyRegistration;
84 private final OvsdbDiagStatusProvider ovsdbStatusProvider;
85 private static List<String> reconcileBridgeInclusionList = new ArrayList<>();
86 private static List<String> reconcileBridgeExclusionList = new ArrayList<>();
87 private OvsdbUpgradeStateListener ovsdbUpgradeStateListener;
90 public SouthboundProvider(@Reference final DataBroker dataBroker,
91 @Reference final EntityOwnershipService entityOwnershipServiceDependency,
92 @Reference final OvsdbConnection ovsdbConnection,
93 @Reference final DOMSchemaService schemaService,
94 @Reference final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer,
95 @Reference final SystemReadyMonitor systemReadyMonitor,
96 @Reference final DiagStatusService diagStatusService,
97 @Reference final UpgradeState upgradeState) {
98 SouthboundProvider.db = dataBroker;
99 this.entityOwnershipService = entityOwnershipServiceDependency;
101 this.ovsdbConnection = ovsdbConnection;
102 this.ovsdbStatusProvider = new OvsdbDiagStatusProvider(diagStatusService);
103 this.instanceIdentifierCodec = new InstanceIdentifierCodec(schemaService,
104 bindingNormalizedNodeSerializer);
105 this.systemReadyMonitor = systemReadyMonitor;
106 this.upgradeState = upgradeState;
107 LOG.info("SouthboundProvider ovsdbConnectionService Initialized");
111 * Used by blueprint when starting the container.
115 LOG.info("SouthboundProvider Session Initiated");
116 ovsdbStatusProvider.reportStatus(ServiceState.STARTING, "OVSDB initialization in progress");
117 this.txInvoker = new TransactionInvokerImpl(db);
118 cm = new OvsdbConnectionManager(db, txInvoker, entityOwnershipService, ovsdbConnection,
119 instanceIdentifierCodec, upgradeState);
120 ovsdbDataTreeChangeListener = new OvsdbDataTreeChangeListener(db, cm, instanceIdentifierCodec);
121 ovsdbOperGlobalListener = new OvsdbOperGlobalListener(db, cm, txInvoker);
123 //Register listener for entityOnwership changes
124 providerOwnershipChangeListener =
125 new SouthboundPluginInstanceEntityOwnershipListener(this,this.entityOwnershipService);
127 //register instance entity to get the ownership of the provider
128 Entity instanceEntity = new Entity(ENTITY_TYPE, ENTITY_TYPE);
130 registration = entityOwnershipService.registerCandidate(instanceEntity);
131 } catch (CandidateAlreadyRegisteredException e) {
132 LOG.warn("OVSDB Southbound Provider instance entity {} was already "
133 + "registered for ownership", instanceEntity, e);
135 InstanceIdentifier<Topology> path = InstanceIdentifier
136 .create(NetworkTopology.class)
137 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID));
138 DataTreeIdentifier<Topology> treeId =
139 DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, path);
141 LOG.trace("Registering listener for path {}", treeId);
142 operTopologyRegistration = db.registerDataTreeChangeListener(treeId, this);
143 ovsdbUpgradeStateListener = new OvsdbUpgradeStateListener(db, cm);
148 public void close() {
149 LOG.info("SouthboundProvider Closed");
152 } catch (InterruptedException e) {
153 ovsdbStatusProvider.reportStatus(ServiceState.ERROR, "OVSDB service shutdown error");
154 LOG.debug("SouthboundProvider failed to close TransactionInvoker.");
157 ovsdbDataTreeChangeListener.close();
158 ovsdbOperGlobalListener.close();
159 registration.close();
160 providerOwnershipChangeListener.close();
161 if (operTopologyRegistration != null) {
162 operTopologyRegistration.close();
163 operTopologyRegistration = null;
165 ovsdbStatusProvider.reportStatus(ServiceState.UNREGISTERED, "OVSDB Service stopped");
166 if (ovsdbUpgradeStateListener != null) {
167 ovsdbUpgradeStateListener.close();
171 private void initializeOvsdbTopology(final LogicalDatastoreType type) {
172 InstanceIdentifier<Topology> path = InstanceIdentifier
173 .create(NetworkTopology.class)
174 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID));
175 ReadWriteTransaction transaction = db.newReadWriteTransaction();
176 FluentFuture<Optional<Topology>> ovsdbTp = transaction.read(type, path);
178 if (!ovsdbTp.get().isPresent()) {
179 TopologyBuilder tpb = new TopologyBuilder();
180 tpb.setTopologyId(SouthboundConstants.OVSDB_TOPOLOGY_ID);
181 transaction.put(type, path, tpb.build(), true);
182 transaction.commit();
184 transaction.cancel();
186 } catch (InterruptedException | ExecutionException e) {
187 LOG.error("Error initializing ovsdb topology", e);
191 public void handleOwnershipChange(final EntityOwnershipChange ownershipChange) {
192 if (ownershipChange.getState().isOwner()) {
193 LOG.info("*This* instance of OVSDB southbound provider is set as a MASTER instance");
194 LOG.info("Initialize OVSDB topology {} in operational and config data store if not already present",
195 SouthboundConstants.OVSDB_TOPOLOGY_ID);
196 initializeOvsdbTopology(LogicalDatastoreType.OPERATIONAL);
197 initializeOvsdbTopology(LogicalDatastoreType.CONFIGURATION);
199 LOG.info("*This* instance of OVSDB southbound provider is set as a SLAVE instance");
204 public void onDataTreeChanged(final Collection<DataTreeModification<Topology>> collection) {
205 if (!registered.getAndSet(true)) {
206 LOG.info("Starting the ovsdb port");
207 ovsdbConnection.registerConnectionListener(cm);
208 LOG.info("Registering deferred system ready listener to start OVSDB Manager later");
209 systemReadyMonitor.registerListener(() -> {
210 ovsdbConnection.startOvsdbManager();
211 LOG.info("Started OVSDB Manager (in system ready listener)");
214 if (operTopologyRegistration != null) {
215 operTopologyRegistration.close();
216 operTopologyRegistration = null;
218 ovsdbStatusProvider.reportStatus(ServiceState.OPERATIONAL, "OVSDB initialization complete");
222 private static class SouthboundPluginInstanceEntityOwnershipListener implements EntityOwnershipListener {
223 private final SouthboundProvider sp;
224 private final EntityOwnershipListenerRegistration listenerRegistration;
226 SouthboundPluginInstanceEntityOwnershipListener(final SouthboundProvider sp,
227 final EntityOwnershipService entityOwnershipService) {
229 listenerRegistration = entityOwnershipService.registerListener(ENTITY_TYPE, this);
232 public void close() {
233 this.listenerRegistration.close();
237 public void ownershipChanged(final EntityOwnershipChange ownershipChange) {
238 sp.handleOwnershipChange(ownershipChange);
242 public void setSkipMonitoringManagerStatus(final boolean flag) {
243 LOG.debug("skipManagerStatus set to {}", flag);
245 SouthboundConstants.SKIP_COLUMN_FROM_TABLE.get("Manager").add("status");
247 SouthboundConstants.SKIP_COLUMN_FROM_TABLE.get("Manager").remove("status");
251 public static void setBridgesReconciliationInclusionList(final List<String> bridgeList) {
252 reconcileBridgeInclusionList = bridgeList;
255 public static void setBridgesReconciliationExclusionList(final List<String> bridgeList) {
256 reconcileBridgeExclusionList = bridgeList;
259 public static List<String> getBridgesReconciliationInclusionList() {
260 return reconcileBridgeInclusionList;
263 public static List<String> getBridgesReconciliationExclusionList() {
264 return reconcileBridgeExclusionList;
268 boolean isRegistered() {
269 return registered.get();
272 public UpgradeState getUpgradeState() {