Skip to content
Snippets Groups Projects
Commit 36701f01 authored by kwanggithub's avatar kwanggithub
Browse files

create load balancer class and REST interface based on quantum LBaaS proposal...

create load balancer class and REST interface based on quantum LBaaS proposal http://wiki.openstack.org/Quantum/LBaaS/API_1.0
parent b628270d
No related branches found
No related tags found
No related merge requests found
Showing
with 437 additions and 0 deletions
package net.floodlightcontroller.loadbalancer;
import java.util.Collection;
public interface ILoadBalancerService {
/**
* List all current Vips.
*/
public Collection<LBVip> listVips();
/**
* List selected Vip by its ID.
* @param vipId Id of requested Vip
*/
public Collection<LBVip> listVip(String vipId);
/**
* Create and return a new Vip.
* @param LBVip vip: data structure with caller provided Vip attributes
* @return LBVip: Created Vip
*/
public LBVip createVip(LBVip vip);
/**
* Update and return an existing Vip.
* @param LBVip vip: data structure with caller provided Vip attributes
* @return LBVip: Updated Vip
*/
public LBVip updateVip(LBVip vip);
/**
* Remove an existing Vip.
* @param String vipId
* @return int: removal status
*/
public int removeVip(String vipId);
/**
* List all current pools.
*/
public Collection<LBPool> listPools();
/**
* List selected pool by its ID.
* @param poolId Id of requested pool
*/
public Collection<LBPool> listPool(String poolId);
/**
* Create and return a new pool.
* @param LBPool pool: data structure with caller provided pool attributes
* @return LBPool: Created pool
*/
public LBPool createPool(LBPool pool);
/**
* Update and return an existing pool.
* @param LBPool pool: data structure with caller provided pool attributes
* @return LBPool: Updated pool
*/
public LBPool updatePool(LBPool pool);
/**
* Remove an existing pool.
* @param String poolId
* @return int: removal status
*/
public int removePool(String poolId);
/**
* List all current members.
*/
public Collection<LBMember> listMembers();
/**
* List selected member by its ID.
* @param memberId Id of requested member
*/
public Collection<LBMember> listMember(String memberId);
/**
* Create and return a new member.
* @param LBMember member: data structure with caller provided member attributes
* @return LBMember: Created member
*/
public LBMember createMember(LBMember member);
/**
* Update and return an existing member.
* @param LBMember member: data structure with caller provided member attributes
* @return LBMember: Updated member
*/
public LBMember updateMember(LBMember member);
/**
* Remove an existing member.
* @param String memberId
* @return int: removal status
*/
public int removeMember(String memberId);
/**
* List all current monitors.
*/
public Collection<LBMonitor> listMonitors();
/**
* List selected monitor by its ID.
* @param monitorId Id of requested monitor
*/
public Collection<LBMonitor> listMonitor(String monitorId);
/**
* Create and return a new monitor.
* @param LBMonitor monitor: data structure with caller provided monitor attributes
* @return LBMonitor: Created monitor
*/
public LBMonitor createMonitor(LBMonitor monitor);
/**
* Update and return an existing monitor.
* @param LBMonitor monitor: data structure with caller provided pool attributes
* @return LBMonitor: Updated monitor
*/
public LBMonitor updateMonitor(LBMonitor monitor);
/**
* Remove an existing monitor.
* @param String monitorId
* @return int: removal status
*/
public int removeMonitor(String monitorId);
}
package net.floodlightcontroller.loadbalancer;
/**
* Data structure for Load Balancer based on
* Quantum proposal http://wiki.openstack.org/LBaaS/CoreResourceModel/proposal
*
* @author KC Wang
*/
public class LBMember {
protected String id;
protected int address;
protected short port;
protected int connectionLimit;
protected short adminState;
protected short status;
}
package net.floodlightcontroller.loadbalancer;
/**
* Data structure for Load Balancer based on
* Quantum proposal http://wiki.openstack.org/LBaaS/CoreResourceModel/proposal
*
* @author KC Wang
*/
public class LBMonitor {
protected String id;
protected String name;
protected short type;
protected short delay;
protected short timeout;
protected short attemptsBeforeDeactivation;
protected String netId;
protected int address;
protected byte protocol;
protected short port;
//protected path??
protected short adminState;
protected short status;
}
package net.floodlightcontroller.loadbalancer;
/**
* Data structure for Load Balancer based on
* Quantum proposal http://wiki.openstack.org/LBaaS/CoreResourceModel/proposal
*
* @author KC Wang
*/
public class LBPool {
protected String id;
protected String name;
protected String netId;
protected byte protocol;
protected LBMember[] members;
protected LBMonitor[] monitors;
protected short adminState;
protected short status;
}
package net.floodlightcontroller.loadbalancer;
/**
* Data structure for Load Balancer based on
* Quantum proposal http://wiki.openstack.org/LBaaS/CoreResourceModel/proposal
*
* @author KC Wang
*/
public class LBStats {
protected int bytesIn;
protected int bytesOut;
protected int activeConnections;
protected int totalConnections;
}
package net.floodlightcontroller.loadbalancer;
import java.util.ArrayList;
/**
* Data structure for Load Balancer based on
* Quantum proposal http://wiki.openstack.org/LBaaS/CoreResourceModel/proposal
*
* @author KC Wang
*/
public class LBVip {
protected String id;
protected String name;
protected String netId;
protected int address;
protected byte protocol;
protected short lbMethod;
protected short port;
protected ArrayList<String> pools;
protected boolean sessionPersistence;
protected int connectionLimit;
protected short adminState;
protected short status;
}
package net.floodlightcontroller.loadbalancer;
import org.restlet.Context;
import org.restlet.Restlet;
import org.restlet.routing.Router;
import net.floodlightcontroller.restserver.RestletRoutable;
import net.floodlightcontroller.virtualnetwork.NoOp;
public class LoadBalancerWebRoutable implements RestletRoutable {
@Override
public Restlet getRestlet(Context context) {
Router router = new Router(context);
router.attach("/vips/", VipsResource.class); // GET, POST
router.attach("/vips/{vip}", VipsResource.class); // GET, PUT, DELETE
router.attach("/pools/", PoolsResource.class); // GET, POST
router.attach("/pools/{pool}", PoolsResource.class); // GET, PUT, DELETE
router.attach("/members/", MembersResource.class); // GET, POST
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.attachDefault(NoOp.class);
return router;
}
@Override
public String basePath() {
return "/quantum/v1.0";
}
}
package net.floodlightcontroller.loadbalancer;
import org.restlet.resource.ServerResource;
public class MembersResource extends ServerResource {
}
package net.floodlightcontroller.loadbalancer;
import org.restlet.resource.ServerResource;
public class MonitorsResource extends ServerResource {
}
package net.floodlightcontroller.loadbalancer;
import org.restlet.resource.ServerResource;
public class PoolMemberResource extends ServerResource {
}
package net.floodlightcontroller.loadbalancer;
import org.restlet.resource.ServerResource;
public class PoolsResource extends ServerResource {
}
package net.floodlightcontroller.loadbalancer;
import java.io.IOException;
import java.util.Collection;
import net.floodlightcontroller.virtualnetwork.NetworkResource;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.MappingJsonFactory;
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;
public class VipsResource extends ServerResource {
protected static Logger log = LoggerFactory.getLogger(NetworkResource.class);
@Get("json")
public Collection <LBVip> retrieve() {
ILoadBalancerService lbs =
(ILoadBalancerService)getContext().getAttributes().
get(ILoadBalancerService.class.getCanonicalName());
String vipId = (String) getRequestAttributes().get("vip");
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 postData) {
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 {
MappingJsonFactory f = new MappingJsonFactory();
JsonParser jp;
LBVip vip = new LBVip();
try {
jp = f.createJsonParser(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;
else if (n.equals("vip")) {
while (jp.nextToken() != JsonToken.END_OBJECT) {
String field = jp.getCurrentName();
if (field.equals("tenant_id")) {
vip.id = jp.getText();
continue;
}
if (field.equals("name")) {
vip.name = jp.getText();
continue;
}
if (field.equals("network_id")) {
vip.netId = jp.getText();
continue;
}
if (field.equals("protocol")) {
vip.protocol = Byte.parseByte(jp.getText());
continue;
}
if (field.equals("port")) {
vip.port = Short.parseShort(jp.getText());
continue;
}
if (field.equals("pool_id")) {
vip.pools.add(jp.getText());
continue;
}
log.warn("Unrecognized field {} in " +
"parsing Vips",
jp.getText());
}
}
}
jp.close();
return vip;
}
}
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