blob: 7ba45385c040c477a617ab6fbb9104cfcad3bb77 [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
Jian Lid3472bf2018-02-12 15:22:04 +090067 * @onos.rsModel NeutronSecurityGroup
sangho6a9ff0d2017-03-27 11:23:37 +090068 */
69 @POST
70 @Consumes(MediaType.APPLICATION_JSON)
71 @Produces(MediaType.APPLICATION_JSON)
72 public Response createSecurityGroups(InputStream input) {
73 log.trace(String.format(MESSAGE, "CREATE"));
74
75 final NeutronSecurityGroup sg = readSecurityGroup(input);
76 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
88 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
89 * is invalid or duplicated port already exists
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
98 final NeutronSecurityGroup sg = readSecurityGroup(input);
99 // Do nothing ..
100 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
101 .path(SECURITY_GROUPS)
102 .path(sg.getId());
103
104 return created(locationBuilder.build()).build();
105 }
106
107 /**
108 * Removes the security group.
109 *
110 * @param id security group ID
111 * @return 204 NO_CONTENT
112 */
113 @DELETE
114 @Path("{id}")
115 @Consumes(MediaType.APPLICATION_JSON)
116 @Produces(MediaType.APPLICATION_JSON)
117 public Response removeSecurityGroup(@PathParam("id") String id) {
118 log.trace(String.format(MESSAGE, "REMOVE " + id));
119
120 adminService.removeSecurityGroup(id);
121 return noContent().build();
122 }
123
124 private NeutronSecurityGroup readSecurityGroup(InputStream input) {
125 try {
126 JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
127 log.trace(mapper().writeValueAsString(jsonTree));
128 return ObjectMapperSingleton.getContext(NeutronSecurityGroup.class)
129 .readerFor(NeutronSecurityGroup.class)
130 .readValue(jsonTree);
131 } catch (Exception e) {
132 throw new IllegalArgumentException();
133 }
134 }
135}