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.NeutronSecurityGroupRule; |
| 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.Path; |
| 28 | import javax.ws.rs.PathParam; |
| 29 | import javax.ws.rs.Produces; |
| 30 | import javax.ws.rs.core.Context; |
| 31 | import javax.ws.rs.core.MediaType; |
| 32 | import javax.ws.rs.core.Response; |
| 33 | import javax.ws.rs.core.UriBuilder; |
| 34 | import javax.ws.rs.core.UriInfo; |
| 35 | import java.io.InputStream; |
| 36 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 37 | import static javax.ws.rs.core.Response.created; |
| 38 | import static javax.ws.rs.core.Response.noContent; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 39 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.jsonToModelEntity; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Handles Security Group Rule Rest API call from Neutron ML2 plugin. |
| 43 | */ |
| 44 | @Path("security-group-rules") |
| 45 | public class OpenstackSecurityGroupRuleWebResource extends AbstractWebResource { |
| 46 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
| 47 | |
| 48 | private static final String MESSAGE = "Received security group rules %s request"; |
| 49 | private static final String SECURITY_GROUP_RULES = "security-group-rules"; |
| 50 | |
| 51 | private final OpenstackSecurityGroupAdminService adminService = |
Jian Li | 40e6361 | 2018-02-21 13:26:20 +0900 | [diff] [blame] | 52 | get(OpenstackSecurityGroupAdminService.class); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 53 | |
| 54 | @Context |
| 55 | private UriInfo uriInfo; |
| 56 | |
| 57 | /** |
| 58 | * Creates a security group from the JSON input stream. |
| 59 | * |
| 60 | * @param input security group JSON input stream |
| 61 | * @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] | 62 | * is invalid or duplicated security group rule ID already exists |
Jian Li | d3472bf | 2018-02-12 15:22:04 +0900 | [diff] [blame] | 63 | * @onos.rsModel NeutronSecurityGroupRule |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 64 | */ |
| 65 | @POST |
| 66 | @Consumes(MediaType.APPLICATION_JSON) |
| 67 | @Produces(MediaType.APPLICATION_JSON) |
| 68 | public Response createSecurityGroupRules(InputStream input) { |
| 69 | log.trace(String.format(MESSAGE, "CREATE")); |
| 70 | |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 71 | final NeutronSecurityGroupRule sgRule = (NeutronSecurityGroupRule) |
| 72 | jsonToModelEntity(input, NeutronSecurityGroupRule.class); |
| 73 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 74 | adminService.createSecurityGroupRule(sgRule); |
| 75 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 76 | .path(SECURITY_GROUP_RULES) |
| 77 | .path(sgRule.getId()); |
| 78 | |
| 79 | return created(locationBuilder.build()).build(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Removes the security group rule. |
| 84 | * |
| 85 | * @param id security group rule ID |
| 86 | * @return 204 NO_CONTENT |
| 87 | */ |
| 88 | @DELETE |
| 89 | @Path("{id}") |
| 90 | @Consumes(MediaType.APPLICATION_JSON) |
| 91 | @Produces(MediaType.APPLICATION_JSON) |
Jian Li | 40e6361 | 2018-02-21 13:26:20 +0900 | [diff] [blame] | 92 | public Response deleteSecurityGroupRule(@PathParam("id") String id) { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 93 | log.trace(String.format(MESSAGE, "REMOVE " + id)); |
| 94 | |
| 95 | adminService.removeSecurityGroupRule(id); |
| 96 | return noContent().build(); |
| 97 | } |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 98 | } |