Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 1 | /* |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +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 | */ |
Priyanka B | b977f56 | 2016-07-22 13:02:03 +0530 | [diff] [blame] | 16 | package org.onosproject.pcerest; |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 17 | |
| 18 | import static javax.ws.rs.core.Response.Status.OK; |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 19 | import static org.onlab.util.Tools.nullIsNotFound; |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 20 | |
Priyanka B | f97e13d | 2016-07-28 10:57:15 +0530 | [diff] [blame] | 21 | import java.util.Collection; |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 22 | import java.io.IOException; |
| 23 | import java.io.InputStream; |
| 24 | import java.util.List; |
| 25 | import java.util.LinkedList; |
| 26 | |
| 27 | import javax.ws.rs.Consumes; |
| 28 | import javax.ws.rs.DELETE; |
| 29 | import javax.ws.rs.GET; |
| 30 | import javax.ws.rs.POST; |
| 31 | import javax.ws.rs.PUT; |
| 32 | import javax.ws.rs.Path; |
| 33 | import javax.ws.rs.PathParam; |
| 34 | import javax.ws.rs.Produces; |
| 35 | import javax.ws.rs.core.MediaType; |
| 36 | import javax.ws.rs.core.Response; |
| 37 | |
| 38 | import org.onosproject.incubator.net.tunnel.Tunnel; |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 39 | import org.onosproject.incubator.net.tunnel.TunnelId; |
Priyanka B | f97e13d | 2016-07-28 10:57:15 +0530 | [diff] [blame] | 40 | import org.onosproject.incubator.net.tunnel.TunnelService; |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 41 | import org.onosproject.net.DeviceId; |
| 42 | import org.onosproject.net.intent.Constraint; |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 43 | import org.onosproject.pce.pceservice.api.PceService; |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 44 | import org.onosproject.pce.pceservice.PcePath; |
| 45 | import org.onosproject.pce.pceservice.DefaultPcePath; |
| 46 | import org.onosproject.pce.pceservice.LspType; |
| 47 | import org.onosproject.rest.AbstractWebResource; |
| 48 | import org.slf4j.Logger; |
| 49 | import org.slf4j.LoggerFactory; |
| 50 | |
| 51 | import com.fasterxml.jackson.databind.JsonNode; |
| 52 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 53 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 54 | |
| 55 | /** |
| 56 | * Query and program pce path. |
| 57 | */ |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 58 | @Path("path") |
| 59 | public class PcePathWebResource extends AbstractWebResource { |
| 60 | |
| 61 | private final Logger log = LoggerFactory.getLogger(PcePathWebResource.class); |
| 62 | public static final String PCE_PATH_NOT_FOUND = "Path not found"; |
| 63 | public static final String PCE_PATH_ID_EXIST = "Path exists"; |
| 64 | public static final String PCE_PATH_ID_NOT_EXIST = "Path does not exist for the identifier"; |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 65 | public static final String PCE_SETUP_PATH_FAILED = "PCE Setup path has failed."; |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * Retrieve details of all paths created. |
| 69 | * |
| 70 | * @return 200 OK |
| 71 | */ |
| 72 | @GET |
| 73 | @Produces(MediaType.APPLICATION_JSON) |
Priyanka B | b977f56 | 2016-07-22 13:02:03 +0530 | [diff] [blame] | 74 | @Consumes(MediaType.APPLICATION_JSON) |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 75 | public Response queryAllPath() { |
| 76 | log.debug("Query all paths."); |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 77 | Iterable<Tunnel> tunnels = get(PceService.class).queryAllPath(); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 78 | ObjectNode result = mapper().createObjectNode(); |
| 79 | ArrayNode pathEntry = result.putArray("paths"); |
| 80 | if (tunnels != null) { |
| 81 | for (final Tunnel tunnel : tunnels) { |
| 82 | PcePath path = DefaultPcePath.builder().of(tunnel).build(); |
| 83 | pathEntry.add(codec(PcePath.class).encode(path, this)); |
| 84 | } |
| 85 | } |
| 86 | return ok(result.toString()).build(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Retrieve details of a specified path id. |
| 91 | * |
| 92 | * @param id path id |
| 93 | * @return 200 OK, 404 if given identifier does not exist |
| 94 | */ |
| 95 | @GET |
| 96 | @Path("{path_id}") |
| 97 | @Produces(MediaType.APPLICATION_JSON) |
Priyanka B | b977f56 | 2016-07-22 13:02:03 +0530 | [diff] [blame] | 98 | @Consumes(MediaType.APPLICATION_JSON) |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 99 | public Response queryPath(@PathParam("path_id") String id) { |
| 100 | log.debug("Query path by identifier {}.", id); |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 101 | Tunnel tunnel = nullIsNotFound(get(PceService.class).queryPath(TunnelId.valueOf(id)), |
| 102 | PCE_PATH_NOT_FOUND); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 103 | PcePath path = DefaultPcePath.builder().of(tunnel).build(); |
Priyanka B | 3fdb9dd | 2016-08-08 10:47:24 +0530 | [diff] [blame^] | 104 | if (path == null) { |
| 105 | return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build(); |
| 106 | } |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 107 | ObjectNode result = mapper().createObjectNode(); |
| 108 | result.set("path", codec(PcePath.class).encode(path, this)); |
| 109 | return ok(result.toString()).build(); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Creates a new path. |
| 114 | * |
| 115 | * @param stream pce path from json |
| 116 | * @return status of the request |
| 117 | */ |
| 118 | @POST |
| 119 | @Consumes(MediaType.APPLICATION_JSON) |
| 120 | @Produces(MediaType.APPLICATION_JSON) |
| 121 | public Response setupPath(InputStream stream) { |
| 122 | log.debug("Setup path."); |
| 123 | try { |
| 124 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 125 | JsonNode port = jsonTree.get("path"); |
Priyanka B | f97e13d | 2016-07-28 10:57:15 +0530 | [diff] [blame] | 126 | TunnelService tunnelService = get(TunnelService.class); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 127 | PcePath path = codec(PcePath.class).decode((ObjectNode) port, this); |
Priyanka B | 3fdb9dd | 2016-08-08 10:47:24 +0530 | [diff] [blame^] | 128 | if (path == null) { |
| 129 | return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build(); |
| 130 | } |
| 131 | |
Priyanka B | f97e13d | 2016-07-28 10:57:15 +0530 | [diff] [blame] | 132 | //Validating tunnel name, duplicated tunnel names not allowed |
| 133 | Collection<Tunnel> existingTunnels = tunnelService.queryTunnel(Tunnel.Type.MPLS); |
| 134 | if (existingTunnels != null) { |
| 135 | for (Tunnel t : existingTunnels) { |
| 136 | if (t.tunnelName().toString().equals(path.name())) { |
| 137 | return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build(); |
| 138 | } |
| 139 | } |
| 140 | } |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 141 | |
| 142 | DeviceId srcDevice = DeviceId.deviceId(path.source()); |
| 143 | DeviceId dstDevice = DeviceId.deviceId(path.destination()); |
| 144 | LspType lspType = path.lspType(); |
| 145 | List<Constraint> listConstrnt = new LinkedList<Constraint>(); |
| 146 | |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 147 | // Add bandwidth |
| 148 | listConstrnt.add(path.bandwidthConstraint()); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 149 | |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 150 | // Add cost |
| 151 | listConstrnt.add(path.costConstraint()); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 152 | |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 153 | Boolean issuccess = nullIsNotFound(get(PceService.class) |
| 154 | .setupPath(srcDevice, dstDevice, path.name(), listConstrnt, lspType), |
| 155 | PCE_SETUP_PATH_FAILED); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 156 | return Response.status(OK).entity(issuccess.toString()).build(); |
| 157 | } catch (IOException e) { |
| 158 | log.error("Exception while creating path {}.", e.toString()); |
| 159 | throw new IllegalArgumentException(e); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Update details of a specified path id. |
| 165 | * |
| 166 | * @param id path id |
| 167 | * @param stream pce path from json |
| 168 | * @return 200 OK, 404 if given identifier does not exist |
| 169 | */ |
| 170 | @PUT |
| 171 | @Path("{path_id}") |
| 172 | @Produces(MediaType.APPLICATION_JSON) |
| 173 | @Consumes(MediaType.APPLICATION_JSON) |
| 174 | public Response updatePath(@PathParam("path_id") String id, |
| 175 | final InputStream stream) { |
| 176 | log.debug("Update path by identifier {}.", id); |
| 177 | try { |
| 178 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 179 | JsonNode pathNode = jsonTree.get("path"); |
| 180 | PcePath path = codec(PcePath.class).decode((ObjectNode) pathNode, this); |
Priyanka B | 3fdb9dd | 2016-08-08 10:47:24 +0530 | [diff] [blame^] | 181 | if (path == null) { |
| 182 | return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build(); |
| 183 | } |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 184 | List<Constraint> constrntList = new LinkedList<Constraint>(); |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 185 | // Assign bandwidth |
| 186 | if (path.bandwidthConstraint() != null) { |
| 187 | constrntList.add(path.bandwidthConstraint()); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 188 | } |
| 189 | |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 190 | // Assign cost |
| 191 | if (path.costConstraint() != null) { |
| 192 | constrntList.add(path.costConstraint()); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 193 | } |
| 194 | |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 195 | Boolean result = nullIsNotFound(get(PceService.class).updatePath(TunnelId.valueOf(id), constrntList), |
| 196 | PCE_PATH_NOT_FOUND); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 197 | return Response.status(OK).entity(result.toString()).build(); |
| 198 | } catch (IOException e) { |
| 199 | log.error("Update path failed because of exception {}.", e.toString()); |
| 200 | throw new IllegalArgumentException(e); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Release a specified path. |
| 206 | * |
| 207 | * @param id path id |
| 208 | * @return 200 OK, 404 if given identifier does not exist |
| 209 | */ |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 210 | @DELETE |
Priyanka B | b977f56 | 2016-07-22 13:02:03 +0530 | [diff] [blame] | 211 | @Path("{path_id}") |
| 212 | @Produces(MediaType.APPLICATION_JSON) |
| 213 | @Consumes(MediaType.APPLICATION_JSON) |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 214 | public Response releasePath(@PathParam("path_id") String id) { |
| 215 | log.debug("Deletes path by identifier {}.", id); |
| 216 | |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 217 | Boolean isSuccess = nullIsNotFound(get(PceService.class).releasePath(TunnelId.valueOf(id)), |
| 218 | PCE_PATH_NOT_FOUND); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 219 | if (!isSuccess) { |
| 220 | log.debug("Path identifier {} does not exist", id); |
| 221 | } |
| 222 | |
| 223 | return Response.status(OK).entity(isSuccess.toString()).build(); |
| 224 | } |
| 225 | } |