blob: 27ffa6fc096b67f01fc928dbfa6d86d16421d4f9 [file] [log] [blame]
sangho6a9ff0d2017-03-27 11:23:37 +09001/*
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 */
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
66 */
67 @POST
68 @Consumes(MediaType.APPLICATION_JSON)
69 @Produces(MediaType.APPLICATION_JSON)
70 public Response createSecurityGroupRules(InputStream input) {
71 log.trace(String.format(MESSAGE, "CREATE"));
72
73 final NeutronSecurityGroupRule sgRule = readSecurityGroupRule(input);
74 adminService.createSecurityGroupRule(sgRule);
75 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
76 .path(SECURITY_GROUP_RULES)
77 .path(sgRule.getId());
78
79 return created(locationBuilder.build()).build();
80 }
81
82 /**
83 * Removes the security group rule.
84 *
85 * @param id security group rule ID
86 * @return 204 NO_CONTENT
87 */
88 @DELETE
89 @Path("{id}")
90 @Consumes(MediaType.APPLICATION_JSON)
91 @Produces(MediaType.APPLICATION_JSON)
92 public Response removeSecurityGroupRule(@PathParam("id") String id) {
93 log.trace(String.format(MESSAGE, "REMOVE " + id));
94
95 adminService.removeSecurityGroupRule(id);
96 return noContent().build();
97 }
98
99 private NeutronSecurityGroupRule readSecurityGroupRule(InputStream input) {
100 try {
101 JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
102 log.trace(mapper().writeValueAsString(jsonTree));
103 return ObjectMapperSingleton.getContext(NeutronSecurityGroupRule.class)
104 .readerFor(NeutronSecurityGroupRule.class)
105 .readValue(jsonTree);
106 } catch (Exception e) {
107 throw new IllegalArgumentException();
108 }
109 }
110}