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