Fix serialization issue

Change-Id: Ie2cc832e59018977066dc3d7fbfec2819a9c5fb2
diff --git a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/cli/XconnectNextListCommand.java b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/cli/XconnectNextListCommand.java
index f7a6b51..18b2033 100644
--- a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/cli/XconnectNextListCommand.java
+++ b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/cli/XconnectNextListCommand.java
@@ -18,7 +18,6 @@
 
 import org.apache.karaf.shell.commands.Command;
 import org.onosproject.cli.AbstractShellCommand;
-import org.onosproject.net.flowobjective.NextObjective;
 import org.onosproject.segmentrouting.xconnect.api.XconnectKey;
 import org.onosproject.segmentrouting.xconnect.api.XconnectService;
 
@@ -39,7 +38,7 @@
         print(xconnectService.getNext());
     }
 
-    private void print(Map<XconnectKey, NextObjective> nextStore) {
+    private void print(Map<XconnectKey, Integer> nextStore) {
         ArrayList<XconnectKey> a = new ArrayList<>(nextStore.keySet());
         a.sort(Comparator
                 .comparing((XconnectKey o) -> o.deviceId().toString())
@@ -50,7 +49,7 @@
             builder.append("\n")
                     .append(k)
                     .append(" --> ")
-                    .append(nextStore.get(k).id())
+                    .append(nextStore.get(k))
         );
         print(builder.toString());
     }
diff --git a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/xconnect/api/XconnectService.java b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/xconnect/api/XconnectService.java
index dfdc422..25f1b01 100644
--- a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/xconnect/api/XconnectService.java
+++ b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/xconnect/api/XconnectService.java
@@ -21,7 +21,6 @@
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.PortNumber;
-import org.onosproject.net.flowobjective.NextObjective;
 
 import java.util.Set;
 
@@ -78,7 +77,7 @@
      *
      * @return current contents of the xconnectNextObjStore
      */
-    ImmutableMap<XconnectKey, NextObjective> getNext();
+    ImmutableMap<XconnectKey, Integer> getNext();
 
     /**
      * Removes given next ID from Xconnect next objective store.
diff --git a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/xconnect/impl/XconnectManager.java b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/xconnect/impl/XconnectManager.java
index e571811..a7eb664 100644
--- a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/xconnect/impl/XconnectManager.java
+++ b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/xconnect/impl/XconnectManager.java
@@ -114,7 +114,7 @@
 
     private ApplicationId appId;
     private ConsistentMap<XconnectKey, Set<PortNumber>> xconnectStore;
-    private ConsistentMap<XconnectKey, NextObjective> xconnectNextObjStore;
+    private ConsistentMap<XconnectKey, Integer> xconnectNextObjStore;
 
     private final MapEventListener<XconnectKey, Set<PortNumber>> xconnectListener = new XconnectMapListener();
     private final DeviceListener deviceListener = new InternalDeviceListener();
@@ -138,7 +138,7 @@
                 .build();
         xconnectStore.addListener(xconnectListener);
 
-        xconnectNextObjStore = storageService.<XconnectKey, NextObjective>consistentMapBuilder()
+        xconnectNextObjStore = storageService.<XconnectKey, Integer>consistentMapBuilder()
                 .withName("onos-sr-xconnect-next")
                 .withRelaxedReadConsistency()
                 .withSerializer(Serializer.using(serializer.build()))
@@ -194,7 +194,7 @@
     }
 
     @Override
-    public ImmutableMap<XconnectKey, NextObjective> getNext() {
+    public ImmutableMap<XconnectKey, Integer> getNext() {
         if (xconnectNextObjStore != null) {
             return ImmutableMap.copyOf(xconnectNextObjStore.asJavaMap());
         } else {
@@ -205,7 +205,7 @@
     @Override
     public void removeNextId(int nextId) {
         xconnectNextObjStore.entrySet().forEach(e -> {
-            if (e.getValue().value().id() == nextId) {
+            if (e.getValue().value() == nextId) {
                 xconnectNextObjStore.remove(e.getKey());
             }
         });
@@ -260,13 +260,13 @@
         }
     }
 
-    void init(DeviceId deviceId) {
+    private void init(DeviceId deviceId) {
         getXconnects().stream()
                 .filter(desc -> desc.key().deviceId().equals(deviceId))
                 .forEach(desc -> populateXConnect(desc.key(), desc.ports()));
     }
 
-    void cleanup(DeviceId deviceId) {
+    private void cleanup(DeviceId deviceId) {
         xconnectNextObjStore.entrySet().stream()
                 .filter(entry -> entry.getKey().deviceId().equals(deviceId))
                 .forEach(entry -> xconnectNextObjStore.remove(entry.getKey()));
@@ -316,11 +316,11 @@
      * @param key XConnect store key
      * @param ports XConnect ports
      */
-    private NextObjective populateNext(XconnectKey key, Set<PortNumber> ports) {
-        NextObjective nextObj;
+    private int populateNext(XconnectKey key, Set<PortNumber> ports) {
         if (xconnectNextObjStore.containsKey(key)) {
-            nextObj = xconnectNextObjStore.get(key).value();
-            log.debug("NextObj for {} found, id={}", key, nextObj.id());
+            int nextId = xconnectNextObjStore.get(key).value();
+            log.debug("NextObj for {} found, id={}", key, nextId);
+            return nextId;
         } else {
             NextObjective.Builder nextObjBuilder = nextObjBuilder(key, ports);
             ObjectiveContext nextContext = new DefaultObjectiveContext(
@@ -331,22 +331,22 @@
                         log.warn("Failed to add XConnect NextObj for {}: {}", key, error);
                         srService.invalidateNextObj(objective.id());
                     });
-            nextObj = nextObjBuilder.add(nextContext);
+            NextObjective nextObj = nextObjBuilder.add(nextContext);
             flowObjectiveService.next(key.deviceId(), nextObj);
-            xconnectNextObjStore.put(key, nextObj);
+            xconnectNextObjStore.put(key, nextObj.id());
             log.debug("NextObj for {} not found. Creating new NextObj with id={}", key, nextObj.id());
+            return nextObj.id();
         }
-        return nextObj;
     }
 
     /**
      * Populates bridging forwarding objectives for given XConnect.
      *
      * @param key XConnect store key
-     * @param nextObj next objective
+     * @param nextId next objective id
      */
-    private void populateFwd(XconnectKey key, NextObjective nextObj) {
-        ForwardingObjective.Builder fwdObjBuilder = fwdObjBuilder(key, nextObj.id());
+    private void populateFwd(XconnectKey key, int nextId) {
+        ForwardingObjective.Builder fwdObjBuilder = fwdObjBuilder(key, nextId);
         ObjectiveContext fwdContext = new DefaultObjectiveContext(
                 (objective) -> log.debug("XConnect FwdObj for {} populated", key),
                 (objective, error) ->
@@ -383,9 +383,9 @@
         ports = addPairPort(key.deviceId(), ports);
         revokeFilter(key, ports);
         if (xconnectNextObjStore.containsKey(key)) {
-            NextObjective nextObj = xconnectNextObjStore.get(key).value();
-            revokeFwd(key, nextObj, null);
-            revokeNext(key, nextObj, null);
+            int nextId = xconnectNextObjStore.get(key).value();
+            revokeFwd(key, nextId, null);
+            revokeNext(key, ports, nextId, null);
         } else {
             log.warn("NextObj for {} does not exist in the store.", key);
         }
@@ -415,10 +415,11 @@
      * Revokes next objectives for given XConnect.
      *
      * @param key XConnect store key
-     * @param nextObj next objective
+     * @param ports ports in the XConnect
+     * @param nextId next objective id
      * @param nextFuture completable future for this next objective operation
      */
-    private void revokeNext(XconnectKey key, NextObjective nextObj,
+    private void revokeNext(XconnectKey key, Set<PortNumber> ports, int nextId,
                             CompletableFuture<ObjectiveError> nextFuture) {
         ObjectiveContext context = new ObjectiveContext() {
             @Override
@@ -438,7 +439,7 @@
                 srService.invalidateNextObj(objective.id());
             }
         };
-        flowObjectiveService.next(key.deviceId(), nextObj.copy().remove(context));
+        flowObjectiveService.next(key.deviceId(), nextObjBuilder(key, ports, nextId).remove(context));
         xconnectNextObjStore.remove(key);
     }
 
@@ -446,12 +447,11 @@
      * Revokes bridging forwarding objectives for given XConnect.
      *
      * @param key XConnect store key
-     * @param nextObj next objective
+     * @param nextId next objective id
      * @param fwdFuture completable future for this forwarding objective operation
      */
-    private void revokeFwd(XconnectKey key, NextObjective nextObj,
-                           CompletableFuture<ObjectiveError> fwdFuture) {
-        ForwardingObjective.Builder fwdObjBuilder = fwdObjBuilder(key, nextObj.id());
+    private void revokeFwd(XconnectKey key, int nextId, CompletableFuture<ObjectiveError> fwdFuture) {
+        ForwardingObjective.Builder fwdObjBuilder = fwdObjBuilder(key, nextId);
         ObjectiveContext context = new ObjectiveContext() {
             @Override
             public void onSuccess(Objective objective) {
@@ -509,13 +509,13 @@
         CompletableFuture<ObjectiveError> nextFuture = new CompletableFuture<>();
 
         if (xconnectNextObjStore.containsKey(key)) {
-            NextObjective nextObj = xconnectNextObjStore.get(key).value();
-            revokeFwd(key, nextObj, fwdFuture);
+            int nextId = xconnectNextObjStore.get(key).value();
+            revokeFwd(key, nextId, fwdFuture);
 
             fwdFuture.thenAcceptAsync(fwdStatus -> {
                 if (fwdStatus == null) {
                     log.debug("Fwd removed. Now remove group {}", key);
-                    revokeNext(key, nextObj, nextFuture);
+                    revokeNext(key, prevPorts, nextId, nextFuture);
                 }
             });
 
@@ -531,14 +531,14 @@
     }
 
     /**
-     * Creates a next objective builder for XConnect.
+     * Creates a next objective builder for XConnect with given nextId.
      *
      * @param key XConnect key
      * @param ports set of XConnect ports
+     * @param nextId next objective id
      * @return next objective builder
      */
-    private NextObjective.Builder nextObjBuilder(XconnectKey key, Set<PortNumber> ports) {
-        int nextId = flowObjectiveService.allocateNextId();
+    private NextObjective.Builder nextObjBuilder(XconnectKey key, Set<PortNumber> ports, int nextId) {
         TrafficSelector metadata =
                 DefaultTrafficSelector.builder().matchVlanId(key.vlanId()).build();
         NextObjective.Builder nextObjBuilder = DefaultNextObjective
@@ -554,6 +554,19 @@
     }
 
     /**
+     * Creates a next objective builder for XConnect.
+     *
+     * @param key XConnect key
+     * @param ports set of XConnect ports
+     * @return next objective builder
+     */
+    private NextObjective.Builder nextObjBuilder(XconnectKey key, Set<PortNumber> ports) {
+        int nextId = flowObjectiveService.allocateNextId();
+        return nextObjBuilder(key, ports, nextId);
+    }
+
+
+    /**
      * Creates a bridging forwarding objective builder for XConnect.
      *
      * @param key XConnect key