Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * 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 | */ |
| 20 | package org.onosproject.acl; |
| 21 | |
| 22 | import com.fasterxml.jackson.databind.JsonNode; |
| 23 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 24 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 25 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 26 | import org.onlab.packet.IPv4; |
| 27 | import org.onlab.packet.Ip4Prefix; |
| 28 | import org.onosproject.rest.AbstractWebResource; |
| 29 | |
| 30 | import javax.ws.rs.Consumes; |
| 31 | import javax.ws.rs.DELETE; |
| 32 | import javax.ws.rs.GET; |
| 33 | import javax.ws.rs.POST; |
| 34 | import javax.ws.rs.Path; |
| 35 | import javax.ws.rs.PathParam; |
| 36 | import javax.ws.rs.core.MediaType; |
| 37 | import javax.ws.rs.core.Response; |
| 38 | import java.io.IOException; |
| 39 | import java.io.InputStream; |
| 40 | import java.net.URI; |
| 41 | import java.net.URISyntaxException; |
| 42 | import java.util.List; |
| 43 | |
| 44 | /** |
| 45 | * Manage ACL rules. |
| 46 | */ |
| 47 | @Path("rules") |
| 48 | public 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 |
| 101 | */ |
| 102 | @POST |
| 103 | @Consumes(MediaType.APPLICATION_JSON) |
| 104 | public Response addAclRule(InputStream stream) throws URISyntaxException { |
| 105 | AclRule newRule = jsonToRule(stream); |
| 106 | return get(AclService.class).addAclRule(newRule) ? |
| 107 | Response.created(new URI(newRule.id().toString())).build() : |
| 108 | Response.serverError().build(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Remove ACL rule. |
| 113 | * |
| 114 | * @param id ACL rule id (in hex string format) |
| 115 | * @return 200 OK |
| 116 | */ |
| 117 | @DELETE |
| 118 | @Path("{id}") |
| 119 | public Response removeAclRule(@PathParam("id") String id) { |
| 120 | RuleId ruleId = new RuleId(Long.parseLong(id.substring(2), 16)); |
| 121 | get(AclService.class).removeAclRule(ruleId); |
| 122 | return Response.ok().build(); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Remove all ACL rules. |
| 127 | * |
| 128 | * @return 200 OK |
| 129 | */ |
| 130 | @DELETE |
| 131 | public Response clearACL() { |
| 132 | get(AclService.class).clearAcl(); |
| 133 | return Response.ok().build(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Turns a JSON string into an ACL rule instance. |
| 138 | */ |
| 139 | private AclRule jsonToRule(InputStream stream) { |
| 140 | JsonNode node; |
| 141 | try { |
| 142 | node = mapper().readTree(stream); |
| 143 | } catch (IOException e) { |
| 144 | throw new IllegalArgumentException("Unable to parse ACL request", e); |
| 145 | } |
| 146 | |
| 147 | AclRule.Builder rule = AclRule.builder(); |
| 148 | |
| 149 | String s = node.path("srcIp").asText(null); |
| 150 | if (s != null) { |
| 151 | rule.srcIp(Ip4Prefix.valueOf(s)); |
| 152 | } |
| 153 | |
| 154 | s = node.path("dstIp").asText(null); |
| 155 | if (s != null) { |
| 156 | rule.dstIp(Ip4Prefix.valueOf(s)); |
| 157 | } |
| 158 | |
| 159 | s = node.path("ipProto").asText(null); |
| 160 | if (s != null) { |
| 161 | if ("TCP".equalsIgnoreCase(s)) { |
| 162 | rule.ipProto(IPv4.PROTOCOL_TCP); |
| 163 | } else if ("UDP".equalsIgnoreCase(s)) { |
| 164 | rule.ipProto(IPv4.PROTOCOL_UDP); |
| 165 | } else if ("ICMP".equalsIgnoreCase(s)) { |
| 166 | rule.ipProto(IPv4.PROTOCOL_ICMP); |
| 167 | } else { |
| 168 | throw new IllegalArgumentException("ipProto must be assigned to TCP, UDP, or ICMP"); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | int port = node.path("dstTpPort").asInt(0); |
| 173 | if (port > 0) { |
| 174 | rule.dstTpPort((short) port); |
| 175 | } |
| 176 | |
| 177 | s = node.path("action").asText(null); |
| 178 | if (s != null) { |
| 179 | if ("allow".equalsIgnoreCase(s)) { |
| 180 | rule.action(AclRule.Action.ALLOW); |
| 181 | } else if ("deny".equalsIgnoreCase(s)) { |
| 182 | rule.action(AclRule.Action.DENY); |
| 183 | } else { |
| 184 | throw new IllegalArgumentException("action must be ALLOW or DENY"); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | return rule.build(); |
| 189 | } |
| 190 | |
| 191 | } |