blob: 0cda20b5d8b9e7532e1eca7b3a44b02ffea8157e [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;
29import org.onosproject.rest.AbstractWebResource;
30
31import javax.ws.rs.Consumes;
32import javax.ws.rs.DELETE;
33import javax.ws.rs.GET;
34import javax.ws.rs.POST;
35import javax.ws.rs.Path;
36import javax.ws.rs.PathParam;
37import javax.ws.rs.core.MediaType;
38import javax.ws.rs.core.Response;
39import java.io.IOException;
40import java.io.InputStream;
41import java.net.URI;
42import java.net.URISyntaxException;
43import java.util.List;
44
Ray Milkey86ee5e82018-04-02 15:33:07 -070045import static org.onlab.util.Tools.readTreeFromStream;
46
Thomas Vachuska9bb32352015-09-25 11:31:22 -070047/**
48 * Manage ACL rules.
49 */
50@Path("rules")
51public class AclWebResource extends AbstractWebResource {
52
53 /**
54 * Get all ACL rules.
55 * Returns array of all ACL rules.
56 *
57 * @return 200 OK
58 */
59 @GET
60 public Response queryAclRule() {
61 List<AclRule> rules = get(AclService.class).getAclRules();
62 ObjectMapper mapper = new ObjectMapper();
63 ObjectNode root = mapper.createObjectNode();
64 ArrayNode arrayNode = mapper.createArrayNode();
65 for (AclRule rule : rules) {
66 ObjectNode node = mapper.createObjectNode();
67 node.put("id", rule.id().toString());
68 if (rule.srcIp() != null) {
69 node.put("srcIp", rule.srcIp().toString());
70 }
71 if (rule.dstIp() != null) {
72 node.put("dstIp", rule.dstIp().toString());
73 }
74 if (rule.ipProto() != 0) {
75 switch (rule.ipProto()) {
76 case IPv4.PROTOCOL_ICMP:
77 node.put("ipProto", "ICMP");
78 break;
79 case IPv4.PROTOCOL_TCP:
80 node.put("ipProto", "TCP");
81 break;
82 case IPv4.PROTOCOL_UDP:
83 node.put("ipProto", "UDP");
84 break;
85 default:
86 break;
87 }
88 }
89 if (rule.dstTpPort() != 0) {
90 node.put("dstTpPort", rule.dstTpPort());
91 }
92 node.put("action", rule.action().toString());
93 arrayNode.add(node);
94 }
95 root.set("aclRules", arrayNode);
96 return Response.ok(root.toString(), MediaType.APPLICATION_JSON_TYPE).build();
97 }
98
99 /**
100 * Add a new ACL rule.
101 *
102 * @param stream JSON data describing the rule
103 * @return 200 OK
Brian O'Connor52515622015-10-09 17:03:44 -0700104 * @throws URISyntaxException uri syntax exception
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700105 */
106 @POST
107 @Consumes(MediaType.APPLICATION_JSON)
108 public Response addAclRule(InputStream stream) throws URISyntaxException {
109 AclRule newRule = jsonToRule(stream);
110 return get(AclService.class).addAclRule(newRule) ?
111 Response.created(new URI(newRule.id().toString())).build() :
112 Response.serverError().build();
113 }
114
115 /**
116 * Remove ACL rule.
117 *
118 * @param id ACL rule id (in hex string format)
Jian Lic2a542b2016-05-10 11:48:19 -0700119 * @return 204 NO CONTENT
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700120 */
121 @DELETE
122 @Path("{id}")
123 public Response removeAclRule(@PathParam("id") String id) {
124 RuleId ruleId = new RuleId(Long.parseLong(id.substring(2), 16));
125 get(AclService.class).removeAclRule(ruleId);
Jian Lic2a542b2016-05-10 11:48:19 -0700126 return Response.noContent().build();
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700127 }
128
129 /**
130 * Remove all ACL rules.
131 *
Jian Lic2a542b2016-05-10 11:48:19 -0700132 * @return 204 NO CONTENT
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700133 */
134 @DELETE
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800135 public Response clearAcl() {
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700136 get(AclService.class).clearAcl();
Jian Lic2a542b2016-05-10 11:48:19 -0700137 return Response.noContent().build();
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700138 }
139
140 /**
141 * Turns a JSON string into an ACL rule instance.
142 */
143 private AclRule jsonToRule(InputStream stream) {
144 JsonNode node;
145 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700146 node = readTreeFromStream(mapper(), stream);
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700147 } catch (IOException e) {
148 throw new IllegalArgumentException("Unable to parse ACL request", e);
149 }
150
151 AclRule.Builder rule = AclRule.builder();
152
153 String s = node.path("srcIp").asText(null);
154 if (s != null) {
155 rule.srcIp(Ip4Prefix.valueOf(s));
156 }
157
158 s = node.path("dstIp").asText(null);
159 if (s != null) {
160 rule.dstIp(Ip4Prefix.valueOf(s));
161 }
162
163 s = node.path("ipProto").asText(null);
164 if (s != null) {
165 if ("TCP".equalsIgnoreCase(s)) {
166 rule.ipProto(IPv4.PROTOCOL_TCP);
167 } else if ("UDP".equalsIgnoreCase(s)) {
168 rule.ipProto(IPv4.PROTOCOL_UDP);
169 } else if ("ICMP".equalsIgnoreCase(s)) {
170 rule.ipProto(IPv4.PROTOCOL_ICMP);
171 } else {
172 throw new IllegalArgumentException("ipProto must be assigned to TCP, UDP, or ICMP");
173 }
174 }
175
176 int port = node.path("dstTpPort").asInt(0);
177 if (port > 0) {
178 rule.dstTpPort((short) port);
179 }
180
181 s = node.path("action").asText(null);
182 if (s != null) {
183 if ("allow".equalsIgnoreCase(s)) {
184 rule.action(AclRule.Action.ALLOW);
185 } else if ("deny".equalsIgnoreCase(s)) {
186 rule.action(AclRule.Action.DENY);
187 } else {
188 throw new IllegalArgumentException("action must be ALLOW or DENY");
189 }
190 }
191
192 return rule.build();
193 }
194
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800195}