blob: da335a31f13a3b2ce0167a4e4bf73a6d7b14782a [file] [log] [blame]
sangho6a9ff0d2017-03-27 11:23:37 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
sangho6a9ff0d2017-03-27 11:23:37 +09003 *
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 */
16package org.onosproject.openstacknetworking.web;
17
sangho6a9ff0d2017-03-27 11:23:37 +090018import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService;
19import org.onosproject.rest.AbstractWebResource;
sangho6a9ff0d2017-03-27 11:23:37 +090020import org.openstack4j.openstack.networking.domain.NeutronSecurityGroup;
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24import javax.ws.rs.Consumes;
25import javax.ws.rs.DELETE;
26import javax.ws.rs.POST;
27import javax.ws.rs.PUT;
28import javax.ws.rs.Path;
29import javax.ws.rs.PathParam;
30import javax.ws.rs.Produces;
31import javax.ws.rs.core.Context;
32import javax.ws.rs.core.MediaType;
33import javax.ws.rs.core.Response;
34import javax.ws.rs.core.UriBuilder;
35import javax.ws.rs.core.UriInfo;
36import java.io.InputStream;
37
sangho6a9ff0d2017-03-27 11:23:37 +090038import static javax.ws.rs.core.Response.created;
39import static javax.ws.rs.core.Response.noContent;
Jian Li091d8d22018-02-20 10:42:06 +090040import static org.onosproject.openstacknetworking.util.OpenstackUtil.jsonToModelEntity;
sangho6a9ff0d2017-03-27 11:23:37 +090041
42/**
43 * Handles Security Group Rest API call from Neutron ML2 plugin.
44 */
45@Path("security-groups")
46public 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 Li40e63612018-02-21 13:26:20 +090053 get(OpenstackSecurityGroupAdminService.class);
sangho6a9ff0d2017-03-27 11:23:37 +090054
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 Li8d8a0c52018-02-21 14:29:32 +090063 * is invalid or duplicated security group ID already exists
Jian Lid3472bf2018-02-12 15:22:04 +090064 * @onos.rsModel NeutronSecurityGroup
sangho6a9ff0d2017-03-27 11:23:37 +090065 */
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 Li091d8d22018-02-20 10:42:06 +090072 final NeutronSecurityGroup sg = (NeutronSecurityGroup)
73 jsonToModelEntity(input, NeutronSecurityGroup.class);
74
sangho6a9ff0d2017-03-27 11:23:37 +090075 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 Li8d8a0c52018-02-21 14:29:32 +090087 * @return 200 OK with the updated security group, 400 BAD_REQUEST if the
88 * request is invalid
Jian Lid3472bf2018-02-12 15:22:04 +090089 * @onos.rsModel NeutronSecurityGroup
sangho6a9ff0d2017-03-27 11:23:37 +090090 */
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 Li8d8a0c52018-02-21 14:29:32 +090097 // Do nothing ...
98 // TODO: this interface will purged sooner or later...
99 return Response.ok().build();
sangho6a9ff0d2017-03-27 11:23:37 +0900100 }
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 }
sangho6a9ff0d2017-03-27 11:23:37 +0900118}