Committed by
Ray Milkey
[ONOS-3110] PortPairWebResource for SFC
Change-Id: I58c6d915c0c07312632e9fdba9c2daeae4e0e6e4
Showing
1 changed file
with
161 additions
and
0 deletions
| 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 | + | ||
| 19 | +import static javax.ws.rs.core.Response.Status.NOT_FOUND; | ||
| 20 | +import static javax.ws.rs.core.Response.Status.OK; | ||
| 21 | +import static org.onlab.util.Tools.nullIsNotFound; | ||
| 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.PortPair; | ||
| 39 | +import org.onosproject.vtnrsc.PortPairId; | ||
| 40 | +import org.onosproject.vtnrsc.portpair.PortPairService; | ||
| 41 | +import org.onosproject.vtnweb.web.PortPairCodec; | ||
| 42 | +import org.slf4j.Logger; | ||
| 43 | +import org.slf4j.LoggerFactory; | ||
| 44 | + | ||
| 45 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 46 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 47 | + | ||
| 48 | +/** | ||
| 49 | + * Query and program port pair. | ||
| 50 | + */ | ||
| 51 | +@Path("port_pairs") | ||
| 52 | +public class PortPairWebResource extends AbstractWebResource { | ||
| 53 | + | ||
| 54 | + private final Logger log = LoggerFactory.getLogger(PortPairWebResource.class); | ||
| 55 | + private final PortPairService service = get(PortPairService.class); | ||
| 56 | + public static final String PORT_PAIR_NOT_FOUND = "Port pair not found"; | ||
| 57 | + public static final String PORT_PAIR_ID_EXIST = "Port pair exists"; | ||
| 58 | + public static final String PORT_PAIR_ID_NOT_EXIST = "Port pair does not exist with identifier"; | ||
| 59 | + | ||
| 60 | + /** | ||
| 61 | + * Get details of all port pairs created. | ||
| 62 | + * | ||
| 63 | + * @return 200 OK | ||
| 64 | + */ | ||
| 65 | + @GET | ||
| 66 | + @Produces(MediaType.APPLICATION_JSON) | ||
| 67 | + public Response getPortPairs() { | ||
| 68 | + Iterable<PortPair> portPairs = service.getPortPairs(); | ||
| 69 | + ObjectNode result = new ObjectMapper().createObjectNode(); | ||
| 70 | + result.set("port_pairs", new PortPairCodec().encode(portPairs, this)); | ||
| 71 | + return ok(result).build(); | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + /** | ||
| 75 | + * Get details of a specified port pair id. | ||
| 76 | + * | ||
| 77 | + * @param id port pair id | ||
| 78 | + * @return 200 OK, 404 if given identifier does not exist | ||
| 79 | + */ | ||
| 80 | + @GET | ||
| 81 | + @Path("{pair_id}") | ||
| 82 | + @Produces(MediaType.APPLICATION_JSON) | ||
| 83 | + public Response getPortPair(@PathParam("portPairId") String id) { | ||
| 84 | + | ||
| 85 | + if (!service.exists(PortPairId.portPairId(id))) { | ||
| 86 | + return Response.status(NOT_FOUND) | ||
| 87 | + .entity(PORT_PAIR_NOT_FOUND).build(); | ||
| 88 | + } | ||
| 89 | + PortPair portPair = nullIsNotFound(service.getPortPair(PortPairId.portPairId(id)), | ||
| 90 | + PORT_PAIR_NOT_FOUND); | ||
| 91 | + | ||
| 92 | + ObjectNode result = new ObjectMapper().createObjectNode(); | ||
| 93 | + result.set("port_pair", new PortPairCodec().encode(portPair, this)); | ||
| 94 | + return ok(result).build(); | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + /** | ||
| 98 | + * Creates a new port pair. | ||
| 99 | + * | ||
| 100 | + * @param stream port pair from JSON | ||
| 101 | + * @return status of the request - CREATED if the JSON is correct, | ||
| 102 | + * BAD_REQUEST if the JSON is invalid | ||
| 103 | + */ | ||
| 104 | + @POST | ||
| 105 | + @Consumes(MediaType.APPLICATION_JSON) | ||
| 106 | + @Produces(MediaType.APPLICATION_JSON) | ||
| 107 | + public Response createPortPair(InputStream stream) { | ||
| 108 | + try { | ||
| 109 | + ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); | ||
| 110 | + | ||
| 111 | + PortPair portPair = codec(PortPair.class).decode(jsonTree, this); | ||
| 112 | + Boolean isSuccess = nullIsNotFound(service.createPortPair(portPair), | ||
| 113 | + PORT_PAIR_NOT_FOUND); | ||
| 114 | + return Response.status(OK).entity(isSuccess.toString()).build(); | ||
| 115 | + } catch (IOException e) { | ||
| 116 | + log.error("Exception while creating port pair {}.", e.toString()); | ||
| 117 | + throw new IllegalArgumentException(e); | ||
| 118 | + } | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + /** | ||
| 122 | + * Update details of a specified port pair id. | ||
| 123 | + * | ||
| 124 | + * @param id port pair id | ||
| 125 | + * @param stream port pair from json | ||
| 126 | + * @return 200 OK, 404 if the given identifier does not exist | ||
| 127 | + */ | ||
| 128 | + @PUT | ||
| 129 | + @Path("{pair_id}") | ||
| 130 | + @Produces(MediaType.APPLICATION_JSON) | ||
| 131 | + @Consumes(MediaType.APPLICATION_JSON) | ||
| 132 | + public Response updatePortPair(@PathParam("pair_id") String id, | ||
| 133 | + final InputStream stream) { | ||
| 134 | + try { | ||
| 135 | + ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); | ||
| 136 | + PortPair portPair = codec(PortPair.class).decode(jsonTree, this); | ||
| 137 | + Boolean isSuccess = nullIsNotFound(service.updatePortPair(portPair), PORT_PAIR_NOT_FOUND); | ||
| 138 | + return Response.status(OK).entity(isSuccess.toString()).build(); | ||
| 139 | + } catch (IOException e) { | ||
| 140 | + log.error("Update port pair failed because of exception {}.", e.toString()); | ||
| 141 | + throw new IllegalArgumentException(e); | ||
| 142 | + } | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | + /** | ||
| 146 | + * Delete details of a specified port pair id. | ||
| 147 | + * | ||
| 148 | + * @param id port pair id | ||
| 149 | + */ | ||
| 150 | + @Path("{pair_id}") | ||
| 151 | + @DELETE | ||
| 152 | + public void deletePortPair(@PathParam("pair_id") String id) { | ||
| 153 | + | ||
| 154 | + PortPairId portPairId = PortPairId.portPairId(id); | ||
| 155 | + Boolean isSuccess = nullIsNotFound(service.removePortPair(portPairId), | ||
| 156 | + PORT_PAIR_NOT_FOUND); | ||
| 157 | + if (!isSuccess) { | ||
| 158 | + log.debug("Port pair identifier {} does not exist", id); | ||
| 159 | + } | ||
| 160 | + } | ||
| 161 | +} |
-
Please register or login to post a comment