Revert "Checkstyle enforcer"
[controller.git] / opendaylight / samples / loadbalancer / src / main / java / org / opendaylight / controller / samples / loadbalancer / LBUtil.java
1 /*
2  * Copyright IBM Corporation, 2013.  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.controller.samples.loadbalancer;
9
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;
20
21 /**
22  * Class defines utilty methods that will be used by different components 
23  * of the load balancer service 
24  *
25  */
26 public class LBUtil {
27     
28     private static final Logger lbuLogger = LoggerFactory.getLogger(LBUtil.class);
29     
30     public LBUtil(){}
31     
32     /**
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.
36      */
37     public Client getClientFromPacket(IPv4 inPkt){
38         lbuLogger.info("Find client information from packet : {}",inPkt.toString());
39         
40         String ip = NetUtils.getInetAddress(inPkt.getSourceAddress()).getHostAddress();
41         
42         String protocol = IPProtocols.getProtocolName(inPkt.getProtocol());
43         
44         lbuLogger.info("client ip {} and protocl {}",ip,protocol);
45         
46         Packet tpFrame= inPkt.getPayload();
47         
48         lbuLogger.info("Get protocol layer {}",tpFrame.toString());
49         
50         short port = 0;
51         
52         if(protocol.equals(IPProtocols.TCP.toString())){
53             TCP tcpFrame = (TCP)tpFrame;
54             port = tcpFrame.getSourcePort();
55         }else{
56             UDP udpFrame = (UDP)tpFrame;
57             port = udpFrame.getSourcePort();
58         }
59         
60         lbuLogger.info("Found port {}",port);
61         
62         Client source = new Client(ip, protocol,port);
63         
64         lbuLogger.info("Client information : {}",source.toString());
65         
66         return source;
67     }
68     
69     /**
70      * Extract the details of the destination machine where this packet 'inPkt' need
71      * to be delivered
72      * @param inPkt Packet that is received by controller for forwarding
73      * @return  Details of the destination machine packet in VIP
74      */
75     public VIP getVIPFromPacket(IPv4 inPkt){
76         
77         lbuLogger.info("Find VIP information from packet : {}",inPkt.toString());
78         
79         String ip = NetUtils.getInetAddress(inPkt.getDestinationAddress()).getHostAddress();
80         
81         String protocol = IPProtocols.getProtocolName(inPkt.getProtocol());
82         
83         Packet tpFrame= inPkt.getPayload();
84         
85         short port = 0;
86         
87         if(protocol.equals(IPProtocols.TCP.toString())){
88             TCP tcpFrame = (TCP)tpFrame;
89             port = tcpFrame.getDestinationPort();
90         }else{
91             
92             UDP udpFrame = (UDP)tpFrame;
93             port = udpFrame.getDestinationPort();
94         }
95         
96         VIP dest = new VIP(null,ip, protocol,port,null);
97         
98         lbuLogger.info("VIP information : {}",dest.toString());
99         
100         return dest;
101     }
102 }