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