blob: 895d35388e548ffb3584379d352ca6824d624cfb [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
Jian Li7b8c3682019-05-12 13:57:15 +090018import org.apache.commons.io.IOUtils;
19import org.onosproject.openstacknetworking.api.OpenstackHaService;
sangho6a9ff0d2017-03-27 11:23:37 +090020import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService;
21import org.onosproject.rest.AbstractWebResource;
sangho6a9ff0d2017-03-27 11:23:37 +090022import org.openstack4j.openstack.networking.domain.NeutronSecurityGroupRule;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26import javax.ws.rs.Consumes;
27import javax.ws.rs.DELETE;
28import javax.ws.rs.POST;
29import javax.ws.rs.Path;
30import javax.ws.rs.PathParam;
31import javax.ws.rs.Produces;
32import javax.ws.rs.core.Context;
33import javax.ws.rs.core.MediaType;
34import javax.ws.rs.core.Response;
35import javax.ws.rs.core.UriBuilder;
36import javax.ws.rs.core.UriInfo;
Jian Li7b8c3682019-05-12 13:57:15 +090037import java.io.IOException;
sangho6a9ff0d2017-03-27 11:23:37 +090038import java.io.InputStream;
39
sangho6a9ff0d2017-03-27 11:23:37 +090040import static javax.ws.rs.core.Response.created;
41import static javax.ws.rs.core.Response.noContent;
Jian Li7b8c3682019-05-12 13:57:15 +090042import static org.onosproject.openstacknetworking.api.Constants.REST_UTF8;
Jian Lidea0fdb2018-04-02 19:02:48 +090043import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.jsonToModelEntity;
Jian Li7b8c3682019-05-12 13:57:15 +090044import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.syncDelete;
45import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.syncPost;
sangho6a9ff0d2017-03-27 11:23:37 +090046
47/**
48 * Handles Security Group Rule Rest API call from Neutron ML2 plugin.
49 */
50@Path("security-group-rules")
51public class OpenstackSecurityGroupRuleWebResource extends AbstractWebResource {
52 protected final Logger log = LoggerFactory.getLogger(getClass());
53
54 private static final String MESSAGE = "Received security group rules %s request";
55 private static final String SECURITY_GROUP_RULES = "security-group-rules";
56
57 private final OpenstackSecurityGroupAdminService adminService =
Jian Li40e63612018-02-21 13:26:20 +090058 get(OpenstackSecurityGroupAdminService.class);
Jian Li7b8c3682019-05-12 13:57:15 +090059 private final OpenstackHaService haService = get(OpenstackHaService.class);
sangho6a9ff0d2017-03-27 11:23:37 +090060
61 @Context
62 private UriInfo uriInfo;
63
64 /**
65 * Creates a security group from the JSON input stream.
66 *
67 * @param input security group JSON input stream
68 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
Jian Li8d8a0c52018-02-21 14:29:32 +090069 * is invalid or duplicated security group rule ID already exists
Jian Li7b8c3682019-05-12 13:57:15 +090070 * @throws IOException exception
Jian Lid3472bf2018-02-12 15:22:04 +090071 * @onos.rsModel NeutronSecurityGroupRule
sangho6a9ff0d2017-03-27 11:23:37 +090072 */
73 @POST
74 @Consumes(MediaType.APPLICATION_JSON)
75 @Produces(MediaType.APPLICATION_JSON)
Jian Li7b8c3682019-05-12 13:57:15 +090076 public Response createSecurityGroupRules(InputStream input) throws IOException {
sangho6a9ff0d2017-03-27 11:23:37 +090077 log.trace(String.format(MESSAGE, "CREATE"));
78
Jian Li7b8c3682019-05-12 13:57:15 +090079 String inputStr = IOUtils.toString(input, REST_UTF8);
80
81 if (!haService.isActive()) {
82 return syncPost(haService, SECURITY_GROUP_RULES, inputStr);
83 }
84
Jian Li091d8d22018-02-20 10:42:06 +090085 final NeutronSecurityGroupRule sgRule = (NeutronSecurityGroupRule)
Jian Li7b8c3682019-05-12 13:57:15 +090086 jsonToModelEntity(inputStr, NeutronSecurityGroupRule.class);
Jian Li091d8d22018-02-20 10:42:06 +090087
sangho6a9ff0d2017-03-27 11:23:37 +090088 adminService.createSecurityGroupRule(sgRule);
89 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
90 .path(SECURITY_GROUP_RULES)
91 .path(sgRule.getId());
92
93 return created(locationBuilder.build()).build();
94 }
95
96 /**
97 * Removes the security group rule.
98 *
99 * @param id security group rule ID
100 * @return 204 NO_CONTENT
101 */
102 @DELETE
103 @Path("{id}")
104 @Consumes(MediaType.APPLICATION_JSON)
105 @Produces(MediaType.APPLICATION_JSON)
Jian Li40e63612018-02-21 13:26:20 +0900106 public Response deleteSecurityGroupRule(@PathParam("id") String id) {
sangho6a9ff0d2017-03-27 11:23:37 +0900107 log.trace(String.format(MESSAGE, "REMOVE " + id));
108
Jian Li7b8c3682019-05-12 13:57:15 +0900109 if (!haService.isActive()) {
110 return syncDelete(haService, SECURITY_GROUP_RULES, id);
111 }
112
sangho6a9ff0d2017-03-27 11:23:37 +0900113 adminService.removeSecurityGroupRule(id);
114 return noContent().build();
115 }
sangho6a9ff0d2017-03-27 11:23:37 +0900116}