blob: 74b572cd12b431860789d1b4ee6b9d0243603084 [file] [log] [blame]
Thomas Vachuska9bb32352015-09-25 11:31:22 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska9bb32352015-09-25 11:31:22 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Ray Milkey85267002016-11-16 11:06:35 -080015 *
16 * Originally created by Pengfei Lu, Network and Cloud Computing Laboratory, Dalian University of Technology, China
17 * Advisers: Keqiu Li, Heng Qi and Haisheng Yu
18 * This work is supported by the State Key Program of National Natural Science of China(Grant No. 61432002)
19 * and Prospective Research Project on Future Networks in Jiangsu Future Networks Innovation Institute.
Thomas Vachuska9bb32352015-09-25 11:31:22 -070020 */
21package org.onosproject.acl;
22
23import com.fasterxml.jackson.databind.JsonNode;
24import com.fasterxml.jackson.databind.ObjectMapper;
25import com.fasterxml.jackson.databind.node.ArrayNode;
26import com.fasterxml.jackson.databind.node.ObjectNode;
27import org.onlab.packet.IPv4;
28import org.onlab.packet.Ip4Prefix;
Nitin Anandfb22901d2018-05-09 12:16:36 +053029import org.onlab.packet.MacAddress;
Thomas Vachuska9bb32352015-09-25 11:31:22 -070030import org.onosproject.rest.AbstractWebResource;
31
32import javax.ws.rs.Consumes;
33import javax.ws.rs.DELETE;
34import javax.ws.rs.GET;
35import javax.ws.rs.POST;
36import javax.ws.rs.Path;
37import javax.ws.rs.PathParam;
38import javax.ws.rs.core.MediaType;
39import javax.ws.rs.core.Response;
40import java.io.IOException;
41import java.io.InputStream;
42import java.net.URI;
43import java.net.URISyntaxException;
44import java.util.List;
45
Ray Milkey86ee5e82018-04-02 15:33:07 -070046import static org.onlab.util.Tools.readTreeFromStream;
47
Thomas Vachuska9bb32352015-09-25 11:31:22 -070048/**
49 * Manage ACL rules.
50 */
51@Path("rules")
52public class AclWebResource extends AbstractWebResource {
53
54 /**
55 * Get all ACL rules.
56 * Returns array of all ACL rules.
57 *
58 * @return 200 OK
59 */
60 @GET
61 public Response queryAclRule() {
62 List<AclRule> rules = get(AclService.class).getAclRules();
63 ObjectMapper mapper = new ObjectMapper();
64 ObjectNode root = mapper.createObjectNode();
65 ArrayNode arrayNode = mapper.createArrayNode();
66 for (AclRule rule : rules) {
67 ObjectNode node = mapper.createObjectNode();
68 node.put("id", rule.id().toString());
Nitin Anandfb22901d2018-05-09 12:16:36 +053069 if (rule.srcMac() != null) {
70 node.put("srcMac", rule.srcMac().toString());
71 }
72 if (rule.dstMac() != null) {
73 node.put("dstMac", rule.dstMac().toString());
74 }
Thomas Vachuska9bb32352015-09-25 11:31:22 -070075 if (rule.srcIp() != null) {
76 node.put("srcIp", rule.srcIp().toString());
77 }
78 if (rule.dstIp() != null) {
79 node.put("dstIp", rule.dstIp().toString());
80 }
Nitin Anandfb22901d2018-05-09 12:16:36 +053081 if (rule.dscp() != 0) {
82 node.put("dscp", rule.dscp());
83 }
Thomas Vachuska9bb32352015-09-25 11:31:22 -070084 if (rule.ipProto() != 0) {
85 switch (rule.ipProto()) {
86 case IPv4.PROTOCOL_ICMP:
87 node.put("ipProto", "ICMP");
88 break;
89 case IPv4.PROTOCOL_TCP:
90 node.put("ipProto", "TCP");
91 break;
92 case IPv4.PROTOCOL_UDP:
93 node.put("ipProto", "UDP");
94 break;
95 default:
96 break;
97 }
98 }
99 if (rule.dstTpPort() != 0) {
100 node.put("dstTpPort", rule.dstTpPort());
101 }
Nitin Anandfb22901d2018-05-09 12:16:36 +0530102 if (rule.srcTpPort() != 0) {
103 node.put("srcTpPort", rule.srcTpPort());
104 }
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700105 node.put("action", rule.action().toString());
106 arrayNode.add(node);
107 }
108 root.set("aclRules", arrayNode);
109 return Response.ok(root.toString(), MediaType.APPLICATION_JSON_TYPE).build();
110 }
111
112 /**
113 * Add a new ACL rule.
114 *
115 * @param stream JSON data describing the rule
116 * @return 200 OK
Brian O'Connor52515622015-10-09 17:03:44 -0700117 * @throws URISyntaxException uri syntax exception
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700118 */
119 @POST
120 @Consumes(MediaType.APPLICATION_JSON)
121 public Response addAclRule(InputStream stream) throws URISyntaxException {
122 AclRule newRule = jsonToRule(stream);
123 return get(AclService.class).addAclRule(newRule) ?
124 Response.created(new URI(newRule.id().toString())).build() :
125 Response.serverError().build();
126 }
127
128 /**
129 * Remove ACL rule.
130 *
131 * @param id ACL rule id (in hex string format)
Jian Lic2a542b2016-05-10 11:48:19 -0700132 * @return 204 NO CONTENT
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700133 */
134 @DELETE
135 @Path("{id}")
136 public Response removeAclRule(@PathParam("id") String id) {
137 RuleId ruleId = new RuleId(Long.parseLong(id.substring(2), 16));
138 get(AclService.class).removeAclRule(ruleId);
Jian Lic2a542b2016-05-10 11:48:19 -0700139 return Response.noContent().build();
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700140 }
141
142 /**
143 * Remove all ACL rules.
144 *
Jian Lic2a542b2016-05-10 11:48:19 -0700145 * @return 204 NO CONTENT
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700146 */
147 @DELETE
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800148 public Response clearAcl() {
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700149 get(AclService.class).clearAcl();
Jian Lic2a542b2016-05-10 11:48:19 -0700150 return Response.noContent().build();
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700151 }
152
153 /**
154 * Turns a JSON string into an ACL rule instance.
155 */
156 private AclRule jsonToRule(InputStream stream) {
157 JsonNode node;
158 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700159 node = readTreeFromStream(mapper(), stream);
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700160 } catch (IOException e) {
161 throw new IllegalArgumentException("Unable to parse ACL request", e);
162 }
163
164 AclRule.Builder rule = AclRule.builder();
165
166 String s = node.path("srcIp").asText(null);
167 if (s != null) {
168 rule.srcIp(Ip4Prefix.valueOf(s));
169 }
170
171 s = node.path("dstIp").asText(null);
172 if (s != null) {
173 rule.dstIp(Ip4Prefix.valueOf(s));
174 }
175
Nitin Anandfb22901d2018-05-09 12:16:36 +0530176 s = node.path("srcMac").asText(null);
177 if (s != null) {
178 rule.srcMac(MacAddress.valueOf(s));
179 }
180
181 s = node.path("dstMac").asText(null);
182 if (s != null) {
183 rule.dstMac(MacAddress.valueOf(s));
184 }
185
186 s = node.path("dscp").asText(null);
187 if (s != null) {
188 rule.dscp(Byte.valueOf(s));
189 }
190
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700191 s = node.path("ipProto").asText(null);
192 if (s != null) {
193 if ("TCP".equalsIgnoreCase(s)) {
194 rule.ipProto(IPv4.PROTOCOL_TCP);
195 } else if ("UDP".equalsIgnoreCase(s)) {
196 rule.ipProto(IPv4.PROTOCOL_UDP);
197 } else if ("ICMP".equalsIgnoreCase(s)) {
198 rule.ipProto(IPv4.PROTOCOL_ICMP);
199 } else {
200 throw new IllegalArgumentException("ipProto must be assigned to TCP, UDP, or ICMP");
201 }
202 }
203
204 int port = node.path("dstTpPort").asInt(0);
205 if (port > 0) {
206 rule.dstTpPort((short) port);
207 }
208
Nitin Anandfb22901d2018-05-09 12:16:36 +0530209 port = node.path("srcTpPort").asInt(0);
210 if (port > 0) {
211 rule.srcTpPort((short) port);
212 }
213
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700214 s = node.path("action").asText(null);
215 if (s != null) {
216 if ("allow".equalsIgnoreCase(s)) {
217 rule.action(AclRule.Action.ALLOW);
218 } else if ("deny".equalsIgnoreCase(s)) {
219 rule.action(AclRule.Action.DENY);
220 } else {
221 throw new IllegalArgumentException("action must be ALLOW or DENY");
222 }
223 }
224
225 return rule.build();
226 }
227
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800228}