AAP stops working when VM is restarted
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / jobs / AddL2GwDevicesToTransportZoneJob.java
1 /*
2  * Copyright (c) 2016 - 2017 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
9 package org.opendaylight.netvirt.elan.l2gw.jobs;
10
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.concurrent.Callable;
15 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayUtils;
16 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache;
17 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZone;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * The Class AddL2GwDevicesToTransportZoneJob.
25  */
26 public class AddL2GwDevicesToTransportZoneJob implements Callable<List<? extends ListenableFuture<?>>> {
27
28     private static final Logger LOG = LoggerFactory.getLogger(AddL2GwDevicesToTransportZoneJob.class);
29     private final ItmRpcService itmRpcService;
30     private final TransportZone transportZone;
31     private final L2GatewayCache l2GatewayCache;
32
33     /**
34      * Instantiates a new adds the l2 gw devices to transport zone job.
35      *
36      * @param itmRpcService the itm rpc service
37      * @param transportZone the transport zone
38      * @param l2GatewayCache the l2gateway cache
39      */
40     public AddL2GwDevicesToTransportZoneJob(ItmRpcService itmRpcService, TransportZone transportZone,
41             L2GatewayCache l2GatewayCache) {
42         this.itmRpcService = itmRpcService;
43         this.transportZone = transportZone;
44         this.l2GatewayCache = l2GatewayCache;
45         LOG.debug("created AddL2GwDevicesToTransportZone Job for tZone {}", transportZone.getZoneName());
46     }
47
48     /**
49      * Gets the job key.
50      *
51      * @return the job key
52      */
53     public String getJobKey() {
54         return "L2GW" + this.transportZone.getZoneName();
55     }
56
57     /*
58      * (non-Javadoc)
59      *
60      * @see java.util.concurrent.Callable#call()
61      */
62     @Override
63     @SuppressWarnings("checkstyle:IllegalCatch")
64     public List<ListenableFuture<?>> call() {
65         LOG.debug("Running AddL2GwDevicesToTransportZone job for {}", this.transportZone.getZoneName());
66         try {
67             // When vxlan transport zone is added, add all l2gw devices to that
68             // transport zone. Doesn't matter if tz already has data or not.
69             for (L2GatewayDevice l2gwDevice : l2GatewayCache.getAll()) {
70                 if (!l2gwDevice.getL2GatewayIds().isEmpty()) {
71                     LOG.debug("Adding l2gw device [{}] to transport zone [{}]", l2gwDevice.getDeviceName(),
72                             this.transportZone.getZoneName());
73                     L2GatewayUtils.createItmTunnels(itmRpcService, l2gwDevice.getHwvtepNodeId(),
74                             l2gwDevice.getDeviceName(), l2gwDevice.getTunnelIp());
75                 }
76             }
77         } catch (Exception e) {
78             LOG.error("Failed during AddL2GwDevicesToTransportZone job ", e);
79         }
80         return Collections.emptyList();
81     }
82
83     /*
84      * (non-Javadoc)
85      *
86      * @see java.lang.Object#toString()
87      */
88     @Override
89     public String toString() {
90         return "AddL2GwDevicesToTransportZoneJob [transportZone=" + transportZone.getZoneName() + "]";
91     }
92 }