Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 8e57fd5 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [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. |
| 15 | */ |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 16 | package org.opencord.cordvtn.rest; |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 17 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 19 | import org.onlab.osgi.DefaultServiceDirectory; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onlab.packet.IpPrefix; |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 22 | import org.onosproject.rest.AbstractWebResource; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 23 | import org.opencord.cordvtn.api.core.ServiceNetworkAdminService; |
| 24 | import org.opencord.cordvtn.api.net.NetworkId; |
| 25 | import org.opencord.cordvtn.api.net.ServiceNetwork; |
| 26 | import org.opencord.cordvtn.impl.DefaultServiceNetwork; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 27 | import org.openstack4j.core.transport.ObjectMapperSingleton; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 28 | import org.openstack4j.openstack.networking.domain.NeutronSubnet; |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import javax.ws.rs.Consumes; |
| 33 | import javax.ws.rs.DELETE; |
| 34 | import javax.ws.rs.POST; |
| 35 | import javax.ws.rs.PUT; |
| 36 | import javax.ws.rs.Path; |
| 37 | import javax.ws.rs.PathParam; |
| 38 | import javax.ws.rs.Produces; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 39 | import javax.ws.rs.core.Context; |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 40 | import javax.ws.rs.core.MediaType; |
| 41 | import javax.ws.rs.core.Response; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 42 | import javax.ws.rs.core.UriBuilder; |
| 43 | import javax.ws.rs.core.UriInfo; |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 44 | import java.io.InputStream; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 45 | |
| 46 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 47 | import static javax.ws.rs.core.Response.Status.OK; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 48 | import static javax.ws.rs.core.Response.created; |
| 49 | import static javax.ws.rs.core.Response.noContent; |
| 50 | import static javax.ws.rs.core.Response.status; |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 51 | |
| 52 | /** |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 53 | * Neutron ML2 mechanism driver implementation for the subnet resource. |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 54 | */ |
| 55 | @Path("subnets") |
| 56 | public class NeutronMl2SubnetsWebResource extends AbstractWebResource { |
| 57 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 58 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 59 | private static final String MESSAGE = "Received subnets %s request"; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 60 | private static final String SUBNETS = "subnets"; |
| 61 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 62 | private final ServiceNetworkAdminService adminService = |
| 63 | DefaultServiceDirectory.getService(ServiceNetworkAdminService.class); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 64 | |
| 65 | @Context |
| 66 | private UriInfo uriInfo; |
| 67 | |
| 68 | /** |
| 69 | * Creates a subnet from the JSON input stream. |
| 70 | * |
| 71 | * @param input subnet JSON input stream |
| 72 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 73 | * is invalid or duplicated subnet already exists |
| 74 | */ |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 75 | @POST |
| 76 | @Consumes(MediaType.APPLICATION_JSON) |
| 77 | @Produces(MediaType.APPLICATION_JSON) |
| 78 | public Response createSubnet(InputStream input) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 79 | log.trace(String.format(MESSAGE, "CREATE")); |
| 80 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 81 | // FIXME do not allow more than one subnet per network |
| 82 | final ServiceNetwork snet = readSubnet(input); |
| 83 | adminService.updateServiceNetwork(snet); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 84 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 85 | .path(SUBNETS) |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 86 | .path(snet.id().id()); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 87 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 88 | // TODO fix Neutron networking-onos to send Network UPDATE when subnet created |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 89 | return created(locationBuilder.build()).build(); |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 92 | /** |
| 93 | * Updates the subnet with the specified identifier. |
| 94 | * |
| 95 | * @param id subnet identifier |
| 96 | * @param input subnet JSON input stream |
| 97 | * @return 200 OK with the updated subnet, 400 BAD_REQUEST if the requested |
| 98 | * subnet does not exist |
| 99 | */ |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 100 | @PUT |
| 101 | @Path("{id}") |
| 102 | @Produces(MediaType.APPLICATION_JSON) |
| 103 | @Consumes(MediaType.APPLICATION_JSON) |
| 104 | public Response updateSubnet(@PathParam("id") String id, InputStream input) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 105 | log.trace(String.format(MESSAGE, "UPDATE " + id)); |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 106 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 107 | final ServiceNetwork snet = readSubnet(input); |
| 108 | adminService.updateServiceNetwork(snet); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 109 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 110 | return status(OK).build(); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Removes the subnet. |
| 115 | * |
| 116 | * @param id subnet identifier |
| 117 | * @return 204 NO_CONTENT, 400 BAD_REQUEST if the subnet does not exist |
| 118 | */ |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 119 | @DELETE |
| 120 | @Path("{id}") |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 121 | @Consumes(MediaType.APPLICATION_JSON) |
| 122 | @Produces(MediaType.APPLICATION_JSON) |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 123 | public Response deleteSubnet(@PathParam("id") String id) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 124 | log.trace(String.format(MESSAGE, "DELETE " + id)); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 125 | return noContent().build(); |
| 126 | } |
| 127 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 128 | private ServiceNetwork readSubnet(InputStream input) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 129 | try { |
| 130 | JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input); |
| 131 | log.trace(mapper().writeValueAsString(jsonTree)); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 132 | NeutronSubnet osSubnet = ObjectMapperSingleton.getContext(NeutronSubnet.class) |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 133 | .readerFor(NeutronSubnet.class) |
| 134 | .readValue(jsonTree); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 135 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 136 | return DefaultServiceNetwork.builder() |
| 137 | .id(NetworkId.of(osSubnet.getNetworkId())) |
| 138 | .subnet(IpPrefix.valueOf(osSubnet.getCidr())) |
| 139 | .serviceIp(IpAddress.valueOf(osSubnet.getGateway())) |
| 140 | .build(); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 141 | } catch (Exception e) { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 142 | throw new IllegalArgumentException(e); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 143 | } |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 144 | } |
| 145 | } |