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