[ONOS-5919] (vCore) separate Intent store

Change-Id: I3b5b67d7286dafdf11bf3d8d4acafb59a3034e22
diff --git a/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkIntentManager.java b/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkIntentManager.java
index 41cd952..48ad983 100644
--- a/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkIntentManager.java
+++ b/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkIntentManager.java
@@ -19,6 +19,7 @@
 import com.google.common.collect.Iterators;
 import org.onosproject.incubator.net.virtual.NetworkId;
 import org.onosproject.incubator.net.virtual.VirtualNetworkIntent;
+import org.onosproject.incubator.net.virtual.VirtualNetworkIntentStore;
 import org.onosproject.incubator.net.virtual.VirtualNetworkService;
 import org.onosproject.incubator.net.virtual.VirtualNetworkStore;
 import org.onosproject.incubator.net.virtual.VirtualPort;
@@ -64,6 +65,7 @@
 
     protected IntentService intentService;
     protected VirtualNetworkStore store;
+    protected VirtualNetworkIntentStore intentStore;
     protected WorkPartitionService partitionService;
 
     /**
@@ -78,6 +80,7 @@
         super(virtualNetworkManager, networkId, IntentEvent.class);
 
         this.store = serviceDirectory.get(VirtualNetworkStore.class);
+        this.intentStore = serviceDirectory.get(VirtualNetworkIntentStore.class);
         this.intentService = serviceDirectory.get(IntentService.class);
         this.partitionService = serviceDirectory.get(WorkPartitionService.class);
     }
@@ -117,7 +120,7 @@
     private boolean validateConnectPoint(ConnectPoint connectPoint) {
         checkNotNull(connectPoint, CP_NULL);
         Port port = getPort(connectPoint.deviceId(), connectPoint.port());
-        return port == null ? false : true;
+        return port != null;
     }
 
     /**
@@ -179,12 +182,12 @@
     @Override
     public Intent getIntent(Key key) {
         checkNotNull(key, KEY_NULL);
-        return store.getIntent(key);
+        return intentStore.getIntent(networkId, key);
     }
 
     @Override
     public Iterable<Intent> getIntents() {
-        return store.getIntents();
+        return intentStore.getIntents(networkId);
     }
 
     @Override
@@ -194,7 +197,7 @@
 
     @Override
     public Iterable<IntentData> getIntentData() {
-        return store.getIntentData();
+        return intentStore.getIntentData(networkId, false, 0);
     }
 
     @Override
@@ -205,7 +208,7 @@
     @Override
     public IntentState getIntentState(Key intentKey) {
         checkNotNull(intentKey, KEY_NULL);
-        return Optional.ofNullable(store.getIntentData(intentKey))
+        return Optional.ofNullable(intentStore.getIntentData(networkId, intentKey))
                 .map(IntentData::state)
                 .orElse(null);
     }
diff --git a/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManager.java b/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManager.java
index 96979f5..d57801b 100644
--- a/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManager.java
+++ b/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManager.java
@@ -40,7 +40,6 @@
 import org.onosproject.incubator.net.virtual.VirtualNetwork;
 import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
 import org.onosproject.incubator.net.virtual.VirtualNetworkEvent;
-import org.onosproject.incubator.net.virtual.VirtualNetworkIntent;
 import org.onosproject.incubator.net.virtual.VirtualNetworkListener;
 import org.onosproject.incubator.net.virtual.VirtualNetworkService;
 import org.onosproject.incubator.net.virtual.VirtualNetworkStore;
@@ -63,10 +62,7 @@
 import org.onosproject.net.flowobjective.FlowObjectiveService;
 import org.onosproject.net.group.GroupService;
 import org.onosproject.net.host.HostService;
-import org.onosproject.net.intent.IntentEvent;
-import org.onosproject.net.intent.IntentListener;
 import org.onosproject.net.intent.IntentService;
-import org.onosproject.net.intent.IntentState;
 import org.onosproject.net.link.LinkService;
 import org.onosproject.net.packet.PacketService;
 import org.onosproject.net.provider.AbstractListenerProviderRegistry;
@@ -112,9 +108,6 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected CoreService coreService;
 
-    private final InternalVirtualIntentListener intentListener =
-            new InternalVirtualIntentListener();
-
     private VirtualNetworkStoreDelegate delegate = this::post;
 
     private ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
@@ -147,7 +140,6 @@
         eventDispatcher.addSink(VirtualEvent.class,
                                 VirtualListenerRegistryManager.getInstance());
         store.setDelegate(delegate);
-        intentService.addListener(intentListener);
         appId = coreService.registerApplication(VIRTUAL_NETWORK_APP_ID_STRING);
         log.info("Started");
     }
@@ -157,7 +149,6 @@
         store.unsetDelegate(delegate);
         eventDispatcher.removeSink(VirtualNetworkEvent.class);
         eventDispatcher.removeSink(VirtualEvent.class);
-        intentService.removeListener(intentListener);
         log.info("Stopped");
     }
 
@@ -517,64 +508,12 @@
         }
     }
 
-    /**
-     * Internal intent event listener.
-     */
-    private class InternalVirtualIntentListener implements IntentListener {
-
-        @Override
-        public void event(IntentEvent event) {
-
-            // Ignore intent events that are not relevant.
-            if (!isRelevant(event)) {
-                return;
-            }
-
-            VirtualNetworkIntent intent = (VirtualNetworkIntent) event.subject();
-
-            switch (event.type()) {
-                case INSTALL_REQ:
-                    store.addOrUpdateIntent(intent, IntentState.INSTALL_REQ);
-                    break;
-                case INSTALLED:
-                    store.addOrUpdateIntent(intent, IntentState.INSTALLED);
-                    break;
-                case WITHDRAW_REQ:
-                    store.addOrUpdateIntent(intent, IntentState.WITHDRAW_REQ);
-                    break;
-                case WITHDRAWN:
-                    store.addOrUpdateIntent(intent, IntentState.WITHDRAWN);
-                    break;
-                case FAILED:
-                    store.addOrUpdateIntent(intent, IntentState.FAILED);
-                    break;
-                case CORRUPT:
-                    store.addOrUpdateIntent(intent, IntentState.CORRUPT);
-                    break;
-                case PURGED:
-                    store.removeIntent(intent.key());
-                default:
-                    break;
-            }
-        }
-
-        @Override
-        public boolean isRelevant(IntentEvent event) {
-            if (event.subject() instanceof VirtualNetworkIntent) {
-                return true;
-            }
-            return false;
-        }
-    }
-
-
     @Override
     protected VirtualNetworkProviderService
     createProviderService(VirtualNetworkProvider provider) {
         return new InternalVirtualNetworkProviderService(provider);
     }
 
-
     /**
      * Service issued to registered virtual network providers so that they
      * can interact with the core.
diff --git a/incubator/net/src/test/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkIntentManagerTest.java b/incubator/net/src/test/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkIntentManagerTest.java
index 4b7cf84..3b59e9a 100644
--- a/incubator/net/src/test/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkIntentManagerTest.java
+++ b/incubator/net/src/test/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkIntentManagerTest.java
@@ -37,8 +37,10 @@
 import org.onosproject.incubator.net.virtual.VirtualLink;
 import org.onosproject.incubator.net.virtual.VirtualNetwork;
 import org.onosproject.incubator.net.virtual.VirtualNetworkIntent;
+import org.onosproject.incubator.net.virtual.VirtualNetworkIntentStore;
 import org.onosproject.incubator.net.virtual.VirtualNetworkStore;
 import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore;
+import org.onosproject.incubator.store.virtual.impl.SimpleVirtualIntentStore;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DefaultPort;
 import org.onosproject.net.EncapsulationType;
@@ -100,6 +102,7 @@
 
     private VirtualNetworkManager manager;
     private static DistributedVirtualNetworkStore virtualNetworkManagerStore;
+    private VirtualNetworkIntentStore intentStore;
     private CoreService coreService;
     private TestableIntentService intentService = new FakeIntentManager();
     private VirtualNetworkIntentManager vnetIntentService;
@@ -117,6 +120,7 @@
     @Before
     public void setUp() throws Exception {
         virtualNetworkManagerStore = new DistributedVirtualNetworkStore();
+        intentStore = new SimpleVirtualIntentStore();
 
         coreService = new VirtualNetworkIntentManagerTest.TestCoreService();
 
@@ -215,7 +219,7 @@
 
         vnetIntentService = new VirtualNetworkIntentManager(manager, virtualNetwork.id());
         vnetIntentService.intentService = intentService;
-        vnetIntentService.store = virtualNetworkManagerStore;
+        vnetIntentService.intentStore = intentStore;
         vnetIntentService.partitionService = workPartitionService;
         return virtualNetwork;
     }
@@ -347,12 +351,12 @@
             switch (event.type()) {
                 case INSTALLED:
                     // Release one permit on the created semaphore since the Intent event was received.
-                    virtualNetworkManagerStore.addOrUpdateIntent(event.subject(), IntentState.INSTALLED);
+//                    virtualNetworkManagerStore.addOrUpdateIntent(event.subject(), IntentState.INSTALLED);
                     created.release();
                     break;
                 case WITHDRAWN:
                     // Release one permit on the removed semaphore since the Intent event was received.
-                    virtualNetworkManagerStore.addOrUpdateIntent(event.subject(), IntentState.WITHDRAWN);
+//                    virtualNetworkManagerStore.addOrUpdateIntent(event.subject(), IntentState.WITHDRAWN);
                     withdrawn.release();
                     break;
                 case PURGED:
@@ -396,4 +400,15 @@
             super(APP_ID, Collections.singletonList(new IntentTestsMocks.MockFlowRule(100)), Collections.emptyList());
         }
     }
+
+//    private void addOrUpdateIntent(Intent intent, IntentState state) {
+//        checkNotNull(intent, "Intent cannot be null");
+//        IntentData intentData = intentStore.(intent.key());
+//        if (intentData == null) {
+//            intentData = new IntentData(intent, state, new WallClockTimestamp(System.currentTimeMillis()));
+//        } else {
+//            intentData = new IntentData(intent, state, intentData.version());
+//        }
+//        intentKeyIntentDataMap.put(intent.key(), intentData);
+//    }
 }
diff --git a/incubator/net/src/test/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManagerTest.java b/incubator/net/src/test/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManagerTest.java
index 765b4cb..9d4086e 100644
--- a/incubator/net/src/test/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManagerTest.java
+++ b/incubator/net/src/test/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManagerTest.java
@@ -71,7 +71,6 @@
 import org.onosproject.net.host.HostService;
 import org.onosproject.net.intent.FakeIntentManager;
 import org.onosproject.net.intent.IntentService;
-import org.onosproject.net.intent.IntentState;
 import org.onosproject.net.intent.Key;
 import org.onosproject.net.intent.MockIdGenerator;
 import org.onosproject.net.intent.TestableIntentService;
@@ -684,57 +683,6 @@
         assertTrue("The virtual network set should be empty.", virtualNetworks.isEmpty());
     }
 
-
-    /**
-     * Tests the addOrUpdateIntent() method in the store with a null intent.
-     */
-    @Test(expected = NullPointerException.class)
-    public void testAddOrUpdateNullIntent() {
-        manager.store.addOrUpdateIntent(null, null);
-    }
-
-    /**
-     * Tests the removeIntent() method in the store with a null intent key.
-     */
-    @Test(expected = NullPointerException.class)
-    public void testRemoveNullIntentKey() {
-        manager.store.removeIntent(null);
-    }
-
-    /**
-     * Tests the addOrUpdateIntent(), getIntents(), getIntent(), removeIntent() methods with the store.
-     */
-    @Test
-    public void testAddOrUpdateIntent() {
-        manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
-        VirtualNetwork virtualNetwork =
-                manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
-        ConnectPoint cp1 = new ConnectPoint(DID1, P1);
-        ConnectPoint cp2 = new ConnectPoint(DID2, P1);
-
-        VirtualNetworkIntent virtualIntent = VirtualNetworkIntent.builder()
-                .networkId(virtualNetwork.id())
-                .key(Key.of("Test", APP_ID))
-                .appId(APP_ID)
-                .ingressPoint(cp1)
-                .egressPoint(cp2)
-                .build();
-
-        // Add the intent to the store.
-        manager.store.addOrUpdateIntent(virtualIntent, IntentState.INSTALL_REQ);
-        assertEquals("The intent size should match.", 1,
-                     manager.store.getIntents().size());
-        assertNotNull("The intent should not be null.",
-                      manager.store.getIntent(virtualIntent.key()));
-
-        // remove the intent from the store.
-        manager.store.removeIntent(virtualIntent.key());
-        assertTrue("The intents should be empty.",
-                   manager.store.getIntents().isEmpty());
-        assertNull("The intent should be null.",
-                   manager.store.getIntent(virtualIntent.key()));
-    }
-
     /**
      * Tests the addTunnelId() method in the store with a null intent.
      */