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