Skip to content
Snippets Groups Projects
Commit 3bf531a5 authored by Diogo's avatar Diogo
Browse files

HTTP packet parsed, starting REST API changes

parent b696cc7a
No related branches found
No related tags found
No related merge requests found
package net.floodlightcontroller.loadbalancer;
import java.io.IOException;
import java.util.Collection;
import org.projectfloodlight.openflow.types.IpProtocol;
import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.Put;
import org.restlet.resource.ServerResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.MappingJsonFactory;
import net.floodlightcontroller.packet.IPv4;
public class L7RulesResource extends ServerResource {
protected static Logger log = LoggerFactory.getLogger(L7RulesResource.class);
@Get("json")
public Collection <LBVip> retrieve() {
ILoadBalancerService lbs =
(ILoadBalancerService)getContext().getAttributes().
get(ILoadBalancerService.class.getCanonicalName());
String vipId = (String) getRequestAttributes().get("rule");
if (vipId!=null)
return lbs.listVip(vipId);
else
return lbs.listVips();
}
@Put
@Post
public LBVip createVip(String postData) {
LBVip vip=null;
try {
vip=jsonToVip(postData);
} catch (IOException e) {
log.error("Could not parse JSON {}", e.getMessage());
}
ILoadBalancerService lbs =
(ILoadBalancerService)getContext().getAttributes().
get(ILoadBalancerService.class.getCanonicalName());
String vipId = (String) getRequestAttributes().get("vip");
if (vipId != null)
return lbs.updateVip(vip);
else
return lbs.createVip(vip);
}
@Delete
public int removeVip() {
String vipId = (String) getRequestAttributes().get("vip");
ILoadBalancerService lbs =
(ILoadBalancerService)getContext().getAttributes().
get(ILoadBalancerService.class.getCanonicalName());
return lbs.removeVip(vipId);
}
protected LBVip jsonToVip(String json) throws IOException {
if (json==null) return null;
MappingJsonFactory f = new MappingJsonFactory();
JsonParser jp;
LBVip vip = new LBVip();
try {
jp = f.createParser(json);
} catch (JsonParseException e) {
throw new IOException(e);
}
jp.nextToken();
if (jp.getCurrentToken() != JsonToken.START_OBJECT) {
throw new IOException("Expected START_OBJECT");
}
while (jp.nextToken() != JsonToken.END_OBJECT) {
if (jp.getCurrentToken() != JsonToken.FIELD_NAME) {
throw new IOException("Expected FIELD_NAME");
}
String n = jp.getCurrentName();
jp.nextToken();
if (jp.getText().equals(""))
continue;
if (n.equals("id")) {
vip.id = jp.getText();
continue;
}
if (n.equals("tenant_id")) {
vip.tenantId = jp.getText();
continue;
}
if (n.equals("name")) {
vip.name = jp.getText();
continue;
}
if (n.equals("network_id")) {
vip.netId = jp.getText();
continue;
}
if (n.equals("protocol")) {
String tmp = jp.getText();
if (tmp.equalsIgnoreCase("TCP")) {
vip.protocol = (byte) IpProtocol.TCP.getIpProtocolNumber();
} else if (tmp.equalsIgnoreCase("UDP")) {
vip.protocol = (byte) IpProtocol.UDP.getIpProtocolNumber();
} else if (tmp.equalsIgnoreCase("ICMP")) {
vip.protocol = (byte) IpProtocol.ICMP.getIpProtocolNumber();
}
continue;
}
if (n.equals("address")) {
vip.address = IPv4.toIPv4Address(jp.getText());
continue;
}
if (n.equals("port")) {
vip.port = Short.parseShort(jp.getText());
continue;
}
if (n.equals("pool_id")) {
vip.pools.add(jp.getText());
continue;
}
log.warn("Unrecognized field {} in " +
"parsing Vips",
jp.getText());
}
jp.close();
return vip;
}
}
......@@ -77,7 +77,7 @@ public class LBPool {
// Get the members that belong to this pool and the statistics for them
if(members.size() > 0){
if (lbMethod == STATISTICS && membersbandwidth.values() !=null) {
if (lbMethod == STATISTICS && membersbandwidth != null && membersbandwidth.values() !=null) {
ArrayList<String> poolMembersId = new ArrayList<String>();
for(String memberId: membersbandwidth.keySet()){
for(int i=0;i<members.size();i++){
......@@ -98,7 +98,7 @@ public class LBPool {
return poolMembersId.get(bandwidthValues.indexOf(Collections.min(bandwidthValues)));
}
// simple round robin
} else if(lbMethod == ROUND_ROBIN || lbMethod == 0){
} else {
previousMemberIndex = (previousMemberIndex + 1) % members.size();
return members.get(previousMemberIndex);
}
......
......@@ -36,7 +36,11 @@ public class LoadBalancerWebRoutable implements RestletRoutable {
router.attach("/members/{member}", MembersResource.class); // GET, PUT, DELETE
router.attach("/pools/{pool}/members", PoolMemberResource.class); //GET
router.attach("/health_monitors/", MonitorsResource.class); //GET, POST
router.attach("/health_monitors/{monitor}", MonitorsResource.class); //GET, PUT, DELETE
router.attach("/health_monitors/{monitor}", MonitorsResource.class); //GET, PUT, DELETE
router.attach("/l7rules/", L7RulesResource.class); //GET, PUT, DELETE
router.attach("/l7rules/{rule}", L7RulesResource.class); //GET, PUT, DELETE
router.attach("/l7policies/", L7RulesResource.class); //GET, PUT, DELETE
router.attach("/l7policies/{policy}", L7RulesResource.class); //GET, PUT, DELETE
router.attachDefault(NoOp.class);
return router;
}
......
package net.floodlightcontroller.packet;
import java.nio.ByteBuffer;
import org.projectfloodlight.openflow.types.IpProtocol;
import org.projectfloodlight.openflow.types.TransportPort;
/**
* @author Diogo Coutinho (diogo.m.coutinho@tecnico.ulisboa.pt)
*
*/
public class HTTP extends BasePacket {
protected byte method;
protected byte uri;
protected byte version;
protected short host; // IP DO SERVER
// SP = (short) 20
// CRLF = 0d 0a
@Override
public byte[] serialize() {
int length;
// if (dataOffset == 0)
// dataOffset = 5; // default header length
// length = dataOffset << 2;
// byte[] payloadData = null;
// if (payload != null) {
// payload.setParent(this);
//
// payloadData = payload.serialize();
// length += payloadData.length;
// }
//
// byte[] data = new byte[length];
// ByteBuffer bb = ByteBuffer.wrap(data);
//
// bb.putShort((short)this.sourcePort.getPort()); //TCP ports are defined to be 16 bits
// bb.putShort((short)this.destinationPort.getPort());
// bb.putInt(this.sequence);
// bb.putInt(this.acknowledge);
// bb.putShort((short) (this.flags | (dataOffset << 12)));
// bb.putShort(this.windowSize);
// bb.putShort(this.checksum);
// bb.putShort(this.urgentPointer);
// if (dataOffset > 5) {
// int padding;
// bb.put(options);
// padding = (dataOffset << 2) - 20 - options.length;
// for (int i = 0; i < padding; i++)
// bb.put((byte) 0);
// }
// if (payloadData != null)
// bb.put(payloadData);
//
// if (this.parent != null && this.parent instanceof IPv4)
// ((IPv4)this.parent).setProtocol(IpProtocol.TCP);
//
// return data;
return null;
}
@Override
public IPacket deserialize(byte[] data, int offset, int length) throws PacketParsingException {
ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
// this.sourcePort = TransportPort.of((int) (bb.getShort() & 0xffff)); // short will be signed, pos or neg
// this.destinationPort = TransportPort.of((int) (bb.getShort() & 0xffff)); // convert range 0 to 65534, not -32768 to 32767
// this.sequence = bb.getInt();
// this.acknowledge = bb.getInt();
// this.flags = bb.getShort();
// this.dataOffset = (byte) ((this.flags >> 12) & 0xf);
// if (this.dataOffset < 5) {
// throw new PacketParsingException("Invalid tcp header length < 20");
// }
// this.flags = (short) (this.flags & 0x1ff);
// this.windowSize = bb.getShort();
// this.checksum = bb.getShort();
// this.urgentPointer = bb.getShort();
// if (this.dataOffset > 5) {
// int optLength = (dataOffset << 2) - 20;
// if (bb.limit() < bb.position()+optLength) {
// optLength = bb.limit() - bb.position();
// }
// try {
// this.options = new byte[optLength];
// bb.get(this.options, 0, optLength);
// } catch (IndexOutOfBoundsException e) {
// this.options = null;
// }
// }
this.payload = new Data();
int remLength = bb.limit()-bb.position();
this.payload = payload.deserialize(data, bb.position(), remLength);
log.info("HTTP payload : {}", this.payload);
this.payload.setParent(this);
return this;
}
}
\ No newline at end of file
......@@ -178,6 +178,7 @@ public class TCP extends BasePacket {
byte[] payloadData = null;
if (payload != null) {
payload.setParent(this);
payloadData = payload.serialize();
length += payloadData.length;
}
......@@ -379,7 +380,7 @@ public class TCP extends BasePacket {
@Override
public IPacket deserialize(byte[] data, int offset, int length)
throws PacketParsingException {
ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
this.sourcePort = TransportPort.of((int) (bb.getShort() & 0xffff)); // short will be signed, pos or neg
this.destinationPort = TransportPort.of((int) (bb.getShort() & 0xffff)); // convert range 0 to 65534, not -32768 to 32767
this.sequence = bb.getInt();
......@@ -405,11 +406,12 @@ public class TCP extends BasePacket {
this.options = null;
}
}
this.payload = new Data();
int remLength = bb.limit()-bb.position();
this.payload = payload.deserialize(data, bb.position(), remLength);
this.payload.setParent(this);
return this;
}
}
}
......@@ -341,7 +341,6 @@ public abstract class ForwardingBase implements IOFMessageListener {
}
pob.setInPort((pi.getVersion().compareTo(OFVersion.OF_12) < 0 ? pi.getInPort() : pi.getMatch().get(MatchField.IN_PORT)));
messageDamper.write(sw, pob.build());
}
......
......@@ -50,10 +50,10 @@ net.floodlightcontroller.core.internal.OFSwitchManager.defaultMaxTablesToReceive
net.floodlightcontroller.core.internal.OFSwitchManager.maxTablesToReceiveTableMissFlowPerDpid={"00:00:00:00:00:00:00:01":"1","2":"1"}
net.floodlightcontroller.core.internal.OFSwitchManager.clearTablesOnInitialHandshakeAsMaster=YES
net.floodlightcontroller.core.internal.OFSwitchManager.clearTablesOnEachTransitionToMaster=YES
net.floodlightcontroller.core.internal.OFSwitchManager.keyStorePath=/path/to.jecks
net.floodlightcontroller.core.internal.OFSwitchManager.keyStorePassword=PassFL
net.floodlightcontroller.core.internal.OFSwitchManager.keyStorePath=/home/diogo/Documents/fdl/floodlight/keystore.jks
net.floodlightcontroller.core.internal.OFSwitchManager.keyStorePassword=diogo123
net.floodlightcontroller.core.internal.OFSwitchManager.useSsl=NO
net.floodlightcontroller.core.internal.OFSwitchManager.supportedOpenFlowVersions=1.0, 1.1, 1.2, 1.3, 1.4, 1.5
net.floodlightcontroller.core.internal.OFSwitchManager.supportedOpenFlowVersions=1.0, 1.1, 1.2, 1.3
net.floodlightcontroller.core.internal.OFSwitchManager.switchesInitialState={"00:00:00:00:00:00:00:01":"ROLE_MASTER","00:00:00:00:00:00:00:02":"ROLE_MASTER", "00:00:00:00:00:00:00:03":"ROLE_MASTER", "00:00:00:00:00:00:00:04":"ROLE_MASTER","00:00:00:00:00:00:00:05":"ROLE_MASTER"}
net.floodlightcontroller.restserver.RestApiServer.keyStorePath=/path/to.jceks
net.floodlightcontroller.restserver.RestApiServer.keyStorePassword=Password
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment