blob: 669a3ad4c9c4937281efc6adf05319fdc7ddc27e [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
18import com.fasterxml.jackson.databind.JsonNode;
19import org.onlab.osgi.DefaultServiceDirectory;
20import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService;
21import org.onosproject.rest.AbstractWebResource;
22import org.openstack4j.core.transport.ObjectMapperSingleton;
23import org.openstack4j.openstack.networking.domain.NeutronSecurityGroupRule;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27import javax.ws.rs.Consumes;
28import javax.ws.rs.DELETE;
29import javax.ws.rs.POST;
30import javax.ws.rs.Path;
31import javax.ws.rs.PathParam;
32import javax.ws.rs.Produces;
33import javax.ws.rs.core.Context;
34import javax.ws.rs.core.MediaType;
35import javax.ws.rs.core.Response;
36import javax.ws.rs.core.UriBuilder;
37import javax.ws.rs.core.UriInfo;
38import java.io.InputStream;
39
40import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
41import static javax.ws.rs.core.Response.created;
42import static javax.ws.rs.core.Response.noContent;
43
44/**
45 * Handles Security Group Rule Rest API call from Neutron ML2 plugin.
46 */
47@Path("security-group-rules")
48public class OpenstackSecurityGroupRuleWebResource extends AbstractWebResource {
49 protected final Logger log = LoggerFactory.getLogger(getClass());
50
51 private static final String MESSAGE = "Received security group rules %s request";
52 private static final String SECURITY_GROUP_RULES = "security-group-rules";
53
54 private final OpenstackSecurityGroupAdminService adminService =
55 DefaultServiceDirectory.getService(OpenstackSecurityGroupAdminService.class);
56
57 @Context
58 private UriInfo uriInfo;
59
60 /**
61 * Creates a security group from the JSON input stream.
62 *
63 * @param input security group JSON input stream
64 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
65 * is invalid or duplicated port already exists
Jian Lid3472bf2018-02-12 15:22:04 +090066 * @onos.rsModel NeutronSecurityGroupRule
sangho6a9ff0d2017-03-27 11:23:37 +090067 */
68 @POST
69 @Consumes(MediaType.APPLICATION_JSON)
70 @Produces(MediaType.APPLICATION_JSON)
71 public Response createSecurityGroupRules(InputStream input) {
72 log.trace(String.format(MESSAGE, "CREATE"));
73
74 final NeutronSecurityGroupRule sgRule = readSecurityGroupRule(input);
75 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 }
99
100 private NeutronSecurityGroupRule readSecurityGroupRule(InputStream input) {
101 try {
102 JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
103 log.trace(mapper().writeValueAsString(jsonTree));
104 return ObjectMapperSingleton.getContext(NeutronSecurityGroupRule.class)
105 .readerFor(NeutronSecurityGroupRule.class)
106 .readValue(jsonTree);
107 } catch (Exception e) {
108 throw new IllegalArgumentException();
109 }
110 }
111}