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