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 | */ |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame^] | 16 | package org.onosproject.pce.rest; |
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 | */ |
| 56 | |
| 57 | @Path("path") |
| 58 | public class PcePathWebResource extends AbstractWebResource { |
| 59 | |
| 60 | private final Logger log = LoggerFactory.getLogger(PcePathWebResource.class); |
| 61 | public static final String PCE_PATH_NOT_FOUND = "Path not found"; |
| 62 | public static final String PCE_PATH_ID_EXIST = "Path exists"; |
| 63 | 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^] | 64 | 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] | 65 | |
| 66 | /** |
| 67 | * Retrieve details of all paths created. |
| 68 | * |
| 69 | * @return 200 OK |
| 70 | */ |
| 71 | @GET |
| 72 | @Produces(MediaType.APPLICATION_JSON) |
| 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) |
| 96 | public Response queryPath(@PathParam("path_id") String id) { |
| 97 | log.debug("Query path by identifier {}.", id); |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame^] | 98 | Tunnel tunnel = nullIsNotFound(get(PceService.class).queryPath(TunnelId.valueOf(id)), |
| 99 | PCE_PATH_NOT_FOUND); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 100 | PcePath path = DefaultPcePath.builder().of(tunnel).build(); |
| 101 | ObjectNode result = mapper().createObjectNode(); |
| 102 | result.set("path", codec(PcePath.class).encode(path, this)); |
| 103 | return ok(result.toString()).build(); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Creates a new path. |
| 108 | * |
| 109 | * @param stream pce path from json |
| 110 | * @return status of the request |
| 111 | */ |
| 112 | @POST |
| 113 | @Consumes(MediaType.APPLICATION_JSON) |
| 114 | @Produces(MediaType.APPLICATION_JSON) |
| 115 | public Response setupPath(InputStream stream) { |
| 116 | log.debug("Setup path."); |
| 117 | try { |
| 118 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 119 | JsonNode port = jsonTree.get("path"); |
| 120 | PcePath path = codec(PcePath.class).decode((ObjectNode) port, this); |
| 121 | |
| 122 | DeviceId srcDevice = DeviceId.deviceId(path.source()); |
| 123 | DeviceId dstDevice = DeviceId.deviceId(path.destination()); |
| 124 | LspType lspType = path.lspType(); |
| 125 | List<Constraint> listConstrnt = new LinkedList<Constraint>(); |
| 126 | |
| 127 | // add cost |
| 128 | //TODO: need to uncomment below lines once Bandwidth and Cost constraint classes are ready |
| 129 | //CostConstraint.Type costType = CostConstraint.Type.values()[Integer.valueOf(path.constraint().cost())]; |
| 130 | //listConstrnt.add(CostConstraint.of(costType)); |
| 131 | |
| 132 | // add bandwidth. Data rate unit is in BPS. |
| 133 | //listConstrnt.add(LocalBandwidthConstraint.of(Double.valueOf(path.constraint().bandwidth()), DataRateUnit |
| 134 | // .valueOf("BPS"))); |
| 135 | |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame^] | 136 | Boolean issuccess = nullIsNotFound(get(PceService.class) |
| 137 | .setupPath(srcDevice, dstDevice, path.name(), listConstrnt, lspType), |
| 138 | PCE_SETUP_PATH_FAILED); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 139 | return Response.status(OK).entity(issuccess.toString()).build(); |
| 140 | } catch (IOException e) { |
| 141 | log.error("Exception while creating path {}.", e.toString()); |
| 142 | throw new IllegalArgumentException(e); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Update details of a specified path id. |
| 148 | * |
| 149 | * @param id path id |
| 150 | * @param stream pce path from json |
| 151 | * @return 200 OK, 404 if given identifier does not exist |
| 152 | */ |
| 153 | @PUT |
| 154 | @Path("{path_id}") |
| 155 | @Produces(MediaType.APPLICATION_JSON) |
| 156 | @Consumes(MediaType.APPLICATION_JSON) |
| 157 | public Response updatePath(@PathParam("path_id") String id, |
| 158 | final InputStream stream) { |
| 159 | log.debug("Update path by identifier {}.", id); |
| 160 | try { |
| 161 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 162 | JsonNode pathNode = jsonTree.get("path"); |
| 163 | PcePath path = codec(PcePath.class).decode((ObjectNode) pathNode, this); |
| 164 | // Assign cost |
| 165 | List<Constraint> constrntList = new LinkedList<Constraint>(); |
| 166 | //TODO: need to uncomment below lines once CostConstraint class is ready |
| 167 | if (path.costConstraint() != null) { |
| 168 | //CostConstraint.Type costType = CostConstraint.Type.values()[path.constraint().cost()]; |
| 169 | //constrntList.add(CostConstraint.of(costType)); |
| 170 | } |
| 171 | |
| 172 | // Assign bandwidth. Data rate unit is in BPS. |
| 173 | if (path.bandwidthConstraint() != null) { |
| 174 | //TODO: need to uncomment below lines once BandwidthConstraint class is ready |
| 175 | //constrntList.add(LocalBandwidthConstraint |
| 176 | // .of(path.constraint().bandwidth(), DataRateUnit.valueOf("BPS"))); |
| 177 | } |
| 178 | |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame^] | 179 | Boolean result = nullIsNotFound(get(PceService.class).updatePath(TunnelId.valueOf(id), constrntList), |
| 180 | PCE_PATH_NOT_FOUND); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 181 | return Response.status(OK).entity(result.toString()).build(); |
| 182 | } catch (IOException e) { |
| 183 | log.error("Update path failed because of exception {}.", e.toString()); |
| 184 | throw new IllegalArgumentException(e); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Release a specified path. |
| 190 | * |
| 191 | * @param id path id |
| 192 | * @return 200 OK, 404 if given identifier does not exist |
| 193 | */ |
| 194 | @Path("{path_id}") |
| 195 | @DELETE |
| 196 | public Response releasePath(@PathParam("path_id") String id) { |
| 197 | log.debug("Deletes path by identifier {}.", id); |
| 198 | |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame^] | 199 | Boolean isSuccess = nullIsNotFound(get(PceService.class).releasePath(TunnelId.valueOf(id)), |
| 200 | PCE_PATH_NOT_FOUND); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 201 | if (!isSuccess) { |
| 202 | log.debug("Path identifier {} does not exist", id); |
| 203 | } |
| 204 | |
| 205 | return Response.status(OK).entity(isSuccess.toString()).build(); |
| 206 | } |
| 207 | } |