blob: 1615c87fa90d841fd5d73dc1a147ffc50f7e1d17 [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.NeutronSecurityGroupRule;
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.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 Rule Rest API call from Neutron ML2 plugin.
44 */
45@Path("security-group-rules")
46public class OpenstackSecurityGroupRuleWebResource extends AbstractWebResource {
47 protected final Logger log = LoggerFactory.getLogger(getClass());
48
49 private static final String MESSAGE = "Received security group rules %s request";
50 private static final String SECURITY_GROUP_RULES = "security-group-rules";
51
52 private final OpenstackSecurityGroupAdminService adminService =
53 DefaultServiceDirectory.getService(OpenstackSecurityGroupAdminService.class);
54
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 rule ID already exists
Jian Lid3472bf2018-02-12 15:22:04 +090064 * @onos.rsModel NeutronSecurityGroupRule
sangho6a9ff0d2017-03-27 11:23:37 +090065 */
66 @POST
67 @Consumes(MediaType.APPLICATION_JSON)
68 @Produces(MediaType.APPLICATION_JSON)
69 public Response createSecurityGroupRules(InputStream input) {
70 log.trace(String.format(MESSAGE, "CREATE"));
71
Jian Li091d8d22018-02-20 10:42:06 +090072 final NeutronSecurityGroupRule sgRule = (NeutronSecurityGroupRule)
73 jsonToModelEntity(input, NeutronSecurityGroupRule.class);
74
sangho6a9ff0d2017-03-27 11:23:37 +090075 adminService.createSecurityGroupRule(sgRule);
76 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
77 .path(SECURITY_GROUP_RULES)
78 .path(sgRule.getId());
79
80 return created(locationBuilder.build()).build();
81 }
82
83 /**
84 * Removes the security group rule.
85 *
86 * @param id security group rule ID
87 * @return 204 NO_CONTENT
88 */
89 @DELETE
90 @Path("{id}")
91 @Consumes(MediaType.APPLICATION_JSON)
92 @Produces(MediaType.APPLICATION_JSON)
93 public Response removeSecurityGroupRule(@PathParam("id") String id) {
94 log.trace(String.format(MESSAGE, "REMOVE " + id));
95
96 adminService.removeSecurityGroupRule(id);
97 return noContent().build();
98 }
sangho6a9ff0d2017-03-27 11:23:37 +090099}