bf349fd62f9355f4cd689d4cf6de0de5c3dbba37
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / recovery / impl / ElanInterfaceRecoveryHandler.java
1 /*
2  * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netvirt.elan.recovery.impl;
9
10 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
11
12 import java.util.concurrent.ExecutionException;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
17 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
18 import org.opendaylight.genius.utils.clustering.EntityOwnershipUtils;
19 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
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;
29
30 @Singleton
31 public class ElanInterfaceRecoveryHandler implements ServiceRecoveryInterface {
32
33     private static final Logger LOG = LoggerFactory.getLogger(ElanInterfaceRecoveryHandler.class);
34
35     private final ManagedNewTransactionRunner txRunner;
36     private final ElanServiceProvider elanServiceProvider;
37     private final EntityOwnershipUtils entityOwnershipUtils;
38
39     @Inject
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);
48     }
49
50     @Override
51     public void recoverService(String entityId) {
52         if (!entityOwnershipUtils.isEntityOwner(HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
53                 HwvtepSouthboundConstants.ELAN_ENTITY_NAME)) {
54             return;
55         }
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);
63             try {
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);
72             }
73         }
74     }
75
76     private static String buildServiceRegistryKey() {
77         return NetvirtElanInterface.class.toString();
78     }
79 }