Check if there's conflict in serializer registration Id.

- remove duplicate detected
- Add little head space, just in case Kryo start registering some more built-ins.

Change-Id: I8b1318d0e11ce1c0680aaef29cc88c96af78ae6f
diff --git a/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java b/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
index 9dd54c8..a62d422 100644
--- a/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
+++ b/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
@@ -347,7 +347,6 @@
                     DefaultFlowEntry.class,
                     StoredFlowEntry.class,
                     DefaultFlowRule.class,
-                    DefaultFlowEntry.class,
                     DefaultPacketRequest.class,
                     PacketPriority.class,
                     FlowEntry.FlowEntryState.class,
@@ -466,7 +465,6 @@
                     PartialFailureConstraint.class,
                     IntentOperation.class,
                     FlowRuleExtPayLoad.class,
-                    Frequency.class,
                     DefaultAnnotations.class,
                     PortStatistics.class,
                     DefaultPortStatistics.class,
diff --git a/utils/misc/src/main/java/org/onlab/util/KryoNamespace.java b/utils/misc/src/main/java/org/onlab/util/KryoNamespace.java
index c04a29f..b03d095 100644
--- a/utils/misc/src/main/java/org/onlab/util/KryoNamespace.java
+++ b/utils/misc/src/main/java/org/onlab/util/KryoNamespace.java
@@ -16,6 +16,7 @@
 package org.onlab.util;
 
 import com.esotericsoftware.kryo.Kryo;
+import com.esotericsoftware.kryo.Registration;
 import com.esotericsoftware.kryo.Serializer;
 import com.esotericsoftware.kryo.io.ByteBufferInput;
 import com.esotericsoftware.kryo.io.ByteBufferOutput;
@@ -59,7 +60,7 @@
     /**
      * Smallest ID free to use for user defined registrations.
      */
-    public static final int INITIAL_ID = 11;
+    public static final int INITIAL_ID = 16;
 
     private static final Logger log = getLogger(KryoNamespace.class);
 
@@ -379,6 +380,7 @@
      */
     @Override
     public Kryo create() {
+        log.trace("Creating Kryo instance for {}", this);
         Kryo kryo = new Kryo();
         kryo.setRegistrationRequired(registrationRequired);
 
@@ -392,17 +394,48 @@
                 id = kryo.getNextRegistrationId();
             }
             for (Pair<Class<?>, Serializer<?>> entry : block.types()) {
-                final Serializer<?> serializer = entry.getRight();
-                if (serializer == null) {
-                    kryo.register(entry.getLeft(), id++);
-                } else {
-                    kryo.register(entry.getLeft(), serializer, id++);
-                }
+                register(kryo, entry.getLeft(), entry.getRight(), id++);
             }
         }
         return kryo;
     }
 
+    /**
+     * Register {@code type} and {@code serializer} to {@code kryo} instance.
+     *
+     * @param kryo       Kryo instance
+     * @param type       type to register
+     * @param serializer Specific serializer to register or null to use default.
+     * @param id         type registration id to use
+     */
+    private static void register(Kryo kryo, Class<?> type, Serializer<?> serializer, int id) {
+        Registration existing = kryo.getRegistration(id);
+        if (existing != null) {
+            if (existing.getType() != type) {
+                log.error("Failed to register {} as {}, {} was already registered.",
+                          type, id, existing.getType());
+
+                throw new IllegalStateException(String.format(
+                          "Failed to register %s as %s, %s was already registered.",
+                          type, id, existing.getType()));
+            }
+            // falling through to register call for now.
+            // Consider skipping, if there's reasonable
+            // way to compare serializer equivalence.
+        }
+        Registration r;
+        if (serializer == null) {
+            r = kryo.register(type, id);
+        } else {
+            r = kryo.register(type, serializer, id);
+        }
+        if (r.getId() != id) {
+            log.warn("{} already registed as {}. Skipping {}.",
+                     r.getType(), r.getId(), id);
+        }
+        log.trace("{} registered as {}", r.getType(), r.getId());
+    }
+
     @Override
     public Kryo borrow() {
         return pool.borrow();