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