2 * Copyright © 2015, 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.netvirt.dhcpservice;
10 import java.io.ByteArrayOutputStream;
11 import java.io.IOException;
12 import java.math.BigInteger;
13 import java.net.InetAddress;
14 import java.net.UnknownHostException;
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.Future;
19 import org.apache.commons.net.util.SubnetUtils;
20 import org.apache.commons.net.util.SubnetUtils.SubnetInfo;
21 import org.opendaylight.controller.liblldp.EtherTypes;
22 import org.opendaylight.controller.liblldp.NetUtils;
23 import org.opendaylight.controller.liblldp.PacketException;
24 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
25 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
26 import org.opendaylight.genius.mdsalutil.MDSALUtil;
27 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
28 import org.opendaylight.genius.mdsalutil.NwConstants;
29 import org.opendaylight.genius.mdsalutil.packet.Ethernet;
30 import org.opendaylight.genius.mdsalutil.packet.IEEE8021Q;
31 import org.opendaylight.genius.mdsalutil.packet.IPProtocols;
32 import org.opendaylight.genius.mdsalutil.packet.IPv4;
33 import org.opendaylight.genius.mdsalutil.packet.UDP;
34 import org.opendaylight.netvirt.dhcpservice.api.DHCP;
35 import org.opendaylight.netvirt.dhcpservice.api.DHCPConstants;
36 import org.opendaylight.netvirt.dhcpservice.api.DHCPUtils;
37 import org.opendaylight.netvirt.dhcpservice.api.DhcpMConstants;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEgressActionsForInterfaceInputBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEgressActionsForInterfaceOutput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetInterfaceFromIfIndexInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetInterfaceFromIfIndexInputBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetInterfaceFromIfIndexOutput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnet.attributes.HostRoutes;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketInReason;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.SendToController;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dhcpservice.config.rev150710.DhcpserviceConfig;
57 import org.opendaylight.yangtools.yang.common.RpcResult;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
61 public class DhcpPktHandler implements PacketProcessingListener {
63 private static final Logger LOG = LoggerFactory.getLogger(DhcpPktHandler.class);
65 private final DhcpManager dhcpMgr;
66 private final OdlInterfaceRpcService interfaceManagerRpc;
67 private final PacketProcessingService pktService;
68 private final DhcpExternalTunnelManager dhcpExternalTunnelManager;
69 private final IInterfaceManager interfaceManager;
70 private final DhcpserviceConfig config;
72 public DhcpPktHandler(final DhcpManager dhcpManager,
73 final DhcpExternalTunnelManager dhcpExternalTunnelManager,
74 final OdlInterfaceRpcService interfaceManagerRpc,
75 final PacketProcessingService pktService,
76 final IInterfaceManager interfaceManager,
77 final DhcpserviceConfig config) {
78 this.interfaceManagerRpc = interfaceManagerRpc;
79 this.pktService = pktService;
80 this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
81 this.dhcpMgr = dhcpManager;
82 this.interfaceManager = interfaceManager;
86 //TODO: Handle this in a separate thread
88 public void onPacketReceived(PacketReceived packet) {
89 if (!config.isControllerDhcpEnabled()) {
92 Class<? extends PacketInReason> pktInReason = packet.getPacketInReason();
93 short tableId = packet.getTableId().getValue();
94 if ((tableId == NwConstants.DHCP_TABLE || tableId == NwConstants.DHCP_TABLE_EXTERNAL_TUNNEL)
95 && isPktInReasonSendtoCtrl(pktInReason)) {
96 byte[] inPayload = packet.getPayload();
97 Ethernet ethPkt = new Ethernet();
99 ethPkt.deserialize(inPayload, 0, inPayload.length * NetUtils.NumBitsInAByte);
100 } catch (PacketException e) {
101 LOG.warn("Failed to decode DHCP Packet.", e);
102 LOG.trace("Received packet {}", packet);
106 pktIn = getDhcpPktIn(ethPkt);
108 LOG.trace("DHCPPkt received: {}", pktIn);
109 LOG.trace("Received Packet: {}", packet);
110 BigInteger metadata = packet.getMatch().getMetadata().getMetadata();
111 long portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
112 String macAddress = DHCPUtils.byteArrayToString(ethPkt.getSourceMACAddress());
113 BigInteger tunnelId =
114 packet.getMatch().getTunnel() == null ? null : packet.getMatch().getTunnel().getTunnelId();
115 String interfaceName = getInterfaceNameFromTag(portTag);
116 InterfaceInfo interfaceInfo =
117 interfaceManager.getInterfaceInfoFromOperationalDataStore(interfaceName);
118 if (interfaceInfo == null) {
119 LOG.error("Failed to get interface info for interface name {}", interfaceName);
122 DHCP replyPkt = handleDhcpPacket(pktIn, interfaceName, macAddress, tunnelId);
123 byte[] pktOut = getDhcpPacketOut(replyPkt, ethPkt, interfaceInfo.getMacAddress());
124 sendPacketOut(pktOut, interfaceInfo.getDpId(), interfaceName, tunnelId);
129 private void sendPacketOut(byte[] pktOut, BigInteger dpnId, String interfaceName, BigInteger tunnelId) {
130 List<Action> action = getEgressAction(interfaceName, tunnelId);
131 TransmitPacketInput output = MDSALUtil.getPacketOut(action, pktOut, dpnId);
132 LOG.trace("Transmitting packet: {}", output);
133 this.pktService.transmitPacket(output);
136 private DHCP handleDhcpPacket(DHCP dhcpPkt, String interfaceName, String macAddress, BigInteger tunnelId) {
137 LOG.trace("DHCP pkt rcvd {}", dhcpPkt);
138 byte msgType = dhcpPkt.getMsgType();
139 if (msgType == DHCPConstants.MSG_DECLINE) {
140 LOG.trace("DHCPDECLINE received");
142 } else if (msgType == DHCPConstants.MSG_RELEASE) {
143 LOG.trace("DHCPRELEASE received");
147 if (tunnelId != null) {
148 port = dhcpExternalTunnelManager.readVniMacToPortCache(tunnelId, macAddress);
150 port = getNeutronPort(interfaceName);
152 Subnet subnet = getNeutronSubnet(port);
153 DhcpInfo dhcpInfo = getDhcpInfo(port, subnet);
154 LOG.trace("NeutronPort: {} \n NeutronSubnet: {}, dhcpInfo{}", port, subnet, dhcpInfo);
156 if (dhcpInfo != null) {
157 if (msgType == DHCPConstants.MSG_DISCOVER) {
158 reply = getReplyToDiscover(dhcpPkt, dhcpInfo);
159 } else if (msgType == DHCPConstants.MSG_REQUEST) {
160 reply = getReplyToRequest(dhcpPkt, dhcpInfo);
167 private DhcpInfo getDhcpInfo(Port port, Subnet subnet) {
168 DhcpInfo dhcpInfo = null;
169 if (port != null && subnet != null) {
170 String clientIp = getIpv4Address(port);
171 String serverIp = null;
172 if (isIpv4Address(subnet.getGatewayIp())) {
173 serverIp = subnet.getGatewayIp().getIpv4Address().getValue();
175 if (clientIp != null && serverIp != null) {
176 List<IpAddress> dnsServers = subnet.getDnsNameservers();
177 dhcpInfo = new DhcpInfo();
178 dhcpInfo.setClientIp(clientIp).setServerIp(serverIp)
179 .setCidr(String.valueOf(subnet.getCidr().getValue())).setHostRoutes(subnet.getHostRoutes())
180 .setDnsServersIpAddrs(dnsServers).setGatewayIp(serverIp);
187 * getIpv4Address and isIpv4Address
188 * Many other modules use/need similar methods. Should
189 * be refactored to a common NeutronUtils module. *
191 private String getIpv4Address(Port port) {
193 for (FixedIps fixedIp : port.getFixedIps()) {
194 if (isIpv4Address(fixedIp.getIpAddress())) {
195 return fixedIp.getIpAddress().getIpv4Address().getValue();
201 private boolean isIpv4Address(IpAddress ip) {
202 return ip.getIpv4Address() != null;
205 private Subnet getNeutronSubnet(Port port) {
206 return dhcpMgr.getNeutronSubnet(port);
209 private Port getNeutronPort(String interfaceName) {
210 return dhcpMgr.getNeutronPort(interfaceName);
213 private DHCP getDhcpPktIn(Ethernet actualEthernetPacket) {
214 Ethernet ethPkt = actualEthernetPacket;
215 if (ethPkt.getEtherType() == (short)NwConstants.ETHTYPE_802_1Q) {
216 ethPkt = (Ethernet)ethPkt.getPayload();
218 // Currently only IPv4 is supported
219 if (ethPkt.getPayload() instanceof IPv4) {
220 IPv4 ipPkt = (IPv4) ethPkt.getPayload();
221 if (ipPkt.getPayload() instanceof UDP) {
222 UDP udpPkt = (UDP) ipPkt.getPayload();
223 if (udpPkt.getSourcePort() == DhcpMConstants.DHCP_CLIENT_PORT
224 && udpPkt.getDestinationPort() == DhcpMConstants.DHCP_SERVER_PORT) {
225 LOG.trace("Matched DHCP_CLIENT_PORT and DHCP_SERVER_PORT");
226 byte[] rawDhcpPayload = udpPkt.getRawPayload();
227 DHCP reply = new DHCP();
229 reply.deserialize(rawDhcpPayload, 0, rawDhcpPayload.length);
230 } catch (PacketException e) {
231 LOG.warn("Failed to deserialize DHCP pkt");
232 LOG.trace("Reason for failure", e);
242 DHCP getReplyToDiscover(DHCP dhcpPkt, DhcpInfo dhcpInfo) {
243 DHCP reply = new DHCP();
244 reply.setOp(DHCPConstants.BOOTREPLY);
245 reply.setHtype(dhcpPkt.getHtype());
246 reply.setHlen(dhcpPkt.getHlen());
247 reply.setHops((byte) 0);
248 reply.setXid(dhcpPkt.getXid());
249 reply.setSecs((short) 0);
251 reply.setYiaddr(dhcpInfo.getClientIp());
252 reply.setSiaddr(dhcpInfo.getServerIp());
254 reply.setFlags(dhcpPkt.getFlags());
255 reply.setGiaddr(dhcpPkt.getGiaddr());
256 reply.setChaddr(dhcpPkt.getChaddr());
258 reply.setMsgType(DHCPConstants.MSG_OFFER);
259 if (dhcpPkt.containsOption(DHCPConstants.OPT_PARAMETER_REQUEST_LIST)) {
260 setParameterListOptions(dhcpPkt, reply, dhcpInfo);
262 setCommonOptions(reply, dhcpInfo);
266 DHCP getReplyToRequest(DHCP dhcpPkt, DhcpInfo dhcpInfo) {
267 boolean sendAck = false;
268 byte[] requestedIp = null;
269 DHCP reply = new DHCP();
270 reply.setOp(DHCPConstants.BOOTREPLY);
271 reply.setHtype(dhcpPkt.getHtype());
272 reply.setHlen(dhcpPkt.getHlen());
273 reply.setHops((byte) 0);
274 reply.setXid(dhcpPkt.getXid());
275 reply.setSecs((short) 0);
277 reply.setFlags(dhcpPkt.getFlags());
278 reply.setGiaddr(dhcpPkt.getGiaddr());
279 reply.setChaddr(dhcpPkt.getChaddr());
280 byte[] allocatedIp = DHCPUtils.strAddrToByteArray(dhcpInfo.getClientIp());
281 if (Arrays.equals(allocatedIp, dhcpPkt.getCiaddr())) {
282 //This means a renew request
285 requestedIp = dhcpPkt.getOptionBytes(DHCPConstants.OPT_REQUESTED_ADDRESS);
286 sendAck = Arrays.equals(allocatedIp, requestedIp);
290 reply.setCiaddr(dhcpPkt.getCiaddr());
291 reply.setYiaddr(dhcpInfo.getClientIp());
292 reply.setSiaddr(dhcpInfo.getServerIp());
293 reply.setMsgType(DHCPConstants.MSG_ACK);
294 if (dhcpPkt.containsOption(DHCPConstants.OPT_PARAMETER_REQUEST_LIST)) {
295 setParameterListOptions(dhcpPkt, reply, dhcpInfo);
297 setCommonOptions(reply, dhcpInfo);
299 reply.setMsgType(DHCPConstants.MSG_NAK);
304 protected byte[] getDhcpPacketOut(DHCP reply, Ethernet etherPkt, String phyAddrees) {
307 * DECLINE or RELEASE don't result in reply packet
311 LOG.trace("Sending DHCP Pkt {}", reply);
313 UDP udpPkt = new UDP();
316 rawPkt = reply.serialize();
317 } catch (PacketException e) {
318 LOG.warn("Failed to serialize packet", e);
321 udpPkt.setRawPayload(rawPkt);
322 udpPkt.setDestinationPort(DhcpMConstants.DHCP_CLIENT_PORT);
323 udpPkt.setSourcePort(DhcpMConstants.DHCP_SERVER_PORT);
324 udpPkt.setLength((short) (rawPkt.length + 8));
327 rawPkt = udpPkt.serialize();
328 } catch (PacketException e) {
329 LOG.warn("Failed to serialize packet", e);
333 boolean computeUdpChecksum = true;
334 if (computeUdpChecksum) {
335 checkSum = computeChecksum(rawPkt, reply.getSiaddr(), NetUtils.intToByteArray4(DhcpMConstants.BCAST_IP));
337 udpPkt.setChecksum(checkSum);
338 IPv4 ip4Reply = new IPv4();
339 ip4Reply.setPayload(udpPkt);
340 ip4Reply.setProtocol(IPProtocols.UDP.byteValue());
341 ip4Reply.setSourceAddress(reply.getSiaddrAsInetAddr());
342 ip4Reply.setDestinationAddress(DhcpMConstants.BCAST_IP);
343 ip4Reply.setTotalLength((short) (rawPkt.length + 20));
344 ip4Reply.setTtl((byte) 32);
345 // create Ethernet Frame
346 Ethernet ether = new Ethernet();
347 if (etherPkt.getEtherType() == (short)NwConstants.ETHTYPE_802_1Q) {
348 IEEE8021Q vlanPacket = (IEEE8021Q) etherPkt.getPayload();
349 IEEE8021Q vlanTagged = new IEEE8021Q();
350 vlanTagged.setCFI(vlanPacket.getCfi());
351 vlanTagged.setPriority(vlanPacket.getPriority());
352 vlanTagged.setVlanId(vlanPacket.getVlanId());
353 vlanTagged.setPayload(ip4Reply);
354 vlanTagged.setEtherType(EtherTypes.IPv4.shortValue());
355 ether.setPayload(vlanTagged);
356 ether.setEtherType((short) NwConstants.ETHTYPE_802_1Q);
358 ether.setEtherType(EtherTypes.IPv4.shortValue());
359 ether.setPayload(ip4Reply);
361 ether.setSourceMACAddress(getServerMacAddress(phyAddrees));
362 ether.setDestinationMACAddress(etherPkt.getSourceMACAddress());
365 rawPkt = ether.serialize();
366 } catch (PacketException e) {
367 LOG.warn("Failed to serialize ethernet reply",e);
373 private byte[] getServerMacAddress(String phyAddress) {
374 // Should we return ControllerMac instead?
375 return DHCPUtils.strMacAddrtoByteArray(phyAddress);
378 public short computeChecksum(byte[] inData, byte[] srcAddr, byte[] destAddr) {
384 for (index = 0; index < inData.length - 1; index = index + 2) {
385 // Skip, if the current bytes are checkSum bytes
386 wordData = (inData[index] << 8 & 0xFF00) + (inData[index + 1] & 0xFF);
387 sum = sum + wordData;
390 if (index < inData.length) {
391 wordData = (inData[index] << 8 & 0xFF00) + (0 & 0xFF);
392 sum = sum + wordData;
395 for (index = 0; index < 4; index = index + 2) {
396 wordData = (srcAddr[index] << 8 & 0xFF00) + (srcAddr[index + 1] & 0xFF);
397 sum = sum + wordData;
400 for (index = 0; index < 4; index = index + 2) {
401 wordData = (destAddr[index] << 8 & 0xFF00) + (destAddr[index + 1] & 0xFF);
402 sum = sum + wordData;
404 sum = sum + 17 + inData.length;
406 while (sum >> 16 != 0) {
408 sum = (sum & 0xFFFF) + carry;
410 short checkSum = (short) ~((short) sum & 0xFFFF);
412 checkSum = (short)0xffff;
417 private void setCommonOptions(DHCP pkt, DhcpInfo dhcpInfo) {
418 pkt.setOptionInt(DHCPConstants.OPT_LEASE_TIME, dhcpMgr.getDhcpLeaseTime());
419 if (dhcpMgr.getDhcpDefDomain() != null) {
420 pkt.setOptionString(DHCPConstants.OPT_DOMAIN_NAME, dhcpMgr.getDhcpDefDomain());
422 if (dhcpMgr.getDhcpLeaseTime() > 0) {
423 pkt.setOptionInt(DHCPConstants.OPT_REBINDING_TIME, dhcpMgr.getDhcpRebindingTime());
424 pkt.setOptionInt(DHCPConstants.OPT_RENEWAL_TIME, dhcpMgr.getDhcpRenewalTime());
426 SubnetUtils util = null;
427 SubnetInfo info = null;
428 util = new SubnetUtils(dhcpInfo.getCidr());
429 info = util.getInfo();
430 String gwIp = dhcpInfo.getGatewayIp();
431 List<String> dnServers = dhcpInfo.getDnsServers();
434 * setParameterListOptions may have initialized some of these
435 * options to maintain order. If we can't fill them, unset to avoid
436 * sending wrong information in reply.
439 pkt.setOptionInetAddr(DHCPConstants.OPT_SERVER_IDENTIFIER, gwIp);
440 pkt.setOptionInetAddr(DHCPConstants.OPT_ROUTERS, gwIp);
442 pkt.unsetOption(DHCPConstants.OPT_SERVER_IDENTIFIER);
443 pkt.unsetOption(DHCPConstants.OPT_ROUTERS);
446 pkt.setOptionInetAddr(DHCPConstants.OPT_SUBNET_MASK, info.getNetmask());
447 pkt.setOptionInetAddr(DHCPConstants.OPT_BROADCAST_ADDRESS, info.getBroadcastAddress());
449 pkt.unsetOption(DHCPConstants.OPT_SUBNET_MASK);
450 pkt.unsetOption(DHCPConstants.OPT_BROADCAST_ADDRESS);
452 if (dnServers != null && dnServers.size() > 0) {
453 pkt.setOptionStrAddrs(DHCPConstants.OPT_DOMAIN_NAME_SERVERS, dnServers);
455 pkt.unsetOption(DHCPConstants.OPT_DOMAIN_NAME_SERVERS);
457 } catch (UnknownHostException e) {
458 // TODO Auto-generated catch block
459 LOG.warn("Failed to set option", e);
463 private void setParameterListOptions(DHCP req, DHCP reply, DhcpInfo dhcpInfo) {
464 byte[] paramList = req.getOptionBytes(DHCPConstants.OPT_PARAMETER_REQUEST_LIST);
465 for (byte element : paramList) {
467 case DHCPConstants.OPT_SUBNET_MASK:
468 case DHCPConstants.OPT_ROUTERS:
469 case DHCPConstants.OPT_SERVER_IDENTIFIER:
470 case DHCPConstants.OPT_DOMAIN_NAME_SERVERS:
471 case DHCPConstants.OPT_BROADCAST_ADDRESS:
472 case DHCPConstants.OPT_LEASE_TIME:
473 case DHCPConstants.OPT_RENEWAL_TIME:
474 case DHCPConstants.OPT_REBINDING_TIME:
475 /* These values will be filled in setCommonOptions
476 * Setting these just to preserve order as
477 * specified in PARAMETER_REQUEST_LIST.
479 reply.setOptionInt(element, 0);
481 case DHCPConstants.OPT_DOMAIN_NAME:
482 reply.setOptionString(element, " ");
484 case DHCPConstants.OPT_CLASSLESS_ROUTE:
485 setOptionClasslessRoute(reply, dhcpInfo);
488 LOG.trace("DHCP Option code {} not supported yet", element);
494 private void setOptionClasslessRoute(DHCP reply, DhcpInfo dhcpInfo) {
495 List<HostRoutes> hostRoutes = dhcpInfo.getHostRoutes();
496 if (hostRoutes == null) {
497 //we can't set this option, so return
500 ByteArrayOutputStream result = new ByteArrayOutputStream();
501 for (HostRoutes hostRoute : hostRoutes) {
502 if (hostRoute.getNexthop().getIpv4Address() == null
503 || hostRoute.getDestination().getIpv4Prefix() == null) {
504 // we only deal with IPv4 addresses
507 String router = hostRoute.getNexthop().getIpv4Address().getValue();
508 String dest = hostRoute.getDestination().getIpv4Prefix().getValue();
510 result.write(convertToClasslessRouteOption(dest, router));
511 } catch (IOException | NullPointerException e) {
512 LOG.trace("Exception {}", e.getMessage());
515 if (result.size() > 0) {
516 reply.setOptionBytes(DHCPConstants.OPT_CLASSLESS_ROUTE , result.toByteArray());
520 protected byte[] convertToClasslessRouteOption(String dest, String router) {
521 ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
522 if (dest == null || router == null) {
528 String[] parts = dest.split("/");
529 if (parts.length < 2) {
532 prefix = Short.valueOf(parts[1]);
535 byteArray.write(prefix.byteValue());
536 SubnetUtils util = new SubnetUtils(dest);
537 SubnetInfo info = util.getInfo();
538 String strNetAddr = info.getNetworkAddress();
540 byte[] netAddr = InetAddress.getByName(strNetAddr).getAddress();
541 //Strip any trailing 0s from netAddr
542 for (int i = 0; i < netAddr.length;i++) {
543 if (netAddr[i] != 0) {
544 byteArray.write(netAddr,i,1);
547 byteArray.write(InetAddress.getByName(router).getAddress());
548 } catch (IOException e) {
551 return byteArray.toByteArray();
554 private boolean isPktInReasonSendtoCtrl(Class<? extends PacketInReason> pktInReason) {
555 return pktInReason == SendToController.class;
558 private String getInterfaceNameFromTag(long portTag) {
559 String interfaceName = null;
560 GetInterfaceFromIfIndexInput input =
561 new GetInterfaceFromIfIndexInputBuilder().setIfIndex((int) portTag).build();
562 Future<RpcResult<GetInterfaceFromIfIndexOutput>> futureOutput =
563 interfaceManagerRpc.getInterfaceFromIfIndex(input);
565 GetInterfaceFromIfIndexOutput output = futureOutput.get().getResult();
566 interfaceName = output.getInterfaceName();
567 } catch (InterruptedException | ExecutionException e) {
568 LOG.error("Error while retrieving the interfaceName from tag using getInterfaceFromIfIndex RPC");
570 LOG.trace("Returning interfaceName {} for tag {} form getInterfaceNameFromTag", interfaceName, portTag);
571 return interfaceName;
574 private List<Action> getEgressAction(String interfaceName, BigInteger tunnelId) {
575 List<Action> actions = null;
577 GetEgressActionsForInterfaceInputBuilder egressAction =
578 new GetEgressActionsForInterfaceInputBuilder().setIntfName(interfaceName);
579 if (tunnelId != null) {
580 egressAction.setTunnelKey(tunnelId.longValue());
582 Future<RpcResult<GetEgressActionsForInterfaceOutput>> result =
583 interfaceManagerRpc.getEgressActionsForInterface(egressAction.build());
584 RpcResult<GetEgressActionsForInterfaceOutput> rpcResult = result.get();
585 if (!rpcResult.isSuccessful()) {
586 LOG.warn("RPC Call to Get egress actions for interface {} returned with Errors {}",
587 interfaceName, rpcResult.getErrors());
589 actions = rpcResult.getResult().getAction();
591 } catch (InterruptedException | ExecutionException e) {
592 LOG.warn("Exception when egress actions for interface {}", interfaceName, e);