From 1a2c552e85fd3a04367e1103c83f5c0f44180ae2 Mon Sep 17 00:00:00 2001
From: Tulio Ribeiro <worm@TAR>
Date: Fri, 25 Mar 2016 13:10:32 +0000
Subject: [PATCH] Tests: ClientTest and BootstrapTest is done!

SyncManager results:
13:08:12.696 ERROR [i.n.u.ResourceLeakDetector:main] LEAK: You are creating too many HashedWheelTimer instances.  HashedWheelTimer is a shared resource that must be reused across the JVM,so that only a few instances are created.
---
 .../net/floodlightcontroller/simpleft/FT.java | 84 ++++++++++++-------
 .../sdnplatform/sync/client/ClientTest.java   | 17 ++--
 .../sync/internal/BootstrapTest.java          | 10 +--
 .../sync/internal/SyncManagerTest.java        | 19 ++---
 4 files changed, 74 insertions(+), 56 deletions(-)

diff --git a/src/main/java/net/floodlightcontroller/simpleft/FT.java b/src/main/java/net/floodlightcontroller/simpleft/FT.java
index b175706b4..e0ce2768f 100644
--- a/src/main/java/net/floodlightcontroller/simpleft/FT.java
+++ b/src/main/java/net/floodlightcontroller/simpleft/FT.java
@@ -2,6 +2,7 @@ package net.floodlightcontroller.simpleft;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
@@ -11,16 +12,24 @@ import java.util.concurrent.TimeUnit;
 
 import net.floodlightcontroller.core.FloodlightContext;
 import net.floodlightcontroller.core.IFloodlightProviderService;
+import net.floodlightcontroller.core.IOFConnectionBackend;
 import net.floodlightcontroller.core.IOFMessageListener;
 import net.floodlightcontroller.core.IOFSwitch;
+import net.floodlightcontroller.core.internal.FloodlightProvider;
+import net.floodlightcontroller.core.internal.IOFConnectionListener;
+import net.floodlightcontroller.core.internal.IOFSwitchManager;
+import net.floodlightcontroller.core.internal.IOFSwitchService;
+import net.floodlightcontroller.core.internal.ISwitchDriverRegistry;
 import net.floodlightcontroller.core.module.FloodlightModuleContext;
 import net.floodlightcontroller.core.module.FloodlightModuleException;
 import net.floodlightcontroller.core.module.IFloodlightModule;
+import net.floodlightcontroller.core.module.IFloodlightModuleContext;
 import net.floodlightcontroller.core.module.IFloodlightService;
 import net.floodlightcontroller.core.util.SingletonTask;
 import net.floodlightcontroller.storage.IStorageSourceService;
 import net.floodlightcontroller.threadpool.IThreadPoolService;
 import net.floodlightcontroller.threadpool.ThreadPool;
+import net.floodlightcontroller.util.TimedCache;
 
 import org.projectfloodlight.openflow.protocol.OFMessage;
 import org.projectfloodlight.openflow.protocol.OFType;
@@ -30,6 +39,8 @@ import org.sdnplatform.sync.ISyncService;
 import org.sdnplatform.sync.ISyncService.Scope;
 import org.sdnplatform.sync.error.SyncException;
 import org.sdnplatform.sync.internal.SyncManager;
+import org.sdnplatform.sync.internal.config.ClusterConfig;
+import org.sdnplatform.sync.internal.config.IClusterConfigProvider;
 import org.sdnplatform.sync.internal.config.Node;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -43,22 +54,21 @@ IStoreListener<String>
 {
 
 	private ISyncService syncService;
-	private IStoreClient<String, Object> storeClient;
+	private IStoreClient<String, Long> storeClient;
+	//private IFloodlightProviderService floodlightProvider;
 	
-	protected FloodlightModuleContext[] moduleContexts;
-	protected SyncManager[] syncManagers;
-	protected final static ObjectMapper mapper = new ObjectMapper();
-	protected String nodeString;
-	ArrayList<Node> nodes;
-
-	ThreadPool tp;
-
-
-
 	protected static Logger logger = LoggerFactory.getLogger(FT.class);
 
 	protected static IThreadPoolService threadPoolService;
-	protected static SingletonTask testTask;	
+	protected static SingletonTask testTask;
+	
+	protected IOFSwitchManager sm;
+		
+	private String controllerId;
+	
+	//private HashMap<String, Long> cluster;
+	//private TimedCache<String> tc = new TimedCache<>(10, 20000);
+	
 
 	@Override
 	public String getName() {
@@ -101,11 +111,10 @@ IStoreListener<String>
 		// TODO Auto-generated method stub
 		Collection<Class<? extends IFloodlightService>> l =
 				new ArrayList<Class<? extends IFloodlightService>>();
-		l.add(IFloodlightProviderService.class);
+		//l.add(IFloodlightProviderService.class);
 		l.add(IStorageSourceService.class);
 		l.add(ISyncService.class);
 		l.add(IThreadPoolService.class);
-
 		return l;
 	}
 
@@ -116,6 +125,11 @@ IStoreListener<String>
 		
 		this.syncService = context.getServiceImpl(ISyncService.class);
 		threadPoolService = context.getServiceImpl(IThreadPoolService.class);
+		//floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
+		
+
+		Map<String, String> configParams = context.getConfigParams(FloodlightProvider.class);
+		controllerId = configParams.get("controllerId");
 			
 	}
 
@@ -125,46 +139,45 @@ IStoreListener<String>
 
 
 		try {
-			this.syncService.registerStore("NIB", Scope.GLOBAL);
+			this.syncService.registerStore("FT", Scope.GLOBAL);
 			this.storeClient = this.syncService
-					.getStoreClient("NIB",
+					.getStoreClient("FT",
 							String.class,
-							Object.class);
-			this.storeClient.put("NIB", new String("Algo aqui..."));
+							Long.class);
+			this.storeClient.put(controllerId, new Long(0L));
 			this.storeClient.addStoreListener(this);
 		} catch (SyncException e) {
 			throw new FloodlightModuleException("Error while setting up sync service", e);
 		}
 
 
-		//logger.debug("Iniciado SYNC, testing now:");
-		String obj;
+		/*String obj;
 		try {
-			obj = this.storeClient.getValue("NIB").toString();
+			obj = this.storeClient.getValue("FT").toString();
 			logger.debug("Sync retrieve: {}", obj);
-			this.storeClient.put("NIB", new String("B"));
+			this.storeClient.put("FT", new String("INIT"));
 		} catch (SyncException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
-		}
+		}*/
 		
 				
 		ScheduledExecutorService ses = threadPoolService.getScheduledExecutor();
 		testTask = new SingletonTask(ses, new Runnable() {
 			int counter=0;
+			Long tsOld=0L;
 			@Override
 			public void run() { 
 				Random r = new Random();
-				
 				try {
-					storeClient.put("NIB", new String("vl:"+r.nextInt(1000)));
-					//storeClient.put("NIB", new String(""+counter++));
-					
+					//storeClient.put(controllerId, new String("vl:"+r.nextInt(1000)));
+					storeClient.put(controllerId, new Long(System.nanoTime()));
 				} catch (SyncException e) {
 					// TODO Auto-generated catch block
 					e.printStackTrace();
 				}
 				
+				
 				testTask.reschedule(4, TimeUnit.SECONDS);
 			}
 		});
@@ -188,9 +201,18 @@ IStoreListener<String>
 		while(keys.hasNext()){
 			String k = keys.next();
 			try {
-				logger.debug("keysModified: Key:{}, Value:{}, Type: {}", 
-						new Object[] {k, storeClient.get(k).getValue().toString(), type.name()}
-						);
+				/*logger.debug("keysModified: Key:{}, Value:{}, Type: {}", 
+						new Object[] {
+							k, 
+							storeClient.get(k).getValue().toString(), 
+							type.name()}
+						);*/
+				if(type.name().equals("REMOTE")){
+					Long ts = storeClient.get(k).getValue();
+					logger.debug("REMOTE: k:{}, ts:{}", k, ts);
+					//cluster.put(k, ts);
+				}
+				
 				//logger.debug("localNodeID: {}",this.syncService.getLocalNodeId());
 			} catch (SyncException e) {
 				// TODO Auto-generated catch block
@@ -200,4 +222,6 @@ IStoreListener<String>
 		}
 	}
 
+
+
 }
diff --git a/src/test/java/org/sdnplatform/sync/client/ClientTest.java b/src/test/java/org/sdnplatform/sync/client/ClientTest.java
index bd6476577..4a9c2f52c 100644
--- a/src/test/java/org/sdnplatform/sync/client/ClientTest.java
+++ b/src/test/java/org/sdnplatform/sync/client/ClientTest.java
@@ -47,14 +47,14 @@ public class ClientTest {
     
     @Before
     public void setUp() throws Exception {
-        keyStoreFile = new File(keyStoreFolder.getRoot(), 
-                "keystore.jceks");
+        keyStoreFile = new File(keyStoreFolder.getRoot(), "keystore.jceks");
+        
         CryptoUtil.writeSharedSecret(keyStoreFile.getAbsolutePath(), 
                                      keyStorePassword, 
                                      CryptoUtil.secureRandom(16));
         
         nodes = new ArrayList<Node>();
-        nodes.add(new Node("localhost", 40101, (short)1, (short)1));
+        nodes.add(new Node("localhost", 6642, (short)1, (short)1));
         nodeString = mapper.writeValueAsString(nodes);
         
         debugCounterService = new MockDebugCounterService();
@@ -76,10 +76,11 @@ public class ClientTest {
         fmc.addConfigParam(syncManager, "keyStorePassword", keyStorePassword);
         tp.init(fmc);
         syncManager.init(fmc);
-
+        
         tp.startUp(fmc);
+        
         syncManager.startUp(fmc);
-
+        
         syncManager.registerStore("global", Scope.GLOBAL);
     }
 
@@ -98,7 +99,7 @@ public class ClientTest {
     public void testClientBasic() throws Exception {
         SyncClientSettings scs = new SyncClientSettings();
         scs.hostname = "localhost";
-        scs.port = 40101;
+        scs.port = 6642;
         scs.storeName = "global";
         scs.debug = true;
         scs.authScheme = AuthScheme.CHALLENGE_RESPONSE;
@@ -112,7 +113,7 @@ public class ClientTest {
         client.connect();
         client.executeCommandLine("get \"key\"");
         assertEquals("", err.toString());
-        assertEquals("Using remote sync service at localhost:40101\n" +
+        assertEquals("Using remote sync service at localhost:6642\n" +
                 "Getting Key:\n" +
                 "\"key\"\n\n" +
                 "Not found\n",
@@ -182,5 +183,7 @@ public class ClientTest {
         
         client.executeCommandLine("help");
         assert(!"".equals(out.toString()));
+        
+        
     }
 }
diff --git a/src/test/java/org/sdnplatform/sync/internal/BootstrapTest.java b/src/test/java/org/sdnplatform/sync/internal/BootstrapTest.java
index 0bd1764f7..b6276bd99 100644
--- a/src/test/java/org/sdnplatform/sync/internal/BootstrapTest.java
+++ b/src/test/java/org/sdnplatform/sync/internal/BootstrapTest.java
@@ -49,10 +49,10 @@ public class BootstrapTest {
         FloodlightModuleContext fmc = new FloodlightModuleContext();
         ThreadPool tp = new ThreadPool();
 
-        int curPort = 6699;
+        int curPort = 6642;
         
         String keyStorePath = new File(dbFolder.getRoot(), 
-                                       "keystore.jceks").getAbsolutePath();
+                "keystore.jceks").getAbsolutePath();
         String keyStorePassword = "bootstrapping is fun!";
         CryptoUtil.writeSharedSecret(keyStorePath, 
                                      keyStorePassword, 
@@ -94,10 +94,8 @@ public class BootstrapTest {
             // config
             unsyncStore.put("localNodePort", String.valueOf(curPort));
             unsyncStore.put(SyncStoreCCProvider.KEY_STORE_PATH, keyStorePath);
-            unsyncStore.put(SyncStoreCCProvider.KEY_STORE_PASSWORD, 
-                            keyStorePassword);
-            unsyncStore.put(SyncStoreCCProvider.AUTH_SCHEME, 
-                            AuthScheme.CHALLENGE_RESPONSE.toString());
+            unsyncStore.put(SyncStoreCCProvider.KEY_STORE_PASSWORD, keyStorePassword);
+            unsyncStore.put(SyncStoreCCProvider.AUTH_SCHEME, AuthScheme.CHALLENGE_RESPONSE.toString());
 
             String curSeed = "";
             if (i > 0) {
diff --git a/src/test/java/org/sdnplatform/sync/internal/SyncManagerTest.java b/src/test/java/org/sdnplatform/sync/internal/SyncManagerTest.java
index 5bb874c26..aa10cf70c 100644
--- a/src/test/java/org/sdnplatform/sync/internal/SyncManagerTest.java
+++ b/src/test/java/org/sdnplatform/sync/internal/SyncManagerTest.java
@@ -75,14 +75,12 @@ public class SyncManagerTest {
         fmc.addService(IThreadPoolService.class, tp);
         fmc.addService(IDebugCounterService.class, new MockDebugCounterService());
         fmc.addService(IDebugEventService.class, new MockDebugEventService());
-        fmc.addConfigParam(syncManager, "configProviders", 
-                           PropertyCCProvider.class.getName());
+        fmc.addConfigParam(syncManager, "configProviders", PropertyCCProvider.class.getName());
         fmc.addConfigParam(syncManager, "nodes", nodeString);
         fmc.addConfigParam(syncManager, "thisNode", ""+thisNode.getNodeId());
         fmc.addConfigParam(syncManager, "persistenceEnabled", "false");
         fmc.addConfigParam(syncManager, "authScheme", "CHALLENGE_RESPONSE");
-        fmc.addConfigParam(syncManager, "keyStorePath", 
-                           keyStoreFile.getAbsolutePath());
+        fmc.addConfigParam(syncManager, "keyStorePath", keyStoreFile.getAbsolutePath());
         fmc.addConfigParam(syncManager, "keyStorePassword", keyStorePassword);
         tp.init(fmc);
         syncManager.init(fmc);
@@ -104,7 +102,7 @@ public class SyncManagerTest {
 
         tp = new ThreadPool();
         
-        syncManagers = new SyncManager[4];
+        syncManagers = new SyncManager[2];
         moduleContexts = new FloodlightModuleContext[4];
 
         nodes = new ArrayList<Node>();
@@ -165,7 +163,6 @@ public class SyncManagerTest {
 
         result = testClient.get(k);
         assertEquals(tb2, result.getValue());
-        
     }
 
     @Test
@@ -383,15 +380,11 @@ public class SyncManagerTest {
     @Test
     public void testReconnect() throws Exception {
         IStoreClient<String, String> client0 =
-                syncManagers[0].getStoreClient("global", 
-                                               String.class, 
-                                               String.class);
+                syncManagers[0].getStoreClient("global", String.class, String.class);
         IStoreClient<String, String> client1 =
-                syncManagers[1].getStoreClient("global", 
-                                               String.class, String.class);
+                syncManagers[1].getStoreClient("global", String.class, String.class);
         IStoreClient<String, String> client2 =
-                syncManagers[2].getStoreClient("global", 
-                                               String.class, String.class);
+                syncManagers[2].getStoreClient("global", String.class, String.class);
 
         client0.put("key0", "value0");
         waitForValue(client2, "key0", "value0", 1000, "client0");
-- 
GitLab