Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.virtualnetwork; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | |
| 5 | import net.floodlightcontroller.util.MACAddress; |
| 6 | |
| 7 | import org.codehaus.jackson.JsonParseException; |
| 8 | import org.codehaus.jackson.JsonParser; |
| 9 | import org.codehaus.jackson.JsonToken; |
| 10 | import org.codehaus.jackson.map.MappingJsonFactory; |
| 11 | import org.restlet.data.Status; |
| 12 | import org.restlet.resource.Delete; |
| 13 | import org.restlet.resource.Put; |
| 14 | import org.slf4j.Logger; |
| 15 | import org.slf4j.LoggerFactory; |
| 16 | |
| 17 | public class HostResource extends org.restlet.resource.ServerResource { |
| 18 | protected static Logger log = LoggerFactory.getLogger(HostResource.class); |
| 19 | |
| 20 | public class HostDefinition { |
| 21 | String port = null; // Logical port name |
| 22 | String guid = null; // Network ID |
| 23 | String mac = null; // MAC Address |
| 24 | String attachment = null; // Attachment name |
| 25 | } |
| 26 | |
| 27 | protected void jsonToHostDefinition(String json, HostDefinition host) throws IOException { |
| 28 | MappingJsonFactory f = new MappingJsonFactory(); |
| 29 | JsonParser jp; |
| 30 | |
| 31 | try { |
| 32 | jp = f.createJsonParser(json); |
| 33 | } catch (JsonParseException e) { |
| 34 | throw new IOException(e); |
| 35 | } |
| 36 | |
| 37 | jp.nextToken(); |
| 38 | if (jp.getCurrentToken() != JsonToken.START_OBJECT) { |
| 39 | throw new IOException("Expected START_OBJECT"); |
| 40 | } |
| 41 | |
| 42 | while (jp.nextToken() != JsonToken.END_OBJECT) { |
| 43 | if (jp.getCurrentToken() != JsonToken.FIELD_NAME) { |
| 44 | throw new IOException("Expected FIELD_NAME"); |
| 45 | } |
| 46 | |
| 47 | String n = jp.getCurrentName(); |
| 48 | jp.nextToken(); |
| 49 | if (jp.getText().equals("")) |
| 50 | continue; |
| 51 | else if (n.equals("attachment")) { |
| 52 | while (jp.nextToken() != JsonToken.END_OBJECT) { |
| 53 | String field = jp.getCurrentName(); |
| 54 | if (field.equals("id")) { |
| 55 | host.attachment = jp.getText(); |
| 56 | } else if (field.equals("mac")) { |
| 57 | host.mac = jp.getText(); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | jp.close(); |
| 64 | } |
| 65 | |
| 66 | @Put |
| 67 | public String addHost(String postData) { |
| 68 | IVirtualNetworkService vns = |
| 69 | (IVirtualNetworkService)getContext().getAttributes(). |
| 70 | get(IVirtualNetworkService.class.getCanonicalName()); |
| 71 | HostDefinition host = new HostDefinition(); |
| 72 | host.port = (String) getRequestAttributes().get("port"); |
| 73 | host.guid = (String) getRequestAttributes().get("network"); |
| 74 | try { |
| 75 | jsonToHostDefinition(postData, host); |
| 76 | } catch (IOException e) { |
| 77 | log.error("Could not parse JSON {}", e.getMessage()); |
| 78 | } |
| 79 | vns.addHost(MACAddress.valueOf(host.mac), host.guid, host.port); |
| 80 | setStatus(Status.SUCCESS_OK); |
| 81 | return "{\"status\":\"ok\"}"; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | @Delete |
| 86 | public String deleteHost() { |
| 87 | String port = (String) getRequestAttributes().get("port"); |
| 88 | IVirtualNetworkService vns = |
| 89 | (IVirtualNetworkService)getContext().getAttributes(). |
| 90 | get(IVirtualNetworkService.class.getCanonicalName()); |
| 91 | vns.deleteHost(null, port); |
| 92 | setStatus(Status.SUCCESS_OK); |
| 93 | return "{\"status\":\"ok\"}"; |
| 94 | } |
| 95 | } |