blob: 244b98483153e2b168f131ea1d29e9081e0019b4 [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.NeutronSecurityGroup;
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.PUT;
31import javax.ws.rs.Path;
32import javax.ws.rs.PathParam;
33import javax.ws.rs.Produces;
34import javax.ws.rs.core.Context;
35import javax.ws.rs.core.MediaType;
36import javax.ws.rs.core.Response;
37import javax.ws.rs.core.UriBuilder;
38import javax.ws.rs.core.UriInfo;
39import java.io.InputStream;
40
41import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
42import static javax.ws.rs.core.Response.created;
43import static javax.ws.rs.core.Response.noContent;
44
45/**
46 * Handles Security Group Rest API call from Neutron ML2 plugin.
47 */
48@Path("security-groups")
49public class OpenstackSecurityGroupWebResource extends AbstractWebResource {
50 protected final Logger log = LoggerFactory.getLogger(getClass());
51
52 private static final String MESSAGE = "Received security groups %s request";
53 private static final String SECURITY_GROUPS = "security-groups";
54
55 private final OpenstackSecurityGroupAdminService adminService =
56 DefaultServiceDirectory.getService(OpenstackSecurityGroupAdminService.class);
57
58 @Context
59 private UriInfo uriInfo;
60
61 /**
62 * Creates a security group from the JSON input stream.
63 *
64 * @param input security group JSON input stream
65 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
66 * is invalid or duplicated port already exists
67 */
68 @POST
69 @Consumes(MediaType.APPLICATION_JSON)
70 @Produces(MediaType.APPLICATION_JSON)
71 public Response createSecurityGroups(InputStream input) {
72 log.trace(String.format(MESSAGE, "CREATE"));
73
74 final NeutronSecurityGroup sg = readSecurityGroup(input);
75 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
87 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
88 * is invalid or duplicated port already exists
89 */
90 @PUT
91 @Consumes(MediaType.APPLICATION_JSON)
92 @Produces(MediaType.APPLICATION_JSON)
93 public Response updateSecurityGroups(InputStream input) {
94 log.trace(String.format(MESSAGE, "UPDATE"));
95
96 final NeutronSecurityGroup sg = readSecurityGroup(input);
97 // Do nothing ..
98 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
99 .path(SECURITY_GROUPS)
100 .path(sg.getId());
101
102 return created(locationBuilder.build()).build();
103 }
104
105 /**
106 * Removes the security group.
107 *
108 * @param id security group ID
109 * @return 204 NO_CONTENT
110 */
111 @DELETE
112 @Path("{id}")
113 @Consumes(MediaType.APPLICATION_JSON)
114 @Produces(MediaType.APPLICATION_JSON)
115 public Response removeSecurityGroup(@PathParam("id") String id) {
116 log.trace(String.format(MESSAGE, "REMOVE " + id));
117
118 adminService.removeSecurityGroup(id);
119 return noContent().build();
120 }
121
122 private NeutronSecurityGroup readSecurityGroup(InputStream input) {
123 try {
124 JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
125 log.trace(mapper().writeValueAsString(jsonTree));
126 return ObjectMapperSingleton.getContext(NeutronSecurityGroup.class)
127 .readerFor(NeutronSecurityGroup.class)
128 .readValue(jsonTree);
129 } catch (Exception e) {
130 throw new IllegalArgumentException();
131 }
132 }
133}