Phaneendra Manda | 6109f95 | 2015-10-29 16:59:30 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Phaneendra Manda | 6109f95 | 2015-10-29 16:59:30 +0530 | [diff] [blame] | 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 | */ |
| 16 | |
| 17 | package org.onosproject.vtnweb.resources; |
| 18 | |
Phaneendra Manda | 6109f95 | 2015-10-29 16:59:30 +0530 | [diff] [blame] | 19 | import static javax.ws.rs.core.Response.Status.OK; |
| 20 | import static org.onlab.util.Tools.nullIsNotFound; |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 21 | import static org.onlab.util.Tools.readTreeFromStream; |
Phaneendra Manda | 6109f95 | 2015-10-29 16:59:30 +0530 | [diff] [blame] | 22 | |
| 23 | import java.io.IOException; |
| 24 | import java.io.InputStream; |
| 25 | |
| 26 | import javax.ws.rs.Consumes; |
| 27 | import javax.ws.rs.DELETE; |
| 28 | import javax.ws.rs.GET; |
| 29 | import javax.ws.rs.POST; |
| 30 | import javax.ws.rs.PUT; |
| 31 | import javax.ws.rs.Path; |
| 32 | import javax.ws.rs.PathParam; |
| 33 | import javax.ws.rs.Produces; |
| 34 | import javax.ws.rs.core.MediaType; |
| 35 | import javax.ws.rs.core.Response; |
| 36 | |
| 37 | import org.onosproject.rest.AbstractWebResource; |
| 38 | import org.onosproject.vtnrsc.PortPairGroup; |
| 39 | import org.onosproject.vtnrsc.PortPairGroupId; |
| 40 | import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService; |
Phaneendra Manda | 6109f95 | 2015-10-29 16:59:30 +0530 | [diff] [blame] | 41 | import org.slf4j.Logger; |
| 42 | import org.slf4j.LoggerFactory; |
| 43 | |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 44 | import com.fasterxml.jackson.databind.JsonNode; |
Phaneendra Manda | 6109f95 | 2015-10-29 16:59:30 +0530 | [diff] [blame] | 45 | import com.fasterxml.jackson.databind.ObjectMapper; |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 46 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Phaneendra Manda | 6109f95 | 2015-10-29 16:59:30 +0530 | [diff] [blame] | 47 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 48 | |
| 49 | /** |
| 50 | * Query and program port pair group. |
| 51 | */ |
| 52 | |
| 53 | @Path("port_pair_groups") |
| 54 | public class PortPairGroupWebResource extends AbstractWebResource { |
| 55 | |
| 56 | private final Logger log = LoggerFactory.getLogger(PortPairGroupWebResource.class); |
Phaneendra Manda | 6109f95 | 2015-10-29 16:59:30 +0530 | [diff] [blame] | 57 | public static final String PORT_PAIR_GROUP_NOT_FOUND = "Port pair group not found"; |
| 58 | public static final String PORT_PAIR_GROUP_ID_EXIST = "Port pair group exists"; |
| 59 | public static final String PORT_PAIR_GROUP_ID_NOT_EXIST = "Port pair group does not exist with identifier"; |
| 60 | |
| 61 | /** |
| 62 | * Get details of all port pair groups created. |
| 63 | * |
| 64 | * @return 200 OK |
| 65 | */ |
| 66 | @GET |
| 67 | @Produces(MediaType.APPLICATION_JSON) |
Wu wenbin | d0b119f | 2016-05-11 18:03:41 +0800 | [diff] [blame] | 68 | @Consumes(MediaType.APPLICATION_JSON) |
Phaneendra Manda | 6109f95 | 2015-10-29 16:59:30 +0530 | [diff] [blame] | 69 | public Response getPortPairGroups() { |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 70 | Iterable<PortPairGroup> portPairGroups = get(PortPairGroupService.class).getPortPairGroups(); |
| 71 | ObjectNode result = mapper().createObjectNode(); |
| 72 | ArrayNode portPairGroupEntry = result.putArray("port_pair_groups"); |
| 73 | if (portPairGroups != null) { |
| 74 | for (final PortPairGroup portPairGroup : portPairGroups) { |
| 75 | portPairGroupEntry.add(codec(PortPairGroup.class).encode(portPairGroup, this)); |
| 76 | } |
| 77 | } |
| 78 | return ok(result.toString()).build(); |
Phaneendra Manda | 6109f95 | 2015-10-29 16:59:30 +0530 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get details of a specified port pair group id. |
| 83 | * |
| 84 | * @param id port pair group id |
| 85 | * @return 200 OK, 404 if given identifier does not exist |
| 86 | */ |
| 87 | @GET |
| 88 | @Path("{group_id}") |
| 89 | @Produces(MediaType.APPLICATION_JSON) |
Wu wenbin | d0b119f | 2016-05-11 18:03:41 +0800 | [diff] [blame] | 90 | @Consumes(MediaType.APPLICATION_JSON) |
Phaneendra Manda | 6109f95 | 2015-10-29 16:59:30 +0530 | [diff] [blame] | 91 | public Response getPortPairGroup(@PathParam("group_id") String id) { |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 92 | PortPairGroup portPairGroup = nullIsNotFound(get(PortPairGroupService.class) |
| 93 | .getPortPairGroup(PortPairGroupId.of(id)), |
Phaneendra Manda | 6109f95 | 2015-10-29 16:59:30 +0530 | [diff] [blame] | 94 | PORT_PAIR_GROUP_NOT_FOUND); |
| 95 | |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 96 | ObjectNode result = mapper().createObjectNode(); |
| 97 | result.set("port_pair_group", codec(PortPairGroup.class).encode(portPairGroup, this)); |
| 98 | return ok(result.toString()).build(); |
Phaneendra Manda | 6109f95 | 2015-10-29 16:59:30 +0530 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Creates a new port pair group. |
| 103 | * |
| 104 | * @param stream port pair group from JSON |
| 105 | * @return status of the request - CREATED if the JSON is correct, |
| 106 | * BAD_REQUEST if the JSON is invalid |
| 107 | */ |
| 108 | @POST |
| 109 | @Consumes(MediaType.APPLICATION_JSON) |
| 110 | @Produces(MediaType.APPLICATION_JSON) |
| 111 | public Response createPortPairGroup(InputStream stream) { |
| 112 | |
| 113 | try { |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 114 | ObjectMapper mapper = new ObjectMapper(); |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 115 | ObjectNode jsonTree = readTreeFromStream(mapper, stream); |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 116 | JsonNode port = jsonTree.get("port_pair_group"); |
Phaneendra Manda | 6109f95 | 2015-10-29 16:59:30 +0530 | [diff] [blame] | 117 | |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 118 | PortPairGroup portPairGroup = codec(PortPairGroup.class).decode((ObjectNode) port, this); |
| 119 | Boolean issuccess = nullIsNotFound(get(PortPairGroupService.class).createPortPairGroup(portPairGroup), |
Phaneendra Manda | 6109f95 | 2015-10-29 16:59:30 +0530 | [diff] [blame] | 120 | PORT_PAIR_GROUP_NOT_FOUND); |
| 121 | return Response.status(OK).entity(issuccess.toString()).build(); |
| 122 | } catch (IOException e) { |
| 123 | log.error("Exception while creating port pair group {}.", e.toString()); |
| 124 | throw new IllegalArgumentException(e); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Update details of a specified port pair group id. |
| 130 | * |
| 131 | * @param id port pair group id |
| 132 | * @param stream port pair group from json |
| 133 | * @return 200 OK, 404 if given identifier does not exist |
| 134 | */ |
| 135 | @PUT |
| 136 | @Path("{group_id}") |
| 137 | @Produces(MediaType.APPLICATION_JSON) |
| 138 | @Consumes(MediaType.APPLICATION_JSON) |
| 139 | public Response updatePortPairGroup(@PathParam("group_id") String id, |
| 140 | final InputStream stream) { |
| 141 | try { |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 142 | ObjectMapper mapper = new ObjectMapper(); |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 143 | ObjectNode jsonTree = readTreeFromStream(mapper, stream); |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 144 | JsonNode port = jsonTree.get("port_pair_group"); |
| 145 | PortPairGroup portPairGroup = codec(PortPairGroup.class).decode((ObjectNode) port, this); |
| 146 | Boolean isSuccess = nullIsNotFound(get(PortPairGroupService.class).updatePortPairGroup(portPairGroup), |
| 147 | PORT_PAIR_GROUP_NOT_FOUND); |
Phaneendra Manda | 6109f95 | 2015-10-29 16:59:30 +0530 | [diff] [blame] | 148 | return Response.status(OK).entity(isSuccess.toString()).build(); |
| 149 | } catch (IOException e) { |
| 150 | log.error("Update port pair group failed because of exception {}.", e.toString()); |
| 151 | throw new IllegalArgumentException(e); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Delete details of a specified port pair group id. |
| 157 | * |
| 158 | * @param id port pair group id |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 159 | * @return 204 NO CONTENT |
Phaneendra Manda | 6109f95 | 2015-10-29 16:59:30 +0530 | [diff] [blame] | 160 | */ |
| 161 | @Path("{group_id}") |
| 162 | @DELETE |
Wu wenbin | d0b119f | 2016-05-11 18:03:41 +0800 | [diff] [blame] | 163 | @Produces(MediaType.APPLICATION_JSON) |
| 164 | @Consumes(MediaType.APPLICATION_JSON) |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 165 | public Response deletePortPairGroup(@PathParam("group_id") String id) { |
Phaneendra Manda | 6109f95 | 2015-10-29 16:59:30 +0530 | [diff] [blame] | 166 | log.debug("Deletes port pair group by identifier {}.", id); |
Mahesh Poojary Huawei | f5f1edd | 2015-11-05 11:28:01 +0530 | [diff] [blame] | 167 | PortPairGroupId portPairGroupId = PortPairGroupId.of(id); |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 168 | Boolean issuccess = nullIsNotFound(get(PortPairGroupService.class).removePortPairGroup(portPairGroupId), |
Phaneendra Manda | 6109f95 | 2015-10-29 16:59:30 +0530 | [diff] [blame] | 169 | PORT_PAIR_GROUP_NOT_FOUND); |
| 170 | if (!issuccess) { |
| 171 | log.debug("Port pair group identifier {} does not exist", id); |
| 172 | } |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 173 | |
| 174 | return Response.noContent().build(); |
Phaneendra Manda | 6109f95 | 2015-10-29 16:59:30 +0530 | [diff] [blame] | 175 | } |
| 176 | } |