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.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.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
26 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
27 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
30 import org.opendaylight.infrautils.diagstatus.DiagStatusService;
31 import org.opendaylight.infrautils.diagstatus.ServiceState;
32 import org.opendaylight.infrautils.ready.SystemReadyMonitor;
33 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
34 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
35 import org.opendaylight.mdsal.eos.binding.api.Entity;
36 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipCandidateRegistration;
37 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipChange;
38 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
39 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListenerRegistration;
40 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
41 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
42 import org.opendaylight.ovsdb.lib.OvsdbConnection;
43 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvoker;
44 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvokerImpl;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
49 import org.opendaylight.yangtools.concepts.ListenerRegistration;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 public class SouthboundProvider implements ClusteredDataTreeChangeListener<Topology>, AutoCloseable {
57 private static final Logger LOG = LoggerFactory.getLogger(SouthboundProvider.class);
59 private static final String ENTITY_TYPE = "ovsdb-southbound-provider";
61 public static DataBroker getDb() {
65 // FIXME: get rid of this static
66 @SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
67 private static DataBroker db;
68 private OvsdbConnectionManager cm;
69 private TransactionInvoker txInvoker;
70 private OvsdbDataTreeChangeListener ovsdbDataTreeChangeListener;
71 private final EntityOwnershipService entityOwnershipService;
72 private EntityOwnershipCandidateRegistration registration;
73 private SouthboundPluginInstanceEntityOwnershipListener providerOwnershipChangeListener;
74 private final OvsdbConnection ovsdbConnection;
75 private final InstanceIdentifierCodec instanceIdentifierCodec;
76 private final SystemReadyMonitor systemReadyMonitor;
78 private final AtomicBoolean registered = new AtomicBoolean(false);
79 private ListenerRegistration<SouthboundProvider> operTopologyRegistration;
80 private final OvsdbDiagStatusProvider ovsdbStatusProvider;
81 private static List<String> reconcileBridgeInclusionList = new ArrayList<>();
82 private static List<String> reconcileBridgeExclusionList = new ArrayList<>();
85 public SouthboundProvider(@Reference final DataBroker dataBroker,
86 @Reference final EntityOwnershipService entityOwnershipServiceDependency,
87 @Reference final OvsdbConnection ovsdbConnection,
88 @Reference final DOMSchemaService schemaService,
89 @Reference final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer,
90 @Reference final SystemReadyMonitor systemReadyMonitor,
91 @Reference final DiagStatusService diagStatusService) {
93 this.entityOwnershipService = entityOwnershipServiceDependency;
95 this.ovsdbConnection = ovsdbConnection;
96 this.ovsdbStatusProvider = new OvsdbDiagStatusProvider(diagStatusService);
97 this.instanceIdentifierCodec = new InstanceIdentifierCodec(schemaService,
98 bindingNormalizedNodeSerializer);
99 this.systemReadyMonitor = systemReadyMonitor;
100 LOG.info("SouthboundProvider ovsdbConnectionService Initialized");
104 * Used by blueprint when starting the container.
108 LOG.info("SouthboundProvider Session Initiated");
109 ovsdbStatusProvider.reportStatus(ServiceState.STARTING, "OVSDB initialization in progress");
110 this.txInvoker = new TransactionInvokerImpl(db);
111 cm = new OvsdbConnectionManager(db, txInvoker, entityOwnershipService, ovsdbConnection,
112 instanceIdentifierCodec);
113 ovsdbDataTreeChangeListener = new OvsdbDataTreeChangeListener(db, cm, instanceIdentifierCodec);
115 //Register listener for entityOnwership changes
116 providerOwnershipChangeListener =
117 new SouthboundPluginInstanceEntityOwnershipListener(this,this.entityOwnershipService);
119 //register instance entity to get the ownership of the provider
120 Entity instanceEntity = new Entity(ENTITY_TYPE, ENTITY_TYPE);
122 registration = entityOwnershipService.registerCandidate(instanceEntity);
123 } catch (CandidateAlreadyRegisteredException e) {
124 LOG.warn("OVSDB Southbound Provider instance entity {} was already "
125 + "registered for ownership", instanceEntity, e);
127 InstanceIdentifier<Topology> path = InstanceIdentifier
128 .create(NetworkTopology.class)
129 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID));
130 DataTreeIdentifier<Topology> treeId =
131 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, path);
133 LOG.trace("Registering listener for path {}", treeId);
134 operTopologyRegistration = db.registerDataTreeChangeListener(treeId, this);
139 public void close() {
140 LOG.info("SouthboundProvider Closed");
143 } catch (InterruptedException e) {
144 ovsdbStatusProvider.reportStatus(ServiceState.ERROR, "OVSDB service shutdown error");
145 LOG.debug("SouthboundProvider failed to close TransactionInvoker.");
148 ovsdbDataTreeChangeListener.close();
149 registration.close();
150 providerOwnershipChangeListener.close();
151 if (operTopologyRegistration != null) {
152 operTopologyRegistration.close();
153 operTopologyRegistration = null;
155 ovsdbStatusProvider.reportStatus(ServiceState.UNREGISTERED, "OVSDB Service stopped");
158 private void initializeOvsdbTopology(LogicalDatastoreType type) {
159 InstanceIdentifier<Topology> path = InstanceIdentifier
160 .create(NetworkTopology.class)
161 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID));
162 ReadWriteTransaction transaction = db.newReadWriteTransaction();
163 CheckedFuture<Optional<Topology>, ReadFailedException> ovsdbTp = transaction.read(type, path);
165 if (!ovsdbTp.get().isPresent()) {
166 TopologyBuilder tpb = new TopologyBuilder();
167 tpb.setTopologyId(SouthboundConstants.OVSDB_TOPOLOGY_ID);
168 transaction.put(type, path, tpb.build(), true);
169 transaction.submit();
171 transaction.cancel();
173 } catch (InterruptedException | ExecutionException e) {
174 LOG.error("Error initializing ovsdb topology", e);
178 public void handleOwnershipChange(EntityOwnershipChange ownershipChange) {
179 if (ownershipChange.getState().isOwner()) {
180 LOG.info("*This* instance of OVSDB southbound provider is set as a MASTER instance");
181 LOG.info("Initialize OVSDB topology {} in operational and config data store if not already present",
182 SouthboundConstants.OVSDB_TOPOLOGY_ID);
183 initializeOvsdbTopology(LogicalDatastoreType.OPERATIONAL);
184 initializeOvsdbTopology(LogicalDatastoreType.CONFIGURATION);
186 LOG.info("*This* instance of OVSDB southbound provider is set as a SLAVE instance");
191 public void onDataTreeChanged(Collection<DataTreeModification<Topology>> collection) {
192 if (!registered.getAndSet(true)) {
193 LOG.info("Starting the ovsdb port");
194 ovsdbConnection.registerConnectionListener(cm);
195 LOG.info("Registering deferred system ready listener to start OVSDB Manager later");
196 systemReadyMonitor.registerListener(() -> {
197 ovsdbConnection.startOvsdbManager();
198 LOG.info("Started OVSDB Manager (in system ready listener)");
200 //mdsal registration/deregistration in mdsal update callback should be avoided
202 if (operTopologyRegistration != null) {
203 operTopologyRegistration.close();
204 operTopologyRegistration = null;
207 ovsdbStatusProvider.reportStatus(ServiceState.OPERATIONAL, "OVSDB initialization complete");
211 private static class SouthboundPluginInstanceEntityOwnershipListener implements EntityOwnershipListener {
212 private final SouthboundProvider sp;
213 private final EntityOwnershipListenerRegistration listenerRegistration;
215 SouthboundPluginInstanceEntityOwnershipListener(SouthboundProvider sp,
216 EntityOwnershipService entityOwnershipService) {
218 listenerRegistration = entityOwnershipService.registerListener(ENTITY_TYPE, this);
221 public void close() {
222 this.listenerRegistration.close();
226 public void ownershipChanged(EntityOwnershipChange ownershipChange) {
227 sp.handleOwnershipChange(ownershipChange);
231 public void setSkipMonitoringManagerStatus(boolean flag) {
232 LOG.debug("skipManagerStatus set to {}", flag);
234 SouthboundConstants.SKIP_COLUMN_FROM_TABLE.get("Manager").add("status");
236 SouthboundConstants.SKIP_COLUMN_FROM_TABLE.get("Manager").remove("status");
240 public static void setBridgesReconciliationInclusionList(List<String> bridgeList) {
241 reconcileBridgeInclusionList = bridgeList;
244 public static void setBridgesReconciliationExclusionList(List<String> bridgeList) {
245 reconcileBridgeExclusionList = bridgeList;
248 public static List<String> getBridgesReconciliationInclusionList() {
249 return reconcileBridgeInclusionList;
252 public static List<String> getBridgesReconciliationExclusionList() {
253 return reconcileBridgeExclusionList;