sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import org.onlab.osgi.DefaultServiceDirectory; |
| 20 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService; |
| 21 | import org.onosproject.rest.AbstractWebResource; |
| 22 | import org.openstack4j.core.transport.ObjectMapperSingleton; |
| 23 | import org.openstack4j.openstack.networking.domain.NeutronSecurityGroup; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | import javax.ws.rs.Consumes; |
| 28 | import javax.ws.rs.DELETE; |
| 29 | import javax.ws.rs.POST; |
| 30 | import javax.ws.rs.PUT; |
| 31 | import javax.ws.rs.Path; |
| 32 | import javax.ws.rs.PathParam; |
| 33 | import javax.ws.rs.Produces; |
| 34 | import javax.ws.rs.core.Context; |
| 35 | import javax.ws.rs.core.MediaType; |
| 36 | import javax.ws.rs.core.Response; |
| 37 | import javax.ws.rs.core.UriBuilder; |
| 38 | import javax.ws.rs.core.UriInfo; |
| 39 | import java.io.InputStream; |
| 40 | |
| 41 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 42 | import static javax.ws.rs.core.Response.created; |
| 43 | import static javax.ws.rs.core.Response.noContent; |
| 44 | |
| 45 | /** |
| 46 | * Handles Security Group Rest API call from Neutron ML2 plugin. |
| 47 | */ |
| 48 | @Path("security-groups") |
| 49 | public class OpenstackSecurityGroupWebResource extends AbstractWebResource { |
| 50 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
| 51 | |
| 52 | private static final String MESSAGE = "Received security groups %s request"; |
| 53 | private static final String SECURITY_GROUPS = "security-groups"; |
| 54 | |
| 55 | private final OpenstackSecurityGroupAdminService adminService = |
| 56 | DefaultServiceDirectory.getService(OpenstackSecurityGroupAdminService.class); |
| 57 | |
| 58 | @Context |
| 59 | private UriInfo uriInfo; |
| 60 | |
| 61 | /** |
| 62 | * Creates a security group from the JSON input stream. |
| 63 | * |
| 64 | * @param input security group JSON input stream |
| 65 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 66 | * is invalid or duplicated port already exists |
| 67 | */ |
| 68 | @POST |
| 69 | @Consumes(MediaType.APPLICATION_JSON) |
| 70 | @Produces(MediaType.APPLICATION_JSON) |
| 71 | public Response createSecurityGroups(InputStream input) { |
| 72 | log.trace(String.format(MESSAGE, "CREATE")); |
| 73 | |
| 74 | final NeutronSecurityGroup sg = readSecurityGroup(input); |
| 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 |
| 87 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 88 | * is invalid or duplicated port already exists |
| 89 | */ |
| 90 | @PUT |
| 91 | @Consumes(MediaType.APPLICATION_JSON) |
| 92 | @Produces(MediaType.APPLICATION_JSON) |
| 93 | public Response updateSecurityGroups(InputStream input) { |
| 94 | log.trace(String.format(MESSAGE, "UPDATE")); |
| 95 | |
| 96 | final NeutronSecurityGroup sg = readSecurityGroup(input); |
| 97 | // Do nothing .. |
| 98 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 99 | .path(SECURITY_GROUPS) |
| 100 | .path(sg.getId()); |
| 101 | |
| 102 | return created(locationBuilder.build()).build(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Removes the security group. |
| 107 | * |
| 108 | * @param id security group ID |
| 109 | * @return 204 NO_CONTENT |
| 110 | */ |
| 111 | @DELETE |
| 112 | @Path("{id}") |
| 113 | @Consumes(MediaType.APPLICATION_JSON) |
| 114 | @Produces(MediaType.APPLICATION_JSON) |
| 115 | public Response removeSecurityGroup(@PathParam("id") String id) { |
| 116 | log.trace(String.format(MESSAGE, "REMOVE " + id)); |
| 117 | |
| 118 | adminService.removeSecurityGroup(id); |
| 119 | return noContent().build(); |
| 120 | } |
| 121 | |
| 122 | private NeutronSecurityGroup readSecurityGroup(InputStream input) { |
| 123 | try { |
| 124 | JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input); |
| 125 | log.trace(mapper().writeValueAsString(jsonTree)); |
| 126 | return ObjectMapperSingleton.getContext(NeutronSecurityGroup.class) |
| 127 | .readerFor(NeutronSecurityGroup.class) |
| 128 | .readValue(jsonTree); |
| 129 | } catch (Exception e) { |
| 130 | throw new IllegalArgumentException(); |
| 131 | } |
| 132 | } |
| 133 | } |