Fix loose typing in ItmMonitorWorker
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / confighelpers / ItmMonitorWorker.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 package org.opendaylight.genius.itm.confighelpers;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.Optional;
15 import java.util.concurrent.Callable;
16 import org.opendaylight.genius.infra.Datastore;
17 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
18 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
19 import org.opendaylight.genius.infra.TypedWriteTransaction;
20 import org.opendaylight.genius.itm.cache.DpnTepStateCache;
21 import org.opendaylight.genius.itm.cache.OvsBridgeRefEntryCache;
22 import org.opendaylight.genius.itm.itmdirecttunnels.renderer.ovs.utilities.DirectTunnelUtils;
23 import org.opendaylight.mdsal.binding.api.DataBroker;
24 import org.opendaylight.mdsal.common.api.ReadFailedException;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelMonitoringTypeBase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorInterval;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorIntervalBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorParams;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorParamsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.meta.rev171210.ovs.bridge.ref.info.OvsBridgeRefEntry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.teps.state.DpnsTeps;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.teps.state.DpnsTepsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.teps.state.DpnsTepsKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.teps.state.dpns.teps.RemoteDpns;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.teps.state.dpns.teps.RemoteDpnsBuilder;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.common.Uint16;
38 import org.opendaylight.yangtools.yang.common.Uint64;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class ItmMonitorWorker implements Callable<List<? extends ListenableFuture<?>>> {
43     private static final Logger LOG = LoggerFactory.getLogger(ItmMonitorWorker.class);
44
45     private final String tzone;
46     private final Class<? extends TunnelMonitoringTypeBase> monitorProtocol;
47     private final DirectTunnelUtils directTunnelUtils;
48     private final DpnTepStateCache dpnTepStateCache;
49     private final OvsBridgeRefEntryCache ovsBridgeRefEntryCache;
50     private final ManagedNewTransactionRunner txRunner;
51     private final Boolean enabled;
52     private final Uint16 interval;
53
54     private ItmMonitorWorker(String tzone, Boolean enabled, Uint16 interval,
55                              Class<? extends TunnelMonitoringTypeBase> monitorProtocol, DataBroker dataBroker,
56                              DirectTunnelUtils directTunnelUtils, DpnTepStateCache dpnTepStateCache,
57                              OvsBridgeRefEntryCache ovsBridgeRefEntryCache) {
58         this.tzone = tzone;
59         this.monitorProtocol = monitorProtocol;
60         this.directTunnelUtils = directTunnelUtils;
61         this.dpnTepStateCache = dpnTepStateCache;
62         this.ovsBridgeRefEntryCache = ovsBridgeRefEntryCache;
63         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
64         LOG.trace("ItmMonitorWorker initialized with  tzone {} and toggleBoolean {}", tzone, enabled);
65         this.enabled = enabled;
66         this.interval = interval;
67         LOG.debug("Toggle monitoring enabled {} interval {} monitor protocol {}", enabled, interval, monitorProtocol);
68     }
69
70     public <T> ItmMonitorWorker(String tzone, Boolean enabled,
71                                 Class<? extends TunnelMonitoringTypeBase> monitorProtocol, DataBroker dataBroker,
72                                 DirectTunnelUtils directTunnelUtils, DpnTepStateCache dpnTepStateCache,
73                                 OvsBridgeRefEntryCache ovsBridgeRefEntryCache) {
74         this(tzone, enabled, null, monitorProtocol, dataBroker, directTunnelUtils, dpnTepStateCache,
75             ovsBridgeRefEntryCache);
76     }
77
78     public <T> ItmMonitorWorker(String tzone, Uint16 interval,
79                                 Class<? extends TunnelMonitoringTypeBase> monitorProtocol, DataBroker dataBroker,
80                                 DirectTunnelUtils directTunnelUtils, DpnTepStateCache dpnTepStateCache,
81                                 OvsBridgeRefEntryCache ovsBridgeRefEntryCache) {
82         this(tzone, null, interval, monitorProtocol, dataBroker, directTunnelUtils, dpnTepStateCache,
83             ovsBridgeRefEntryCache);
84     }
85
86     @Override
87     public List<ListenableFuture<Void>> call() {
88         LOG.debug("ItmMonitorWorker invoked with tzone = {} enabled {}", tzone, enabled);
89         List<ListenableFuture<Void>> futures = new ArrayList<>();
90         return toggleTunnelMonitoring(futures);
91     }
92
93     private List<ListenableFuture<Void>> toggleTunnelMonitoring(List<ListenableFuture<Void>> futures) {
94         futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.OPERATIONAL,
95             tx -> updateOperationalDS(tx)));
96         Collection<DpnsTeps> dpnsTepsCollection = dpnTepStateCache.getAllPresent();
97         LOG.debug("toggleTunnelMonitoring: DpnsTepsList size {}", dpnsTepsCollection.size());
98         if (dpnsTepsCollection.isEmpty()) {
99             LOG.info("There are no teps configured");
100         }
101         else {
102             futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.CONFIGURATION,
103                 tx -> {
104                     for (DpnsTeps dpnTeps : dpnsTepsCollection) {
105                         toggleForDirectEnabled(dpnTeps, tx);
106                     }
107                 }
108             ));
109         }
110         return futures;
111     }
112
113     private void updateOperationalDS(TypedWriteTransaction<Datastore.Operational> tx) {
114         LOG.debug("toggleTunnelMonitoring: Updating Operational DS");
115         if (enabled != null) {
116             InstanceIdentifier<TunnelMonitorParams> iid = InstanceIdentifier.builder(TunnelMonitorParams.class).build();
117             TunnelMonitorParams monitorBuilder = new TunnelMonitorParamsBuilder()
118                 .setEnabled(enabled).setMonitorProtocol(monitorProtocol).build();
119             LOG.debug("toggleTunnelMonitoring: TunnelMonitorParams {}", monitorBuilder);
120             tx.merge(iid, monitorBuilder);
121         } else if (interval != null) {
122             InstanceIdentifier<TunnelMonitorInterval> iid = InstanceIdentifier.builder(TunnelMonitorInterval.class)
123                                                                 .build();
124             TunnelMonitorInterval intervalBuilder = new TunnelMonitorIntervalBuilder().setInterval(interval).build();
125             LOG.debug("updateTunnelMonitorInterval: TunnelMonitorInterval {}", intervalBuilder);
126             tx.merge(iid, intervalBuilder);
127         }
128     }
129
130     private void toggleForDirectEnabled(DpnsTeps dpnTeps, TypedWriteTransaction<Datastore.Configuration> tx)
131             throws ReadFailedException {
132         List<RemoteDpns> remoteDpnTepNewList = new ArrayList<>();
133         RemoteDpns remoteDpnNew = null;
134         Optional<OvsBridgeRefEntry> ovsBridgeRefEntry = ovsBridgeRefEntryCache.get(dpnTeps.getSourceDpnId());
135         for (RemoteDpns remoteDpn : dpnTeps.nonnullRemoteDpns().values()) {
136             if (enabled != null) {
137                 LOG.debug("toggleMonitoring: tunnelInterfaceName: {}, monitorEnable = {} ",
138                     remoteDpn.getTunnelName(), enabled);
139                 remoteDpnNew = new RemoteDpnsBuilder(remoteDpn).setMonitoringEnabled(enabled).build();
140             }
141             else if (interval != null) {
142                 LOG.debug("updateMonitoring: tunnelInterfaceName: {}, interval = {} ",
143                     remoteDpn.getTunnelName(), interval);
144                 remoteDpnNew = new RemoteDpnsBuilder(remoteDpn).setMonitoringInterval(interval).build();
145             }
146             remoteDpnTepNewList.add(remoteDpnNew);
147             LOG.debug("toggleMonitoring: RemoteDpnNew {}", remoteDpnNew);
148             directTunnelUtils.updateBfdConfiguration(dpnTeps.getSourceDpnId(),
149                                                         remoteDpnNew, ovsBridgeRefEntry);
150         }
151         updateMonitoringDS(dpnTeps.getSourceDpnId(), remoteDpnTepNewList, tx);
152     }
153
154     public void updateMonitoringDS(Uint64 sourceDpnId, List<RemoteDpns> remoteDpnTepNewList,
155                                    TypedWriteTransaction<Datastore.Configuration> tx) {
156         InstanceIdentifier<DpnsTeps> iid = DirectTunnelUtils.createDpnTepsInstanceIdentifier(sourceDpnId);
157         DpnsTepsBuilder builder = new DpnsTepsBuilder().withKey(new DpnsTepsKey(sourceDpnId))
158             .setRemoteDpns(remoteDpnTepNewList);
159         LOG.debug("DirectTunnelUtils - Builder remoteDPNs: {}", builder.getRemoteDpns());
160         tx.merge(iid, builder.build());
161     }
162 }