Bulk merge of l2gw changes
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / recovery / impl / L2GatewayInstanceRecoveryHandler.java
1 /*
2  * Copyright (c) 2019 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.l2gw.recovery.impl;
9
10 import static org.opendaylight.mdsal.binding.util.Datastore.CONFIGURATION;
11
12 import java.util.List;
13 import java.util.Optional;
14 import java.util.concurrent.ExecutionException;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
18 import org.opendaylight.mdsal.binding.api.DataBroker;
19 import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunner;
20 import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunnerImpl;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayConnectionUtils;
23 import org.opendaylight.serviceutils.srm.ServiceRecoveryInterface;
24 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.L2gateways;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gatewayKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.types.rev180626.NetvirtL2gwNode;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 @Singleton
38 public class L2GatewayInstanceRecoveryHandler implements ServiceRecoveryInterface {
39
40     private static final Logger LOG = LoggerFactory.getLogger(L2GatewayInstanceRecoveryHandler.class);
41
42     private final ManagedNewTransactionRunner managedNewTransactionRunner;
43     private final DataBroker dataBroker;
44     private L2GatewayConnectionUtils l2GatewayConnectionUtils;
45
46     @Inject
47     public L2GatewayInstanceRecoveryHandler(DataBroker dataBroker, L2GatewayConnectionUtils l2GatewayConnectionUtils,
48                                             ServiceRecoveryRegistry serviceRecoveryRegistry) {
49         this.dataBroker = dataBroker;
50         this.managedNewTransactionRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
51         this.l2GatewayConnectionUtils = l2GatewayConnectionUtils;
52         serviceRecoveryRegistry.registerServiceRecoveryRegistry(buildServiceRegistryKey(), this);
53     }
54
55     @Override
56     @SuppressWarnings("ForbidCertainMethod")
57     public void recoverService(String entityId) {
58         LOG.info("recover l2gateway {}", entityId);
59         Uuid uuid = Uuid.getDefaultInstance(entityId);
60
61         InstanceIdentifier<L2gateway> l2gatewayInstanceIdentifier = InstanceIdentifier.create(Neutron.class)
62                 .child(L2gateways.class).child(L2gateway.class, new L2gatewayKey(uuid));
63
64         Optional<L2gateway> l2gatewayOptional = Optional.empty();
65         try {
66             l2gatewayOptional = SingleTransactionDataBroker.syncReadOptional(dataBroker,
67                     LogicalDatastoreType.CONFIGURATION, l2gatewayInstanceIdentifier);
68         } catch (ExecutionException | InterruptedException e) {
69             LOG.error("recoverService: Exception while reading L2gateway DS for the entity {}", entityId, e);
70         }
71         L2gateway l2gateway = l2gatewayOptional.get();
72
73         List<L2gatewayConnection> l2gatewayConnections = l2GatewayConnectionUtils.getL2GwConnectionsByL2GatewayId(uuid);
74         // Do a delete of l2 gateway connection instances.
75         //No null check required since l2gatewayConnections is known to be non-null.
76         LOG.info("Deleting all l2 gateway connections of l2 gateway instance {}", l2gateway.key());
77         for (L2gatewayConnection l2gatewayConnection: l2gatewayConnections) {
78             final InstanceIdentifier<L2gatewayConnection> iid = InstanceIdentifier.builder(Neutron.class)
79                     .child(L2gatewayConnections.class)
80                     .child(L2gatewayConnection.class, l2gatewayConnection.key()).build();
81             LOG.info("Deleting l2 gateway connection {}",l2gatewayConnection.key());
82             managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
83                 tx -> tx.delete(iid));
84             LOG.info("Recreating l2 gateway connection {}",l2gatewayConnection.key());
85             managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
86                 tx -> tx.put(iid, l2gatewayConnection));
87         }
88         LOG.info("Finished recreation of all l2 gateway connections of l2 gateway instance {}", l2gateway.key());
89     }
90
91     public String buildServiceRegistryKey() {
92         return NetvirtL2gwNode.class.toString();
93     }
94 }