2 * Copyright (c) 2015 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.vpnservice.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.HashMap;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.Future;
22 import org.apache.commons.lang3.tuple.ImmutablePair;
23 import org.apache.commons.net.util.SubnetUtils;
24 import org.apache.commons.net.util.SubnetUtils.SubnetInfo;
25 import org.opendaylight.controller.liblldp.EtherTypes;
26 import org.opendaylight.controller.liblldp.NetUtils;
27 import org.opendaylight.controller.liblldp.PacketException;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.vpnservice.dhcpservice.api.DHCP;
31 import org.opendaylight.vpnservice.dhcpservice.api.DHCPConstants;
32 import org.opendaylight.vpnservice.dhcpservice.api.DHCPMConstants;
33 import org.opendaylight.vpnservice.dhcpservice.api.DHCPUtils;
34 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
35 import org.opendaylight.vpnservice.mdsalutil.MetaDataUtil;
36 import org.opendaylight.vpnservice.mdsalutil.NwConstants;
37 import org.opendaylight.vpnservice.mdsalutil.packet.Ethernet;
38 import org.opendaylight.vpnservice.mdsalutil.packet.IEEE8021Q;
39 import org.opendaylight.vpnservice.mdsalutil.packet.IPProtocols;
40 import org.opendaylight.vpnservice.mdsalutil.packet.IPv4;
41 import org.opendaylight.vpnservice.mdsalutil.packet.UDP;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnet.attributes.HostRoutes;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketInReason;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.SendToController;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetEgressActionsForInterfaceInputBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetEgressActionsForInterfaceOutput;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetInterfaceFromIfIndexInput;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetInterfaceFromIfIndexInputBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetInterfaceFromIfIndexOutput;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.OdlInterfaceRpcService;
61 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
62 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
63 import org.opendaylight.yangtools.yang.common.RpcResult;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
67 import com.google.common.base.Optional;
69 public class DhcpPktHandler implements AutoCloseable, PacketProcessingListener {
71 private static final Logger LOG = LoggerFactory.getLogger(DhcpPktHandler.class);
72 private final DataBroker dataBroker;
73 private final DhcpManager dhcpMgr;
74 private OdlInterfaceRpcService interfaceManagerRpc;
75 private static HashMap<String, ImmutablePair<BigInteger, String>> localCache = new HashMap<String, ImmutablePair<BigInteger, String>>();
76 private boolean computeUdpChecksum = true;
77 private PacketProcessingService pktService;
79 public DhcpPktHandler(final DataBroker broker, final DhcpManager dhcpManager) {
80 this.dataBroker = broker;
81 dhcpMgr = dhcpManager;
84 //TODO: Handle this in a separate thread
86 public void onPacketReceived(PacketReceived packet) {
87 LOG.trace("Pkt received: {}", packet);
88 Class<? extends PacketInReason> pktInReason = packet.getPacketInReason();
89 short tableId = packet.getTableId().getValue();
90 if (isPktInReasonSendtoCtrl(pktInReason) && ((NwConstants.DHCP_TABLE == tableId))) {
91 byte[] inPayload = packet.getPayload();
92 Ethernet ethPkt = new Ethernet();
94 ethPkt.deserialize(inPayload, 0, inPayload.length * NetUtils.NumBitsInAByte);
95 } catch (Exception e) {
96 LOG.warn("Failed to decode DHCP Packet", e);
101 pktIn = getDhcpPktIn(ethPkt);
103 LOG.trace("DHCPPkt received: {}", pktIn);
104 BigInteger metadata = packet.getMatch().getMetadata().getMetadata();
105 long portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
106 String interfaceName = getInterfaceNameFromTag(portTag);
107 ImmutablePair<BigInteger, String> pair = getDpnIdPhysicalAddressFromInterfaceName(interfaceName);
108 DHCP replyPkt = handleDhcpPacket(pktIn, interfaceName);
109 byte[] pktOut = getDhcpPacketOut(replyPkt, ethPkt, pair.getRight());
110 sendPacketOut(pktOut, pair.getLeft(), interfaceName);
112 } catch (Exception e) {
113 LOG.warn("Failed to get DHCP Reply {}", e);
118 private void sendPacketOut(byte[] pktOut, BigInteger dpnId, String interfaceName) {
119 LOG.trace("Sending packet out DpId {}, portId {}, vlanId {}, interfaceName {}", dpnId, interfaceName);
120 List<Action> action = getEgressAction(interfaceName);
121 TransmitPacketInput output = MDSALUtil.getPacketOut(action, pktOut, dpnId);
122 LOG.trace("Transmitting packet: {}",output);
123 this.pktService.transmitPacket(output);
126 private DHCP handleDhcpPacket(DHCP dhcpPkt, String interfaceName) {
127 LOG.debug("DHCP pkt rcvd {}", dhcpPkt);
128 byte msgType = dhcpPkt.getMsgType();
129 if (msgType == DHCPConstants.MSG_DECLINE) {
130 LOG.debug("DHCPDECLINE received");
132 } else if (msgType == DHCPConstants.MSG_RELEASE) {
133 LOG.debug("DHCPRELEASE received");
137 Port nPort = getNeutronPort(interfaceName);
138 Subnet nSubnet = getNeutronSubnet(nPort);
139 DhcpInfo dhcpInfo = getDhcpInfo(nPort, nSubnet);
140 LOG.trace("NeutronPort: {} \n NeutronSubnet: {}, dhcpInfo{}",nPort, nSubnet, dhcpInfo);
142 if (dhcpInfo != null) {
143 if (msgType == DHCPConstants.MSG_DISCOVER) {
144 reply = getReplyToDiscover(dhcpPkt, dhcpInfo);
145 } else if (msgType == DHCPConstants.MSG_REQUEST) {
146 reply = getReplyToRequest(dhcpPkt, dhcpInfo);
153 private DhcpInfo getDhcpInfo(Port nPort, Subnet nSubnet) {
154 DhcpInfo dhcpInfo = null;
155 if( (nPort != null) && (nSubnet != null) ) {
156 String clientIp = nPort.getFixedIps().get(0).getIpAddress().getIpv4Address().getValue();
157 String serverIp = nSubnet.getGatewayIp().getIpv4Address().getValue();
158 List<IpAddress> dnsServers = nSubnet.getDnsNameservers();
159 dhcpInfo = new DhcpInfo();
160 dhcpInfo.setClientIp(clientIp).setServerIp(serverIp)
161 .setCidr(nSubnet.getCidr()).setHostRoutes(nSubnet.getHostRoutes())
162 .setDnsServersIpAddrs(dnsServers).setGatewayIp(serverIp);
164 //FIXME: Delete this test code
165 LOG.error("TestOnly Code");
166 dhcpInfo = new DhcpInfo();
167 dhcpInfo.setClientIp("1.1.1.3").setServerIp("1.1.1.1")
168 .setCidr("1.1.1.0/24").addDnsServer("1.1.1.1");
169 LOG.warn("Failed to get Subnet info for DHCP reply");
174 private Subnet getNeutronSubnet(Port nPort) {
175 return dhcpMgr.getNeutronSubnet(nPort);
178 private Port getNeutronPort(String interfaceName) {
179 return dhcpMgr.getNeutronPort(interfaceName);
182 private DHCP getDhcpPktIn(Ethernet actualEthernetPacket) {
183 Ethernet ethPkt = actualEthernetPacket;
184 LOG.trace("Inside getDhcpPktIn ethPkt {} \n getPayload {}", ethPkt, ethPkt.getPayload());
185 if (ethPkt.getEtherType() == (short)NwConstants.ETHTYPE_802_1Q) {
186 ethPkt = (Ethernet)ethPkt.getPayload();
188 if (ethPkt.getPayload() instanceof IPv4) {
189 IPv4 ipPkt = (IPv4) ethPkt.getPayload();
190 if (ipPkt.getPayload() instanceof UDP) {
191 UDP udpPkt = (UDP) ipPkt.getPayload();
192 if ((udpPkt.getSourcePort() == DHCPMConstants.dhcpClientPort)
193 && (udpPkt.getDestinationPort() == DHCPMConstants.dhcpServerPort)) {
194 LOG.trace("Matched dhcpClientPort and dhcpServerPort");
195 byte[] rawDhcpPayload = udpPkt.getRawPayload();
196 DHCP reply = new DHCP();
198 reply.deserialize(rawDhcpPayload, 0, rawDhcpPayload.length);
199 } catch (PacketException e) {
200 LOG.warn("Failed to deserialize DHCP pkt {}", e);
210 DHCP getReplyToDiscover(DHCP dhcpPkt, DhcpInfo dhcpInfo) {
211 DHCP reply = new DHCP();
212 reply.setOp(DHCPConstants.BOOTREPLY);
213 reply.setHtype(dhcpPkt.getHtype());
214 reply.setHlen(dhcpPkt.getHlen());
215 reply.setHops((byte) 0);
216 reply.setXid(dhcpPkt.getXid());
217 reply.setSecs((short) 0);
219 reply.setYiaddr(dhcpInfo.getClientIp());
220 reply.setSiaddr(dhcpInfo.getServerIp());
222 reply.setFlags(dhcpPkt.getFlags());
223 reply.setGiaddr(dhcpPkt.getGiaddr());
224 reply.setChaddr(dhcpPkt.getChaddr());
226 reply.setMsgType(DHCPConstants.MSG_OFFER);
227 if(dhcpPkt.containsOption(DHCPConstants.OPT_PARAMETER_REQUEST_LIST)) {
228 setParameterListOptions(dhcpPkt, reply, dhcpInfo);
230 setCommonOptions(reply, dhcpInfo);
234 DHCP getReplyToRequest(DHCP dhcpPkt, DhcpInfo dhcpInfo) {
235 boolean sendAck = false;
236 byte[] requestedIp = null;
237 DHCP reply = new DHCP();
238 reply.setOp(DHCPConstants.BOOTREPLY);
239 reply.setHtype(dhcpPkt.getHtype());
240 reply.setHlen(dhcpPkt.getHlen());
241 reply.setHops((byte) 0);
242 reply.setXid(dhcpPkt.getXid());
243 reply.setSecs((short) 0);
245 reply.setFlags(dhcpPkt.getFlags());
246 reply.setGiaddr(dhcpPkt.getGiaddr());
247 reply.setChaddr(dhcpPkt.getChaddr());
248 byte[] allocatedIp = DHCPUtils.strAddrToByteArray(dhcpInfo.getClientIp());
249 if(Arrays.equals(allocatedIp, dhcpPkt.getCiaddr())) {
250 //This means a renew request
253 requestedIp = dhcpPkt.getOptionBytes(DHCPConstants.OPT_REQUESTED_ADDRESS);
254 sendAck = Arrays.equals(allocatedIp, requestedIp);
258 reply.setCiaddr(dhcpPkt.getCiaddr());
259 reply.setYiaddr(dhcpInfo.getClientIp());
260 reply.setSiaddr(dhcpInfo.getServerIp());
261 reply.setMsgType(DHCPConstants.MSG_ACK);
262 if(dhcpPkt.containsOption(DHCPConstants.OPT_PARAMETER_REQUEST_LIST)) {
263 setParameterListOptions(dhcpPkt, reply, dhcpInfo);
265 setCommonOptions(reply, dhcpInfo);
268 reply.setMsgType(DHCPConstants.MSG_NAK);
273 protected byte[] getDhcpPacketOut(DHCP reply, Ethernet etherPkt, String phyAddrees) {
276 * DECLINE or RELEASE don't result in reply packet
280 LOG.debug("Sending DHCP Pkt {}", reply);
282 UDP udpPkt = new UDP();
285 rawPkt = reply.serialize();
286 } catch (PacketException e2) {
287 // TODO Auto-generated catch block
288 e2.printStackTrace();
291 udpPkt.setRawPayload(rawPkt);
292 udpPkt.setDestinationPort(DHCPMConstants.dhcpClientPort);
293 udpPkt.setSourcePort(DHCPMConstants.dhcpServerPort);
294 udpPkt.setLength((short) (rawPkt.length + 8));
296 IPv4 ip4Reply = new IPv4();
298 rawPkt = udpPkt.serialize();
299 } catch (PacketException e) {
300 // TODO Auto-generated catch block
305 if(this.computeUdpChecksum) {
306 checkSum = computeChecksum(rawPkt, reply.getSiaddr(),
307 NetUtils.intToByteArray4(DHCPMConstants.BCAST_IP));
309 udpPkt.setChecksum(checkSum);
310 ip4Reply.setPayload(udpPkt);
311 ip4Reply.setProtocol(IPProtocols.UDP.byteValue());
312 ip4Reply.setSourceAddress(reply.getSiaddrAsInetAddr());
313 ip4Reply.setDestinationAddress(DHCPMConstants.BCAST_IP);
314 ip4Reply.setTotalLength((short) (rawPkt.length+20));
315 ip4Reply.setTtl((byte) 32);
316 // create Ethernet Frame
317 Ethernet ether = new Ethernet();
318 if (etherPkt.getEtherType() == (short)NwConstants.ETHTYPE_802_1Q) {
319 IEEE8021Q vlanPacket = (IEEE8021Q) etherPkt.getPayload();
320 IEEE8021Q vlanTagged = new IEEE8021Q();
321 vlanTagged.setCFI(vlanPacket.getCfi());
322 vlanTagged.setPriority(vlanPacket.getPriority());
323 vlanTagged.setVlanId(vlanPacket.getVlanId());
324 vlanTagged.setPayload(ip4Reply);
325 vlanTagged.setEtherType(EtherTypes.IPv4.shortValue());
326 ether.setPayload(vlanTagged);
327 ether.setEtherType((short) NwConstants.ETHTYPE_802_1Q);
329 ether.setEtherType(EtherTypes.IPv4.shortValue());
330 ether.setPayload(ip4Reply);
333 ether.setSourceMACAddress(getServerMacAddress(phyAddrees));
334 ether.setDestinationMACAddress(etherPkt.getSourceMACAddress());
337 rawPkt = ether.serialize();
338 } catch (PacketException e) {
339 LOG.warn("Failed to serialize ethernet reply",e);
345 private byte[] getServerMacAddress(String phyAddress) {
346 // Should we return ControllerMac instead?
347 return DHCPUtils.strMacAddrtoByteArray(phyAddress);
350 public short computeChecksum(byte[] inData, byte[] srcAddr, byte[] destAddr) {
351 short checkSum = (short) 0;
352 int sum = 0, carry = 0;
355 for (i = 0; i < inData.length - 1; i = i + 2) {
356 // Skip, if the current bytes are checkSum bytes
357 wordData = ((inData[i] << 8) & 0xFF00) + (inData[i + 1] & 0xFF);
358 sum = sum + wordData;
361 if (i < inData.length) {
362 wordData = ((inData[i] << 8) & 0xFF00) + (0 & 0xFF);
363 sum = sum + wordData;
366 for (i = 0; i < 4; i = i + 2) {
367 wordData = ((srcAddr[i] << 8) & 0xFF00) + (srcAddr[i + 1] & 0xFF);
368 sum = sum + wordData;
371 for (i = 0; i < 4; i = i + 2) {
372 wordData = ((destAddr[i] << 8) & 0xFF00) + (destAddr[i + 1] & 0xFF);
373 sum = sum + wordData;
375 sum = sum + 17 + inData.length;
377 while((sum >> 16) != 0) {
379 sum = (sum & 0xFFFF)+ carry;
381 checkSum = (short) ~((short) sum & 0xFFFF);
383 checkSum = (short)0xffff;
388 private void setCommonOptions(DHCP pkt, DhcpInfo dhcpInfo) {
389 pkt.setOptionInt(DHCPConstants.OPT_LEASE_TIME, dhcpMgr.getDhcpLeaseTime());
390 if (dhcpMgr.getDhcpDefDomain() != null) {
391 pkt.setOptionString(DHCPConstants.OPT_DOMAIN_NAME, dhcpMgr.getDhcpDefDomain());
393 if(dhcpMgr.getDhcpLeaseTime() > 0) {
394 pkt.setOptionInt(DHCPConstants.OPT_REBINDING_TIME, dhcpMgr.getDhcpRebindingTime());
395 pkt.setOptionInt(DHCPConstants.OPT_RENEWAL_TIME, dhcpMgr.getDhcpRenewalTime());
397 SubnetUtils util = null;
398 SubnetInfo info = null;
399 util = new SubnetUtils(dhcpInfo.getCidr());
400 info = util.getInfo();
401 String gwIp = dhcpInfo.getGatewayIp();
402 List<String> dnServers = dhcpInfo.getDnsServers();
405 * setParameterListOptions may have initialized some of these
406 * options to maintain order. If we can't fill them, unset to avoid
407 * sending wrong information in reply.
410 pkt.setOptionInetAddr(DHCPConstants.OPT_SERVER_IDENTIFIER, gwIp);
411 pkt.setOptionInetAddr(DHCPConstants.OPT_ROUTERS, gwIp);
413 pkt.unsetOption(DHCPConstants.OPT_SERVER_IDENTIFIER);
414 pkt.unsetOption(DHCPConstants.OPT_ROUTERS);
417 pkt.setOptionInetAddr(DHCPConstants.OPT_SUBNET_MASK, info.getNetmask());
418 pkt.setOptionInetAddr(DHCPConstants.OPT_BROADCAST_ADDRESS, info.getBroadcastAddress());
420 pkt.unsetOption(DHCPConstants.OPT_SUBNET_MASK);
421 pkt.unsetOption(DHCPConstants.OPT_BROADCAST_ADDRESS);
423 if ((dnServers != null) && (dnServers.size() > 0)) {
424 pkt.setOptionStrAddrs(DHCPConstants.OPT_DOMAIN_NAME_SERVERS, dnServers);
426 pkt.unsetOption(DHCPConstants.OPT_DOMAIN_NAME_SERVERS);
428 } catch (UnknownHostException e) {
429 // TODO Auto-generated catch block
434 private void setParameterListOptions(DHCP req, DHCP reply, DhcpInfo dhcpInfo) {
435 byte[] paramList = req.getOptionBytes(DHCPConstants.OPT_PARAMETER_REQUEST_LIST);
436 for(int i = 0; i < paramList.length; i++) {
437 switch (paramList[i]) {
438 case DHCPConstants.OPT_SUBNET_MASK:
439 case DHCPConstants.OPT_ROUTERS:
440 case DHCPConstants.OPT_SERVER_IDENTIFIER:
441 case DHCPConstants.OPT_DOMAIN_NAME_SERVERS:
442 case DHCPConstants.OPT_BROADCAST_ADDRESS:
443 case DHCPConstants.OPT_LEASE_TIME:
444 case DHCPConstants.OPT_RENEWAL_TIME:
445 case DHCPConstants.OPT_REBINDING_TIME:
446 /* These values will be filled in setCommonOptions
447 * Setting these just to preserve order as
448 * specified in PARAMETER_REQUEST_LIST.
450 reply.setOptionInt(paramList[i], 0);
452 case DHCPConstants.OPT_DOMAIN_NAME:
453 reply.setOptionString(paramList[i], " ");
455 case DHCPConstants.OPT_CLASSLESS_ROUTE:
456 setOptionClasslessRoute(reply, dhcpInfo);
459 LOG.debug("DHCP Option code {} not supported yet", paramList[i]);
464 private void setOptionClasslessRoute(DHCP reply, DhcpInfo dhcpInfo) {
465 List<HostRoutes> hostRoutes = dhcpInfo.getHostRoutes();
466 if(hostRoutes == null) {
467 //we can't set this option, so return
470 ByteArrayOutputStream result = new ByteArrayOutputStream();
471 Iterator<HostRoutes> iter = hostRoutes.iterator();
472 while(iter.hasNext()) {
473 HostRoutes hostRoute = iter.next();
474 if(hostRoute.getNexthop().getIpv4Address() == null ||
475 hostRoute.getDestination().getIpv4Prefix() == null ) {
476 // we only deal with IPv4 addresses
479 String router = hostRoute.getNexthop().getIpv4Address().getValue();
480 String dest = hostRoute.getDestination().getIpv4Prefix().getValue();
482 result.write(convertToClasslessRouteOption(dest, router));
483 } catch (IOException | NullPointerException e) {
484 LOG.debug("Exception {}",e.getMessage());
487 if (result.size() > 0) {
488 reply.setOptionBytes(DHCPConstants.OPT_CLASSLESS_ROUTE , result.toByteArray());
492 protected byte[] convertToClasslessRouteOption(String dest, String router) {
493 ByteArrayOutputStream bArr = new ByteArrayOutputStream();
501 String[] parts = dest.split("/");
502 if (parts.length < 2) {
503 prefix = new Short((short)0);
505 prefix = Short.valueOf(parts[1]);
508 bArr.write(prefix.byteValue());
509 SubnetUtils util = new SubnetUtils(dest);
510 SubnetInfo info = util.getInfo();
511 String strNetAddr = info.getNetworkAddress();
513 byte[] netAddr = InetAddress.getByName(strNetAddr).getAddress();
514 //Strip any trailing 0s from netAddr
515 for(int i = 0; i < netAddr.length;i++) {
516 if(netAddr[i] != 0) {
517 bArr.write(netAddr,i,1);
520 bArr.write(InetAddress.getByName(router).getAddress());
521 } catch (IOException e) {
524 return bArr.toByteArray();
527 private boolean isPktInReasonSendtoCtrl(Class<? extends PacketInReason> pktInReason) {
528 return (pktInReason == SendToController.class);
532 public void close() throws Exception {
533 // TODO Auto-generated method stub
536 public void setPacketProcessingService(PacketProcessingService packetService) {
537 this.pktService = packetService;
540 public void setInterfaceManagerRpc(OdlInterfaceRpcService interfaceManagerRpc) {
541 LOG.trace("Registered interfaceManager successfully");;
542 this.interfaceManagerRpc = interfaceManagerRpc;
545 private String getInterfaceNameFromTag(long portTag) {
546 String interfaceName = null;
547 GetInterfaceFromIfIndexInput input = new GetInterfaceFromIfIndexInputBuilder().setIfIndex(new Integer((int)portTag)).build();
548 Future<RpcResult<GetInterfaceFromIfIndexOutput>> futureOutput = interfaceManagerRpc.getInterfaceFromIfIndex(input);
550 GetInterfaceFromIfIndexOutput output = futureOutput.get().getResult();
551 interfaceName = output.getInterfaceName();
552 } catch (InterruptedException | ExecutionException e) {
553 LOG.error("Error while retrieving the interfaceName from tag using getInterfaceFromIfIndex RPC");
555 LOG.trace("Returning interfaceName {} for tag {} form getInterfaceNameFromTag", interfaceName, portTag);
556 return interfaceName;
559 private org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface getInterfaceStateFromOperDS(String interfaceName) {
560 InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId =
561 buildStateInterfaceId(interfaceName);
562 Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateOptional =
563 MDSALUtil.read(LogicalDatastoreType.OPERATIONAL, ifStateId, dataBroker);
564 if (!ifStateOptional.isPresent()) {
568 return ifStateOptional.get();
571 private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> buildStateInterfaceId(String interfaceName) {
572 InstanceIdentifierBuilder<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> idBuilder =
573 InstanceIdentifier.builder(InterfacesState.class)
574 .child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.class,
575 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey(interfaceName));
576 InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> id = idBuilder.build();
580 private List<Action> getEgressAction(String interfaceName) {
581 List<Action> actions = null;
583 Future<RpcResult<GetEgressActionsForInterfaceOutput>> result =
584 interfaceManagerRpc.getEgressActionsForInterface(
585 new GetEgressActionsForInterfaceInputBuilder().setIntfName(interfaceName).build());
586 RpcResult<GetEgressActionsForInterfaceOutput> rpcResult = result.get();
587 if(!rpcResult.isSuccessful()) {
588 LOG.warn("RPC Call to Get egress actions for interface {} returned with Errors {}", interfaceName, rpcResult.getErrors());
590 actions = rpcResult.getResult().getAction();
592 } catch (InterruptedException | ExecutionException e) {
593 LOG.warn("Exception when egress actions for interface {}", interfaceName, e);
598 private ImmutablePair<BigInteger, String> getDpnIdPhysicalAddressFromInterfaceName(String interfaceName) {
599 ImmutablePair<BigInteger, String> pair = localCache.get(interfaceName);
600 if (pair!=null && pair.getLeft() != null && pair.getRight() != null) {
603 NodeConnectorId nodeConnectorId = null;
604 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceState = getInterfaceStateFromOperDS(interfaceName);
605 if(interfaceState != null) {
606 List<String> ofportIds = interfaceState.getLowerLayerIf();
607 nodeConnectorId = new NodeConnectorId(ofportIds.get(0));
609 BigInteger dpId = BigInteger.valueOf(MDSALUtil.getDpnIdFromPortName(nodeConnectorId));
610 String phyAddress = interfaceState==null ? "":interfaceState.getPhysAddress().getValue();
611 pair = new ImmutablePair<BigInteger, String>(dpId, phyAddress);
612 localCache.put(interfaceName, pair);