2 * Copyright © 2015, 2017 Ericsson India Global Services Pvt Ltd. 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.hwvtepsouthbound;
10 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
12 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
13 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
14 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
15 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
16 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
17 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
18 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
19 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.controller.sal.core.api.model.SchemaService;
23 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
24 import org.opendaylight.ovsdb.hwvtepsouthbound.reconciliation.configuration.HwvtepReconciliationManager;
25 import org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md.TransactionInvoker;
26 import org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md.TransactionInvokerImpl;
27 import org.opendaylight.ovsdb.lib.OvsdbConnection;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import com.google.common.base.Optional;
37 import com.google.common.util.concurrent.CheckedFuture;
39 public class HwvtepSouthboundProvider implements AutoCloseable {
41 private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundProvider.class);
42 private static final String ENTITY_TYPE = "ovsdb-hwvtepsouthbound-provider";
44 public static DataBroker getDb() {
48 private static DataBroker db;
49 private final EntityOwnershipService entityOwnershipService;
50 private final OvsdbConnection ovsdbConnection;
52 private HwvtepConnectionManager cm;
53 private TransactionInvoker txInvoker;
54 private EntityOwnershipCandidateRegistration registration;
55 private HwvtepsbPluginInstanceEntityOwnershipListener providerOwnershipChangeListener;
56 private HwvtepDataChangeListener hwvtepDTListener;
57 private HwvtepReconciliationManager hwvtepReconciliationManager;
59 public HwvtepSouthboundProvider(final DataBroker dataBroker,
60 final EntityOwnershipService entityOwnershipServiceDependency,
61 final OvsdbConnection ovsdbConnection,
62 final SchemaService schemaService,
63 final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer) {
65 this.entityOwnershipService = entityOwnershipServiceDependency;
67 this.ovsdbConnection = ovsdbConnection;
68 HwvtepSouthboundUtil.setInstanceIdentifierCodec(new InstanceIdentifierCodec(schemaService,
69 bindingNormalizedNodeSerializer));
70 LOG.info("HwvtepSouthboundProvider ovsdbConnectionService: {}", ovsdbConnection);
74 * Used by blueprint when starting the container.
77 LOG.info("HwvtepSouthboundProvider Session Initiated");
78 txInvoker = new TransactionInvokerImpl(db);
79 cm = new HwvtepConnectionManager(db, txInvoker, entityOwnershipService);
80 hwvtepDTListener = new HwvtepDataChangeListener(db, cm);
81 hwvtepReconciliationManager = new HwvtepReconciliationManager(db, cm);
82 //Register listener for entityOnwership changes
83 providerOwnershipChangeListener =
84 new HwvtepsbPluginInstanceEntityOwnershipListener(this,this.entityOwnershipService);
86 //register instance entity to get the ownership of the provider
87 Entity instanceEntity = new Entity(ENTITY_TYPE, ENTITY_TYPE);
89 Optional<EntityOwnershipState> ownershipStateOpt = entityOwnershipService.getOwnershipState(instanceEntity);
90 registration = entityOwnershipService.registerCandidate(instanceEntity);
91 if (ownershipStateOpt.isPresent()) {
92 EntityOwnershipState ownershipState = ownershipStateOpt.get();
93 if (ownershipState.hasOwner() && !ownershipState.isOwner()) {
94 ovsdbConnection.registerConnectionListener(cm);
95 ovsdbConnection.startOvsdbManager();
98 } catch (CandidateAlreadyRegisteredException e) {
99 LOG.warn("HWVTEP Southbound Provider instance entity {} was already "
100 + "registered for ownership", instanceEntity, e);
105 public void close() throws Exception {
106 LOG.info("HwvtepSouthboundProvider Closed");
111 if(registration != null) {
112 registration.close();
115 if(providerOwnershipChangeListener != null) {
116 providerOwnershipChangeListener.close();
117 providerOwnershipChangeListener = null;
119 if(hwvtepDTListener != null) {
120 hwvtepDTListener.close();
121 hwvtepDTListener = null;
125 private void initializeHwvtepTopology(LogicalDatastoreType type) {
126 InstanceIdentifier<Topology> path = InstanceIdentifier
127 .create(NetworkTopology.class)
128 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID));
129 ReadWriteTransaction transaction = db.newReadWriteTransaction();
130 CheckedFuture<Optional<Topology>, ReadFailedException> hwvtepTp = transaction.read(type, path);
132 if (!hwvtepTp.get().isPresent()) {
133 TopologyBuilder tpb = new TopologyBuilder();
134 tpb.setTopologyId(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
135 transaction.put(type, path, tpb.build(), true);
136 transaction.submit();
138 transaction.cancel();
140 } catch (Exception e) {
141 LOG.error("Error initializing hwvtep topology", e);
145 public void handleOwnershipChange(EntityOwnershipChange ownershipChange) {
146 if (ownershipChange.isOwner()) {
147 LOG.info("*This* instance of HWVTEP southbound provider is set as a MASTER instance");
148 LOG.info("Initialize HWVTEP topology {} in operational and config data store if not already present"
149 ,HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
150 initializeHwvtepTopology(LogicalDatastoreType.OPERATIONAL);
151 initializeHwvtepTopology(LogicalDatastoreType.CONFIGURATION);
153 LOG.info("*This* instance of HWVTEP southbound provider is set as a SLAVE instance");
155 ovsdbConnection.registerConnectionListener(cm);
156 ovsdbConnection.startOvsdbManager();
159 private class HwvtepsbPluginInstanceEntityOwnershipListener implements EntityOwnershipListener {
160 private HwvtepSouthboundProvider hsp;
161 private EntityOwnershipListenerRegistration listenerRegistration;
163 HwvtepsbPluginInstanceEntityOwnershipListener(HwvtepSouthboundProvider hsp,
164 EntityOwnershipService entityOwnershipService) {
166 listenerRegistration = entityOwnershipService.registerListener(ENTITY_TYPE, this);
169 public void close() {
170 this.listenerRegistration.close();
173 public void ownershipChanged(EntityOwnershipChange ownershipChange) {
174 hsp.handleOwnershipChange(ownershipChange);