Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 3 | * |
| 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 Milkey | 8526700 | 2016-11-16 11:06:35 -0800 | [diff] [blame] | 15 | * |
| 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 Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 20 | */ |
| 21 | package org.onosproject.acl; |
| 22 | |
| 23 | import com.fasterxml.jackson.databind.JsonNode; |
| 24 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 25 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 26 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 27 | import org.onlab.packet.IPv4; |
| 28 | import org.onlab.packet.Ip4Prefix; |
| 29 | import org.onosproject.rest.AbstractWebResource; |
| 30 | |
| 31 | import javax.ws.rs.Consumes; |
| 32 | import javax.ws.rs.DELETE; |
| 33 | import javax.ws.rs.GET; |
| 34 | import javax.ws.rs.POST; |
| 35 | import javax.ws.rs.Path; |
| 36 | import javax.ws.rs.PathParam; |
| 37 | import javax.ws.rs.core.MediaType; |
| 38 | import javax.ws.rs.core.Response; |
| 39 | import java.io.IOException; |
| 40 | import java.io.InputStream; |
| 41 | import java.net.URI; |
| 42 | import java.net.URISyntaxException; |
| 43 | import java.util.List; |
| 44 | |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 45 | import static org.onlab.util.Tools.readTreeFromStream; |
| 46 | |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Manage ACL rules. |
| 49 | */ |
| 50 | @Path("rules") |
| 51 | public 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'Connor | 5251562 | 2015-10-09 17:03:44 -0700 | [diff] [blame] | 104 | * @throws URISyntaxException uri syntax exception |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 105 | */ |
| 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 Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 119 | * @return 204 NO CONTENT |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 120 | */ |
| 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 Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 126 | return Response.noContent().build(); |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Remove all ACL rules. |
| 131 | * |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 132 | * @return 204 NO CONTENT |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 133 | */ |
| 134 | @DELETE |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 135 | public Response clearAcl() { |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 136 | get(AclService.class).clearAcl(); |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 137 | return Response.noContent().build(); |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 138 | } |
| 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 Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 146 | node = readTreeFromStream(mapper(), stream); |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 147 | } 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 Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 195 | } |