Work toward ONOS-1451: Separate Event Key space per instance

Misc changes and cleanup (Step 2):
 * Added OnosInstanceId field to class TopologyEvent. For now it is
   just assigned, but is not used.

 * Minor refactoring of class TopologyEvent so it will be easier to make it
   immutable after its its internals and usage are inalized.

 * Modified the signature of method PathIntentMap.getIntentsByLink()
   to use LinkTuple for the lookup instead of LinkEvent.

 * Replaced (only inside onos/core/topology) the usage of Apache's
   Validate with Google's Preconditions for null-checking and assignment.

   With Apache's Validate we have to use two statements:
    Validate.notNull(foo);
    this.foo = foo;

   With Google's Preconditions we can do it with a single statement:
    this.foo = checkNotNull(foo);

   NOTE: Apache's commons-lang version 3.x adds Google's semantics.
   In the future, we need to decide (across all ONOS code) whether
   to use Google's Preconditions or Apache's Validate 3.x

 * Removed one of the LinkEvent convenience constructors, because it is used
   only in 1-2 places in the unit tests, and nowhere in the main code.

 * Few other (minor) cleanup changes

Change-Id: I05dae593ae1244a0af23515e7c73911f21275479
diff --git a/src/test/java/net/onrc/onos/core/intent/PathIntentMapTest.java b/src/test/java/net/onrc/onos/core/intent/PathIntentMapTest.java
index 8318476..1cadfb6 100644
--- a/src/test/java/net/onrc/onos/core/intent/PathIntentMapTest.java
+++ b/src/test/java/net/onrc/onos/core/intent/PathIntentMapTest.java
@@ -190,16 +190,17 @@
         intents.executeOperations(operations);
         assertEquals(2, intents.getAllIntents().size());
 
-        Collection<PathIntent> pathIntents = intents.getIntentsByLink(new LinkEvent(link12));
+        Collection<PathIntent> pathIntents =
+            intents.getIntentsByLink(link12.getLinkTuple());
         assertEquals(2, pathIntents.size());
         assertTrue(pathIntents.contains(intent1));
         assertTrue(pathIntents.contains(intent2));
 
-        pathIntents = intents.getIntentsByLink(new LinkEvent(link23));
+        pathIntents = intents.getIntentsByLink(link23.getLinkTuple());
         assertEquals(1, pathIntents.size());
         assertTrue(pathIntents.contains(intent1));
 
-        pathIntents = intents.getIntentsByLink(new LinkEvent(link24));
+        pathIntents = intents.getIntentsByLink(link24.getLinkTuple());
         assertEquals(1, pathIntents.size());
         assertTrue(pathIntents.contains(intent2));
     }
diff --git a/src/test/java/net/onrc/onos/core/topology/TopologyManagerTest.java b/src/test/java/net/onrc/onos/core/topology/TopologyManagerTest.java
index 9bdffd5..f7fe753 100644
--- a/src/test/java/net/onrc/onos/core/topology/TopologyManagerTest.java
+++ b/src/test/java/net/onrc/onos/core/topology/TopologyManagerTest.java
@@ -50,6 +50,7 @@
     private IControllerRegistryService registryService;
     private CopyOnWriteArrayList<ITopologyListener> topologyListeners;
     private Collection<TopologyEvent> allTopologyEvents;
+    private OnosInstanceId onosInstanceId = new OnosInstanceId("ONOS-Test-Instance-ID");
 
     @SuppressWarnings("unchecked")
     @Before
@@ -107,6 +108,8 @@
                 anyObject(LinkEvent.class)))
                 .andReturn(true).anyTimes();
 
+        expect(registryService.getOnosInstanceId()).andReturn(onosInstanceId).anyTimes();
+
         replay(datagridService);
         replay(registryService);
         replay(dataStoreService);
@@ -275,11 +278,9 @@
 
         // Generate a new Switch Mastership event
         Dpid dpid = new Dpid(100L);
-        OnosInstanceId onosInstanceId =
-            new OnosInstanceId("ONOS-Test-Instance-ID");
         Role role = Role.MASTER;
         MastershipEvent mastershipEvent =
-            new MastershipEvent(dpid, onosInstanceId, role);
+            new MastershipEvent(dpid, this.onosInstanceId, role);
 
         // Call the topologyManager function for adding the event
         theTopologyManager.putSwitchMastershipEvent(mastershipEvent);
@@ -302,11 +303,9 @@
 
         // Generate a new Switch Mastership event
         Dpid dpid = new Dpid(100L);
-        OnosInstanceId onosInstanceId =
-            new OnosInstanceId("ONOS-Test-Instance-ID");
         Role role = Role.MASTER;
         MastershipEvent mastershipEvent =
-            new MastershipEvent(dpid, onosInstanceId, role);
+            new MastershipEvent(dpid, this.onosInstanceId, role);
 
         // Call the topologyManager function for removing the event
         theTopologyManager.removeSwitchMastershipEvent(mastershipEvent);
@@ -351,7 +350,8 @@
         theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2);
 
         // Create the link
-        LinkEvent linkEvent = new LinkEvent(sw1DPId, port1Id, sw2DPId, port2Id);
+        LinkEvent linkEvent = new LinkEvent(new SwitchPort(sw1DPId, port1Id),
+                                            new SwitchPort(sw2DPId, port2Id));
         theTopologyManager.putLinkDiscoveryEvent(linkEvent);
 
         // Verify the function calls
@@ -393,7 +393,9 @@
         theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2);
 
         // Remove the link
-        LinkEvent linkEventRemove = new LinkEvent(sw1DPId, port1Id, sw2DPId, port2Id);
+        LinkEvent linkEventRemove =
+            new LinkEvent(new SwitchPort(sw1DPId, port1Id),
+                          new SwitchPort(sw2DPId, port2Id));
         theTopologyManager.removeLinkDiscoveryEvent(linkEventRemove);
 
         // Verify the function calls
diff --git a/src/test/java/net/onrc/onos/core/util/serializers/KryoFactoryTest.java b/src/test/java/net/onrc/onos/core/util/serializers/KryoFactoryTest.java
index 9431cfe..712a50f 100644
--- a/src/test/java/net/onrc/onos/core/util/serializers/KryoFactoryTest.java
+++ b/src/test/java/net/onrc/onos/core/util/serializers/KryoFactoryTest.java
@@ -46,6 +46,7 @@
     private static final Dpid DPID_B = new Dpid(Long.MAX_VALUE);
     private static final PortNumber PORT_NO_A = new PortNumber((short) 42);
     private static final PortNumber PORT_NO_B = new PortNumber((short) 65534);
+    private static final String ONOS_INSTANCE_NAME = "ONOS-Instance-Test";
 
     private static final double SEC_IN_NANO = 1000 * 1000 * 1000.0;
 
@@ -172,7 +173,8 @@
         // To be more strict, we should be checking serialized byte[].
         { // CHECKSTYLE IGNORE THIS LINE
             HostEvent obj = new HostEvent(MACAddress.valueOf(0x12345678));
-            obj.createStringAttribute(TopologyElement.TYPE, TopologyElement.TYPE_PACKET_LAYER);
+            obj.createStringAttribute(TopologyElement.TYPE,
+                                      TopologyElement.TYPE_PACKET_LAYER);
             obj.addAttachmentPoint(new SwitchPort(DPID_A, PORT_NO_A));
             // avoid using System.currentTimeMillis() var-int size may change
             obj.setLastSeenTime(392860800000L);
@@ -184,8 +186,10 @@
         }
 
         { // CHECKSTYLE IGNORE THIS LINE
-            LinkEvent obj = new LinkEvent(DPID_A, PORT_NO_A, DPID_B, PORT_NO_B);
-            obj.createStringAttribute(TopologyElement.TYPE, TopologyElement.TYPE_PACKET_LAYER);
+            LinkEvent obj = new LinkEvent(new SwitchPort(DPID_A, PORT_NO_A),
+                                          new SwitchPort(DPID_B, PORT_NO_B));
+            obj.createStringAttribute(TopologyElement.TYPE,
+                                      TopologyElement.TYPE_PACKET_LAYER);
             obj.freeze();
             Result result = benchType(obj, EqualityCheck.EQUALS);
             results.add(result);
@@ -195,7 +199,8 @@
 
         { // CHECKSTYLE IGNORE THIS LINE
             PortEvent obj = new PortEvent(DPID_A, PORT_NO_A);
-            obj.createStringAttribute(TopologyElement.TYPE, TopologyElement.TYPE_PACKET_LAYER);
+            obj.createStringAttribute(TopologyElement.TYPE,
+                                      TopologyElement.TYPE_PACKET_LAYER);
             obj.freeze();
             Result result = benchType(obj, EqualityCheck.EQUALS);
             results.add(result);
@@ -205,7 +210,8 @@
 
         { // CHECKSTYLE IGNORE THIS LINE
             SwitchEvent obj = new SwitchEvent(DPID_A);
-            obj.createStringAttribute(TopologyElement.TYPE, TopologyElement.TYPE_PACKET_LAYER);
+            obj.createStringAttribute(TopologyElement.TYPE,
+                                      TopologyElement.TYPE_PACKET_LAYER);
             obj.freeze();
             Result result = benchType(obj, EqualityCheck.EQUALS);
             results.add(result);
@@ -215,23 +221,27 @@
 
         { // CHECKSTYLE IGNORE THIS LINE
             SwitchEvent evt = new SwitchEvent(DPID_A);
-            evt.createStringAttribute(TopologyElement.TYPE, TopologyElement.TYPE_PACKET_LAYER);
+            evt.createStringAttribute(TopologyElement.TYPE,
+                                      TopologyElement.TYPE_PACKET_LAYER);
             evt.freeze();
+            OnosInstanceId onosInstanceId =
+                new OnosInstanceId(ONOS_INSTANCE_NAME);
 
             // using the back door to access package-scoped constructor
             Constructor<TopologyEvent> swConst
-                = TopologyEvent.class.getDeclaredConstructor(SwitchEvent.class);
+                = TopologyEvent.class.getDeclaredConstructor(SwitchEvent.class,
+                        OnosInstanceId.class);
             swConst.setAccessible(true);
-            TopologyEvent obj = swConst.newInstance(evt);
+            TopologyEvent obj = swConst.newInstance(evt, onosInstanceId);
 
             Result result = benchType(obj, EqualityCheck.TO_STRING);
             results.add(result);
             // update me if serialized form is expected to change
-            assertEquals(27, result.size);
+            assertEquals(47, result.size);
         }
 
         { // CHECKSTYLE IGNORE THIS LINE
-            OnosInstanceId id = new OnosInstanceId("Some_Instance_Name");
+            OnosInstanceId id = new OnosInstanceId(ONOS_INSTANCE_NAME);
 
             Result result = benchType(id, EqualityCheck.EQUALS);
             results.add(result);