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 com.google.common.base.Optional;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import java.util.Collection;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.TimeUnit;
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.apache.aries.blueprint.annotation.service.Service;
22 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
25 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
26 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
29 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
30 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
31 import org.opendaylight.mdsal.eos.binding.api.Entity;
32 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipCandidateRegistration;
33 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipChange;
34 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
35 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListenerRegistration;
36 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
37 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
38 import org.opendaylight.ovsdb.hwvtepsouthbound.reconciliation.configuration.HwvtepReconciliationManager;
39 import org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md.TransactionInvoker;
40 import org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md.TransactionInvokerImpl;
41 import org.opendaylight.ovsdb.lib.OvsdbConnection;
42 import org.opendaylight.ovsdb.utils.mdsal.utils.Scheduler;
43 import org.opendaylight.ovsdb.utils.mdsal.utils.ShardStatusMonitor;
44 import org.opendaylight.serviceutils.upgrade.UpgradeState;
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 @Service(classes = HwvtepSouthboundProvider.class) // only because HwvtepCacheDisplayCmd needs a @Reference to this
56 public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener<Topology>, AutoCloseable {
58 private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundProvider.class);
59 private static final String ENTITY_TYPE = "ovsdb-hwvtepsouthbound-provider";
61 private final DataBroker dataBroker;
62 private final EntityOwnershipService entityOwnershipService;
63 private final OvsdbConnection ovsdbConnection;
65 private HwvtepConnectionManager cm;
66 private TransactionInvoker txInvoker;
67 private EntityOwnershipCandidateRegistration registration;
68 private HwvtepsbPluginInstanceEntityOwnershipListener providerOwnershipChangeListener;
69 private HwvtepDataChangeListener hwvtepDTListener;
70 private HwvtepReconciliationManager hwvtepReconciliationManager;
71 private final AtomicBoolean registered = new AtomicBoolean(false);
72 private ListenerRegistration<HwvtepSouthboundProvider> operTopologyRegistration;
73 private int shardStatusCheckRetryCount = 1000;
74 private UpgradeState upgradeState;
77 public HwvtepSouthboundProvider(@Reference final DataBroker dataBroker,
78 @Reference final EntityOwnershipService entityOwnershipServiceDependency,
79 @Reference final OvsdbConnection ovsdbConnection,
80 @Reference final DOMSchemaService schemaService,
81 @Reference final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer,
82 @Reference final UpgradeState upgradeState) {
83 this.dataBroker = dataBroker;
84 this.entityOwnershipService = entityOwnershipServiceDependency;
86 this.ovsdbConnection = ovsdbConnection;
87 this.upgradeState = upgradeState;
88 HwvtepSouthboundUtil.setInstanceIdentifierCodec(new InstanceIdentifierCodec(schemaService,
89 bindingNormalizedNodeSerializer));
90 LOG.info("HwvtepSouthboundProvider ovsdbConnectionService: {}", ovsdbConnection);
94 * Used by blueprint when starting the container.
98 boolean isDatastoreAvailable = false;
101 while (retryCount < shardStatusCheckRetryCount) {
102 isDatastoreAvailable = ShardStatusMonitor.getShardStatus(ShardStatusMonitor.TOPOLOGY_SHARDS);
103 if (isDatastoreAvailable) {
106 LOG.warn("Hwvtep: retrying shard status check for the {} time", ++retryCount);
109 if (isDatastoreAvailable) {
110 LOG.info("Hwvtep is UP");
113 } catch (InterruptedException e) {
114 LOG.error("Error in intializing the Hwvtep Southbound ", e);
118 private void init2() {
119 LOG.info("HwvtepSouthboundProvider Session Initiated");
120 txInvoker = new TransactionInvokerImpl(dataBroker);
121 cm = new HwvtepConnectionManager(dataBroker, txInvoker, entityOwnershipService, ovsdbConnection);
122 registerConfigListenerPostUpgrade();
123 //Register listener for entityOnwership changes
124 providerOwnershipChangeListener =
125 new HwvtepsbPluginInstanceEntityOwnershipListener(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("HWVTEP 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(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID));
138 DataTreeIdentifier<Topology> treeId =
139 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, path);
141 LOG.trace("Registering listener for path {}", treeId);
142 operTopologyRegistration = dataBroker.registerDataTreeChangeListener(treeId, this);
143 Scheduler.getScheduledExecutorService().schedule(() -> {
144 if (!registered.get()) {
146 LOG.error("Timed out to get eos notification opening the port now");
148 }, HwvtepSouthboundConstants.PORT_OPEN_MAX_DELAY_IN_MINS, TimeUnit.MINUTES);
151 private void registerConfigListenerPostUpgrade() {
152 if (upgradeState.isUpgradeInProgress()) {
153 LOG.error("Upgrade is in progress delay config data change listener registration");
154 Scheduler.getScheduledExecutorService().schedule(() -> registerConfigListenerPostUpgrade(),
155 60, TimeUnit.SECONDS);
158 hwvtepDTListener = new HwvtepDataChangeListener(dataBroker, cm);
159 hwvtepReconciliationManager = new HwvtepReconciliationManager(dataBroker, cm);
164 @SuppressWarnings("checkstyle:IllegalCatch")
165 public void close() throws Exception {
166 LOG.info("HwvtepSouthboundProvider Closed");
167 if (txInvoker != null) {
171 } catch (Exception e) {
172 LOG.error("HWVTEP Southbound Provider failed to close TransactionInvoker", e);
179 if (registration != null) {
180 registration.close();
183 if (providerOwnershipChangeListener != null) {
184 providerOwnershipChangeListener.close();
185 providerOwnershipChangeListener = null;
187 if (hwvtepDTListener != null) {
188 hwvtepDTListener.close();
189 hwvtepDTListener = null;
191 if (operTopologyRegistration != null) {
192 operTopologyRegistration.close();
193 operTopologyRegistration = null;
197 private void initializeHwvtepTopology(final LogicalDatastoreType type) {
198 InstanceIdentifier<Topology> path = InstanceIdentifier
199 .create(NetworkTopology.class)
200 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID));
201 ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
202 CheckedFuture<Optional<Topology>, ReadFailedException> hwvtepTp = transaction.read(type, path);
204 if (!hwvtepTp.get().isPresent()) {
205 TopologyBuilder tpb = new TopologyBuilder();
206 tpb.setTopologyId(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
207 transaction.put(type, path, tpb.build(), true);
208 transaction.submit();
210 transaction.cancel();
212 } catch (InterruptedException | ExecutionException e) {
213 LOG.error("Error initializing hwvtep topology", e);
217 public void handleOwnershipChange(final EntityOwnershipChange ownershipChange) {
218 if (ownershipChange.getState().isOwner()) {
219 LOG.info("*This* instance of HWVTEP southbound provider is set as a MASTER instance");
220 LOG.info("Initialize HWVTEP topology {} in operational and config data store if not already present",
221 HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
222 initializeHwvtepTopology(LogicalDatastoreType.OPERATIONAL);
223 initializeHwvtepTopology(LogicalDatastoreType.CONFIGURATION);
225 LOG.info("*This* instance of HWVTEP southbound provider is set as a SLAVE instance");
231 public void onDataTreeChanged(final Collection<DataTreeModification<Topology>> collection) {
234 if (operTopologyRegistration != null) {
235 operTopologyRegistration.close();
236 operTopologyRegistration = null;
240 private void openOvsdbPort() {
241 if (!registered.getAndSet(true)) {
242 LOG.info("Starting the ovsdb port");
243 ovsdbConnection.registerConnectionListener(cm);
244 ovsdbConnection.startOvsdbManager();
248 public void setShardStatusCheckRetryCount(int retryCount) {
249 this.shardStatusCheckRetryCount = retryCount;
252 private static class HwvtepsbPluginInstanceEntityOwnershipListener implements EntityOwnershipListener {
253 private final HwvtepSouthboundProvider hsp;
254 private final EntityOwnershipListenerRegistration listenerRegistration;
256 HwvtepsbPluginInstanceEntityOwnershipListener(final HwvtepSouthboundProvider hsp,
257 final EntityOwnershipService entityOwnershipService) {
259 listenerRegistration = entityOwnershipService.registerListener(ENTITY_TYPE, this);
262 public void close() {
263 this.listenerRegistration.close();
267 public void ownershipChanged(final EntityOwnershipChange ownershipChange) {
268 hsp.handleOwnershipChange(ownershipChange);
272 public HwvtepConnectionManager getHwvtepConnectionManager() {