2 * Copyright (c) 2016 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.alivenessmonitor.internal;
10 import static org.opendaylight.genius.alivenessmonitor.internal.AlivenessMonitorUtil.getMonitorStateId;
12 import com.google.common.base.Optional;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.concurrent.Semaphore;
20 import javax.annotation.PostConstruct;
21 import javax.inject.Inject;
22 import javax.inject.Singleton;
23 import org.opendaylight.controller.liblldp.Packet;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.genius.datastoreutils.hwvtep.HwvtepAbstractDataTreeChangeListener;
28 import org.opendaylight.genius.mdsalutil.MDSALUtil;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.EtherTypes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.LivenessState;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.endpoint.EndpointType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.endpoint.endpoint.type.Interface;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.configs.MonitoringInfo;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.profiles.MonitorProfile;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitoring.states.MonitoringState;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitoring.states.MonitoringStateBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorRef;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.Tunnels;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelsKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.tunnel.attributes.BfdLocalConfigs;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.tunnel.attributes.BfdLocalConfigsBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.tunnel.attributes.BfdLocalConfigsKey;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.tunnel.attributes.BfdParams;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.tunnel.attributes.BfdParamsBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.tunnel.attributes.BfdParamsKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.tunnel.attributes.BfdRemoteConfigs;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.tunnel.attributes.BfdRemoteConfigsBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.tunnel.attributes.BfdRemoteConfigsKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.tunnel.attributes.BfdStatus;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
65 public class HwVtepTunnelsStateHandler extends HwvtepAbstractDataTreeChangeListener<Tunnels, HwVtepTunnelsStateHandler>
66 implements AlivenessProtocolHandler, AutoCloseable {
68 private static final Logger LOG = LoggerFactory.getLogger(HwVtepTunnelsStateHandler.class);
69 private final DataBroker dataBroker;
70 private final AlivenessMonitor alivenessMonitor;
73 public HwVtepTunnelsStateHandler(final DataBroker dataBroker, final AlivenessMonitor alivenessMonitor) {
74 super(Tunnels.class, HwVtepTunnelsStateHandler.class);
75 this.dataBroker = dataBroker;
76 this.alivenessMonitor = alivenessMonitor;
81 LOG.info("{} start", getClass().getSimpleName());
82 alivenessMonitor.registerHandler(EtherTypes.Bfd, this);
83 registerListener(LogicalDatastoreType.CONFIGURATION, this.dataBroker);
87 protected InstanceIdentifier<Tunnels> getWildCardPath() {
88 return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class).child(Node.class)
89 .augmentation(PhysicalSwitchAugmentation.class).child(Tunnels.class);
93 protected HwVtepTunnelsStateHandler getDataTreeChangeListener() {
94 return HwVtepTunnelsStateHandler.this;
98 protected void removed(InstanceIdentifier<Tunnels> identifier, Tunnels del) {
99 // TODO Auto-generated method stub
103 protected void updated(InstanceIdentifier<Tunnels> identifier, Tunnels oldTunnelInfo, Tunnels updatedTunnelInfo) {
104 List<BfdStatus> oldBfdStatus = oldTunnelInfo.getBfdStatus();
105 List<BfdStatus> newBfdStatus = updatedTunnelInfo.getBfdStatus();
106 LivenessState oldTunnelOpState = getTunnelOpState(oldBfdStatus);
107 final LivenessState newTunnelOpState = getTunnelOpState(newBfdStatus);
108 if (oldTunnelOpState == newTunnelOpState) {
109 LOG.debug("Tunnel state of old tunnel {} and update tunnel {} are same", oldTunnelInfo, updatedTunnelInfo);
112 updatedTunnelInfo.getTunnelUuid();
113 String interfaceName = "<TODO>";
114 // TODO: find out the corresponding interface using tunnelIdentifier or
115 // any attributes of tunneInfo object
116 final String monitorKey = getBfdMonitorKey(interfaceName);
117 LOG.debug("Processing monitorKey: {} for received Tunnels update DCN", monitorKey);
119 final Semaphore lock = alivenessMonitor.lockMap.get(monitorKey);
120 LOG.debug("Acquiring lock for monitor key : {} to process monitor DCN", monitorKey);
121 alivenessMonitor.acquireLock(lock);
123 final ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
125 ListenableFuture<Optional<MonitoringState>> stateResult = tx.read(LogicalDatastoreType.OPERATIONAL,
126 getMonitorStateId(monitorKey));
127 Futures.addCallback(stateResult, new FutureCallback<Optional<MonitoringState>>() {
130 public void onSuccess(Optional<MonitoringState> optState) {
131 if (optState.isPresent()) {
132 final MonitoringState currentState = optState.get();
133 if (currentState.getState() == newTunnelOpState) {
136 final boolean stateChanged = true;
137 final MonitoringState state = new MonitoringStateBuilder().setMonitorKey(monitorKey)
138 .setState(newTunnelOpState).build();
139 tx.merge(LogicalDatastoreType.OPERATIONAL, getMonitorStateId(monitorKey), state);
140 ListenableFuture<Void> writeResult = tx.submit();
142 Futures.addCallback(writeResult, new FutureCallback<Void>() {
145 public void onSuccess(Void arg0) {
146 alivenessMonitor.releaseLock(lock);
148 // send notifications
149 LOG.info("Sending notification for monitor Id : {} with Current State: {}",
150 currentState.getMonitorId(), newTunnelOpState);
151 alivenessMonitor.publishNotification(currentState.getMonitorId(), newTunnelOpState);
153 if (LOG.isTraceEnabled()) {
154 LOG.trace("Successful in writing monitoring state {} to ODS", state);
160 public void onFailure(Throwable error) {
161 alivenessMonitor.releaseLock(lock);
162 LOG.warn("Error in writing monitoring state : {} to Datastore", monitorKey, error);
163 if (LOG.isTraceEnabled()) {
164 LOG.trace("Error in writing monitoring state: {} to Datastore", state);
169 LOG.warn("Monitoring State not available for key: {} to process the Packet received", monitorKey);
170 // Complete the transaction
172 alivenessMonitor.releaseLock(lock);
177 public void onFailure(Throwable error) {
178 LOG.error("Error when reading Monitoring State for key: {} to process the Packet received", monitorKey,
180 // FIXME: Not sure if the transaction status is valid to cancel
182 alivenessMonitor.releaseLock(lock);
187 private LivenessState getTunnelOpState(List<BfdStatus> tunnelBfdStatus) {
188 LivenessState livenessState = LivenessState.Unknown;
189 if (tunnelBfdStatus == null || tunnelBfdStatus.isEmpty()) {
190 return livenessState;
192 for (BfdStatus bfdState : tunnelBfdStatus) {
193 if (bfdState.getBfdStatusKey().equalsIgnoreCase(AlivenessMonitorConstants.BFD_OP_STATE)) {
194 String bfdOpState = bfdState.getBfdStatusValue();
195 if (bfdOpState.equalsIgnoreCase(AlivenessMonitorConstants.BFD_STATE_UP)) {
196 livenessState = LivenessState.Up;
198 livenessState = LivenessState.Down;
203 return livenessState;
207 protected void added(InstanceIdentifier<Tunnels> identifier, Tunnels add) {
208 // TODO: need to add the code to enable BFD if tunnels are created
209 // dynamically by TOR switch
213 public Class<?> getPacketClass() {
218 public String handlePacketIn(Packet protocolPacket, PacketReceived packetReceived) {
222 void resetMonitoringTask(MonitoringInfo monitorInfo, boolean isEnable) {
223 // TODO: get the corresponding hwvtep tunnel from the sourceInterface
225 // implments renderer for hwvtep vxlan tunnels
226 TunnelsKey tunnelKey = null;
227 String nodeId = null;
228 String topologyId = null;
229 Optional<Tunnels> tunnelsOptional = alivenessMonitor.read(LogicalDatastoreType.CONFIGURATION,
230 getTunnelIdentifier(topologyId, nodeId, tunnelKey));
231 if (!tunnelsOptional.isPresent()) {
232 LOG.warn("Tunnel {} is not present on the Node {}. So not disabling the BFD monitoing", tunnelKey, nodeId);
235 Tunnels tunnel = tunnelsOptional.get();
236 List<BfdParams> tunnelBfdParams = tunnel.getBfdParams();
237 if (tunnelBfdParams == null || tunnelBfdParams.isEmpty()) {
238 LOG.debug("there is no bfd params available for the tunnel {}", tunnel);
240 Iterator<BfdParams> tunnelBfdParamsInterator = tunnelBfdParams.iterator();
241 while (tunnelBfdParamsInterator.hasNext()) {
242 BfdParams bfdParam = tunnelBfdParamsInterator.next();
243 if (bfdParam.getBfdParamKey().equals(AlivenessMonitorConstants.BFD_PARAM_ENABLE)) {
244 tunnelBfdParamsInterator.remove();
248 setBfdParamForEnable(tunnelBfdParams, isEnable);
249 Tunnels tunnelWithBfdReset = new TunnelsBuilder().setKey(tunnelKey).setBfdParams(tunnelBfdParams).build();
250 MDSALUtil.syncUpdate(dataBroker, LogicalDatastoreType.CONFIGURATION,
251 getTunnelIdentifier(topologyId, nodeId, tunnelKey), tunnelWithBfdReset);
255 public void startMonitoringTask(MonitoringInfo monitorInfo) {
256 EndpointType source = monitorInfo.getSource().getEndpointType();
257 if (source instanceof Interface) {
258 Interface intf = (Interface) source;
259 intf.getInterfaceName();
261 LOG.warn("Invalid source endpoint. Could not retrieve source interface to configure BFD");
264 MonitorProfile profile;
265 long profileId = monitorInfo.getProfileId();
266 Optional<MonitorProfile> optProfile = alivenessMonitor.getMonitorProfile(profileId);
267 if (optProfile.isPresent()) {
268 profile = optProfile.get();
270 LOG.warn("No monitor profile associated with id {}. " + "Could not send Monitor packet for monitor-id {}",
271 profileId, monitorInfo);
274 // TODO: get the corresponding hwvtep tunnel from the sourceInterface
276 // Implements renderer for hwvtep vxlan tunnels
277 String tunnelLocalMacAddress = "<TODO>";
278 String tunnelLocalIpAddress = "<TODO>";
279 String tunnelRemoteMacAddress = "<TODO>";
280 String tunnelRemoteIpAddress = "<TODO>";
281 List<BfdParams> bfdParams = new ArrayList<>();
282 fillBfdParams(bfdParams, profile);
283 List<BfdLocalConfigs> bfdLocalConfigs = new ArrayList<>();
284 fillBfdLocalConfigs(bfdLocalConfigs, tunnelLocalMacAddress, tunnelLocalIpAddress);
285 List<BfdRemoteConfigs> bfdRemoteConfigs = new ArrayList<>();
286 fillBfdRemoteConfigs(bfdRemoteConfigs, tunnelRemoteMacAddress, tunnelRemoteIpAddress);
287 TunnelsKey tunnelKey = null;
288 Tunnels tunnelWithBfd = new TunnelsBuilder().setKey(tunnelKey).setBfdParams(bfdParams)
289 .setBfdLocalConfigs(bfdLocalConfigs).setBfdRemoteConfigs(bfdRemoteConfigs).build();
290 // TODO: get the following parameters from the interface and use it to
291 // update hwvtep datastore
292 // and not sure sure tunnels are creating immediately once interface mgr
293 // writes termination point
294 // into hwvtep datastore. if tunnels are not created during that time,
295 // then start monitoring has to
296 // be done as part of tunnel add DCN handling.
297 HwvtepPhysicalLocatorRef remoteRef = null;
298 HwvtepPhysicalLocatorRef localRef = null;
299 String topologyId = "";
301 MDSALUtil.syncUpdate(dataBroker, LogicalDatastoreType.CONFIGURATION,
302 getTunnelIdentifier(topologyId, nodeId, new TunnelsKey(localRef, remoteRef)), tunnelWithBfd);
305 private void fillBfdRemoteConfigs(List<BfdRemoteConfigs> bfdRemoteConfigs, String tunnelRemoteMacAddress,
306 String tunnelRemoteIpAddress) {
308 .add(getBfdRemoteConfig(AlivenessMonitorConstants.BFD_CONFIG_BFD_DST_MAC, tunnelRemoteMacAddress));
311 private BfdRemoteConfigs getBfdRemoteConfig(String key, String value) {
312 return new BfdRemoteConfigsBuilder().setBfdRemoteConfigKey(key).setBfdRemoteConfigValue(value)
313 .setKey(new BfdRemoteConfigsKey(key)).build();
316 private void fillBfdLocalConfigs(List<BfdLocalConfigs> bfdLocalConfigs, String tunnelLocalMacAddress,
317 String tunnelLocalIpAddress) {
318 bfdLocalConfigs.add(getBfdLocalConfig(AlivenessMonitorConstants.BFD_CONFIG_BFD_DST_MAC, tunnelLocalMacAddress));
319 bfdLocalConfigs.add(getBfdLocalConfig(AlivenessMonitorConstants.BFD_CONFIG_BFD_DST_IP, tunnelLocalIpAddress));
322 private BfdLocalConfigs getBfdLocalConfig(String key, String value) {
323 return new BfdLocalConfigsBuilder().setBfdLocalConfigKey(key).setKey(new BfdLocalConfigsKey(key))
324 .setBfdLocalConfigValue(value).build();
327 private void fillBfdParams(List<BfdParams> bfdParams, MonitorProfile profile) {
328 setBfdParamForEnable(bfdParams, true);
329 bfdParams.add(getBfdParams(AlivenessMonitorConstants.BFD_PARAM_MIN_RX, Long.toString(profile.getMinRx())));
330 bfdParams.add(getBfdParams(AlivenessMonitorConstants.BFD_PARAM_MIN_TX, Long.toString(profile.getMinTx())));
332 getBfdParams(AlivenessMonitorConstants.BFD_PARAM_DECAY_MIN_RX, Long.toString(profile.getDecayMinRx())));
333 bfdParams.add(getBfdParams(AlivenessMonitorConstants.BFD_PARAM_FORWARDING_IF_RX, profile.getForwardingIfRx()));
334 bfdParams.add(getBfdParams(AlivenessMonitorConstants.BFD_PARAM_CPATH_DOWN, profile.getCpathDown()));
335 bfdParams.add(getBfdParams(AlivenessMonitorConstants.BFD_PARAM_CHECK_TNL_KEY, profile.getCheckTnlKey()));
338 private void setBfdParamForEnable(List<BfdParams> bfdParams, boolean isEnabled) {
339 bfdParams.add(getBfdParams(AlivenessMonitorConstants.BFD_PARAM_ENABLE, Boolean.toString(isEnabled)));
342 private BfdParams getBfdParams(String key, String value) {
343 return new BfdParamsBuilder().setBfdParamKey(key).setKey(new BfdParamsKey(key)).setBfdParamValue(value).build();
347 public String getUniqueMonitoringKey(MonitoringInfo monitorInfo) {
348 String interfaceName = getInterfaceName(monitorInfo.getSource().getEndpointType());
349 return getBfdMonitorKey(interfaceName);
352 private String getBfdMonitorKey(String interfaceName) {
353 return interfaceName + "bfd";
356 private String getInterfaceName(EndpointType endpoint) {
357 String interfaceName = null;
358 if (endpoint instanceof Interface) {
359 interfaceName = ((Interface) endpoint).getInterfaceName();
361 return interfaceName;
364 public static InstanceIdentifier<Tunnels> getTunnelIdentifier(String topologyId, String nodeId,
365 TunnelsKey tunnelsKey) {
366 return InstanceIdentifier.builder(NetworkTopology.class)
367 .child(Topology.class, new TopologyKey(new TopologyId(topologyId)))
368 .child(Node.class, new NodeKey(new NodeId(nodeId))).augmentation(PhysicalSwitchAugmentation.class)
369 .child(Tunnels.class, tunnelsKey).build();