blob: 3a2e41d2578e43bdcc4368160cac1c6bfa9547c6 [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.onlab.osgi.DefaultServiceDirectory;
19import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService;
20import org.onosproject.rest.AbstractWebResource;
sangho6a9ff0d2017-03-27 11:23:37 +090021import org.openstack4j.openstack.networking.domain.NeutronSecurityGroup;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25import javax.ws.rs.Consumes;
26import javax.ws.rs.DELETE;
27import javax.ws.rs.POST;
28import javax.ws.rs.PUT;
29import javax.ws.rs.Path;
30import javax.ws.rs.PathParam;
31import javax.ws.rs.Produces;
32import javax.ws.rs.core.Context;
33import javax.ws.rs.core.MediaType;
34import javax.ws.rs.core.Response;
35import javax.ws.rs.core.UriBuilder;
36import javax.ws.rs.core.UriInfo;
37import java.io.InputStream;
38
sangho6a9ff0d2017-03-27 11:23:37 +090039import static javax.ws.rs.core.Response.created;
40import static javax.ws.rs.core.Response.noContent;
Jian Li091d8d22018-02-20 10:42:06 +090041import static org.onosproject.openstacknetworking.util.OpenstackUtil.jsonToModelEntity;
sangho6a9ff0d2017-03-27 11:23:37 +090042
43/**
44 * Handles Security Group Rest API call from Neutron ML2 plugin.
45 */
46@Path("security-groups")
47public class OpenstackSecurityGroupWebResource extends AbstractWebResource {
48 protected final Logger log = LoggerFactory.getLogger(getClass());
49
50 private static final String MESSAGE = "Received security groups %s request";
51 private static final String SECURITY_GROUPS = "security-groups";
52
53 private final OpenstackSecurityGroupAdminService adminService =
54 DefaultServiceDirectory.getService(OpenstackSecurityGroupAdminService.class);
55
56 @Context
57 private UriInfo uriInfo;
58
59 /**
60 * Creates a security group from the JSON input stream.
61 *
62 * @param input security group JSON input stream
63 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
Jian Li8d8a0c52018-02-21 14:29:32 +090064 * is invalid or duplicated security group ID already exists
Jian Lid3472bf2018-02-12 15:22:04 +090065 * @onos.rsModel NeutronSecurityGroup
sangho6a9ff0d2017-03-27 11:23:37 +090066 */
67 @POST
68 @Consumes(MediaType.APPLICATION_JSON)
69 @Produces(MediaType.APPLICATION_JSON)
70 public Response createSecurityGroups(InputStream input) {
71 log.trace(String.format(MESSAGE, "CREATE"));
72
Jian Li091d8d22018-02-20 10:42:06 +090073 final NeutronSecurityGroup sg = (NeutronSecurityGroup)
74 jsonToModelEntity(input, NeutronSecurityGroup.class);
75
sangho6a9ff0d2017-03-27 11:23:37 +090076 adminService.createSecurityGroup(sg);
77 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
78 .path(SECURITY_GROUPS)
79 .path(sg.getId());
80
81 return created(locationBuilder.build()).build();
82 }
83
84 /**
85 * Updates a security group from the JSON input stream.
86 *
87 * @param input security group JSON input stream
Jian Li8d8a0c52018-02-21 14:29:32 +090088 * @return 200 OK with the updated security group, 400 BAD_REQUEST if the
89 * request is invalid
Jian Lid3472bf2018-02-12 15:22:04 +090090 * @onos.rsModel NeutronSecurityGroup
sangho6a9ff0d2017-03-27 11:23:37 +090091 */
92 @PUT
93 @Consumes(MediaType.APPLICATION_JSON)
94 @Produces(MediaType.APPLICATION_JSON)
95 public Response updateSecurityGroups(InputStream input) {
96 log.trace(String.format(MESSAGE, "UPDATE"));
97
Jian Li8d8a0c52018-02-21 14:29:32 +090098 // Do nothing ...
99 // TODO: this interface will purged sooner or later...
100 return Response.ok().build();
sangho6a9ff0d2017-03-27 11:23:37 +0900101 }
102
103 /**
104 * Removes the security group.
105 *
106 * @param id security group ID
107 * @return 204 NO_CONTENT
108 */
109 @DELETE
110 @Path("{id}")
111 @Consumes(MediaType.APPLICATION_JSON)
112 @Produces(MediaType.APPLICATION_JSON)
113 public Response removeSecurityGroup(@PathParam("id") String id) {
114 log.trace(String.format(MESSAGE, "REMOVE " + id));
115
116 adminService.removeSecurityGroup(id);
117 return noContent().build();
118 }
sangho6a9ff0d2017-03-27 11:23:37 +0900119}