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.genius.interfacemanager.recovery.impl;
10 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12 import java.time.Duration;
13 import java.util.Collections;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.apache.aries.blueprint.annotation.service.Reference;
17 import org.opendaylight.genius.datastoreutils.listeners.DataTreeEventCallbackRegistrar;
18 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
19 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
20 import org.opendaylight.genius.interfacemanager.IfmConstants;
21 import org.opendaylight.genius.interfacemanager.IfmUtil;
22 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
23 import org.opendaylight.genius.utils.clustering.EntityOwnershipUtils;
24 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
25 import org.opendaylight.mdsal.binding.api.DataBroker;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.serviceutils.srm.ServiceRecoveryInterface;
28 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.types.rev180626.GeniusIfmInterface;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 public class InterfaceInstanceRecoveryHandler implements ServiceRecoveryInterface {
39 private static final Logger LOG = LoggerFactory.getLogger(InterfaceInstanceRecoveryHandler.class);
41 private final InterfaceManagerCommonUtils interfaceManagerCommonUtils;
42 private final JobCoordinator jobCoordinator;
43 private final ManagedNewTransactionRunner txRunner;
44 private final EntityOwnershipUtils entityOwnershipUtils;
45 private final DataTreeEventCallbackRegistrar eventCallbacks;
48 public InterfaceInstanceRecoveryHandler(@Reference DataBroker dataBroker,
49 InterfaceManagerCommonUtils interfaceManagerCommonUtils,
50 @Reference JobCoordinator jobCoordinator,
51 ServiceRecoveryRegistry serviceRecoveryRegistry,
52 EntityOwnershipUtils entityOwnershipUtils,
53 @Reference DataTreeEventCallbackRegistrar eventCallbacks) {
54 this.interfaceManagerCommonUtils = interfaceManagerCommonUtils;
55 this.jobCoordinator = jobCoordinator;
56 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
57 this.entityOwnershipUtils = entityOwnershipUtils;
58 this.eventCallbacks = eventCallbacks;
59 serviceRecoveryRegistry.registerServiceRecoveryRegistry(buildServiceRegistryKey(), this);
63 public void recoverService(String entityId) {
64 if (!entityOwnershipUtils.isEntityOwner(IfmConstants.INTERFACE_CONFIG_ENTITY,
65 IfmConstants.INTERFACE_CONFIG_ENTITY)) {
68 LOG.info("recover interface instance {}", entityId);
69 // Fetch the interface from interface config DS first.
70 Interface interfaceConfig = interfaceManagerCommonUtils.getInterfaceFromConfigDS(entityId);
71 if (interfaceConfig != null) {
72 // Do a delete and recreate of the interface configuration.
73 InstanceIdentifier<Interface> interfaceId = InterfaceManagerCommonUtils.getInterfaceIdentifier(
74 new InterfaceKey(entityId));
75 LOG.trace("deleting interface instance {}", entityId);
76 jobCoordinator.enqueueJob(entityId, () -> Collections.singletonList(
77 txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> tx.delete(interfaceId))),
78 IfmConstants.JOB_MAX_RETRIES);
79 eventCallbacks.onRemove(LogicalDatastoreType.OPERATIONAL,
80 IfmUtil.buildStateInterfaceId(entityId), (unused) -> {
81 recreateInterfaceInstance(entityId, interfaceConfig, interfaceId);
82 return DataTreeEventCallbackRegistrar.NextAction.UNREGISTER;
83 }, Duration.ofMillis(5000), (id) -> {
84 recreateInterfaceInstance(entityId, interfaceConfig, interfaceId);
89 private void recreateInterfaceInstance(String entityId, Interface interfaceConfig,
90 InstanceIdentifier<Interface> interfaceId) {
91 LOG.trace("recreating interface instance {}, {}", entityId, interfaceConfig);
92 jobCoordinator.enqueueJob(entityId, () -> Collections.singletonList(
93 txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
94 tx -> tx.put(interfaceId, interfaceConfig))),
95 IfmConstants.JOB_MAX_RETRIES);
98 private String buildServiceRegistryKey() {
99 return GeniusIfmInterface.class.toString();