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(); |
| 104 | ObjectNode result = mapper().createObjectNode(); |
| 105 | result.set("path", codec(PcePath.class).encode(path, this)); |
| 106 | return ok(result.toString()).build(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Creates a new path. |
| 111 | * |
| 112 | * @param stream pce path from json |
| 113 | * @return status of the request |
| 114 | */ |
| 115 | @POST |
| 116 | @Consumes(MediaType.APPLICATION_JSON) |
| 117 | @Produces(MediaType.APPLICATION_JSON) |
| 118 | public Response setupPath(InputStream stream) { |
| 119 | log.debug("Setup path."); |
| 120 | try { |
| 121 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 122 | JsonNode port = jsonTree.get("path"); |
Priyanka B | f97e13d | 2016-07-28 10:57:15 +0530 | [diff] [blame] | 123 | TunnelService tunnelService = get(TunnelService.class); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 124 | PcePath path = codec(PcePath.class).decode((ObjectNode) port, this); |
Priyanka B | f97e13d | 2016-07-28 10:57:15 +0530 | [diff] [blame] | 125 | //Validating tunnel name, duplicated tunnel names not allowed |
| 126 | Collection<Tunnel> existingTunnels = tunnelService.queryTunnel(Tunnel.Type.MPLS); |
| 127 | if (existingTunnels != null) { |
| 128 | for (Tunnel t : existingTunnels) { |
| 129 | if (t.tunnelName().toString().equals(path.name())) { |
| 130 | return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build(); |
| 131 | } |
| 132 | } |
| 133 | } |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 134 | |
| 135 | DeviceId srcDevice = DeviceId.deviceId(path.source()); |
| 136 | DeviceId dstDevice = DeviceId.deviceId(path.destination()); |
| 137 | LspType lspType = path.lspType(); |
| 138 | List<Constraint> listConstrnt = new LinkedList<Constraint>(); |
| 139 | |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 140 | // Add bandwidth |
| 141 | listConstrnt.add(path.bandwidthConstraint()); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 142 | |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 143 | // Add cost |
| 144 | listConstrnt.add(path.costConstraint()); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 145 | |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 146 | Boolean issuccess = nullIsNotFound(get(PceService.class) |
| 147 | .setupPath(srcDevice, dstDevice, path.name(), listConstrnt, lspType), |
| 148 | PCE_SETUP_PATH_FAILED); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 149 | return Response.status(OK).entity(issuccess.toString()).build(); |
| 150 | } catch (IOException e) { |
| 151 | log.error("Exception while creating path {}.", e.toString()); |
| 152 | throw new IllegalArgumentException(e); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Update details of a specified path id. |
| 158 | * |
| 159 | * @param id path id |
| 160 | * @param stream pce path from json |
| 161 | * @return 200 OK, 404 if given identifier does not exist |
| 162 | */ |
| 163 | @PUT |
| 164 | @Path("{path_id}") |
| 165 | @Produces(MediaType.APPLICATION_JSON) |
| 166 | @Consumes(MediaType.APPLICATION_JSON) |
| 167 | public Response updatePath(@PathParam("path_id") String id, |
| 168 | final InputStream stream) { |
| 169 | log.debug("Update path by identifier {}.", id); |
| 170 | try { |
| 171 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 172 | JsonNode pathNode = jsonTree.get("path"); |
| 173 | PcePath path = codec(PcePath.class).decode((ObjectNode) pathNode, this); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 174 | List<Constraint> constrntList = new LinkedList<Constraint>(); |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 175 | // Assign bandwidth |
| 176 | if (path.bandwidthConstraint() != null) { |
| 177 | constrntList.add(path.bandwidthConstraint()); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 178 | } |
| 179 | |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 180 | // Assign cost |
| 181 | if (path.costConstraint() != null) { |
| 182 | constrntList.add(path.costConstraint()); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 183 | } |
| 184 | |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 185 | Boolean result = nullIsNotFound(get(PceService.class).updatePath(TunnelId.valueOf(id), constrntList), |
| 186 | PCE_PATH_NOT_FOUND); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 187 | return Response.status(OK).entity(result.toString()).build(); |
| 188 | } catch (IOException e) { |
| 189 | log.error("Update path failed because of exception {}.", e.toString()); |
| 190 | throw new IllegalArgumentException(e); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Release a specified path. |
| 196 | * |
| 197 | * @param id path id |
| 198 | * @return 200 OK, 404 if given identifier does not exist |
| 199 | */ |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 200 | @DELETE |
Priyanka B | b977f56 | 2016-07-22 13:02:03 +0530 | [diff] [blame] | 201 | @Path("{path_id}") |
| 202 | @Produces(MediaType.APPLICATION_JSON) |
| 203 | @Consumes(MediaType.APPLICATION_JSON) |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 204 | public Response releasePath(@PathParam("path_id") String id) { |
| 205 | log.debug("Deletes path by identifier {}.", id); |
| 206 | |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 207 | Boolean isSuccess = nullIsNotFound(get(PceService.class).releasePath(TunnelId.valueOf(id)), |
| 208 | PCE_PATH_NOT_FOUND); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 209 | if (!isSuccess) { |
| 210 | log.debug("Path identifier {} does not exist", id); |
| 211 | } |
| 212 | |
| 213 | return Response.status(OK).entity(isSuccess.toString()).build(); |
| 214 | } |
| 215 | } |