2 * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.genius.itm.confighelpers;
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.Uint64;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class ItmMonitorWorker implements Callable<List<? extends ListenableFuture<?>>> {
43 private static final Logger LOG = LoggerFactory.getLogger(ItmMonitorWorker.class);
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 Integer interval;
54 public <T> ItmMonitorWorker(String tzone, T monitoring,
55 Class<? extends TunnelMonitoringTypeBase> monitorProtocol, DataBroker dataBroker,
56 DirectTunnelUtils directTunnelUtils,
57 DpnTepStateCache dpnTepStateCache,
58 OvsBridgeRefEntryCache ovsBridgeRefEntryCache) {
60 this.monitorProtocol = monitorProtocol;
61 this.directTunnelUtils = directTunnelUtils;
62 this.dpnTepStateCache = dpnTepStateCache;
63 this.ovsBridgeRefEntryCache = ovsBridgeRefEntryCache;
64 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
65 LOG.trace("ItmMonitorWorker initialized with tzone {} and toggleBoolean {}", tzone, monitoring);
66 if (monitoring instanceof Boolean) {
67 this.enabled = (Boolean) monitoring;
71 this.interval = (Integer) monitoring;
74 LOG.debug("Toggle monitoring enabled {} interval {} monitor protocol {}", enabled, interval, monitorProtocol);
77 @Override public List<ListenableFuture<Void>> call() {
78 LOG.debug("ItmMonitorWorker invoked with tzone = {} enabled {}", tzone, enabled);
79 List<ListenableFuture<Void>> futures = new ArrayList<>();
80 return toggleTunnelMonitoring(futures);
83 private List<ListenableFuture<Void>> toggleTunnelMonitoring(List<ListenableFuture<Void>> futures) {
84 futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.OPERATIONAL,
85 tx -> updateOperationalDS(tx)));
86 Collection<DpnsTeps> dpnsTepsCollection = dpnTepStateCache.getAllPresent();
87 LOG.debug("toggleTunnelMonitoring: DpnsTepsList size {}", dpnsTepsCollection.size());
88 if (dpnsTepsCollection.isEmpty()) {
89 LOG.info("There are no teps configured");
92 futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.CONFIGURATION,
94 for (DpnsTeps dpnTeps : dpnsTepsCollection) {
95 toggleForDirectEnabled(dpnTeps, tx);
103 private void updateOperationalDS(TypedWriteTransaction<Datastore.Operational> tx) {
104 LOG.debug("toggleTunnelMonitoring: Updating Operational DS");
105 if (enabled != null) {
106 InstanceIdentifier<TunnelMonitorParams> iid = InstanceIdentifier.builder(TunnelMonitorParams.class).build();
107 TunnelMonitorParams monitorBuilder = new TunnelMonitorParamsBuilder()
108 .setEnabled(enabled).setMonitorProtocol(monitorProtocol).build();
109 LOG.debug("toggleTunnelMonitoring: TunnelMonitorParams {}", monitorBuilder);
110 tx.merge(iid, monitorBuilder);
111 } else if (interval != null) {
112 InstanceIdentifier<TunnelMonitorInterval> iid = InstanceIdentifier.builder(TunnelMonitorInterval.class)
114 TunnelMonitorInterval intervalBuilder = new TunnelMonitorIntervalBuilder().setInterval(interval).build();
115 LOG.debug("updateTunnelMonitorInterval: TunnelMonitorInterval {}", intervalBuilder);
116 tx.merge(iid, intervalBuilder);
121 private void toggleForDirectEnabled(DpnsTeps dpnTeps, TypedWriteTransaction<Datastore.Configuration> tx)
122 throws ReadFailedException {
123 List<RemoteDpns> remoteDpnTepNewList = new ArrayList<>();
124 RemoteDpns remoteDpnNew = null;
125 Optional<OvsBridgeRefEntry> ovsBridgeRefEntry = ovsBridgeRefEntryCache.get(dpnTeps.getSourceDpnId());
126 for (RemoteDpns remoteDpn : dpnTeps.nonnullRemoteDpns().values()) {
127 if (enabled != null) {
128 LOG.debug("toggleMonitoring: tunnelInterfaceName: {}, monitorEnable = {} ",
129 remoteDpn.getTunnelName(), enabled);
130 remoteDpnNew = new RemoteDpnsBuilder(remoteDpn).setMonitoringEnabled(enabled).build();
132 else if (interval != null) {
133 LOG.debug("updateMonitoring: tunnelInterfaceName: {}, interval = {} ",
134 remoteDpn.getTunnelName(), interval);
135 remoteDpnNew = new RemoteDpnsBuilder(remoteDpn).setMonitoringInterval(interval).build();
137 remoteDpnTepNewList.add(remoteDpnNew);
138 LOG.debug("toggleMonitoring: RemoteDpnNew {}", remoteDpnNew);
139 directTunnelUtils.updateBfdConfiguration(dpnTeps.getSourceDpnId(),
140 remoteDpnNew, ovsBridgeRefEntry);
142 updateMonitoringDS(dpnTeps.getSourceDpnId(), remoteDpnTepNewList, tx);
145 public void updateMonitoringDS(Uint64 sourceDpnId, List<RemoteDpns> remoteDpnTepNewList,
146 TypedWriteTransaction<Datastore.Configuration> tx) {
147 InstanceIdentifier<DpnsTeps> iid = DirectTunnelUtils.createDpnTepsInstanceIdentifier(sourceDpnId);
148 DpnsTepsBuilder builder = new DpnsTepsBuilder().withKey(new DpnsTepsKey(sourceDpnId))
149 .setRemoteDpns(remoteDpnTepNewList);
150 LOG.debug("DirectTunnelUtils - Builder remoteDPNs: {}", builder.getRemoteDpns());
151 tx.merge(iid, builder.build());