[ONOS-4160] PCE REST
Change-Id: Iec0554190876c7363114c03aa5d2b25b42ade071
diff --git a/apps/pce/app/src/main/java/org/onosproject/pce/rest/PcePathWebResource.java b/apps/pce/app/src/main/java/org/onosproject/pce/rest/PcePathWebResource.java
new file mode 100644
index 0000000..abd7c88
--- /dev/null
+++ b/apps/pce/app/src/main/java/org/onosproject/pce/rest/PcePathWebResource.java
@@ -0,0 +1,207 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.pce.rest;
+
+import static javax.ws.rs.core.Response.Status.OK;
+import static org.onlab.util.Tools.nullIsNotFound;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+import java.util.LinkedList;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import org.onosproject.incubator.net.tunnel.Tunnel;
+import org.onosproject.incubator.net.tunnel.TunnelId;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.intent.Constraint;
+import org.onosproject.pce.pceservice.api.PceService;
+import org.onosproject.pce.pceservice.PcePath;
+import org.onosproject.pce.pceservice.DefaultPcePath;
+import org.onosproject.pce.pceservice.LspType;
+import org.onosproject.rest.AbstractWebResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+/**
+ * Query and program pce path.
+ */
+
+@Path("path")
+public class PcePathWebResource extends AbstractWebResource {
+
+ private final Logger log = LoggerFactory.getLogger(PcePathWebResource.class);
+ public static final String PCE_PATH_NOT_FOUND = "Path not found";
+ public static final String PCE_PATH_ID_EXIST = "Path exists";
+ public static final String PCE_PATH_ID_NOT_EXIST = "Path does not exist for the identifier";
+ public static final String PCE_SETUP_PATH_FAILED = "PCE Setup path has failed.";
+
+ /**
+ * Retrieve details of all paths created.
+ *
+ * @return 200 OK
+ */
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response queryAllPath() {
+ log.debug("Query all paths.");
+ Iterable<Tunnel> tunnels = get(PceService.class).queryAllPath();
+ ObjectNode result = mapper().createObjectNode();
+ ArrayNode pathEntry = result.putArray("paths");
+ if (tunnels != null) {
+ for (final Tunnel tunnel : tunnels) {
+ PcePath path = DefaultPcePath.builder().of(tunnel).build();
+ pathEntry.add(codec(PcePath.class).encode(path, this));
+ }
+ }
+ return ok(result.toString()).build();
+ }
+
+ /**
+ * Retrieve details of a specified path id.
+ *
+ * @param id path id
+ * @return 200 OK, 404 if given identifier does not exist
+ */
+ @GET
+ @Path("{path_id}")
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response queryPath(@PathParam("path_id") String id) {
+ log.debug("Query path by identifier {}.", id);
+ Tunnel tunnel = nullIsNotFound(get(PceService.class).queryPath(TunnelId.valueOf(id)),
+ PCE_PATH_NOT_FOUND);
+ PcePath path = DefaultPcePath.builder().of(tunnel).build();
+ ObjectNode result = mapper().createObjectNode();
+ result.set("path", codec(PcePath.class).encode(path, this));
+ return ok(result.toString()).build();
+ }
+
+ /**
+ * Creates a new path.
+ *
+ * @param stream pce path from json
+ * @return status of the request
+ */
+ @POST
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response setupPath(InputStream stream) {
+ log.debug("Setup path.");
+ try {
+ ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
+ JsonNode port = jsonTree.get("path");
+ PcePath path = codec(PcePath.class).decode((ObjectNode) port, this);
+
+ DeviceId srcDevice = DeviceId.deviceId(path.source());
+ DeviceId dstDevice = DeviceId.deviceId(path.destination());
+ LspType lspType = path.lspType();
+ List<Constraint> listConstrnt = new LinkedList<Constraint>();
+
+ // add cost
+ //TODO: need to uncomment below lines once Bandwidth and Cost constraint classes are ready
+ //CostConstraint.Type costType = CostConstraint.Type.values()[Integer.valueOf(path.constraint().cost())];
+ //listConstrnt.add(CostConstraint.of(costType));
+
+ // add bandwidth. Data rate unit is in BPS.
+ //listConstrnt.add(LocalBandwidthConstraint.of(Double.valueOf(path.constraint().bandwidth()), DataRateUnit
+ // .valueOf("BPS")));
+
+ Boolean issuccess = nullIsNotFound(get(PceService.class)
+ .setupPath(srcDevice, dstDevice, path.name(), listConstrnt, lspType),
+ PCE_SETUP_PATH_FAILED);
+ return Response.status(OK).entity(issuccess.toString()).build();
+ } catch (IOException e) {
+ log.error("Exception while creating path {}.", e.toString());
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ /**
+ * Update details of a specified path id.
+ *
+ * @param id path id
+ * @param stream pce path from json
+ * @return 200 OK, 404 if given identifier does not exist
+ */
+ @PUT
+ @Path("{path_id}")
+ @Produces(MediaType.APPLICATION_JSON)
+ @Consumes(MediaType.APPLICATION_JSON)
+ public Response updatePath(@PathParam("path_id") String id,
+ final InputStream stream) {
+ log.debug("Update path by identifier {}.", id);
+ try {
+ ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
+ JsonNode pathNode = jsonTree.get("path");
+ PcePath path = codec(PcePath.class).decode((ObjectNode) pathNode, this);
+ // Assign cost
+ List<Constraint> constrntList = new LinkedList<Constraint>();
+ //TODO: need to uncomment below lines once CostConstraint class is ready
+ if (path.costConstraint() != null) {
+ //CostConstraint.Type costType = CostConstraint.Type.values()[path.constraint().cost()];
+ //constrntList.add(CostConstraint.of(costType));
+ }
+
+ // Assign bandwidth. Data rate unit is in BPS.
+ if (path.bandwidthConstraint() != null) {
+ //TODO: need to uncomment below lines once BandwidthConstraint class is ready
+ //constrntList.add(LocalBandwidthConstraint
+ // .of(path.constraint().bandwidth(), DataRateUnit.valueOf("BPS")));
+ }
+
+ Boolean result = nullIsNotFound(get(PceService.class).updatePath(TunnelId.valueOf(id), constrntList),
+ PCE_PATH_NOT_FOUND);
+ return Response.status(OK).entity(result.toString()).build();
+ } catch (IOException e) {
+ log.error("Update path failed because of exception {}.", e.toString());
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ /**
+ * Release a specified path.
+ *
+ * @param id path id
+ * @return 200 OK, 404 if given identifier does not exist
+ */
+ @Path("{path_id}")
+ @DELETE
+ public Response releasePath(@PathParam("path_id") String id) {
+ log.debug("Deletes path by identifier {}.", id);
+
+ Boolean isSuccess = nullIsNotFound(get(PceService.class).releasePath(TunnelId.valueOf(id)),
+ PCE_PATH_NOT_FOUND);
+ if (!isSuccess) {
+ log.debug("Path identifier {} does not exist", id);
+ }
+
+ return Response.status(OK).entity(isSuccess.toString()).build();
+ }
+}