InterfaceManager Service Recovery
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / recovery / impl / InterfaceInstanceRecoveryHandler.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.genius.interfacemanager.recovery.impl;
9
10 import java.util.Collections;
11 import javax.inject.Inject;
12 import javax.inject.Singleton;
13
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
17 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
18 import org.opendaylight.genius.interfacemanager.IfmConstants;
19 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
20 import org.opendaylight.genius.interfacemanager.recovery.ServiceRecoveryInterface;
21 import org.opendaylight.genius.interfacemanager.recovery.registry.ServiceRecoveryRegistry;
22 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.srm.types.rev170711.GeniusIfmInterface;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 @Singleton
31 public class InterfaceInstanceRecoveryHandler implements ServiceRecoveryInterface {
32
33     private static final Logger LOG = LoggerFactory.getLogger(InterfaceInstanceRecoveryHandler.class);
34
35     private final InterfaceManagerCommonUtils interfaceManagerCommonUtils;
36     private final JobCoordinator jobCoordinator;
37     private final ManagedNewTransactionRunner txRunner;
38
39     @Inject
40     public InterfaceInstanceRecoveryHandler(DataBroker dataBroker,
41                                             InterfaceManagerCommonUtils interfaceManagerCommonUtils,
42                                             JobCoordinator jobCoordinator,
43                                             ServiceRecoveryRegistry serviceRecoveryRegistry) {
44         this.interfaceManagerCommonUtils = interfaceManagerCommonUtils;
45         this.jobCoordinator = jobCoordinator;
46         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
47         serviceRecoveryRegistry.registerServiceRecoveryRegistry(buildServiceRegistryKey(), this);
48     }
49
50     @Override
51     public void recoverService(String entityId) {
52         LOG.info("recover interface instance {}", entityId);
53         // Fetch the interface from interface config DS first.
54         Interface interfaceConfig = interfaceManagerCommonUtils.getInterfaceFromConfigDS(entityId);
55         if (interfaceConfig != null) {
56             // Do a delete and recreate of the interface configuration.
57             InstanceIdentifier<Interface> interfaceId = InterfaceManagerCommonUtils.getInterfaceIdentifier(
58                     new InterfaceKey(entityId));
59             LOG.trace("deleting interface instance {}", entityId);
60             jobCoordinator.enqueueJob(entityId, () -> Collections.singletonList(
61                 txRunner.callWithNewWriteOnlyTransactionAndSubmit(
62                     tx -> tx.delete(LogicalDatastoreType.CONFIGURATION, interfaceId))),
63                     IfmConstants.JOB_MAX_RETRIES);
64             LOG.trace("recreating interface instance {}, {}", entityId, interfaceConfig);
65             jobCoordinator.enqueueJob(entityId, () -> Collections.singletonList(
66                 txRunner.callWithNewWriteOnlyTransactionAndSubmit(
67                     tx -> tx.put(LogicalDatastoreType.CONFIGURATION, interfaceId, interfaceConfig))),
68                     IfmConstants.JOB_MAX_RETRIES);
69         }
70     }
71
72     private String buildServiceRegistryKey() {
73         return GeniusIfmInterface.class.toString();
74     }
75 }