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 | |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 18 | import org.apache.commons.io.IOUtils; |
| 19 | import org.onosproject.openstacknetworking.api.OpenstackHaService; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 20 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService; |
| 21 | import org.onosproject.rest.AbstractWebResource; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 22 | import org.openstack4j.openstack.networking.domain.NeutronSecurityGroupRule; |
| 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
| 26 | import javax.ws.rs.Consumes; |
| 27 | import javax.ws.rs.DELETE; |
| 28 | import javax.ws.rs.POST; |
| 29 | import javax.ws.rs.Path; |
| 30 | import javax.ws.rs.PathParam; |
| 31 | import javax.ws.rs.Produces; |
| 32 | import javax.ws.rs.core.Context; |
| 33 | import javax.ws.rs.core.MediaType; |
| 34 | import javax.ws.rs.core.Response; |
| 35 | import javax.ws.rs.core.UriBuilder; |
| 36 | import javax.ws.rs.core.UriInfo; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 37 | import java.io.IOException; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 38 | import java.io.InputStream; |
| 39 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 40 | import static javax.ws.rs.core.Response.created; |
| 41 | import static javax.ws.rs.core.Response.noContent; |
Daniel Park | c717c0f | 2019-09-15 16:38:06 +0900 | [diff] [blame^] | 42 | import static org.onosproject.openstacknetworking.api.Constants.DEFAULT_ACTIVE_IP_ADDRESS; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 43 | import static org.onosproject.openstacknetworking.api.Constants.REST_UTF8; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 44 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.jsonToModelEntity; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 45 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.syncDelete; |
| 46 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.syncPost; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Handles Security Group Rule Rest API call from Neutron ML2 plugin. |
| 50 | */ |
| 51 | @Path("security-group-rules") |
| 52 | public class OpenstackSecurityGroupRuleWebResource extends AbstractWebResource { |
| 53 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
| 54 | |
| 55 | private static final String MESSAGE = "Received security group rules %s request"; |
| 56 | private static final String SECURITY_GROUP_RULES = "security-group-rules"; |
| 57 | |
| 58 | private final OpenstackSecurityGroupAdminService adminService = |
Jian Li | 40e6361 | 2018-02-21 13:26:20 +0900 | [diff] [blame] | 59 | get(OpenstackSecurityGroupAdminService.class); |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 60 | private final OpenstackHaService haService = get(OpenstackHaService.class); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 61 | |
| 62 | @Context |
| 63 | private UriInfo uriInfo; |
| 64 | |
| 65 | /** |
| 66 | * Creates a security group from the JSON input stream. |
| 67 | * |
| 68 | * @param input security group JSON input stream |
| 69 | * @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] | 70 | * is invalid or duplicated security group rule ID already exists |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 71 | * @throws IOException exception |
Jian Li | d3472bf | 2018-02-12 15:22:04 +0900 | [diff] [blame] | 72 | * @onos.rsModel NeutronSecurityGroupRule |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 73 | */ |
| 74 | @POST |
| 75 | @Consumes(MediaType.APPLICATION_JSON) |
| 76 | @Produces(MediaType.APPLICATION_JSON) |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 77 | public Response createSecurityGroupRules(InputStream input) throws IOException { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 78 | log.trace(String.format(MESSAGE, "CREATE")); |
| 79 | |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 80 | String inputStr = IOUtils.toString(input, REST_UTF8); |
| 81 | |
Daniel Park | c717c0f | 2019-09-15 16:38:06 +0900 | [diff] [blame^] | 82 | if (!haService.isActive() |
| 83 | && !DEFAULT_ACTIVE_IP_ADDRESS.equals(haService.getActiveIp())) { |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 84 | return syncPost(haService, SECURITY_GROUP_RULES, inputStr); |
| 85 | } |
| 86 | |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 87 | final NeutronSecurityGroupRule sgRule = (NeutronSecurityGroupRule) |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 88 | jsonToModelEntity(inputStr, NeutronSecurityGroupRule.class); |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 89 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 90 | adminService.createSecurityGroupRule(sgRule); |
| 91 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 92 | .path(SECURITY_GROUP_RULES) |
| 93 | .path(sgRule.getId()); |
| 94 | |
| 95 | return created(locationBuilder.build()).build(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Removes the security group rule. |
| 100 | * |
| 101 | * @param id security group rule ID |
| 102 | * @return 204 NO_CONTENT |
| 103 | */ |
| 104 | @DELETE |
| 105 | @Path("{id}") |
| 106 | @Consumes(MediaType.APPLICATION_JSON) |
| 107 | @Produces(MediaType.APPLICATION_JSON) |
Jian Li | 40e6361 | 2018-02-21 13:26:20 +0900 | [diff] [blame] | 108 | public Response deleteSecurityGroupRule(@PathParam("id") String id) { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 109 | log.trace(String.format(MESSAGE, "REMOVE " + id)); |
| 110 | |
Daniel Park | c717c0f | 2019-09-15 16:38:06 +0900 | [diff] [blame^] | 111 | if (!haService.isActive() |
| 112 | && !DEFAULT_ACTIVE_IP_ADDRESS.equals(haService.getActiveIp())) { |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 113 | return syncDelete(haService, SECURITY_GROUP_RULES, id); |
| 114 | } |
| 115 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 116 | adminService.removeSecurityGroupRule(id); |
| 117 | return noContent().build(); |
| 118 | } |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 119 | } |