xuzhang | 91b2ff4 | 2015-08-11 11:05:19 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.vtnweb.resources; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.io.InputStream; |
| 23 | import java.util.Collections; |
| 24 | import java.util.HashMap; |
| 25 | import java.util.HashSet; |
| 26 | import java.util.Map; |
| 27 | import java.util.Set; |
| 28 | import java.util.concurrent.ConcurrentMap; |
| 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.PUT; |
| 35 | import javax.ws.rs.Path; |
| 36 | import javax.ws.rs.PathParam; |
| 37 | import javax.ws.rs.Produces; |
| 38 | import javax.ws.rs.core.MediaType; |
| 39 | import javax.ws.rs.core.Response; |
| 40 | |
| 41 | import org.onlab.packet.IpAddress; |
| 42 | import org.onlab.packet.IpAddress.Version; |
| 43 | import org.onlab.packet.IpPrefix; |
| 44 | import org.onlab.util.ItemNotFoundException; |
| 45 | import org.onosproject.app.vtnrsc.AllocationPool; |
| 46 | import org.onosproject.app.vtnrsc.DefaultAllocationPool; |
| 47 | import org.onosproject.app.vtnrsc.DefaultHostRoute; |
| 48 | import org.onosproject.app.vtnrsc.DefaultSubnet; |
| 49 | import org.onosproject.app.vtnrsc.HostRoute; |
| 50 | import org.onosproject.app.vtnrsc.Subnet; |
| 51 | import org.onosproject.app.vtnrsc.Subnet.Mode; |
| 52 | import org.onosproject.app.vtnrsc.SubnetId; |
| 53 | import org.onosproject.app.vtnrsc.TenantId; |
| 54 | import org.onosproject.app.vtnrsc.TenantNetworkId; |
| 55 | import org.onosproject.app.vtnrsc.subnet.SubnetService; |
| 56 | import org.onosproject.app.vtnrsc.web.SubnetCodec; |
| 57 | import org.onosproject.rest.AbstractWebResource; |
| 58 | import org.slf4j.Logger; |
| 59 | import org.slf4j.LoggerFactory; |
| 60 | |
| 61 | import com.fasterxml.jackson.databind.JsonNode; |
| 62 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 63 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 64 | import com.google.common.collect.Maps; |
| 65 | |
| 66 | @Path("subnets") |
| 67 | public class SubnetWebResource extends AbstractWebResource { |
| 68 | private final Logger log = LoggerFactory.getLogger(SubnetWebResource.class); |
| 69 | public static final String SUBNET_NOT_CREATE = "Subnets is failed to create!"; |
| 70 | public static final String SUBNET_NOT_FOUND = "Subnets is failed to update!"; |
| 71 | public static final String JSON_NOT_NULL = "JsonNode can not be null"; |
| 72 | |
| 73 | @GET |
| 74 | @Produces(MediaType.APPLICATION_JSON) |
| 75 | public Response listSubnets() { |
| 76 | Iterable<Subnet> subnets = get(SubnetService.class).getSubnets(); |
| 77 | ObjectNode result = new ObjectMapper().createObjectNode(); |
| 78 | result.set("subnets", new SubnetCodec().encode(subnets, this)); |
| 79 | return ok(result.toString()).build(); |
| 80 | } |
| 81 | |
| 82 | @GET |
| 83 | @Path("{subnetUUID}") |
| 84 | @Produces(MediaType.APPLICATION_JSON) |
| 85 | public Response getSubnet(@PathParam("subnetUUID") String id) { |
| 86 | |
| 87 | if (!get(SubnetService.class).exists(SubnetId.subnetId(id))) { |
| 88 | return ok("the subnet does not exists").build(); |
| 89 | } |
| 90 | Subnet sub = nullIsNotFound(get(SubnetService.class) |
| 91 | .getSubnet(SubnetId.subnetId(id)), |
| 92 | SUBNET_NOT_FOUND); |
| 93 | |
| 94 | ObjectNode result = new ObjectMapper().createObjectNode(); |
| 95 | result.set("subnet", new SubnetCodec().encode(sub, this)); |
| 96 | return ok(result.toString()).build(); |
| 97 | } |
| 98 | |
| 99 | @POST |
| 100 | @Produces(MediaType.APPLICATION_JSON) |
| 101 | @Consumes(MediaType.APPLICATION_JSON) |
| 102 | public Response createSubnet(final InputStream input) { |
| 103 | |
| 104 | try { |
| 105 | ObjectMapper mapper = new ObjectMapper(); |
| 106 | JsonNode subnode = mapper.readTree(input); |
| 107 | Iterable<Subnet> subnets = createOrUpdateByInputStream(subnode); |
| 108 | Boolean result = nullIsNotFound((get(SubnetService.class) |
| 109 | .createSubnets(subnets)), |
| 110 | SUBNET_NOT_CREATE); |
| 111 | |
| 112 | if (!result) { |
| 113 | return Response.status(204).entity(SUBNET_NOT_CREATE).build(); |
| 114 | } |
| 115 | return Response.status(202).entity(result.toString()).build(); |
| 116 | } catch (Exception e) { |
| 117 | return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 118 | .build(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | @PUT |
| 123 | @Path("{subnetUUID}") |
| 124 | @Produces(MediaType.APPLICATION_JSON) |
| 125 | @Consumes(MediaType.APPLICATION_JSON) |
| 126 | public Response updateSubnet(@PathParam("id") String id, |
| 127 | final InputStream input) { |
| 128 | try { |
| 129 | ObjectMapper mapper = new ObjectMapper(); |
| 130 | JsonNode subnode = mapper.readTree(input); |
| 131 | Iterable<Subnet> subnets = createOrUpdateByInputStream(subnode); |
| 132 | Boolean result = nullIsNotFound(get(SubnetService.class) |
| 133 | .updateSubnets(subnets), SUBNET_NOT_FOUND); |
| 134 | if (!result) { |
| 135 | return Response.status(204).entity(SUBNET_NOT_FOUND).build(); |
| 136 | } |
| 137 | return Response.status(203).entity(result.toString()).build(); |
| 138 | } catch (Exception e) { |
| 139 | return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 140 | .build(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | @Path("{subnetUUID}") |
| 145 | @DELETE |
| 146 | public Response deleteSingleSubnet(@PathParam("subnetUUID") String id) |
| 147 | throws IOException { |
| 148 | try { |
| 149 | SubnetId subId = SubnetId.subnetId(id); |
| 150 | Set<SubnetId> subIds = new HashSet<SubnetId>(); |
| 151 | subIds.add(subId); |
| 152 | get(SubnetService.class).removeSubnets(subIds); |
| 153 | return Response.status(201).entity("SUCCESS").build(); |
| 154 | } catch (Exception e) { |
| 155 | return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 156 | .build(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | private Iterable<Subnet> createOrUpdateByInputStream(JsonNode subnode) { |
| 161 | checkNotNull(subnode, JSON_NOT_NULL); |
| 162 | Iterable<Subnet> subnets = null; |
| 163 | JsonNode subnetNodes = subnode.get("subnets"); |
| 164 | if (subnetNodes == null) { |
| 165 | subnetNodes = subnode.get("subnet"); |
| 166 | } |
| 167 | log.debug("subnetNodes is {}", subnetNodes.toString()); |
| 168 | if (subnetNodes.isArray()) { |
| 169 | subnets = changeJsonToSubs(subnetNodes); |
| 170 | } else { |
| 171 | subnets = changeJsonToSub(subnetNodes); |
| 172 | } |
| 173 | return subnets; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Returns a collection of subnets from subnetNodes. |
| 178 | * |
| 179 | * @param subnetNodes the subnet json node |
| 180 | * @return subnets a collection of subnets |
| 181 | */ |
| 182 | public Iterable<Subnet> changeJsonToSubs(JsonNode subnetNodes) { |
| 183 | checkNotNull(subnetNodes, JSON_NOT_NULL); |
| 184 | Map<SubnetId, Subnet> subMap = new HashMap<SubnetId, Subnet>(); |
| 185 | for (JsonNode subnetNode : subnetNodes) { |
| 186 | if (subnetNode.hasNonNull("id")) { |
| 187 | return null; |
| 188 | } |
| 189 | SubnetId id = SubnetId.subnetId(subnetNode.get("id").asText()); |
| 190 | String subnetName = subnetNode.get("name").asText(); |
| 191 | TenantId tenantId = TenantId.tenantId(subnetNode.get("tenant_id") |
| 192 | .asText()); |
| 193 | TenantNetworkId networkId = TenantNetworkId.networkId(subnetNode |
| 194 | .get("network_id").asText()); |
| 195 | Version ipVersion = Version.valueOf(subnetNode.get("ip_version") |
| 196 | .asText()); |
| 197 | IpPrefix cidr = IpPrefix.valueOf(subnetNode.get("cidr").asText()); |
| 198 | IpAddress gatewayIp = IpAddress.valueOf(subnetNode |
| 199 | .get("gateway_ip").asText()); |
| 200 | Boolean dhcpEnabled = subnetNode.get("enable_dhcp").asBoolean(); |
| 201 | Boolean shared = subnetNode.get("shared").asBoolean(); |
| 202 | JsonNode hostRoutes = subnetNode.get("host_routes"); |
| 203 | Iterable<HostRoute> hostRoutesIt = jsonNodeToHostRoutes(hostRoutes); |
| 204 | JsonNode allocationPools = subnetNode.get("allocation_pools"); |
| 205 | Iterable<AllocationPool> allocationPoolsIt = jsonNodeToAllocationPools(allocationPools); |
| 206 | Mode ipV6AddressMode = Mode.valueOf(subnetNode |
| 207 | .get("ipv6_address_mode").asText()); |
| 208 | Mode ipV6RaMode = Mode.valueOf(subnetNode.get("ipv6_ra_mode") |
| 209 | .asText()); |
| 210 | Subnet subnet = new DefaultSubnet(id, subnetName, networkId, |
| 211 | tenantId, ipVersion, cidr, |
| 212 | gatewayIp, dhcpEnabled, shared, |
| 213 | hostRoutesIt, ipV6AddressMode, |
| 214 | ipV6RaMode, allocationPoolsIt); |
| 215 | subMap.put(id, subnet); |
| 216 | } |
| 217 | return Collections.unmodifiableCollection(subMap.values()); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Returns a collection of subnets from subnetNodes. |
| 222 | * |
| 223 | * @param subnetNodes the subnet json node |
| 224 | * @return subnets a collection of subnets |
| 225 | */ |
| 226 | public Iterable<Subnet> changeJsonToSub(JsonNode subnetNodes) { |
| 227 | checkNotNull(subnetNodes, JSON_NOT_NULL); |
| 228 | Map<SubnetId, Subnet> subMap = new HashMap<SubnetId, Subnet>(); |
| 229 | SubnetId id = SubnetId.subnetId(subnetNodes.get("id").asText()); |
| 230 | String subnetName = subnetNodes.get("name").asText(); |
| 231 | TenantId tenantId = TenantId.tenantId(subnetNodes.get("tenant_id") |
| 232 | .asText()); |
| 233 | TenantNetworkId networkId = TenantNetworkId.networkId(subnetNodes |
| 234 | .get("network_id").asText()); |
| 235 | Version ipVersion = Version.valueOf(subnetNodes.get("ip_version") |
| 236 | .asText()); |
| 237 | IpPrefix cidr = IpPrefix.valueOf(subnetNodes.get("cidr").asText()); |
| 238 | IpAddress gatewayIp = IpAddress.valueOf(subnetNodes.get("gateway_ip") |
| 239 | .asText()); |
| 240 | Boolean dhcpEnabled = subnetNodes.get("enable_dhcp").asBoolean(); |
| 241 | Boolean shared = subnetNodes.get("shared").asBoolean(); |
| 242 | JsonNode hostRoutes = subnetNodes.get("host_routes"); |
| 243 | Iterable<HostRoute> hostRoutesIt = jsonNodeToHostRoutes(hostRoutes); |
| 244 | JsonNode allocationPools = subnetNodes.get("allocation_pools"); |
| 245 | Iterable<AllocationPool> allocationPoolsIt = jsonNodeToAllocationPools(allocationPools); |
| 246 | Mode ipV6AddressMode = Mode.valueOf(subnetNodes |
| 247 | .get("ipv6_address_mode").asText()); |
| 248 | Mode ipV6RaMode = Mode |
| 249 | .valueOf(subnetNodes.get("ipv6_ra_mode").asText()); |
| 250 | Subnet subnet = new DefaultSubnet(id, subnetName, networkId, tenantId, |
| 251 | ipVersion, cidr, gatewayIp, |
| 252 | dhcpEnabled, shared, hostRoutesIt, |
| 253 | ipV6AddressMode, ipV6RaMode, |
| 254 | allocationPoolsIt); |
| 255 | subMap.put(id, subnet); |
| 256 | return Collections.unmodifiableCollection(subMap.values()); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Changes JsonNode alocPools to a collection of the alocPools. |
| 261 | * |
| 262 | * @param allocationPools the allocationPools JsonNode |
| 263 | * @return a collection of allocationPools |
| 264 | */ |
| 265 | public Iterable<AllocationPool> jsonNodeToAllocationPools(JsonNode allocationPools) { |
| 266 | checkNotNull(allocationPools, JSON_NOT_NULL); |
| 267 | ConcurrentMap<Integer, AllocationPool> alocplMaps = Maps |
| 268 | .newConcurrentMap(); |
| 269 | Integer i = 0; |
| 270 | for (JsonNode node : allocationPools) { |
| 271 | IpAddress startIp = IpAddress.valueOf(node.get("start").asText()); |
| 272 | IpAddress endIp = IpAddress.valueOf(node.get("end").asText()); |
| 273 | AllocationPool alocPls = new DefaultAllocationPool(startIp, endIp); |
| 274 | alocplMaps.putIfAbsent(i, alocPls); |
| 275 | i++; |
| 276 | } |
| 277 | return Collections.unmodifiableCollection(alocplMaps.values()); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Changes hostRoutes JsonNode to a collection of the hostRoutes. |
| 282 | * |
| 283 | * @param hostRoutes the hostRoutes json node |
| 284 | * @return a collection of hostRoutes |
| 285 | */ |
| 286 | public Iterable<HostRoute> jsonNodeToHostRoutes(JsonNode hostRoutes) { |
| 287 | checkNotNull(hostRoutes, JSON_NOT_NULL); |
| 288 | ConcurrentMap<Integer, HostRoute> hostRouteMaps = Maps |
| 289 | .newConcurrentMap(); |
| 290 | Integer i = 0; |
| 291 | for (JsonNode node : hostRoutes) { |
| 292 | IpAddress nexthop = IpAddress.valueOf(node.get("nexthop").asText()); |
| 293 | IpPrefix destination = IpPrefix.valueOf(node.get("destination") |
| 294 | .asText()); |
| 295 | HostRoute hostRoute = new DefaultHostRoute(nexthop, destination); |
| 296 | hostRouteMaps.putIfAbsent(i, hostRoute); |
| 297 | i++; |
| 298 | } |
| 299 | return Collections.unmodifiableCollection(hostRouteMaps.values()); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Returns the specified item if that items is null; otherwise throws not |
| 304 | * found exception. |
| 305 | * |
| 306 | * @param item item to check |
| 307 | * @param <T> item type |
| 308 | * @param message not found message |
| 309 | * @return item if not null |
| 310 | * @throws org.onlab.util.ItemNotFoundException if item is null |
| 311 | */ |
| 312 | protected <T> T nullIsNotFound(T item, String message) { |
| 313 | if (item == null) { |
| 314 | throw new ItemNotFoundException(message); |
| 315 | } |
| 316 | return item; |
| 317 | } |
| 318 | |
| 319 | } |