sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [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 | */ |
| 16 | package org.onosproject.openstacknetworking.web; |
| 17 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 18 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService; |
| 19 | import org.onosproject.rest.AbstractWebResource; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 20 | import org.openstack4j.openstack.networking.domain.NeutronSecurityGroup; |
| 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
| 24 | import javax.ws.rs.Consumes; |
| 25 | import javax.ws.rs.DELETE; |
| 26 | import javax.ws.rs.POST; |
| 27 | import javax.ws.rs.PUT; |
| 28 | import javax.ws.rs.Path; |
| 29 | import javax.ws.rs.PathParam; |
| 30 | import javax.ws.rs.Produces; |
| 31 | import javax.ws.rs.core.Context; |
| 32 | import javax.ws.rs.core.MediaType; |
| 33 | import javax.ws.rs.core.Response; |
| 34 | import javax.ws.rs.core.UriBuilder; |
| 35 | import javax.ws.rs.core.UriInfo; |
| 36 | import java.io.InputStream; |
| 37 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 38 | import static javax.ws.rs.core.Response.created; |
| 39 | import static javax.ws.rs.core.Response.noContent; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 40 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.jsonToModelEntity; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Handles Security Group Rest API call from Neutron ML2 plugin. |
| 44 | */ |
| 45 | @Path("security-groups") |
| 46 | public class OpenstackSecurityGroupWebResource extends AbstractWebResource { |
| 47 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
| 48 | |
| 49 | private static final String MESSAGE = "Received security groups %s request"; |
| 50 | private static final String SECURITY_GROUPS = "security-groups"; |
| 51 | |
| 52 | private final OpenstackSecurityGroupAdminService adminService = |
Jian Li | 40e6361 | 2018-02-21 13:26:20 +0900 | [diff] [blame] | 53 | get(OpenstackSecurityGroupAdminService.class); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 54 | |
| 55 | @Context |
| 56 | private UriInfo uriInfo; |
| 57 | |
| 58 | /** |
| 59 | * Creates a security group from the JSON input stream. |
| 60 | * |
| 61 | * @param input security group JSON input stream |
| 62 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
Jian Li | 8d8a0c5 | 2018-02-21 14:29:32 +0900 | [diff] [blame] | 63 | * is invalid or duplicated security group ID already exists |
Jian Li | d3472bf | 2018-02-12 15:22:04 +0900 | [diff] [blame] | 64 | * @onos.rsModel NeutronSecurityGroup |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 65 | */ |
| 66 | @POST |
| 67 | @Consumes(MediaType.APPLICATION_JSON) |
| 68 | @Produces(MediaType.APPLICATION_JSON) |
| 69 | public Response createSecurityGroups(InputStream input) { |
| 70 | log.trace(String.format(MESSAGE, "CREATE")); |
| 71 | |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 72 | final NeutronSecurityGroup sg = (NeutronSecurityGroup) |
| 73 | jsonToModelEntity(input, NeutronSecurityGroup.class); |
| 74 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 75 | adminService.createSecurityGroup(sg); |
| 76 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 77 | .path(SECURITY_GROUPS) |
| 78 | .path(sg.getId()); |
| 79 | |
| 80 | return created(locationBuilder.build()).build(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Updates a security group from the JSON input stream. |
| 85 | * |
| 86 | * @param input security group JSON input stream |
Jian Li | 8d8a0c5 | 2018-02-21 14:29:32 +0900 | [diff] [blame] | 87 | * @return 200 OK with the updated security group, 400 BAD_REQUEST if the |
| 88 | * request is invalid |
Jian Li | d3472bf | 2018-02-12 15:22:04 +0900 | [diff] [blame] | 89 | * @onos.rsModel NeutronSecurityGroup |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 90 | */ |
| 91 | @PUT |
| 92 | @Consumes(MediaType.APPLICATION_JSON) |
| 93 | @Produces(MediaType.APPLICATION_JSON) |
| 94 | public Response updateSecurityGroups(InputStream input) { |
| 95 | log.trace(String.format(MESSAGE, "UPDATE")); |
| 96 | |
Jian Li | 8d8a0c5 | 2018-02-21 14:29:32 +0900 | [diff] [blame] | 97 | // Do nothing ... |
| 98 | // TODO: this interface will purged sooner or later... |
| 99 | return Response.ok().build(); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Removes the security group. |
| 104 | * |
| 105 | * @param id security group ID |
| 106 | * @return 204 NO_CONTENT |
| 107 | */ |
| 108 | @DELETE |
| 109 | @Path("{id}") |
| 110 | @Consumes(MediaType.APPLICATION_JSON) |
| 111 | @Produces(MediaType.APPLICATION_JSON) |
| 112 | public Response removeSecurityGroup(@PathParam("id") String id) { |
| 113 | log.trace(String.format(MESSAGE, "REMOVE " + id)); |
| 114 | |
| 115 | adminService.removeSecurityGroup(id); |
| 116 | return noContent().build(); |
| 117 | } |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 118 | } |