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