2 * Copyright (c) 2018 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.netvirt.elan.recovery.impl;
10 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12 import java.util.concurrent.ExecutionException;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
16 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
17 import org.opendaylight.genius.utils.clustering.EntityOwnershipUtils;
18 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
19 import org.opendaylight.mdsal.binding.api.DataBroker;
20 import org.opendaylight.netvirt.elan.internal.ElanServiceProvider;
21 import org.opendaylight.netvirt.elan.utils.ElanUtils;
22 import org.opendaylight.serviceutils.srm.ServiceRecoveryInterface;
23 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.types.rev180626.NetvirtElanInterface;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 public class ElanInterfaceRecoveryHandler implements ServiceRecoveryInterface {
33 private static final Logger LOG = LoggerFactory.getLogger(ElanInterfaceRecoveryHandler.class);
35 private final ManagedNewTransactionRunner txRunner;
36 private final ElanServiceProvider elanServiceProvider;
37 private final EntityOwnershipUtils entityOwnershipUtils;
40 public ElanInterfaceRecoveryHandler(DataBroker dataBroker,
41 ElanServiceProvider elanServiceProvider,
42 ServiceRecoveryRegistry serviceRecoveryRegistry,
43 EntityOwnershipUtils entityOwnershipUtils) {
44 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
45 this.elanServiceProvider = elanServiceProvider;
46 this.entityOwnershipUtils = entityOwnershipUtils;
47 serviceRecoveryRegistry.registerServiceRecoveryRegistry(buildServiceRegistryKey(), this);
51 public void recoverService(String entityId) {
52 if (!entityOwnershipUtils.isEntityOwner(HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
53 HwvtepSouthboundConstants.ELAN_ENTITY_NAME)) {
56 LOG.info("recover elan interface {}", entityId);
57 // Fetch the elan interface from elan interface config DS first.
58 ElanInterface elanInterface = elanServiceProvider.getElanInterfaceByElanInterfaceName(entityId);
59 if (elanInterface != null) {
60 // Do a delete and recreate of the elan interface configuration.
61 InstanceIdentifier<ElanInterface> elanInterfaceId = ElanUtils
62 .getElanInterfaceConfigurationDataPathId(entityId);
64 LOG.trace("deleting elan interface {}", entityId);
65 txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
66 tx -> tx.delete(elanInterfaceId)).get();
67 LOG.trace("recreating elan interface {}, {}", entityId, elanInterface);
68 txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
69 tx -> tx.put(elanInterfaceId, elanInterface)).get();
70 } catch (InterruptedException | ExecutionException e) {
71 LOG.error("Service recovery failed for elan interface {}", entityId, e);
76 private static String buildServiceRegistryKey() {
77 return NetvirtElanInterface.class.toString();