2 * Copyright IBM Corporation, 2013. 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.controller.samples.loadbalancer;
10 import org.opendaylight.controller.sal.packet.IPv4;
11 import org.opendaylight.controller.sal.packet.Packet;
12 import org.opendaylight.controller.sal.packet.TCP;
13 import org.opendaylight.controller.sal.packet.UDP;
14 import org.opendaylight.controller.sal.utils.IPProtocols;
15 import org.opendaylight.controller.sal.utils.NetUtils;
16 import org.opendaylight.controller.samples.loadbalancer.entities.Client;
17 import org.opendaylight.controller.samples.loadbalancer.entities.VIP;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * Class defines utilty methods that will be used by different components
23 * of the load balancer service
28 private static final Logger lbuLogger = LoggerFactory.getLogger(LBUtil.class);
33 * Extract the details of the source machine that sent this packet 'inPkt'
34 * @param inPkt Packet that is received by the controller
35 * @return Details of the source machine in Client object.
37 public Client getClientFromPacket(IPv4 inPkt){
38 lbuLogger.info("Find client information from packet : {}",inPkt.toString());
40 String ip = NetUtils.getInetAddress(inPkt.getSourceAddress()).getHostAddress();
42 String protocol = IPProtocols.getProtocolName(inPkt.getProtocol());
44 lbuLogger.info("client ip {} and protocl {}",ip,protocol);
46 Packet tpFrame= inPkt.getPayload();
48 lbuLogger.info("Get protocol layer {}",tpFrame.toString());
52 if(protocol.equals(IPProtocols.TCP.toString())){
53 TCP tcpFrame = (TCP)tpFrame;
54 port = tcpFrame.getSourcePort();
56 UDP udpFrame = (UDP)tpFrame;
57 port = udpFrame.getSourcePort();
60 lbuLogger.info("Found port {}",port);
62 Client source = new Client(ip, protocol,port);
64 lbuLogger.info("Client information : {}",source.toString());
70 * Extract the details of the destination machine where this packet 'inPkt' need
72 * @param inPkt Packet that is received by controller for forwarding
73 * @return Details of the destination machine packet in VIP
75 public VIP getVIPFromPacket(IPv4 inPkt){
77 lbuLogger.info("Find VIP information from packet : {}",inPkt.toString());
79 String ip = NetUtils.getInetAddress(inPkt.getDestinationAddress()).getHostAddress();
81 String protocol = IPProtocols.getProtocolName(inPkt.getProtocol());
83 Packet tpFrame= inPkt.getPayload();
87 if(protocol.equals(IPProtocols.TCP.toString())){
88 TCP tcpFrame = (TCP)tpFrame;
89 port = tcpFrame.getDestinationPort();
92 UDP udpFrame = (UDP)tpFrame;
93 port = udpFrame.getDestinationPort();
96 VIP dest = new VIP(null,ip, protocol,port,null);
98 lbuLogger.info("VIP information : {}",dest.toString());