Thomas Vachuska

Adding ability to push flow rule batches via REST API.

Change-Id: I60a9b121cd9cc35a9704e59b8f1fde413af0a72f
...@@ -19,6 +19,7 @@ import java.io.IOException; ...@@ -19,6 +19,7 @@ import java.io.IOException;
19 import java.io.InputStream; 19 import java.io.InputStream;
20 import java.net.URI; 20 import java.net.URI;
21 import java.net.URISyntaxException; 21 import java.net.URISyntaxException;
22 +import java.util.List;
22 import java.util.stream.StreamSupport; 23 import java.util.stream.StreamSupport;
23 24
24 import javax.ws.rs.Consumes; 25 import javax.ws.rs.Consumes;
...@@ -50,11 +51,13 @@ import com.fasterxml.jackson.databind.node.ObjectNode; ...@@ -50,11 +51,13 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
50 51
51 @Path("flows") 52 @Path("flows")
52 public class FlowsWebResource extends AbstractWebResource { 53 public class FlowsWebResource extends AbstractWebResource {
54 +
53 public static final String DEVICE_NOT_FOUND = "Device is not found"; 55 public static final String DEVICE_NOT_FOUND = "Device is not found";
56 + public static final String FLOWS = "flows";
54 57
55 final FlowRuleService service = get(FlowRuleService.class); 58 final FlowRuleService service = get(FlowRuleService.class);
56 final ObjectNode root = mapper().createObjectNode(); 59 final ObjectNode root = mapper().createObjectNode();
57 - final ArrayNode flowsNode = root.putArray("flows"); 60 + final ArrayNode flowsNode = root.putArray(FLOWS);
58 61
59 /** 62 /**
60 * Get all flow entries. Returns array of all flow rules in the system. 63 * Get all flow entries. Returns array of all flow rules in the system.
...@@ -78,6 +81,34 @@ public class FlowsWebResource extends AbstractWebResource { ...@@ -78,6 +81,34 @@ public class FlowsWebResource extends AbstractWebResource {
78 } 81 }
79 82
80 /** 83 /**
84 + * Create new flow rules. Creates and installs a new flow rules.<br>
85 + * Instructions description:
86 + * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Instructions
87 + * <br>
88 + * Criteria description:
89 + * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Criteria
90 + *
91 + * @onos.rsModel FlowsBatchPost
92 + * @param stream flow rules JSON
93 + * @return status of the request - CREATED if the JSON is correct,
94 + * BAD_REQUEST if the JSON is invalid
95 + */
96 + @POST
97 + @Consumes(MediaType.APPLICATION_JSON)
98 + @Produces(MediaType.APPLICATION_JSON)
99 + public Response createFlows(InputStream stream) {
100 + try {
101 + ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
102 + ArrayNode flowsArray = (ArrayNode) jsonTree.get(FLOWS);
103 + List<FlowRule> rules = codec(FlowRule.class).decode(flowsArray, this);
104 + service.applyFlowRules(rules.toArray(new FlowRule[rules.size()]));
105 + } catch (IOException ex) {
106 + throw new IllegalArgumentException(ex);
107 + }
108 + return Response.ok().build();
109 + }
110 +
111 + /**
81 * Get flow entries of a device. Returns array of all flow rules for the 112 * Get flow entries of a device. Returns array of all flow rules for the
82 * specified device. 113 * specified device.
83 * @onos.rsModel Flows 114 * @onos.rsModel Flows
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
8 "flows": { 8 "flows": {
9 "type": "array", 9 "type": "array",
10 "xml": { 10 "xml": {
11 - "name": "hosts", 11 + "name": "flows",
12 "wrapped": true 12 "wrapped": true
13 }, 13 },
14 "items": { 14 "items": {
......
1 +{
2 + "type": "object",
3 + "title": "flows",
4 + "required": [
5 + "flows"
6 + ],
7 + "properties": {
8 + "flows": {
9 + "type": "array",
10 + "xml": {
11 + "name": "flows",
12 + "wrapped": true
13 + },
14 + "items": {
15 + "type": "object",
16 + "title": "flow",
17 + "required": [
18 + "priority",
19 + "timeout",
20 + "isPermanent",
21 + "deviceId"
22 + ],
23 + "properties": {
24 + "priority": {
25 + "type": "integer",
26 + "format": "int64",
27 + "example": 400000
28 + },
29 + "timeout": {
30 + "type": "integer",
31 + "format": "int64",
32 + "example": 0
33 + },
34 + "isPermanent": {
35 + "type": "boolean",
36 + "example": true
37 + },
38 + "deviceId": {
39 + "type": "string",
40 + "example": "of:0000000000000001"
41 + },
42 + "treatment": {
43 + "type": "object",
44 + "title": "treatment",
45 + "required": [
46 + "instructions",
47 + "deferred"
48 + ],
49 + "properties": {
50 + "instructions": {
51 + "type": "array",
52 + "title": "treatment",
53 + "required": [
54 + "properties",
55 + "port"
56 + ],
57 + "items": {
58 + "type": "object",
59 + "title": "instructions",
60 + "required": [
61 + "type",
62 + "port"
63 + ],
64 + "properties": {
65 + "type": {
66 + "type": "string",
67 + "example": "OUTPUT"
68 + },
69 + "port": {
70 + "type": "string",
71 + "example": "CONTROLLER"
72 + }
73 + }
74 + }
75 + }
76 + }
77 + },
78 + "selector": {
79 + "type": "object",
80 + "title": "selector",
81 + "required": [
82 + "criteria"
83 + ],
84 + "properties": {
85 + "criteria": {
86 + "type": "array",
87 + "xml": {
88 + "name": "criteria",
89 + "wrapped": true
90 + },
91 + "items": {
92 + "type": "object",
93 + "title": "criteria",
94 + "properties": {
95 + "type": {
96 + "type": "string",
97 + "description": "Ethernet field name",
98 + "example": "ETH_TYPE"
99 + },
100 + "ethType": {
101 + "type": "int64",
102 + "format": "int64",
103 + "example": "0x88cc",
104 + "description": "Ethernet frame type"
105 + }
106 + }
107 + }
108 + }
109 + }
110 + }
111 + }
112 + }
113 + }
114 + }
115 +}
...\ No newline at end of file ...\ No newline at end of file