Merge "Porting the modules from vpnservice to genius"
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / 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.genius.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.genius.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.genius.interfacemanager.rev160406.IfTunnel;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.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
49     public List<ListenableFuture<Void>> call() throws Exception {
50         List<ListenableFuture<Void>> futures = new ArrayList<>() ;
51         logger.debug("Invoking Tunnel Monitor Worker tzone = {} Interval= {}",tzone,interval );
52         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
53         toggleTunnelMonitoring(hwVteps,interval,tzone,t,exists);
54         futures.add(t.submit());
55         return futures;
56     }
57
58     private void toggleTunnelMonitoring(List<HwVtep> hwVteps, Integer interval, String tzone, WriteTransaction t, Boolean exists) {
59         //exists means hwVteps exist for this tzone
60
61         //List<String> TunnelList = ItmUtils.getTunnelsofTzone(hwVteps, tzone, dataBroker, exists);
62         List<String> TunnelList = ItmUtils.getInternalTunnelsofTzone(tzone,dataBroker);
63         if(TunnelList !=null &&!TunnelList.isEmpty()) {
64             for (String tunnel : TunnelList)
65                 toggle(tunnel, interval,t);
66         }
67     }
68
69     private void toggle(String tunnelInterfaceName, Integer interval, WriteTransaction t) {
70         if (tunnelInterfaceName != null) {
71             logger.debug("tunnel {} will have monitor interval {}", tunnelInterfaceName, interval);
72             InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(tunnelInterfaceName);
73             IfTunnel tunnel = new IfTunnelBuilder().setMonitorInterval(interval.longValue() * 1000).build();
74             InterfaceBuilder builder = new InterfaceBuilder().setKey(new InterfaceKey(tunnelInterfaceName))
75                             .addAugmentation(IfTunnel.class, tunnel);
76             t.merge(LogicalDatastoreType.CONFIGURATION, trunkIdentifier, builder.build());
77         }
78     }
79 }
80
81