Fix for ONOS-5152:Encoder for Disjoint Rest API with NPE:onos-1.7

Change-Id: I172e247353f7d70f6d7761e8a6be27f21b3c2684
diff --git a/core/api/src/main/java/org/onosproject/net/DefaultDisjointPath.java b/core/api/src/main/java/org/onosproject/net/DefaultDisjointPath.java
index 1f549a6..c4e0398 100644
--- a/core/api/src/main/java/org/onosproject/net/DefaultDisjointPath.java
+++ b/core/api/src/main/java/org/onosproject/net/DefaultDisjointPath.java
@@ -45,6 +45,16 @@
         this.path2 = path2;
     }
 
+    /**
+     * Creates a disjoint path pair from single default paths.
+     *
+     * @param providerId provider identity
+     * @param path1      primary path
+     */
+    public DefaultDisjointPath(ProviderId providerId, DefaultPath path1) {
+        this(providerId, path1, null);
+    }
+
     @Override
     public List<Link> links() {
         if (usingPath1) {
diff --git a/core/api/src/main/java/org/onosproject/net/topology/AbstractPathService.java b/core/api/src/main/java/org/onosproject/net/topology/AbstractPathService.java
index d718d15..8b24cbb 100644
--- a/core/api/src/main/java/org/onosproject/net/topology/AbstractPathService.java
+++ b/core/api/src/main/java/org/onosproject/net/topology/AbstractPathService.java
@@ -239,6 +239,9 @@
             primary = path.primary();
             backup = path.backup();
         }
+        if (backup == null) {
+        return new DefaultDisjointPath(PID, (DefaultPath) edgeToEdgePath(srcLink, dstLink, primary));
+        }
         return new DefaultDisjointPath(PID, (DefaultPath) edgeToEdgePath(srcLink, dstLink, primary),
                                        (DefaultPath) edgeToEdgePath(srcLink, dstLink, backup));
     }
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java b/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
index da9b8f1..090727f 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
@@ -43,6 +43,7 @@
 import org.onosproject.net.Link;
 import org.onosproject.net.MastershipRole;
 import org.onosproject.net.Path;
+import org.onosproject.net.DisjointPath;
 import org.onosproject.net.Port;
 import org.onosproject.net.device.PortStatistics;
 import org.onosproject.net.driver.Driver;
@@ -123,6 +124,7 @@
         registerCodec(Topology.class, new TopologyCodec());
         registerCodec(TopologyCluster.class, new TopologyClusterCodec());
         registerCodec(Path.class, new PathCodec());
+        registerCodec(DisjointPath.class, new DisjointPathCodec());
         registerCodec(Group.class, new GroupCodec());
         registerCodec(Driver.class, new DriverCodec());
         registerCodec(GroupBucket.class, new GroupBucketCodec());
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/DisjointPathCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/DisjointPathCodec.java
new file mode 100644
index 0000000..585886d
--- /dev/null
+++ b/core/common/src/main/java/org/onosproject/codec/impl/DisjointPathCodec.java
@@ -0,0 +1,61 @@
+/*
+ * 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.codec.impl;
+
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.net.Link;
+import org.onosproject.net.DisjointPath;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * DisjointPath JSON codec.
+ */
+public final class DisjointPathCodec extends AnnotatedCodec<DisjointPath> {
+    @Override
+    public ObjectNode encode(DisjointPath disjointPath, CodecContext context) {
+        checkNotNull(disjointPath, "Path cannot be null");
+        JsonCodec<Link> codec = context.codec(Link.class);
+        ObjectNode result = context.mapper()
+                .createObjectNode();
+
+        ObjectNode primary =  context.mapper()
+                .createObjectNode()
+                .put("cost", disjointPath.primary().cost());
+
+        result.set("primary", primary);
+        ArrayNode jsonLinks = primary.putArray("links");
+        for (Link link : disjointPath.primary().links()) {
+               jsonLinks.add(codec.encode(link, context));
+        }
+        if (disjointPath.backup() != null) {
+            ObjectNode backup = context.mapper()
+                .createObjectNode()
+                .put("cost", disjointPath.backup().cost());
+            result.set("backup", backup);
+            ArrayNode jsonLinks1 = backup.putArray("links");
+            for (Link link1 : disjointPath.backup().links()) {
+                       jsonLinks1.add(codec.encode(link1, context));
+        }
+        }
+        return annotate(result, disjointPath, context);
+    }
+
+}
+
diff --git a/web/api/src/main/java/org/onosproject/rest/resources/PathsWebResource.java b/web/api/src/main/java/org/onosproject/rest/resources/PathsWebResource.java
index 5b1494b..856ea9c 100644
--- a/web/api/src/main/java/org/onosproject/rest/resources/PathsWebResource.java
+++ b/web/api/src/main/java/org/onosproject/rest/resources/PathsWebResource.java
@@ -77,12 +77,12 @@
     }
 
     /**
-     * Gets all shortest disjoint paths between any two hosts or devices.
-     * Returns array of all shortest disjoint paths between any two elements.
+     * Gets all shortest disjoint path pairs between any two hosts or devices.
+     * Returns array of all shortest disjoint path pairs between any two elements.
      * @onos.rsModel Paths
      * @param src source identifier
      * @param dst destination identifier
-     * @return 200 OK with array of all shortest disjoint paths between any two elements
+     * @return 200 OK with array of all shortest disjoint path pairs between any two elements
      */
     @GET
     @Produces(MediaType.APPLICATION_JSON)