Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 80dff97 | 2017-08-03 22:46:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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 | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -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.MacAddress; |
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.PortId; |
| 26 | import org.opencord.cordvtn.api.net.ServicePort; |
| 27 | import org.opencord.cordvtn.impl.DefaultServicePort; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 28 | import org.openstack4j.core.transport.ObjectMapperSingleton; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 29 | import org.openstack4j.openstack.networking.domain.NeutronPort; |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | import javax.ws.rs.Consumes; |
| 34 | import javax.ws.rs.DELETE; |
| 35 | import javax.ws.rs.POST; |
| 36 | import javax.ws.rs.PUT; |
| 37 | import javax.ws.rs.Path; |
| 38 | import javax.ws.rs.PathParam; |
| 39 | import javax.ws.rs.Produces; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 40 | import javax.ws.rs.core.Context; |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 41 | import javax.ws.rs.core.MediaType; |
| 42 | import javax.ws.rs.core.Response; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 43 | import javax.ws.rs.core.UriBuilder; |
| 44 | import javax.ws.rs.core.UriInfo; |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 45 | import java.io.InputStream; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 46 | |
| 47 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 48 | import static javax.ws.rs.core.Response.Status.OK; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 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 | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 52 | |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 53 | |
| 54 | /** |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 55 | * Neutron ML2 mechanism driver implementation for the port resource. |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 56 | */ |
| 57 | @Path("ports") |
| 58 | public class NeutronMl2PortsWebResource extends AbstractWebResource { |
| 59 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 60 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 61 | private static final String MESSAGE = "Received ports %s request"; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 62 | private static final String PORTS = "ports"; |
Hyunsun Moon | 5510e34 | 2017-02-23 19:41:00 +0900 | [diff] [blame] | 63 | private static final String PORT_NAME_PREFIX = "tap"; |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 64 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 65 | private final ServiceNetworkAdminService adminService = |
| 66 | DefaultServiceDirectory.getService(ServiceNetworkAdminService.class); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 67 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 68 | @Context |
| 69 | private UriInfo uriInfo; |
| 70 | |
| 71 | /** |
| 72 | * Creates a port from the JSON input stream. |
| 73 | * |
| 74 | * @param input port JSON input stream |
| 75 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 76 | * is invalid or duplicated port already exists |
| 77 | */ |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 78 | @POST |
| 79 | @Consumes(MediaType.APPLICATION_JSON) |
| 80 | @Produces(MediaType.APPLICATION_JSON) |
| 81 | public Response createPorts(InputStream input) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 82 | log.trace(String.format(MESSAGE, "CREATE")); |
| 83 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 84 | final ServicePort sport = readPort(input); |
| 85 | adminService.createServicePort(sport); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 86 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 87 | .path(PORTS) |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 88 | .path(sport.id().id()); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 89 | |
| 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 port with the specified identifier. |
| 95 | * |
| 96 | * @param id port identifier |
| 97 | * @param input port JSON input stream |
| 98 | * @return 200 OK with the updated port, 400 BAD_REQUEST if the requested |
| 99 | * port does not exist |
| 100 | */ |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 101 | @PUT |
| 102 | @Path("{id}") |
| 103 | @Consumes(MediaType.APPLICATION_JSON) |
| 104 | @Produces(MediaType.APPLICATION_JSON) |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 105 | public Response updatePort(@PathParam("id") String id, InputStream input) { |
| 106 | log.trace(String.format(MESSAGE, "UPDATE " + id)); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 107 | |
Hyunsun Moon | 5510e34 | 2017-02-23 19:41:00 +0900 | [diff] [blame] | 108 | final ServicePort sport = readPort(input); |
| 109 | adminService.updateServicePort(sport); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 110 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 111 | return status(OK).build(); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Removes the port with the given id. |
| 116 | * |
| 117 | * @param id port identifier |
| 118 | * @return 204 NO_CONTENT, 400 BAD_REQUEST if the port does not exist |
| 119 | */ |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 120 | @DELETE |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 121 | @Path("{id}") |
| 122 | @Consumes(MediaType.APPLICATION_JSON) |
| 123 | @Produces(MediaType.APPLICATION_JSON) |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 124 | public Response deletePorts(@PathParam("id") String id) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 125 | log.trace(String.format(MESSAGE, "DELETE " + id)); |
| 126 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 127 | adminService.removeServicePort(PortId.of(id)); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 128 | return noContent().build(); |
| 129 | } |
| 130 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 131 | private ServicePort readPort(InputStream input) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 132 | try { |
| 133 | JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input); |
| 134 | log.trace(mapper().writeValueAsString(jsonTree)); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 135 | NeutronPort osPort = ObjectMapperSingleton.getContext(NeutronPort.class) |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 136 | .readerFor(NeutronPort.class) |
| 137 | .readValue(jsonTree); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 138 | |
| 139 | ServicePort.Builder sportBuilder = DefaultServicePort.builder() |
| 140 | .id(PortId.of(osPort.getId())) |
Hyunsun Moon | 5510e34 | 2017-02-23 19:41:00 +0900 | [diff] [blame] | 141 | .name(PORT_NAME_PREFIX + osPort.getId().substring(0, 11)) |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 142 | .networkId(NetworkId.of(osPort.getNetworkId())); |
| 143 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 144 | if (osPort.getMacAddress() != null) { |
| 145 | sportBuilder.mac(MacAddress.valueOf(osPort.getMacAddress())); |
| 146 | } |
| 147 | if (!osPort.getFixedIps().isEmpty()) { |
| 148 | sportBuilder.ip(IpAddress.valueOf( |
| 149 | osPort.getFixedIps().iterator().next().getIpAddress())); |
| 150 | } |
| 151 | |
| 152 | return sportBuilder.build(); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 153 | } catch (Exception e) { |
| 154 | throw new IllegalArgumentException(); |
| 155 | } |
| 156 | } |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 157 | } |