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; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onlab.osgi.DefaultServiceDirectory; |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 22 | import org.onosproject.rest.AbstractWebResource; |
Hyunsun Moon | fd5a24e | 2016-10-19 19:15:48 -0700 | [diff] [blame] | 23 | import org.opencord.cordvtn.api.core.CordVtnAdminService; |
| 24 | import org.opencord.cordvtn.api.net.SubnetId; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 25 | import org.openstack4j.core.transport.ObjectMapperSingleton; |
| 26 | import org.openstack4j.model.network.Subnet; |
| 27 | import org.openstack4j.openstack.networking.domain.NeutronSubnet; |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
| 31 | import javax.ws.rs.Consumes; |
| 32 | import javax.ws.rs.DELETE; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 33 | import javax.ws.rs.GET; |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 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 | import java.util.Set; |
| 46 | |
| 47 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 48 | import static javax.ws.rs.core.Response.Status.NOT_FOUND; |
| 49 | import static javax.ws.rs.core.Response.created; |
| 50 | import static javax.ws.rs.core.Response.noContent; |
| 51 | import static javax.ws.rs.core.Response.status; |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 52 | |
| 53 | /** |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 54 | * Neutron ML2 mechanism driver implementation for the subnet resource. |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 55 | */ |
| 56 | @Path("subnets") |
| 57 | public class NeutronMl2SubnetsWebResource extends AbstractWebResource { |
| 58 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 59 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 60 | private static final String MESSAGE = "Received subnets %s request"; |
| 61 | private static final String SUBNET = "subnet"; |
| 62 | private static final String SUBNETS = "subnets"; |
| 63 | |
| 64 | private final CordVtnAdminService adminService = |
| 65 | DefaultServiceDirectory.getService(CordVtnAdminService.class); |
| 66 | |
| 67 | @Context |
| 68 | private UriInfo uriInfo; |
| 69 | |
| 70 | /** |
| 71 | * Creates a subnet from the JSON input stream. |
| 72 | * |
| 73 | * @param input subnet JSON input stream |
| 74 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 75 | * is invalid or duplicated subnet already exists |
| 76 | */ |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 77 | @POST |
| 78 | @Consumes(MediaType.APPLICATION_JSON) |
| 79 | @Produces(MediaType.APPLICATION_JSON) |
| 80 | public Response createSubnet(InputStream input) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 81 | log.trace(String.format(MESSAGE, "CREATE")); |
| 82 | |
| 83 | final NeutronSubnet subnet = readSubnet(input); |
| 84 | adminService.createSubnet(subnet); |
| 85 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 86 | .path(SUBNETS) |
| 87 | .path(subnet.getId()); |
| 88 | |
| 89 | // TODO fix networking-onos to send Network UPDATE when subnet created |
| 90 | return created(locationBuilder.build()).build(); |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 93 | /** |
| 94 | * Updates the subnet with the specified identifier. |
| 95 | * |
| 96 | * @param id subnet identifier |
| 97 | * @param input subnet JSON input stream |
| 98 | * @return 200 OK with the updated subnet, 400 BAD_REQUEST if the requested |
| 99 | * subnet does not exist |
| 100 | */ |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 101 | @PUT |
| 102 | @Path("{id}") |
| 103 | @Produces(MediaType.APPLICATION_JSON) |
| 104 | @Consumes(MediaType.APPLICATION_JSON) |
| 105 | public Response updateSubnet(@PathParam("id") String id, InputStream input) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 106 | log.trace(String.format(MESSAGE, "UPDATE " + id)); |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 107 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 108 | final NeutronSubnet subnet = readSubnet(input); |
| 109 | adminService.updateSubnet(subnet); |
| 110 | |
| 111 | ObjectNode result = this.mapper().createObjectNode(); |
| 112 | return ok(result.set(SUBNET, writeSubnet(subnet))).build(); |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 115 | /** |
| 116 | * Returns all subnets. |
| 117 | * |
| 118 | * @return 200 OK with set of subnets |
| 119 | */ |
| 120 | @GET |
| 121 | @Consumes(MediaType.APPLICATION_JSON) |
| 122 | @Produces(MediaType.APPLICATION_JSON) |
| 123 | public Response getSubnets() { |
| 124 | log.trace(String.format(MESSAGE, "GET")); |
| 125 | |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 126 | Set<Subnet> subnets = adminService.subnets(); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 127 | ArrayNode arrayNodes = mapper().createArrayNode(); |
| 128 | subnets.stream().forEach(subnet -> { |
| 129 | arrayNodes.add(writeSubnet(subnet)); |
| 130 | }); |
| 131 | |
| 132 | ObjectNode result = this.mapper().createObjectNode(); |
| 133 | return ok(result.set(SUBNETS, arrayNodes)).build(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Returns the subnet with the given subnet id. |
| 138 | * |
| 139 | * @param id subnet id |
| 140 | * @return 200 OK with the subnet, 404 NOT_FOUND if the subnet does not exist |
| 141 | */ |
| 142 | @GET |
| 143 | @Path("{id}") |
| 144 | @Consumes(MediaType.APPLICATION_JSON) |
| 145 | @Produces(MediaType.APPLICATION_JSON) |
| 146 | public Response getSubnet(@PathParam("id") String id) { |
| 147 | log.trace(String.format(MESSAGE, "GET " + id)); |
| 148 | |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 149 | Subnet subnet = adminService.subnet(SubnetId.of(id)); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 150 | if (subnet == null) { |
| 151 | return status(NOT_FOUND).build(); |
| 152 | } |
| 153 | |
| 154 | ObjectNode result = this.mapper().createObjectNode(); |
| 155 | return ok(result.set(SUBNET, writeSubnet(subnet))).build(); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Removes the subnet. |
| 160 | * |
| 161 | * @param id subnet identifier |
| 162 | * @return 204 NO_CONTENT, 400 BAD_REQUEST if the subnet does not exist |
| 163 | */ |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 164 | @DELETE |
| 165 | @Path("{id}") |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 166 | @Consumes(MediaType.APPLICATION_JSON) |
| 167 | @Produces(MediaType.APPLICATION_JSON) |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 168 | public Response deleteSubnet(@PathParam("id") String id) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 169 | log.trace(String.format(MESSAGE, "DELETE " + id)); |
| 170 | |
| 171 | adminService.removeSubnet(SubnetId.of(id)); |
| 172 | return noContent().build(); |
| 173 | } |
| 174 | |
| 175 | private NeutronSubnet readSubnet(InputStream input) { |
| 176 | try { |
| 177 | JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input); |
| 178 | log.trace(mapper().writeValueAsString(jsonTree)); |
| 179 | return ObjectMapperSingleton.getContext(NeutronSubnet.class) |
| 180 | .readerFor(NeutronSubnet.class) |
| 181 | .readValue(jsonTree); |
| 182 | } catch (Exception e) { |
| 183 | throw new IllegalArgumentException(); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | private ObjectNode writeSubnet(Subnet subnet) { |
| 188 | try { |
| 189 | String strSubnet = ObjectMapperSingleton.getContext(NeutronSubnet.class) |
| 190 | .writerFor(NeutronSubnet.class) |
| 191 | .writeValueAsString(subnet); |
| 192 | log.trace(strSubnet); |
| 193 | return (ObjectNode) mapper().readTree(strSubnet.getBytes()); |
| 194 | } catch (Exception e) { |
| 195 | throw new IllegalStateException(); |
| 196 | } |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 197 | } |
| 198 | } |