blob: 9086f13a7cf58c4f2c2697a3a974bb5d02f1c6d7 [file] [log] [blame]
Thomas Vachuska9bb32352015-09-25 11:31:22 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska9bb32352015-09-25 11:31:22 -07003 * Originally created by Pengfei Lu, Network and Cloud Computing Laboratory, Dalian University of Technology, China
4 * Advisers: Keqiu Li, Heng Qi and Haisheng Yu
5 * This work is supported by the State Key Program of National Natural Science of China(Grant No. 61432002)
6 * and Prospective Research Project on Future Networks in Jiangsu Future Networks Innovation Institute.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20package org.onosproject.acl;
21
22import com.fasterxml.jackson.databind.JsonNode;
23import com.fasterxml.jackson.databind.ObjectMapper;
24import com.fasterxml.jackson.databind.node.ArrayNode;
25import com.fasterxml.jackson.databind.node.ObjectNode;
26import org.onlab.packet.IPv4;
27import org.onlab.packet.Ip4Prefix;
28import org.onosproject.rest.AbstractWebResource;
29
30import javax.ws.rs.Consumes;
31import javax.ws.rs.DELETE;
32import javax.ws.rs.GET;
33import javax.ws.rs.POST;
34import javax.ws.rs.Path;
35import javax.ws.rs.PathParam;
36import javax.ws.rs.core.MediaType;
37import javax.ws.rs.core.Response;
38import java.io.IOException;
39import java.io.InputStream;
40import java.net.URI;
41import java.net.URISyntaxException;
42import java.util.List;
43
44/**
45 * Manage ACL rules.
46 */
47@Path("rules")
48public class AclWebResource extends AbstractWebResource {
49
50 /**
51 * Get all ACL rules.
52 * Returns array of all ACL rules.
53 *
54 * @return 200 OK
55 */
56 @GET
57 public Response queryAclRule() {
58 List<AclRule> rules = get(AclService.class).getAclRules();
59 ObjectMapper mapper = new ObjectMapper();
60 ObjectNode root = mapper.createObjectNode();
61 ArrayNode arrayNode = mapper.createArrayNode();
62 for (AclRule rule : rules) {
63 ObjectNode node = mapper.createObjectNode();
64 node.put("id", rule.id().toString());
65 if (rule.srcIp() != null) {
66 node.put("srcIp", rule.srcIp().toString());
67 }
68 if (rule.dstIp() != null) {
69 node.put("dstIp", rule.dstIp().toString());
70 }
71 if (rule.ipProto() != 0) {
72 switch (rule.ipProto()) {
73 case IPv4.PROTOCOL_ICMP:
74 node.put("ipProto", "ICMP");
75 break;
76 case IPv4.PROTOCOL_TCP:
77 node.put("ipProto", "TCP");
78 break;
79 case IPv4.PROTOCOL_UDP:
80 node.put("ipProto", "UDP");
81 break;
82 default:
83 break;
84 }
85 }
86 if (rule.dstTpPort() != 0) {
87 node.put("dstTpPort", rule.dstTpPort());
88 }
89 node.put("action", rule.action().toString());
90 arrayNode.add(node);
91 }
92 root.set("aclRules", arrayNode);
93 return Response.ok(root.toString(), MediaType.APPLICATION_JSON_TYPE).build();
94 }
95
96 /**
97 * Add a new ACL rule.
98 *
99 * @param stream JSON data describing the rule
100 * @return 200 OK
Brian O'Connor52515622015-10-09 17:03:44 -0700101 * @throws URISyntaxException uri syntax exception
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700102 */
103 @POST
104 @Consumes(MediaType.APPLICATION_JSON)
105 public Response addAclRule(InputStream stream) throws URISyntaxException {
106 AclRule newRule = jsonToRule(stream);
107 return get(AclService.class).addAclRule(newRule) ?
108 Response.created(new URI(newRule.id().toString())).build() :
109 Response.serverError().build();
110 }
111
112 /**
113 * Remove ACL rule.
114 *
115 * @param id ACL rule id (in hex string format)
116 * @return 200 OK
117 */
118 @DELETE
119 @Path("{id}")
120 public Response removeAclRule(@PathParam("id") String id) {
121 RuleId ruleId = new RuleId(Long.parseLong(id.substring(2), 16));
122 get(AclService.class).removeAclRule(ruleId);
123 return Response.ok().build();
124 }
125
126 /**
127 * Remove all ACL rules.
128 *
129 * @return 200 OK
130 */
131 @DELETE
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800132 public Response clearAcl() {
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700133 get(AclService.class).clearAcl();
134 return Response.ok().build();
135 }
136
137 /**
138 * Turns a JSON string into an ACL rule instance.
139 */
140 private AclRule jsonToRule(InputStream stream) {
141 JsonNode node;
142 try {
143 node = mapper().readTree(stream);
144 } catch (IOException e) {
145 throw new IllegalArgumentException("Unable to parse ACL request", e);
146 }
147
148 AclRule.Builder rule = AclRule.builder();
149
150 String s = node.path("srcIp").asText(null);
151 if (s != null) {
152 rule.srcIp(Ip4Prefix.valueOf(s));
153 }
154
155 s = node.path("dstIp").asText(null);
156 if (s != null) {
157 rule.dstIp(Ip4Prefix.valueOf(s));
158 }
159
160 s = node.path("ipProto").asText(null);
161 if (s != null) {
162 if ("TCP".equalsIgnoreCase(s)) {
163 rule.ipProto(IPv4.PROTOCOL_TCP);
164 } else if ("UDP".equalsIgnoreCase(s)) {
165 rule.ipProto(IPv4.PROTOCOL_UDP);
166 } else if ("ICMP".equalsIgnoreCase(s)) {
167 rule.ipProto(IPv4.PROTOCOL_ICMP);
168 } else {
169 throw new IllegalArgumentException("ipProto must be assigned to TCP, UDP, or ICMP");
170 }
171 }
172
173 int port = node.path("dstTpPort").asInt(0);
174 if (port > 0) {
175 rule.dstTpPort((short) port);
176 }
177
178 s = node.path("action").asText(null);
179 if (s != null) {
180 if ("allow".equalsIgnoreCase(s)) {
181 rule.action(AclRule.Action.ALLOW);
182 } else if ("deny".equalsIgnoreCase(s)) {
183 rule.action(AclRule.Action.DENY);
184 } else {
185 throw new IllegalArgumentException("action must be ALLOW or DENY");
186 }
187 }
188
189 return rule.build();
190 }
191
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800192}