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