Added unconditional datastore operations: (force create/delete)

Change-Id: If20ed1a2cd92797870a10f6564d8e9769c5fe4ed
diff --git a/src/main/java/net/onrc/onos/datastore/RCObject.java b/src/main/java/net/onrc/onos/datastore/RCObject.java
index 5bea2c5..4370d00 100644
--- a/src/main/java/net/onrc/onos/datastore/RCObject.java
+++ b/src/main/java/net/onrc/onos/datastore/RCObject.java
@@ -122,7 +122,8 @@
 	serializeAndSetValue(defaultKryo.get(), this.propertyMap);
     }
 
-    protected void serializeAndSetValue(Kryo kryo, Map<Object, Object> javaObject) {
+    protected void serializeAndSetValue(Kryo kryo,
+	    Map<Object, Object> javaObject) {
 
 	// value
 	byte[] rcTemp = new byte[1024 * 1024];
@@ -144,7 +145,8 @@
 	return deserializeObjectFromValue(kryo, HashMap.class);
     }
 
-    protected <T extends Map> T deserializeObjectFromValue(Kryo kryo, Class<T> type) {
+    protected <T extends Map> T deserializeObjectFromValue(Kryo kryo,
+	    Class<T> type) {
 	if (this.value == null)
 	    return null;
 
@@ -160,7 +162,6 @@
      *
      * Fails if the Object with same key already exists.
      *
-     * @note create an Empty Object if no object has never been set.
      * @throws ObjectExistsException
      */
     public void create() throws ObjectExistsException {
@@ -215,9 +216,10 @@
      * Fails if the Object with key does not exists.
      *
      * @throws ObjectDoesntExistException
+     * @throws WrongVersionException
      */
-    public void delete() throws ObjectDoesntExistException {
-	this.version = table.delete(key);
+    public void delete() throws ObjectDoesntExistException, WrongVersionException {
+	this.version = table.delete(key, this.version);
     }
 
     /**
@@ -301,8 +303,8 @@
     public static Collection<? extends RCObject> getAllObjects() {
 	// TODO implement
 	throw new UnsupportedOperationException("Not implemented yet");
-	//Collection<? extends RCObject> list = new ArrayList<>();
-	//return list;
+	// Collection<? extends RCObject> list = new ArrayList<>();
+	// return list;
     }
 
 }
diff --git a/src/main/java/net/onrc/onos/datastore/RCTable.java b/src/main/java/net/onrc/onos/datastore/RCTable.java
index 4836f24..55de05a 100644
--- a/src/main/java/net/onrc/onos/datastore/RCTable.java
+++ b/src/main/java/net/onrc/onos/datastore/RCTable.java
@@ -92,7 +92,14 @@
 
     // TODO: Enumerate whole table?
 
-    // Reject if exist
+    /**
+     * Create a Key-Value entry on table.
+     *
+     * @param key
+     * @param value
+     * @return version of the created entry
+     * @throws ObjectExistsException
+     */
     public long create(final byte[] key, final byte[] value)
 	    throws ObjectExistsException {
 
@@ -106,7 +113,28 @@
 	return updated_version;
     }
 
-    // read
+    /**
+     * Create a Key-Value entry on table, without existance checking.
+     *
+     * @param key
+     * @param value
+     * @return version of the created entry
+     */
+    public long forceCreate(final byte[] key, final byte[] value) {
+	JRamCloud rcClient = RCClient.getClient();
+
+	long updated_version = rcClient.write(this.rcTableId, key, value);
+	return updated_version;
+
+    }
+
+    /**
+     * Read a Key-Value entry from table.
+     *
+     * @param key
+     * @return Corresponding {@link Entry}
+     * @throws ObjectDoesntExistException
+     */
     public Entry read(final byte[] key) throws ObjectDoesntExistException {
 
 	JRamCloud rcClient = RCClient.getClient();
@@ -120,7 +148,17 @@
 	return new Entry(rcObj.key, rcObj.value, rcObj.version);
     }
 
-    // Reject if version neq
+    /**
+     * Update an existing Key-Value entry in table.
+     *
+     * @param key
+     * @param value
+     * @param version
+     *            expected version in the data store
+     * @return version after update
+     * @throws ObjectDoesntExistException
+     * @throws WrongVersionException
+     */
     public long update(final byte[] key, final byte[] value, final long version)
 	    throws ObjectDoesntExistException, WrongVersionException {
 
@@ -135,7 +173,14 @@
 	return updated_version;
     }
 
-    // Reject if not exist
+    /**
+     * Update an existing Key-Value entry in table, without checking version.
+     *
+     * @param key
+     * @param value
+     * @return version after update
+     * @throws ObjectDoesntExistException
+     */
     public long update(final byte[] key, final byte[] value)
 	    throws ObjectDoesntExistException {
 
@@ -150,16 +195,38 @@
 
     }
 
-    // Reject if not exist
-    public long delete(final byte[] key) throws ObjectDoesntExistException {
+    /**
+     * Remove an existing Key-Value entry in table
+     *
+     * @param key
+     * @param version
+     *            expected version in the data store
+     * @return version of removed object
+     * @throws ObjectDoesntExistException
+     * @throws WrongVersionException
+     */
+    public long delete(final byte[] key, final long version)
+	    throws ObjectDoesntExistException, WrongVersionException {
 	JRamCloud rcClient = RCClient.getClient();
 
-	// FIXME underlying JRamCloud does not support cond remove
+	// FIXME underlying JRamCloud does not support cond. remove now
 	RejectRules rules = rcClient.new RejectRules();
 	rules.setDoesntExists();
+	rules.setNeVersion(version);
 
 	long removed_version = rcClient.remove(this.rcTableId, key, rules);
 	return removed_version;
     }
 
+    /**
+     * Remove a Key-Value entry in table
+     *
+     * @param key
+     * @return version of removed object or -1, if it did not exist.
+     */
+    public long forceDelete(final byte[] key) {
+	JRamCloud rcClient = RCClient.getClient();
+	long removed_version = rcClient.remove(this.rcTableId, key);
+	return removed_version;
+    }
 }
diff --git a/src/main/java/net/onrc/onos/datastore/topology/RCSwitch.java b/src/main/java/net/onrc/onos/datastore/topology/RCSwitch.java
index de9d5a4..175209d 100644
--- a/src/main/java/net/onrc/onos/datastore/topology/RCSwitch.java
+++ b/src/main/java/net/onrc/onos/datastore/topology/RCSwitch.java
@@ -239,7 +239,7 @@
 	assert (swRead2.getAllPortIds().size() == 1);
 	try {
 	    swRead2.delete();
-	} catch (ObjectDoesntExistException e) {
+	} catch (ObjectDoesntExistException | WrongVersionException e) {
 	    log.debug("Deleting Switch Failed", e);
 	}
 
@@ -476,57 +476,57 @@
 	try {
 	    sw1.read();
 	    sw1.delete();
-	} catch (ObjectDoesntExistException e) {
+	} catch (ObjectDoesntExistException | WrongVersionException e) {
 	    log.debug("Delete Switch Failed", e);
 	}
 	try {
 	    sw1p1.read();
 	    sw1p1.delete();
-	} catch (ObjectDoesntExistException e) {
+	} catch (ObjectDoesntExistException | WrongVersionException e) {
 	    log.debug("Delete Port Failed", e);
 	}
 	try {
 	    sw1p2.read();
 	    sw1p2.delete();
-	} catch (ObjectDoesntExistException e) {
+	} catch (ObjectDoesntExistException | WrongVersionException e) {
 	    log.debug("Delete Port Failed", e);
 	}
 	try {
 	    d1.read();
 	    d1.delete();
-	} catch (ObjectDoesntExistException e) {
+	} catch (ObjectDoesntExistException | WrongVersionException e) {
 	    log.debug("Delete Device Failed", e);
 	}
 
 	try {
 	    l1.read();
 	    l1.delete();
-	} catch (ObjectDoesntExistException e) {
+	} catch (ObjectDoesntExistException | WrongVersionException e) {
 	    log.debug("Delete Link Failed", e);
 	}
 
 	try {
 	    sw2.read();
 	    sw2.delete();
-	} catch (ObjectDoesntExistException e) {
+	} catch (ObjectDoesntExistException | WrongVersionException e) {
 	    log.debug("Delete Switch Failed", e);
 	}
 	try {
 	    sw2p1.read();
 	    sw2p1.delete();
-	} catch (ObjectDoesntExistException e) {
+	} catch (ObjectDoesntExistException | WrongVersionException e) {
 	    log.debug("Delete Port Failed", e);
 	}
 	try {
 	    sw2p2.read();
 	    sw2p2.delete();
-	} catch (ObjectDoesntExistException e) {
+	} catch (ObjectDoesntExistException | WrongVersionException e) {
 	    log.debug("Delete Port Failed", e);
 	}
 	try {
 	    d2.read();
 	    d2.delete();
-	} catch (ObjectDoesntExistException e) {
+	} catch (ObjectDoesntExistException | WrongVersionException e) {
 	    log.debug("Delete Device Failed", e);
 	}