Fix alivenessmonitor-impl-protocols deprecation warnings
[genius.git] / alivenessmonitor / alivenessmonitor-impl-protocols / src / main / java / org / opendaylight / genius / alivenessmonitor / protocols / internal / AlivenessProtocolHandlerARP.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.alivenessmonitor.protocols.internal;
9
10 import static java.util.Objects.requireNonNull;
11 import static org.opendaylight.genius.alivenessmonitor.protocols.AlivenessMonitorAndProtocolsConstants.SEPERATOR;
12
13 import com.google.common.base.Strings;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19 import java.util.Map;
20 import java.util.Optional;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.Future;
23 import javax.inject.Inject;
24 import javax.inject.Singleton;
25 import org.apache.aries.blueprint.annotation.service.Reference;
26 import org.opendaylight.genius.alivenessmonitor.protocols.AlivenessProtocolHandlerRegistry;
27 import org.opendaylight.genius.alivenessmonitor.utils.AlivenessMonitorUtil;
28 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
29 import org.opendaylight.genius.mdsalutil.NWUtil;
30 import org.opendaylight.genius.mdsalutil.packet.ARP;
31 import org.opendaylight.mdsal.binding.api.DataBroker;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorProtocolType;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.endpoint.EndpointType;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.endpoint.endpoint.type.Interface;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.configs.MonitoringInfo;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.OdlArputilService;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.SendArpRequestInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.SendArpRequestInputBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.SendArpRequestOutput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.interfaces.InterfaceAddress;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.interfaces.InterfaceAddressBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetInterfaceFromIfIndexInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetInterfaceFromIfIndexInputBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetInterfaceFromIfIndexOutput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
49 import org.opendaylight.yangtools.yang.common.RpcResult;
50 import org.opendaylight.yangtools.yang.common.Uint64;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 @Singleton
55 public class AlivenessProtocolHandlerARP extends AbstractAlivenessProtocolHandler<ARP> {
56
57     private static final Logger LOG = LoggerFactory.getLogger(AlivenessProtocolHandlerARP.class);
58
59     private final OdlArputilService arpService;
60     private final OdlInterfaceRpcService interfaceManager;
61
62     @Inject
63     public AlivenessProtocolHandlerARP(
64             @Reference final DataBroker dataBroker,
65             @Reference final AlivenessProtocolHandlerRegistry alivenessProtocolHandlerRegistry,
66             final OdlInterfaceRpcService interfaceManager,
67             final OdlArputilService arpService) {
68         super(dataBroker, alivenessProtocolHandlerRegistry, MonitorProtocolType.Arp);
69         this.interfaceManager = interfaceManager;
70         this.arpService = arpService;
71     }
72
73     @Override
74     public Class<ARP> getPacketClass() {
75         return ARP.class;
76     }
77
78     @Override
79     @SuppressFBWarnings("NP_NONNULL_RETURN_VIOLATION")
80     public String handlePacketIn(ARP packet, PacketReceived packetReceived) {
81         short tableId = packetReceived.getTableId().getValue().toJava();
82         int arpType = packet.getOpCode();
83
84         if (LOG.isTraceEnabled()) {
85             LOG.trace("packet: {}, tableId {}, arpType {}", packetReceived, tableId, arpType);
86         }
87
88         if (arpType == ARP.REPLY) {
89             if (LOG.isTraceEnabled()) {
90                 LOG.trace("packet: {}", packetReceived);
91             }
92
93             Uint64 metadata = packetReceived.getMatch().getMetadata().getMetadata();
94             int portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
95             String interfaceName = null;
96
97             try {
98                 GetInterfaceFromIfIndexInput input = new GetInterfaceFromIfIndexInputBuilder().setIfIndex(portTag)
99                         .build();
100                 Future<RpcResult<GetInterfaceFromIfIndexOutput>> output = interfaceManager
101                         .getInterfaceFromIfIndex(input);
102                 RpcResult<GetInterfaceFromIfIndexOutput> result = output.get();
103                 if (result.isSuccessful()) {
104                     GetInterfaceFromIfIndexOutput ifIndexOutput = result.getResult();
105                     interfaceName = ifIndexOutput.getInterfaceName();
106                 } else {
107                     LOG.warn("RPC call to get interface name for if index {} failed with errors {}", portTag,
108                             result.getErrors());
109                     return null;
110                 }
111             } catch (InterruptedException | ExecutionException e) {
112                 LOG.warn("Error retrieving interface Name for tag {}", portTag, e);
113             }
114             if (!Strings.isNullOrEmpty(interfaceName)) {
115                 String sourceIp = NWUtil.toStringIpAddress(packet.getSenderProtocolAddress());
116                 String targetIp = NWUtil.toStringIpAddress(packet.getTargetProtocolAddress());
117                 return getMonitoringKey(interfaceName, targetIp, sourceIp);
118             } else {
119                 LOG.debug("No interface associated with tag {} to interpret the received ARP Reply", portTag);
120             }
121         }
122
123         return null;
124     }
125
126     @Override
127     public void startMonitoringTask(MonitoringInfo monitorInfo) {
128         if (arpService == null) {
129             LOG.debug("ARP Service not available to send the packet");
130             return;
131         }
132         EndpointType source = monitorInfo.getSource().getEndpointType();
133         final String sourceInterface = requireNonNull(AlivenessMonitorUtil.getInterfaceName(source),
134                 "Source interface is required to send ARP Packet for monitoring");
135
136         final String srcIp = requireNonNull(AlivenessMonitorUtil.getIpAddress(source),
137                 "Source Ip address is required to send ARP Packet for monitoring");
138         final Optional<PhysAddress> srcMacAddressOptional = getMacAddress(source);
139         if (srcMacAddressOptional.isPresent()) {
140             PhysAddress srcMacAddress = srcMacAddressOptional.get();
141             EndpointType target = monitorInfo.getDestination().getEndpointType();
142             final String targetIp = requireNonNull(AlivenessMonitorUtil.getIpAddress(target),
143                     "Target Ip address is required to send ARP Packet for monitoring");
144             if (LOG.isTraceEnabled()) {
145                 LOG.trace("sendArpRequest interface {}, senderIPAddress {}, targetAddress {}", sourceInterface, srcIp,
146                         targetIp);
147             }
148             InterfaceAddressBuilder interfaceAddressBuilder = new InterfaceAddressBuilder()
149                     .setInterface(sourceInterface).setIpAddress(IpAddressBuilder.getDefaultInstance(srcIp));
150             if (srcMacAddress != null) {
151                 interfaceAddressBuilder.setMacaddress(srcMacAddress);
152             }
153             final InterfaceAddress interfaceAddress = interfaceAddressBuilder.build();
154             SendArpRequestInput input = new SendArpRequestInputBuilder()
155                     .setInterfaceAddress(Map.of(interfaceAddress.key(), interfaceAddress))
156                     .setIpaddress(IpAddressBuilder.getDefaultInstance(targetIp)).build();
157             ListenableFuture<RpcResult<SendArpRequestOutput>> future = arpService.sendArpRequest(input);
158             final String msgFormat = String.format("Send ARP Request on interface %s to destination %s",
159                     sourceInterface, targetIp);
160             Futures.addCallback(future, new FutureCallback<RpcResult<SendArpRequestOutput>>() {
161                 @Override
162                 public void onFailure(Throwable error) {
163                     LOG.error("Error - {}", msgFormat, error);
164                 }
165
166                 @Override
167                 public void onSuccess(RpcResult<SendArpRequestOutput> result) {
168                     if (result != null && !result.isSuccessful()) {
169                         LOG.warn("Rpc call to {} failed {}", msgFormat,
170                                 AlivenessMonitorUtil.getErrorText(result.getErrors()));
171                     } else {
172                         LOG.debug("Successful RPC Result - {}", msgFormat);
173                     }
174                 }
175             }, MoreExecutors.directExecutor());
176         }
177     }
178
179     @Override
180     public String getUniqueMonitoringKey(MonitoringInfo monitorInfo) {
181         String interfaceName = AlivenessMonitorUtil.getInterfaceName(monitorInfo.getSource().getEndpointType());
182         String sourceIp = AlivenessMonitorUtil.getIpAddress(monitorInfo.getSource().getEndpointType());
183         String targetIp = AlivenessMonitorUtil.getIpAddress(monitorInfo.getDestination().getEndpointType());
184         return getMonitoringKey(interfaceName, sourceIp, targetIp);
185     }
186
187     private String getMonitoringKey(String interfaceName, String sourceIp, String targetIp) {
188         return interfaceName + SEPERATOR + sourceIp + SEPERATOR + targetIp + SEPERATOR + MonitorProtocolType.Arp;
189     }
190
191     private Optional<PhysAddress> getMacAddress(EndpointType source) {
192         Optional<PhysAddress> result = Optional.empty();
193         if (source instanceof Interface) {
194             result = Optional.of(((Interface) source).getMacAddress());
195         }
196         return result;
197     }
198 }