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.concurrent.ExecutionException;
17 import java.util.concurrent.atomic.AtomicBoolean;
18 import javax.annotation.PostConstruct;
19 import javax.annotation.PreDestroy;
20 import javax.inject.Inject;
21 import javax.inject.Singleton;
22 import org.apache.aries.blueprint.annotation.service.Reference;
23 import org.opendaylight.infrautils.diagstatus.DiagStatusService;
24 import org.opendaylight.infrautils.diagstatus.ServiceState;
25 import org.opendaylight.infrautils.ready.SystemReadyMonitor;
26 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
27 import org.opendaylight.mdsal.binding.api.DataBroker;
28 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
29 import org.opendaylight.mdsal.binding.api.DataTreeModification;
30 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
31 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
32 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
33 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
34 import org.opendaylight.mdsal.eos.binding.api.Entity;
35 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipCandidateRegistration;
36 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipChange;
37 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
38 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListenerRegistration;
39 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
40 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
41 import org.opendaylight.ovsdb.lib.OvsdbConnection;
42 import org.opendaylight.ovsdb.southbound.reconciliation.OvsdbUpgradeStateListener;
43 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvoker;
44 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvokerImpl;
45 import org.opendaylight.serviceutils.upgrade.UpgradeState;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
50 import org.opendaylight.yangtools.concepts.ListenerRegistration;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 public class SouthboundProvider implements ClusteredDataTreeChangeListener<Topology>, AutoCloseable {
57 private static final Logger LOG = LoggerFactory.getLogger(SouthboundProvider.class);
58 private static final String ENTITY_TYPE = "ovsdb-southbound-provider";
60 // FIXME: get rid of this static
61 @SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
62 private static DataBroker db;
64 public static DataBroker getDb() {
68 private OvsdbConnectionManager cm;
69 private TransactionInvoker txInvoker;
70 private OvsdbDataTreeChangeListener ovsdbDataTreeChangeListener;
71 private OvsdbOperGlobalListener ovsdbOperGlobalListener;
72 private final EntityOwnershipService entityOwnershipService;
73 private EntityOwnershipCandidateRegistration registration;
74 private SouthboundPluginInstanceEntityOwnershipListener providerOwnershipChangeListener;
75 private final OvsdbConnection ovsdbConnection;
76 private final InstanceIdentifierCodec instanceIdentifierCodec;
77 private final SystemReadyMonitor systemReadyMonitor;
78 private final UpgradeState upgradeState;
79 private final AtomicBoolean registered = new AtomicBoolean(false);
80 private ListenerRegistration<SouthboundProvider> operTopologyRegistration;
81 private final OvsdbDiagStatusProvider ovsdbStatusProvider;
82 private static List<String> reconcileBridgeInclusionList = new ArrayList<>();
83 private static List<String> reconcileBridgeExclusionList = new ArrayList<>();
84 private OvsdbUpgradeStateListener ovsdbUpgradeStateListener;
87 public SouthboundProvider(@Reference final DataBroker dataBroker,
88 @Reference final EntityOwnershipService entityOwnershipServiceDependency,
89 @Reference final OvsdbConnection ovsdbConnection,
90 @Reference final DOMSchemaService schemaService,
91 @Reference final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer,
92 @Reference final SystemReadyMonitor systemReadyMonitor,
93 @Reference final DiagStatusService diagStatusService,
94 @Reference final UpgradeState upgradeState) {
95 SouthboundProvider.db = dataBroker;
96 this.entityOwnershipService = entityOwnershipServiceDependency;
98 this.ovsdbConnection = ovsdbConnection;
99 this.ovsdbStatusProvider = new OvsdbDiagStatusProvider(diagStatusService);
100 this.instanceIdentifierCodec = new InstanceIdentifierCodec(schemaService,
101 bindingNormalizedNodeSerializer);
102 this.systemReadyMonitor = systemReadyMonitor;
103 this.upgradeState = upgradeState;
104 LOG.info("SouthboundProvider ovsdbConnectionService Initialized");
108 * Used by blueprint when starting the container.
112 LOG.info("SouthboundProvider Session Initiated");
113 ovsdbStatusProvider.reportStatus(ServiceState.STARTING, "OVSDB initialization in progress");
114 this.txInvoker = new TransactionInvokerImpl(db);
115 cm = new OvsdbConnectionManager(db, txInvoker, entityOwnershipService, ovsdbConnection,
116 instanceIdentifierCodec, upgradeState);
117 ovsdbDataTreeChangeListener = new OvsdbDataTreeChangeListener(db, cm, instanceIdentifierCodec);
118 ovsdbOperGlobalListener = new OvsdbOperGlobalListener(db, cm, txInvoker);
120 //Register listener for entityOnwership changes
121 providerOwnershipChangeListener =
122 new SouthboundPluginInstanceEntityOwnershipListener(this,this.entityOwnershipService);
124 //register instance entity to get the ownership of the provider
125 Entity instanceEntity = new Entity(ENTITY_TYPE, ENTITY_TYPE);
127 registration = entityOwnershipService.registerCandidate(instanceEntity);
128 } catch (CandidateAlreadyRegisteredException e) {
129 LOG.warn("OVSDB Southbound Provider instance entity {} was already "
130 + "registered for ownership", instanceEntity, e);
132 InstanceIdentifier<Topology> path = InstanceIdentifier
133 .create(NetworkTopology.class)
134 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID));
135 DataTreeIdentifier<Topology> treeId =
136 DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, path);
138 LOG.trace("Registering listener for path {}", treeId);
139 operTopologyRegistration = db.registerDataTreeChangeListener(treeId, this);
140 ovsdbUpgradeStateListener = new OvsdbUpgradeStateListener(db, cm);
145 public void close() {
146 LOG.info("SouthboundProvider Closed");
149 } catch (InterruptedException e) {
150 ovsdbStatusProvider.reportStatus(ServiceState.ERROR, "OVSDB service shutdown error");
151 LOG.debug("SouthboundProvider failed to close TransactionInvoker.");
154 ovsdbDataTreeChangeListener.close();
155 ovsdbOperGlobalListener.close();
156 registration.close();
157 providerOwnershipChangeListener.close();
158 if (operTopologyRegistration != null) {
159 operTopologyRegistration.close();
160 operTopologyRegistration = null;
162 ovsdbStatusProvider.reportStatus(ServiceState.UNREGISTERED, "OVSDB Service stopped");
163 if (ovsdbUpgradeStateListener != null) {
164 ovsdbUpgradeStateListener.close();
168 private static void initializeOvsdbTopology(final LogicalDatastoreType type) {
169 InstanceIdentifier<Topology> path = InstanceIdentifier
170 .create(NetworkTopology.class)
171 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID));
172 ReadWriteTransaction transaction = db.newReadWriteTransaction();
173 FluentFuture<Boolean> ovsdbTp = transaction.exists(type, path);
175 if (!ovsdbTp.get().booleanValue()) {
176 TopologyBuilder tpb = new TopologyBuilder();
177 tpb.setTopologyId(SouthboundConstants.OVSDB_TOPOLOGY_ID);
178 transaction.mergeParentStructurePut(type, path, tpb.build());
179 transaction.commit();
181 transaction.cancel();
183 } catch (InterruptedException | ExecutionException e) {
184 LOG.error("Error initializing ovsdb topology", e);
188 public void handleOwnershipChange(final EntityOwnershipChange ownershipChange) {
189 if (ownershipChange.getState().isOwner()) {
190 LOG.info("*This* instance of OVSDB southbound provider is set as a MASTER instance");
191 LOG.info("Initialize OVSDB topology {} in operational and config data store if not already present",
192 SouthboundConstants.OVSDB_TOPOLOGY_ID);
193 initializeOvsdbTopology(LogicalDatastoreType.OPERATIONAL);
194 initializeOvsdbTopology(LogicalDatastoreType.CONFIGURATION);
196 LOG.info("*This* instance of OVSDB southbound provider is set as a SLAVE instance");
201 public void onDataTreeChanged(final Collection<DataTreeModification<Topology>> collection) {
202 if (!registered.getAndSet(true)) {
203 LOG.info("Starting the ovsdb port");
204 ovsdbConnection.registerConnectionListener(cm);
205 LOG.info("Registering deferred system ready listener to start OVSDB Manager later");
206 systemReadyMonitor.registerListener(() -> {
207 ovsdbConnection.startOvsdbManager();
208 LOG.info("Started OVSDB Manager (in system ready listener)");
211 if (operTopologyRegistration != null) {
212 operTopologyRegistration.close();
213 operTopologyRegistration = null;
215 ovsdbStatusProvider.reportStatus(ServiceState.OPERATIONAL, "OVSDB initialization complete");
219 private static class SouthboundPluginInstanceEntityOwnershipListener implements EntityOwnershipListener {
220 private final SouthboundProvider sp;
221 private final EntityOwnershipListenerRegistration listenerRegistration;
223 SouthboundPluginInstanceEntityOwnershipListener(final SouthboundProvider sp,
224 final EntityOwnershipService entityOwnershipService) {
226 listenerRegistration = entityOwnershipService.registerListener(ENTITY_TYPE, this);
229 public void close() {
230 this.listenerRegistration.close();
234 public void ownershipChanged(final EntityOwnershipChange ownershipChange) {
235 sp.handleOwnershipChange(ownershipChange);
239 public void setSkipMonitoringManagerStatus(final boolean flag) {
240 LOG.debug("skipManagerStatus set to {}", flag);
242 SouthboundConstants.SKIP_COLUMN_FROM_TABLE.get("Manager").add("status");
244 SouthboundConstants.SKIP_COLUMN_FROM_TABLE.get("Manager").remove("status");
248 public static void setBridgesReconciliationInclusionList(final List<String> bridgeList) {
249 reconcileBridgeInclusionList = bridgeList;
252 public static void setBridgesReconciliationExclusionList(final List<String> bridgeList) {
253 reconcileBridgeExclusionList = bridgeList;
256 public static List<String> getBridgesReconciliationInclusionList() {
257 return reconcileBridgeInclusionList;
260 public static List<String> getBridgesReconciliationExclusionList() {
261 return reconcileBridgeExclusionList;
265 boolean isRegistered() {
266 return registered.get();
269 public UpgradeState getUpgradeState() {