Thomas Vachuska
Committed by Gerrit Code Review

Adding ability to push flow rule batches via REST API.

Change-Id: I60a9b121cd9cc35a9704e59b8f1fde413af0a72f
...@@ -41,6 +41,7 @@ import javax.ws.rs.core.UriBuilder; ...@@ -41,6 +41,7 @@ import javax.ws.rs.core.UriBuilder;
41 import javax.ws.rs.core.UriInfo; 41 import javax.ws.rs.core.UriInfo;
42 import java.io.IOException; 42 import java.io.IOException;
43 import java.io.InputStream; 43 import java.io.InputStream;
44 +import java.util.List;
44 import java.util.stream.StreamSupport; 45 import java.util.stream.StreamSupport;
45 46
46 /** 47 /**
...@@ -54,10 +55,11 @@ public class FlowsWebResource extends AbstractWebResource { ...@@ -54,10 +55,11 @@ public class FlowsWebResource extends AbstractWebResource {
54 UriInfo uriInfo; 55 UriInfo uriInfo;
55 56
56 public static final String DEVICE_NOT_FOUND = "Device is not found"; 57 public static final String DEVICE_NOT_FOUND = "Device is not found";
58 + public static final String FLOWS = "flows";
57 59
58 final FlowRuleService service = get(FlowRuleService.class); 60 final FlowRuleService service = get(FlowRuleService.class);
59 final ObjectNode root = mapper().createObjectNode(); 61 final ObjectNode root = mapper().createObjectNode();
60 - final ArrayNode flowsNode = root.putArray("flows"); 62 + final ArrayNode flowsNode = root.putArray(FLOWS);
61 63
62 /** 64 /**
63 * Get all flow entries. Returns array of all flow rules in the system. 65 * Get all flow entries. Returns array of all flow rules in the system.
...@@ -81,6 +83,34 @@ public class FlowsWebResource extends AbstractWebResource { ...@@ -81,6 +83,34 @@ public class FlowsWebResource extends AbstractWebResource {
81 } 83 }
82 84
83 /** 85 /**
86 + * Create new flow rules. Creates and installs a new flow rules.<br>
87 + * Instructions description:
88 + * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Instructions
89 + * <br>
90 + * Criteria description:
91 + * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Criteria
92 + *
93 + * @onos.rsModel FlowsBatchPost
94 + * @param stream flow rules JSON
95 + * @return status of the request - CREATED if the JSON is correct,
96 + * BAD_REQUEST if the JSON is invalid
97 + */
98 + @POST
99 + @Consumes(MediaType.APPLICATION_JSON)
100 + @Produces(MediaType.APPLICATION_JSON)
101 + public Response createFlows(InputStream stream) {
102 + try {
103 + ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
104 + ArrayNode flowsArray = (ArrayNode) jsonTree.get(FLOWS);
105 + List<FlowRule> rules = codec(FlowRule.class).decode(flowsArray, this);
106 + service.applyFlowRules(rules.toArray(new FlowRule[rules.size()]));
107 + } catch (IOException ex) {
108 + throw new IllegalArgumentException(ex);
109 + }
110 + return Response.ok().build();
111 + }
112 +
113 + /**
84 * Get flow entries of a device. Returns array of all flow rules for the 114 * Get flow entries of a device. Returns array of all flow rules for the
85 * specified device. 115 * specified device.
86 * @onos.rsModel Flows 116 * @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