[ONOS-4945] PCE Json fix

Change-Id: I701ed1044c603ea60becf8ba606098e4154a7733
diff --git a/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/PceCodecRegistrator.java b/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/PceCodecRegistrator.java
new file mode 100644
index 0000000..685d330
--- /dev/null
+++ b/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/PceCodecRegistrator.java
@@ -0,0 +1,49 @@
+/*
+ * 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.pcerest;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.codec.CodecService;
+import org.onosproject.pce.pceservice.PcePath;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Implementation of the json codec brokering service for pce app.
+ */
+@Component(immediate = true)
+public class PceCodecRegistrator {
+
+    private static Logger log = LoggerFactory.getLogger(PceCodecRegistrator.class);
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CodecService codecService;
+
+    @Activate
+    public void activate() {
+        codecService.registerCodec(PcePath.class, new PcePathCodec());
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        log.info("Stopped");
+    }
+}
diff --git a/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/PcePathCodec.java b/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/PcePathCodec.java
new file mode 100644
index 0000000..034d045
--- /dev/null
+++ b/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/PcePathCodec.java
@@ -0,0 +1,123 @@
+/*
+ * 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.pcerest;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.pce.pceservice.PcePath;
+import org.onosproject.pce.pceservice.DefaultPcePath;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.fasterxml.jackson.databind.JsonNode;
+
+/**
+ * PCE path json codec.
+ */
+public final class PcePathCodec extends JsonCodec<PcePath> {
+    private final Logger log = LoggerFactory.getLogger(PcePathCodec.class);
+    private static final String SOURCE = "source";
+    private static final String DESTINATION = "destination";
+    private static final String LSP_TYPE = "pathType";
+    private static final String SYMBOLIC_PATH_NAME = "name";
+    private static final String CONSTRAINT = "constraint";
+    private static final String COST = "cost";
+    private static final String BANDWIDTH = "bandwidth";
+    private static final String PATH_ID = "pathId";
+    private static final String MISSING_MEMBER_MESSAGE = " member is required in pce-path";
+
+    @Override
+    public PcePath decode(ObjectNode json, CodecContext context) {
+        if (json == null || !json.isObject()) {
+            log.error("Empty json input");
+            return null;
+        }
+
+        // build pce-path
+        PcePath.Builder resultBuilder = new DefaultPcePath.Builder();
+
+        // retrieve source
+        JsonNode jNode = json.get(SOURCE);
+        if (jNode != null) {
+            String src = jNode.asText();
+            resultBuilder.source(src);
+        }
+
+        // retrieve destination
+        jNode = json.get(DESTINATION);
+        if (jNode != null) {
+            String dst = jNode.asText();
+            resultBuilder.destination(dst);
+        }
+
+        // retrieve lsp-type
+        jNode = json.get(LSP_TYPE);
+        if (jNode != null) {
+            String lspType = jNode.asText();
+            resultBuilder.lspType(lspType);
+        }
+
+        // retrieve symbolic-path-name
+        jNode = json.get(SYMBOLIC_PATH_NAME);
+        if (jNode != null) {
+            String name = jNode.asText();
+            resultBuilder.name(name);
+        }
+
+        // retrieve constraint
+        JsonNode constraintJNode = (JsonNode) json.path(CONSTRAINT);
+        if ((constraintJNode != null) && (!constraintJNode.isMissingNode())) {
+            // retrieve cost
+            jNode = constraintJNode.get(COST);
+            if (jNode != null) {
+                String cost = jNode.asText();
+                resultBuilder.costConstraint(cost);
+            }
+
+            // retrieve bandwidth
+            jNode = constraintJNode.get(BANDWIDTH);
+            if (jNode != null) {
+                String bandwidth = jNode.asText();
+                resultBuilder.bandwidthConstraint(bandwidth);
+            }
+        }
+
+        return resultBuilder.build();
+    }
+
+    @Override
+    public ObjectNode encode(PcePath path, CodecContext context) {
+        checkNotNull(path, "path output cannot be null");
+        ObjectNode result = context.mapper()
+                .createObjectNode()
+                .put(PATH_ID, path.id().id())
+                .put(SOURCE, path.source())
+                .put(DESTINATION, path.destination())
+                .put(LSP_TYPE, path.lspType().type())
+                .put(SYMBOLIC_PATH_NAME, path.name());
+
+        ObjectNode constraintNode = context.mapper()
+                .createObjectNode()
+                .put(COST, path.costConstraint().toString())
+                .put(BANDWIDTH, path.bandwidthConstraint().toString());
+
+        result.set(CONSTRAINT, constraintNode);
+        return result;
+    }
+}
diff --git a/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/PcePathWebResource.java b/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/PcePathWebResource.java
new file mode 100644
index 0000000..1b7c48b
--- /dev/null
+++ b/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/PcePathWebResource.java
@@ -0,0 +1,203 @@
+/*
+ * 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.pcerest;
+
+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)
+    @Consumes(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)
+    @Consumes(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 bandwidth
+            listConstrnt.add(path.bandwidthConstraint());
+
+            // Add cost
+            listConstrnt.add(path.costConstraint());
+
+            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);
+            List<Constraint> constrntList = new LinkedList<Constraint>();
+            // Assign bandwidth
+            if (path.bandwidthConstraint() != null) {
+                constrntList.add(path.bandwidthConstraint());
+            }
+
+            // Assign cost
+            if (path.costConstraint() != null) {
+                constrntList.add(path.costConstraint());
+            }
+
+            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
+     */
+    @DELETE
+    @Path("{path_id}")
+    @Produces(MediaType.APPLICATION_JSON)
+    @Consumes(MediaType.APPLICATION_JSON)
+    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();
+    }
+}
diff --git a/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/PceWebApplication.java b/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/PceWebApplication.java
new file mode 100644
index 0000000..09fc42a
--- /dev/null
+++ b/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/PceWebApplication.java
@@ -0,0 +1,32 @@
+/*
+ * 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.pcerest;
+
+import org.onlab.rest.AbstractWebApplication;
+
+import java.util.Set;
+
+/**
+ * PCE rest api web application.
+ */
+public class PceWebApplication extends AbstractWebApplication {
+    @Override
+    public Set<Class<?>> getClasses() {
+        return getClasses(PcePathWebResource.class);
+    }
+}
+
diff --git a/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/package-info.java b/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/package-info.java
new file mode 100644
index 0000000..3cc04cd
--- /dev/null
+++ b/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * 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.
+ */
+
+/**
+ * PCE rest application.
+ */
+
+package org.onosproject.pcerest;
diff --git a/apps/pce/pcerest/src/main/resources/WEB-INF/web.xml b/apps/pce/pcerest/src/main/resources/WEB-INF/web.xml
new file mode 100644
index 0000000..66dc27a
--- /dev/null
+++ b/apps/pce/pcerest/src/main/resources/WEB-INF/web.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
+         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+         id="ONOS" version="2.5">
+    <display-name>PCE REST API v1.0</display-name>
+
+    <servlet>
+        <servlet-name>JAX-RS Service</servlet-name>
+        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
+        <init-param>
+            <param-name>javax.ws.rs.Application</param-name>
+            <param-value>org.onosproject.pcerest.PceWebApplication</param-value>
+        </init-param>
+        <load-on-startup>1</load-on-startup>
+    </servlet>
+
+    <servlet-mapping>
+        <servlet-name>JAX-RS Service</servlet-name>
+        <url-pattern>/*</url-pattern>
+    </servlet-mapping>
+</web-app>
diff --git a/apps/pce/pcerest/src/test/java/org/onosproject/pcerest/MockPceCodecContext.java b/apps/pce/pcerest/src/test/java/org/onosproject/pcerest/MockPceCodecContext.java
new file mode 100644
index 0000000..07b94fc
--- /dev/null
+++ b/apps/pce/pcerest/src/test/java/org/onosproject/pcerest/MockPceCodecContext.java
@@ -0,0 +1,67 @@
+/*
+ * 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.pcerest;
+
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.CodecService;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.codec.impl.CodecManager;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+/**
+ * Mock codec context for use in codec unit tests.
+ */
+public class MockPceCodecContext implements CodecContext {
+
+    private final ObjectMapper mapper = new ObjectMapper();
+    private final CodecManager codecManager = new CodecManager();
+    private final PceCodecRegistrator manager = new PceCodecRegistrator();
+
+    /**
+     * Constructs a new mock codec context.
+     */
+    public MockPceCodecContext() {
+        codecManager.activate();
+        manager.codecService = codecManager;
+        manager.activate();
+    }
+
+    @Override
+    public ObjectMapper mapper() {
+        return mapper;
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public <T> T getService(Class<T> serviceClass) {
+        return null;
+    }
+
+    @Override
+    public <T> JsonCodec<T> codec(Class<T> entityClass) {
+        return codecManager.getCodec(entityClass);
+    }
+
+    /**
+     * Get the codec manager.
+     *
+     * @return instance of codec manager
+     */
+    public CodecService codecManager() {
+        return codecManager;
+    }
+}
diff --git a/apps/pce/pcerest/src/test/java/org/onosproject/pcerest/PcePathCodecTest.java b/apps/pce/pcerest/src/test/java/org/onosproject/pcerest/PcePathCodecTest.java
new file mode 100644
index 0000000..b8d69c5
--- /dev/null
+++ b/apps/pce/pcerest/src/test/java/org/onosproject/pcerest/PcePathCodecTest.java
@@ -0,0 +1,99 @@
+/*
+ * 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.pcerest;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import org.onlab.util.DataRateUnit;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.pce.pceservice.PcePath;
+import org.onosproject.net.intent.constraint.BandwidthConstraint;
+import org.onosproject.net.intent.Constraint;
+import org.onosproject.pce.pceservice.constraint.CostConstraint;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+/**
+ * PCE path codec unit tests.
+ */
+public class PcePathCodecTest {
+
+    MockPceCodecContext context;
+    JsonCodec<PcePath> pcePathCodec;
+    /**
+     * Sets up for each test. Creates a context and fetches the PCE path codec.
+     */
+    @Before
+    public void setUp() {
+        context = new MockPceCodecContext();
+        pcePathCodec = context.codec(PcePath.class);
+        assertThat(pcePathCodec, notNullValue());
+    }
+
+    /**
+     * Reads in a pce-path from the given resource and decodes it.
+     *
+     * @param resourceName resource to use to read the json for the pce-path
+     * @return decoded pce-path
+     * @throws IOException if processing the resource fails
+     */
+    private PcePath getPcePath(String resourceName) throws IOException {
+        InputStream jsonStream = PcePathCodecTest.class
+                .getResourceAsStream(resourceName);
+        ObjectMapper mapper = new ObjectMapper();
+        JsonNode json = mapper.readTree(jsonStream);
+        assertThat(json, notNullValue());
+        PcePath pcePath = pcePathCodec.decode((ObjectNode) json, context);
+        assertThat(pcePath, notNullValue());
+        return pcePath;
+    }
+
+    /**
+     * Checks that a simple pce-path is decoded properly.
+     *
+     * @throws IOException if the resource cannot be processed
+     */
+    @Test
+    public void codecPcePathTest() throws IOException {
+
+        PcePath pcePath = getPcePath("pcePath.json");
+
+        assertThat(pcePath, notNullValue());
+
+        assertThat(pcePath.source().toString(), is("11.0.0.1"));
+        assertThat(pcePath.destination(), is("11.0.0.2"));
+        assertThat(pcePath.lspType().toString(), is("WITHOUT_SIGNALLING_AND_WITHOUT_SR"));
+        // testing cost type
+        String cost = "2";
+        Constraint costConstraint = CostConstraint.of(CostConstraint.Type.values()[Integer.valueOf(cost) - 1]);
+        assertThat(pcePath.costConstraint(), is(costConstraint));
+        // testing bandwidth
+        String bandwidth = "200";
+        Constraint bandwidthConstraint = BandwidthConstraint.of(Double.valueOf(bandwidth), DataRateUnit
+                    .valueOf("BPS"));
+        assertThat(pcePath.bandwidthConstraint(), is(bandwidthConstraint));
+    }
+}
diff --git a/apps/pce/pcerest/src/test/java/org/onosproject/pcerest/PcePathResourceTest.java b/apps/pce/pcerest/src/test/java/org/onosproject/pcerest/PcePathResourceTest.java
new file mode 100644
index 0000000..a61a294
--- /dev/null
+++ b/apps/pce/pcerest/src/test/java/org/onosproject/pcerest/PcePathResourceTest.java
@@ -0,0 +1,286 @@
+/*
+ * 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.pcerest;
+
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+
+import static org.onosproject.net.Link.Type.DIRECT;
+
+import com.eclipsesource.json.Json;
+import com.eclipsesource.json.JsonObject;
+
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.util.LinkedList;
+import java.util.List;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.NotFoundException;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import org.onlab.osgi.ServiceDirectory;
+import org.onlab.osgi.TestServiceDirectory;
+import org.onlab.packet.IpAddress;
+import org.onlab.rest.BaseResource;
+import org.onosproject.codec.CodecService;
+import org.onosproject.core.DefaultGroupId;
+import org.onosproject.incubator.net.tunnel.DefaultTunnel;
+import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
+import org.onosproject.incubator.net.tunnel.Tunnel;
+import org.onosproject.incubator.net.tunnel.TunnelId;
+import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
+import org.onosproject.incubator.net.tunnel.TunnelName;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.DefaultLink;
+import org.onosproject.net.DefaultPath;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Link;
+import org.onosproject.pce.pceservice.api.PceService;
+import org.onosproject.pce.pceservice.PcepAnnotationKeys;
+import org.onosproject.net.Path;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.provider.ProviderId;
+
+/**
+ * Unit tests for pce path REST APIs.
+ */
+public class PcePathResourceTest extends PceResourceTest {
+    private final PceService pceService = createMock(PceService.class);
+    private final TunnelEndPoint src = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(23423));
+    private final TunnelEndPoint dst = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(32421));
+    private final DefaultGroupId groupId = new DefaultGroupId(92034);
+    private final TunnelName tunnelName = TunnelName.tunnelName("TunnelName");
+    private final TunnelId tunnelId = TunnelId.valueOf("41654654");
+    private final ProviderId producerName = new ProviderId("producer1", "13");
+    private Path path;
+    private Tunnel tunnel;
+    private DeviceId deviceId1;
+    private DeviceId deviceId2;
+    private DeviceId deviceId3;
+    private DeviceId deviceId4;
+    private DeviceId deviceId5;
+    private PortNumber port1;
+    private PortNumber port2;
+    private PortNumber port3;
+    private PortNumber port4;
+    private PortNumber port5;
+
+    /**
+     * Sets up the global values for all the tests.
+     */
+    @Before
+    public void setUpTest() {
+       // Mock environment setup
+       MockPceCodecContext context = new MockPceCodecContext();
+       ServiceDirectory testDirectory = new TestServiceDirectory().add(PceService.class, pceService)
+                                                                  .add(CodecService.class, context.codecManager());
+       BaseResource.setServiceDirectory(testDirectory);
+
+       // Tunnel creation
+       // Links
+       ProviderId providerId = new ProviderId("of", "foo");
+       deviceId1 = DeviceId.deviceId("of:A");
+       deviceId2 = DeviceId.deviceId("of:B");
+       deviceId3 = DeviceId.deviceId("of:C");
+       deviceId4 = DeviceId.deviceId("of:D");
+       deviceId5 = DeviceId.deviceId("of:E");
+       port1 = PortNumber.portNumber(1);
+       port2 = PortNumber.portNumber(2);
+       port3 = PortNumber.portNumber(3);
+       port4 = PortNumber.portNumber(4);
+       port5 = PortNumber.portNumber(5);
+       List<Link> linkList = new LinkedList<>();
+
+       Link l1 = DefaultLink.builder()
+                            .providerId(providerId)
+                            .annotations(DefaultAnnotations.builder().set("key1", "yahoo").build())
+                            .src(new ConnectPoint(deviceId1, port1))
+                            .dst(new ConnectPoint(deviceId2, port2))
+                            .type(DIRECT)
+                            .state(Link.State.ACTIVE)
+                            .build();
+       linkList.add(l1);
+       Link l2 = DefaultLink.builder()
+                            .providerId(providerId)
+                            .annotations(DefaultAnnotations.builder().set("key2", "yahoo").build())
+                            .src(new ConnectPoint(deviceId2, port2))
+                            .dst(new ConnectPoint(deviceId3, port3))
+                            .type(DIRECT)
+                            .state(Link.State.ACTIVE)
+                            .build();
+       linkList.add(l2);
+       Link l3 = DefaultLink.builder()
+                            .providerId(providerId)
+                            .annotations(DefaultAnnotations.builder().set("key3", "yahoo").build())
+                            .src(new ConnectPoint(deviceId3, port3))
+                            .dst(new ConnectPoint(deviceId4, port4))
+                            .type(DIRECT)
+                            .state(Link.State.ACTIVE)
+                            .build();
+       linkList.add(l3);
+       Link l4 = DefaultLink.builder()
+                            .providerId(providerId)
+                            .annotations(DefaultAnnotations.builder().set("key4", "yahoo").build())
+                            .src(new ConnectPoint(deviceId4, port4))
+                            .dst(new ConnectPoint(deviceId5, port5))
+                            .type(DIRECT)
+                            .state(Link.State.ACTIVE)
+                            .build();
+       linkList.add(l4);
+
+       // Path
+       path = new DefaultPath(providerId, linkList, 10);
+
+       // Annotations
+       DefaultAnnotations.Builder builderAnn = DefaultAnnotations.builder();
+       builderAnn.set(PcepAnnotationKeys.LSP_SIG_TYPE, "WITH_SIGNALLING");
+       builderAnn.set(PcepAnnotationKeys.COST_TYPE, "COST");
+       builderAnn.set(PcepAnnotationKeys.BANDWIDTH, "200");
+
+       // Tunnel
+       tunnel = new DefaultTunnel(producerName, src, dst, Tunnel.Type.VXLAN,
+                                  Tunnel.State.ACTIVE, groupId, tunnelId,
+                                  tunnelName, path, builderAnn.build());
+    }
+
+    /**
+     * Cleans up.
+     */
+    @After
+    public void tearDownTest() {
+    }
+
+    /**
+     * Tests the result of the rest api GET when there are no pce paths.
+     */
+    @Test
+    public void testPcePathsEmpty() {
+        expect(pceService.queryAllPath())
+                         .andReturn(null)
+                         .anyTimes();
+        replay(pceService);
+        WebTarget wt = target();
+        String response = wt.path("path").request().get(String.class);
+        assertThat(response, is("{\"paths\":[]}"));
+    }
+
+    /**
+     * Tests the result of a rest api GET for pce path id.
+     */
+    @Test
+    public void testGetTunnelId() {
+        expect(pceService.queryPath(anyObject()))
+                         .andReturn(tunnel)
+                         .anyTimes();
+        replay(pceService);
+
+        WebTarget wt = target();
+        String response = wt.path("path/1").request().get(String.class);
+        JsonObject result = Json.parse(response).asObject();
+        assertThat(result, notNullValue());
+    }
+
+    /**
+     * Tests that a fetch of a non-existent pce path object throws an exception.
+     */
+    @Test
+    public void testBadGet() {
+        expect(pceService.queryPath(anyObject()))
+                         .andReturn(null)
+                         .anyTimes();
+        replay(pceService);
+
+        WebTarget wt = target();
+        try {
+            wt.path("path/1").request().get(String.class);
+            fail("Fetch of non-existent pce path did not throw an exception");
+        } catch (NotFoundException ex) {
+            assertThat(ex.getMessage(), containsString("HTTP 404 Not Found"));
+        }
+    }
+
+    /**
+     * Tests creating a pce path with POST.
+     */
+    @Test
+    public void testPost() {
+        expect(pceService.setupPath(anyObject(), anyObject(), anyObject(), anyObject(), anyObject()))
+                         .andReturn(true)
+                         .anyTimes();
+        replay(pceService);
+
+        WebTarget wt = target();
+        InputStream jsonStream = PcePathResourceTest.class.getResourceAsStream("post-PcePath.json");
+
+        Response response = wt.path("path")
+                              .request(MediaType.APPLICATION_JSON_TYPE)
+                              .post(Entity.json(jsonStream));
+        assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
+    }
+
+    /**
+     * Tests creating a pce path with PUT.
+     */
+    @Test
+    public void testPut() {
+        expect(pceService.updatePath(anyObject(), anyObject()))
+                         .andReturn(true)
+                         .anyTimes();
+        replay(pceService);
+
+        WebTarget wt = target();
+        InputStream jsonStream = PcePathResourceTest.class.getResourceAsStream("post-PcePath.json");
+
+        Response response = wt.path("path/1")
+                              .request(MediaType.APPLICATION_JSON_TYPE)
+                              .put(Entity.json(jsonStream));
+        assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
+    }
+
+    /**
+     * Tests deleting a pce path.
+     */
+    @Test
+    public void testDelete() {
+        expect(pceService.releasePath(anyObject()))
+                         .andReturn(true)
+                         .anyTimes();
+        replay(pceService);
+
+        WebTarget wt = target();
+
+        String location = "path/1";
+
+        Response deleteResponse = wt.path(location)
+                                    .request(MediaType.APPLICATION_JSON_TYPE)
+                                    .delete();
+        assertThat(deleteResponse.getStatus(), is(HttpURLConnection.HTTP_OK));
+    }
+}
diff --git a/apps/pce/pcerest/src/test/java/org/onosproject/pcerest/PceResourceTest.java b/apps/pce/pcerest/src/test/java/org/onosproject/pcerest/PceResourceTest.java
new file mode 100644
index 0000000..18fa46b
--- /dev/null
+++ b/apps/pce/pcerest/src/test/java/org/onosproject/pcerest/PceResourceTest.java
@@ -0,0 +1,32 @@
+/*
+ * 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.pcerest;
+
+import org.glassfish.jersey.server.ResourceConfig;
+import org.glassfish.jersey.test.JerseyTest;
+
+/**
+ * Base class for pce rest api tests.  Performs common configuration operations.
+ */
+public class PceResourceTest extends JerseyTest {
+
+    /**
+     * Creates a new web-resource test.
+     */
+    public PceResourceTest() {
+        super(ResourceConfig.forApplicationClass(PceWebApplication.class));
+    }
+}
diff --git a/apps/pce/pcerest/src/test/resources/org/onosproject/pcerest/pcePath.json b/apps/pce/pcerest/src/test/resources/org/onosproject/pcerest/pcePath.json
new file mode 100644
index 0000000..4e6084e
--- /dev/null
+++ b/apps/pce/pcerest/src/test/resources/org/onosproject/pcerest/pcePath.json
@@ -0,0 +1,11 @@
+{
+   "source":"11.0.0.1",
+   "destination":"11.0.0.2",
+   "pathType":"2",
+   "name":"pcc2",
+   "description":"path-create",
+   "constraint":
+    {  "cost":2,
+       "bandwidth":200.0
+    }
+}
diff --git a/apps/pce/pcerest/src/test/resources/org/onosproject/pcerest/post-PcePath.json b/apps/pce/pcerest/src/test/resources/org/onosproject/pcerest/post-PcePath.json
new file mode 100644
index 0000000..fcd99e2
--- /dev/null
+++ b/apps/pce/pcerest/src/test/resources/org/onosproject/pcerest/post-PcePath.json
@@ -0,0 +1,11 @@
+{"path": {"source":"11.0.0.1",
+          "destination":"11.0.0.2",
+          "pathType":"2",
+          "name":"pcc2",
+          "description":"path-create",
+          "constraint":
+            {"cost":2,
+             "bandwidth":200.0
+            }
+         }
+}