blob: 2efe52a1f14deb6f8f26169962a60ad9deef1e54 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001package net.floodlightcontroller.virtualnetwork;
2
3import java.io.IOException;
4import java.util.Collection;
5
6import net.floodlightcontroller.packet.IPv4;
7
8import org.codehaus.jackson.JsonParseException;
9import org.codehaus.jackson.JsonParser;
10import org.codehaus.jackson.JsonToken;
11import org.codehaus.jackson.map.MappingJsonFactory;
12import org.restlet.data.Status;
13import org.restlet.resource.Delete;
14import org.restlet.resource.Get;
15import org.restlet.resource.Post;
16import org.restlet.resource.Put;
17import org.restlet.resource.ServerResource;
18import org.slf4j.Logger;
19import org.slf4j.LoggerFactory;
20
21public class NetworkResource extends ServerResource {
22 protected static Logger log = LoggerFactory.getLogger(NetworkResource.class);
23
24 public class NetworkDefinition {
25 public String name = null;
26 public String guid = null;
27 public String gateway = null;
28 }
29
30 protected void jsonToNetworkDefinition(String json, NetworkDefinition network) throws IOException {
31 MappingJsonFactory f = new MappingJsonFactory();
32 JsonParser jp;
33
34 try {
35 jp = f.createJsonParser(json);
36 } catch (JsonParseException e) {
37 throw new IOException(e);
38 }
39
40 jp.nextToken();
41 if (jp.getCurrentToken() != JsonToken.START_OBJECT) {
42 throw new IOException("Expected START_OBJECT");
43 }
44
45 while (jp.nextToken() != JsonToken.END_OBJECT) {
46 if (jp.getCurrentToken() != JsonToken.FIELD_NAME) {
47 throw new IOException("Expected FIELD_NAME");
48 }
49
50 String n = jp.getCurrentName();
51 jp.nextToken();
52 if (jp.getText().equals(""))
53 continue;
54 else if (n.equals("network")) {
55 while (jp.nextToken() != JsonToken.END_OBJECT) {
56 String field = jp.getCurrentName();
57 if (field.equals("name")) {
58 network.name = jp.getText();
59 } else if (field.equals("gateway")) {
60 String gw = jp.getText();
61 if ((gw != null) && (!gw.equals("null")))
62 network.gateway = gw;
63 } else if (field.equals("id")) {
64 network.guid = jp.getText();
65 } else {
66 log.warn("Unrecognized field {} in " +
67 "parsing network definition",
68 jp.getText());
69 }
70 }
71 }
72 }
73
74 jp.close();
75 }
76
77 @Get("json")
78 public Collection <VirtualNetwork> retrieve() {
79 IVirtualNetworkService vns =
80 (IVirtualNetworkService)getContext().getAttributes().
81 get(IVirtualNetworkService.class.getCanonicalName());
82
83 return vns.listNetworks();
84 }
85
86 @Put
87 @Post
88 public String createNetwork(String postData) {
89 NetworkDefinition network = new NetworkDefinition();
90 try {
91 jsonToNetworkDefinition(postData, network);
92 } catch (IOException e) {
93 log.error("Could not parse JSON {}", e.getMessage());
94 }
95
96 // We try to get the ID from the URI only if it's not
97 // in the POST data
98 if (network.guid == null) {
99 String guid = (String) getRequestAttributes().get("network");
100 if ((guid != null) && (!guid.equals("null")))
101 network.guid = guid;
102 }
103
104 IVirtualNetworkService vns =
105 (IVirtualNetworkService)getContext().getAttributes().
106 get(IVirtualNetworkService.class.getCanonicalName());
107
108 Integer gw = null;
109 if (network.gateway != null) {
110 try {
111 gw = IPv4.toIPv4Address(network.gateway);
112 } catch (IllegalArgumentException e) {
113 log.warn("Could not parse gateway {} as IP for network {}, setting as null",
114 network.gateway, network.name);
115 network.gateway = null;
116 }
117 }
118 vns.createNetwork(network.guid, network.name, gw);
119 setStatus(Status.SUCCESS_OK);
120 return "{\"status\":\"ok\"}";
121 }
122
123 @Delete
124 public String deleteNetwork() {
125 IVirtualNetworkService vns =
126 (IVirtualNetworkService)getContext().getAttributes().
127 get(IVirtualNetworkService.class.getCanonicalName());
128 String guid = (String) getRequestAttributes().get("network");
129 vns.deleteNetwork(guid);
130 setStatus(Status.SUCCESS_OK);
131 return "{\"status\":\"ok\"}";
132 }
133}