Bump versions by 0.1.0 for next dev cycle
[vpnservice.git] / itm / itm-impl / src / main / java / org / opendaylight / vpnservice / itm / confighelpers / ItmMonitorIntervalWorker.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 package org.opendaylight.vpnservice.itm.confighelpers;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.vpnservice.itm.impl.ItmUtils;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnelBuilder;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.concurrent.Callable;
27
28 /**
29  * Created by eanraju on 23-Mar-16.
30  */
31 public class ItmMonitorIntervalWorker implements Callable<List<ListenableFuture<Void>>> {
32     private static final Logger logger = LoggerFactory.getLogger(ItmMonitorIntervalWorker.class) ;
33     private DataBroker dataBroker;
34     private String tzone;
35     private Integer interval;
36     private List<HwVtep> hwVteps;
37     private  Boolean exists;
38
39     public ItmMonitorIntervalWorker(List<HwVtep> hwVteps,String tzone,Integer interval, DataBroker dataBroker, Boolean exists){
40         this.dataBroker = dataBroker;
41         this.tzone = tzone;
42         this.interval = interval;
43         this.hwVteps = hwVteps;
44         this.exists = exists;
45         logger.trace("ItmMonitorToggleWorker initialized with  tzone {} and Interval {}",tzone,interval );
46     }
47
48     @Override public List<ListenableFuture<Void>> call() throws Exception {
49         List<ListenableFuture<Void>> futures = new ArrayList<>() ;
50         logger.debug("Invoking Tunnel Monitor Worker tzone = {} Interval= {}",tzone,interval );
51         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
52         toggleTunnelMonitoring(hwVteps,interval,tzone,t,exists);
53         futures.add(t.submit());
54         return futures;
55     }
56
57     private void toggleTunnelMonitoring(List<HwVtep> hwVteps,Integer interval, String tzone, WriteTransaction t,Boolean exists) {
58         //exists means hwVteps exist for this tzone
59
60         //List<String> TunnelList = ItmUtils.getTunnelsofTzone(hwVteps, tzone, dataBroker, exists);
61         List<String> TunnelList = ItmUtils.getInternalTunnelsofTzone(tzone,dataBroker);
62         if(TunnelList !=null &&!TunnelList.isEmpty()) {
63             for (String tunnel : TunnelList)
64                 toggle(tunnel, interval,t);
65         }
66     }
67
68     private void toggle(String tunnelInterfaceName, Integer interval, WriteTransaction t) {
69         if (tunnelInterfaceName != null) {
70             logger.debug("tunnel {} will have monitor interval {}", tunnelInterfaceName, interval);
71             InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(tunnelInterfaceName);
72             IfTunnel tunnel = new IfTunnelBuilder().setMonitorInterval(interval.longValue() * 1000).build();
73             InterfaceBuilder builder = new InterfaceBuilder().setKey(new InterfaceKey(tunnelInterfaceName))
74                             .addAugmentation(IfTunnel.class, tunnel);
75             t.merge(LogicalDatastoreType.CONFIGURATION, trunkIdentifier, builder.build());
76         }
77     }
78 }
79
80