Fix how default partitions are created

Change-Id: Icb4881fb87b982d88502a0201175dbc2bc209c9c
diff --git a/core/store/dist/src/main/java/org/onosproject/store/cluster/impl/DefaultPartition.java b/core/api/src/main/java/org/onosproject/cluster/DefaultPartition.java
similarity index 92%
rename from core/store/dist/src/main/java/org/onosproject/store/cluster/impl/DefaultPartition.java
rename to core/api/src/main/java/org/onosproject/cluster/DefaultPartition.java
index 82a3ba7..5832608 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/cluster/impl/DefaultPartition.java
+++ b/core/api/src/main/java/org/onosproject/cluster/DefaultPartition.java
@@ -13,17 +13,13 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.onosproject.store.cluster.impl;
+package org.onosproject.cluster;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
 import java.util.Collection;
 import java.util.Objects;
 
-import org.onosproject.cluster.NodeId;
-import org.onosproject.cluster.Partition;
-import org.onosproject.cluster.PartitionId;
-
 import com.google.common.base.MoreObjects;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Sets;
diff --git a/core/net/src/main/java/org/onosproject/cluster/impl/ClusterManager.java b/core/net/src/main/java/org/onosproject/cluster/impl/ClusterManager.java
index 11bba9c..9444969 100644
--- a/core/net/src/main/java/org/onosproject/cluster/impl/ClusterManager.java
+++ b/core/net/src/main/java/org/onosproject/cluster/impl/ClusterManager.java
@@ -34,6 +34,7 @@
 import org.onosproject.cluster.ClusterStore;
 import org.onosproject.cluster.ClusterStoreDelegate;
 import org.onosproject.cluster.ControllerNode;
+import org.onosproject.cluster.DefaultPartition;
 import org.onosproject.cluster.NodeId;
 import org.onosproject.cluster.Partition;
 import org.onosproject.cluster.PartitionId;
@@ -179,35 +180,18 @@
         Collections.sort(sorted, (o1, o2) -> o1.id().toString().compareTo(o2.id().toString()));
         Collection<Partition> partitions = Lists.newArrayList();
         // add p0 partition
-        partitions.add(new Partition() {
-            @Override
-            public PartitionId getId() {
-                return PartitionId.from((0));
-            }
-            @Override
-            public Collection<NodeId> getMembers() {
-                return Sets.newHashSet(Collections2.transform(nodes, ControllerNode::id));
-            }
-        });
+        partitions.add(new DefaultPartition(PartitionId.from(0),
+                                            Sets.newHashSet(Collections2.transform(nodes, ControllerNode::id))));
         // add extended partitions
         int length = nodes.size();
-        int count = 3;
+        int count = Math.min(3, length);
         for (int i = 0; i < length; i++) {
             int index = i;
             Set<NodeId> set = new HashSet<>(count);
             for (int j = 0; j < count; j++) {
                 set.add(sorted.get((i + j) % length).id());
             }
-            partitions.add(new Partition() {
-                @Override
-                public PartitionId getId() {
-                    return PartitionId.from((index + 1));
-                }
-                @Override
-                public Collection<NodeId> getMembers() {
-                    return set;
-                }
-            });
+            partitions.add(new DefaultPartition(PartitionId.from((index + 1)), set));
         }
         return partitions;
     }
diff --git a/core/store/dist/src/main/java/org/onosproject/store/cluster/impl/StaticClusterMetadataStore.java b/core/store/dist/src/main/java/org/onosproject/store/cluster/impl/StaticClusterMetadataStore.java
index da4ec99..06bdc3f 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/cluster/impl/StaticClusterMetadataStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/cluster/impl/StaticClusterMetadataStore.java
@@ -41,6 +41,7 @@
 import org.onosproject.cluster.ClusterMetadataStoreDelegate;
 import org.onosproject.cluster.ControllerNode;
 import org.onosproject.cluster.DefaultControllerNode;
+import org.onosproject.cluster.DefaultPartition;
 import org.onosproject.cluster.NodeId;
 import org.onosproject.cluster.Partition;
 import org.onosproject.cluster.PartitionId;
@@ -94,6 +95,8 @@
         module.addSerializer(ControllerNode.class, new ControllerNodeSerializer());
         module.addDeserializer(ControllerNode.class, new ControllerNodeDeserializer());
         module.addDeserializer(Partition.class, new PartitionDeserializer());
+        module.addSerializer(PartitionId.class, new PartitionIdSerializer());
+        module.addDeserializer(PartitionId.class, new PartitionIdDeserializer());
         mapper.registerModule(module);
         File metadataFile = new File(CLUSTER_METADATA_FILE);
         if (metadataFile.exists()) {
@@ -107,21 +110,14 @@
             String localIp = getSiteLocalAddress();
             ControllerNode localNode =
                     new DefaultControllerNode(new NodeId(localIp), IpAddress.valueOf(localIp), DEFAULT_ONOS_PORT);
-            Partition defaultPartition = new Partition() {
-                @Override
-                public PartitionId getId() {
-                    return PartitionId.from(1);
-                }
-
-                @Override
-                public Collection<NodeId> getMembers() {
-                    return Sets.newHashSet(localNode.id());
-                }
-            };
+            // p0 partition
+            Partition basePartition = new DefaultPartition(PartitionId.from(0), Sets.newHashSet(localNode.id()));
+            // p1 partition
+            Partition extendedPartition = new DefaultPartition(PartitionId.from(1), Sets.newHashSet(localNode.id()));
             metadata.set(ClusterMetadata.builder()
                     .withName("default")
                     .withControllerNodes(Arrays.asList(localNode))
-                    .withPartitions(Lists.newArrayList(defaultPartition))
+                    .withPartitions(Lists.newArrayList(basePartition, extendedPartition))
                     .build());
             version = System.currentTimeMillis();
         }
@@ -194,6 +190,23 @@
         }
     }
 
+    private static class PartitionIdSerializer extends JsonSerializer<PartitionId> {
+        @Override
+        public void serialize(PartitionId partitionId, JsonGenerator jgen, SerializerProvider provider)
+          throws IOException, JsonProcessingException {
+            jgen.writeNumber(partitionId.asInt());
+        }
+    }
+
+    private class PartitionIdDeserializer extends JsonDeserializer<PartitionId> {
+        @Override
+        public PartitionId deserialize(JsonParser jp, DeserializationContext ctxt)
+          throws IOException, JsonProcessingException {
+            JsonNode node = jp.getCodec().readTree(jp);
+            return new PartitionId(node.asInt());
+        }
+    }
+
     private static class ControllerNodeSerializer extends JsonSerializer<ControllerNode> {
         @Override
         public void serialize(ControllerNode node, JsonGenerator jgen, SerializerProvider provider)
diff --git a/providers/lldp/src/test/java/org/onosproject/provider/lldp/impl/LldpLinkProviderTest.java b/providers/lldp/src/test/java/org/onosproject/provider/lldp/impl/LldpLinkProviderTest.java
index 940be57..4215332 100644
--- a/providers/lldp/src/test/java/org/onosproject/provider/lldp/impl/LldpLinkProviderTest.java
+++ b/providers/lldp/src/test/java/org/onosproject/provider/lldp/impl/LldpLinkProviderTest.java
@@ -36,6 +36,7 @@
 import org.onosproject.cluster.ClusterMetadataService;
 import org.onosproject.cluster.ControllerNode;
 import org.onosproject.cluster.DefaultControllerNode;
+import org.onosproject.cluster.DefaultPartition;
 import org.onosproject.cluster.NodeId;
 import org.onosproject.cluster.Partition;
 import org.onosproject.cluster.PartitionId;
@@ -79,7 +80,6 @@
 import org.onosproject.net.provider.ProviderId;
 
 import java.nio.ByteBuffer;
-import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -953,16 +953,7 @@
         public ClusterMetadata getClusterMetadata() {
             final NodeId nid = new NodeId("test-node");
             final IpAddress addr = IpAddress.valueOf(0);
-            final Partition p = new Partition() {
-                public PartitionId getId() {
-                    return PartitionId.from(1);
-                }
-
-                @Override
-                public Collection<NodeId> getMembers() {
-                    return Sets.newHashSet(nid);
-                }
-            };
+            final Partition p = new DefaultPartition(PartitionId.from(1), Sets.newHashSet(nid));
             return ClusterMetadata.builder()
                     .withName("test-cluster")
                     .withControllerNodes(Sets.newHashSet(new DefaultControllerNode(nid, addr)))