Merge branch 'master' of https://github.com/OPENNETWORKINGLAB/ONOS

Conflicts:
	start-onos.sh
diff --git a/src/main/java/net/floodlightcontroller/core/INetMapTopologyObjects.java b/src/main/java/net/floodlightcontroller/core/INetMapTopologyObjects.java
index e72baf7..5264648 100644
--- a/src/main/java/net/floodlightcontroller/core/INetMapTopologyObjects.java
+++ b/src/main/java/net/floodlightcontroller/core/INetMapTopologyObjects.java
@@ -42,7 +42,8 @@
 		@Adjacency(label="on")
 		public Iterable<IPortObject> getPorts();
 		
-		@Adjacency(label="on")
+		@JsonIgnore
+		@GremlinGroovy("_().out('on').has('number',portnum)")
 		public IPortObject getPort(final short port_num);
 		
 		@Adjacency(label="on")
diff --git a/src/main/java/net/floodlightcontroller/flowcache/FlowManager.java b/src/main/java/net/floodlightcontroller/flowcache/FlowManager.java
index 098e02f..e84a3e8 100644
--- a/src/main/java/net/floodlightcontroller/flowcache/FlowManager.java
+++ b/src/main/java/net/floodlightcontroller/flowcache/FlowManager.java
@@ -102,169 +102,17 @@
     private static Logger log = LoggerFactory.getLogger(FlowManager.class);
 
     // The periodic task(s)
-    private final ScheduledExecutorService measureShortestPathScheduler =
-	Executors.newScheduledThreadPool(1);
-    private final ScheduledExecutorService measureMapReaderScheduler =
-	Executors.newScheduledThreadPool(1);
     private final ScheduledExecutorService mapReaderScheduler =
 	Executors.newScheduledThreadPool(1);
 
-    private BlockingQueue<Runnable> shortestPathQueue = new LinkedBlockingQueue<Runnable>();
-    private ThreadPoolExecutor shortestPathExecutor =
-	new ThreadPoolExecutor(10, 10, 5, TimeUnit.SECONDS, shortestPathQueue);
-
-    class ShortestPathTask implements Runnable {
-	private int hint;
-	private ITopoRouteService topoRouteService;
-	private ArrayList<DataPath> dpList;
-
-	public ShortestPathTask(int hint,
-				ITopoRouteService topoRouteService,
-				ArrayList<DataPath> dpList) {
-	    this.hint = hint;
-	    this.topoRouteService = topoRouteService;
-	    this.dpList = dpList;
-	}
-
-	@Override
-	public void run() {
-	    /*
-	    String logMsg = "MEASUREMENT: Running Thread hint " + this.hint;
-	    log.debug(logMsg);
-	    long startTime = System.nanoTime();
-	    */
-	    for (DataPath dp : this.dpList) {
-		topoRouteService.getTopoShortestPath(dp.srcPort(), dp.dstPort());
-	    }
-	    /*
-	    long estimatedTime = System.nanoTime() - startTime;
-	    double rate = (estimatedTime > 0)? ((double)dpList.size() * 1000000000) / estimatedTime: 0.0;
-	    logMsg = "MEASUREMENT: Computed Thread hint " + hint + ": " + dpList.size() + " shortest paths in " + (double)estimatedTime / 1000000000 + " sec: " + rate + " flows/s";
-	    log.debug(logMsg);
-	    */
-	}
-    }
-
-    final Runnable measureShortestPath = new Runnable() {
-	    public void run() {
-		log.debug("Recomputing Shortest Paths from the Network Map Flows...");
-		if (floodlightProvider == null) {
-		    log.debug("FloodlightProvider service not found!");
-		    return;
-		}
-
-		if (topoRouteService == null) {
-		    log.debug("Topology Route Service not found");
-		    return;
-		}
-
-		int leftoverQueueSize = shortestPathExecutor.getQueue().size();
-		if (leftoverQueueSize > 0) {
-		    String logMsg = "MEASUREMENT: Leftover Shortest Path Queue Size: " + leftoverQueueSize;
-		    log.debug(logMsg);
-		    return;
-		}
-		log.debug("MEASUREMENT: Beginning Shortest Path Computation");
-
-		//
-		// Recompute the Shortest Paths for all Flows
-		//
-		int counter = 0;
-		int hint = 0;
-		ArrayList<DataPath> dpList = new ArrayList<DataPath>();
-		long startTime = System.nanoTime();
-
-		topoRouteService.prepareShortestPathTopo();
-
-		Iterable<IFlowPath> allFlowPaths = conn.utils().getAllFlowPaths(conn);
-		for (IFlowPath flowPathObj : allFlowPaths) {
-		    FlowId flowId = new FlowId(flowPathObj.getFlowId());
-
-		    // log.debug("Found Path {}", flowId.toString());
-		    Dpid srcDpid = new Dpid(flowPathObj.getSrcSwitch());
-		    Port srcPort = new Port(flowPathObj.getSrcPort());
-		    Dpid dstDpid = new Dpid(flowPathObj.getDstSwitch());
-		    Port dstPort = new Port(flowPathObj.getDstPort());
-		    SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
-		    SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
-
-		    /*
-		    DataPath dp = new DataPath();
-		    dp.setSrcPort(srcSwitchPort);
-		    dp.setDstPort(dstSwitchPort);
-		    dpList.add(dp);
-		    if ((dpList.size() % 10) == 0) {
-			shortestPathExecutor.execute(
-				new ShortestPathTask(hint, topoRouteService,
-						     dpList));
-			dpList = new ArrayList<DataPath>();
-			hint++;
-		    }
-		    */
-
-		    DataPath dataPath =
-			topoRouteService.getTopoShortestPath(srcSwitchPort,
-							     dstSwitchPort);
-		    counter++;
-		}
-		if (dpList.size() > 0) {
-		    shortestPathExecutor.execute(
-			new ShortestPathTask(hint, topoRouteService,
-					     dpList));
-		}
-
-		/*
-		// Wait for all tasks to finish
-		try {
-		    while (shortestPathExecutor.getQueue().size() > 0) {
-			Thread.sleep(100);
-		    }
-		} catch (InterruptedException ex) {
-		    log.debug("MEASUREMENT: Shortest Path Computation interrupted");
-		}
-		*/
-
-		conn.endTx(Transaction.COMMIT);
-		topoRouteService.dropShortestPathTopo();
-
-		long estimatedTime = System.nanoTime() - startTime;
-		double rate = (estimatedTime > 0)? ((double)counter * 1000000000) / estimatedTime: 0.0;
-		String logMsg = "MEASUREMENT: Computed " + counter + " shortest paths in " + (double)estimatedTime / 1000000000 + " sec: " + rate + " flows/s";
-		log.debug(logMsg);
-	    }
-	};
-
-    final Runnable measureMapReader = new Runnable() {
-	    public void run() {
-		if (floodlightProvider == null) {
-		    log.debug("FloodlightProvider service not found!");
-		    return;
-		}
-
-		//
-		// Fetch all Flow Entries
-		//
-		int counter = 0;
-		long startTime = System.nanoTime();
-		Iterable<IFlowEntry> allFlowEntries = conn.utils().getAllFlowEntries(conn);
-		for (IFlowEntry flowEntryObj : allFlowEntries) {
-		    counter++;
-		    FlowEntryId flowEntryId =
-			new FlowEntryId(flowEntryObj.getFlowEntryId());
-		    String userState = flowEntryObj.getUserState();
-		    String switchState = flowEntryObj.getSwitchState();
-		}
-		conn.endTx(Transaction.COMMIT);
-
-		long estimatedTime = System.nanoTime() - startTime;
-		double rate = (estimatedTime > 0)? ((double)counter * 1000000000) / estimatedTime: 0.0;
-		String logMsg = "MEASUREMENT: Fetched " + counter + " flow entries in " + (double)estimatedTime / 1000000000 + " sec: " + rate + " entries/s";
-		log.debug(logMsg);
-	    }
-	};
-
     final Runnable mapReader = new Runnable() {
 	    public void run() {
+		long startTime = System.nanoTime();
+		int counterAllFlowEntries = 0;
+		int counterMyNotUpdatedFlowEntries = 0;
+		int counterAllFlowPaths = 0;
+		int counterMyFlowPaths = 0;
+
 		if (floodlightProvider == null) {
 		    log.debug("FloodlightProvider service not found!");
 		    return;
@@ -284,6 +132,7 @@
 		Iterable<IFlowEntry> allFlowEntries =
 		    conn.utils().getAllFlowEntries(conn);
 		for (IFlowEntry flowEntryObj : allFlowEntries) {
+		    counterAllFlowEntries++;
 		    String flowEntryIdStr = flowEntryObj.getFlowEntryId();
 		    String userState = flowEntryObj.getUserState();
 		    String switchState = flowEntryObj.getSwitchState();
@@ -298,14 +147,6 @@
 		    FlowEntryId flowEntryId = new FlowEntryId(flowEntryIdStr);
 		    Dpid dpid = new Dpid(dpidStr);
 
-		    /*
-		    log.debug("Found Flow Entry Id = {} {}",
-			      flowEntryId.toString(),
-			      "DPID = " + dpid.toString() +
-			      " User State: " + userState +
-			      " Switch State: " + switchState);
-		    */
-
 		    if (! switchState.equals("FE_SWITCH_NOT_UPDATED"))
 			continue;	// Ignore the entry: nothing to do
 
@@ -316,11 +157,15 @@
 		    myFlowEntries.put(flowEntryId.value(), flowEntryObj);
 		}
 
+		log.debug("MEASUREMENT: Found {} My Flow Entries NOT_UPDATED",
+			  myFlowEntries.size());
+
 		//
 		// Process my Flow Entries
 		//
 		boolean processed_measurement_flow = false;
 		for (Map.Entry<Long, IFlowEntry> entry : myFlowEntries.entrySet()) {
+		    counterMyNotUpdatedFlowEntries++;
 		    IFlowEntry flowEntryObj = entry.getValue();
 		    IFlowPath flowObj =
 			conn.utils().getFlowPathByFlowEntry(conn,
@@ -329,11 +174,14 @@
 			continue;		// Should NOT happen
 
 		    // Code for measurement purpose
+		    // TODO: Commented-out for now
+		    /*
 		    {
 			if (flowObj.getFlowId().equals(measurementFlowIdStr)) {
 			    processed_measurement_flow = true;
 			}
 		    }
+		    */
 
 		    //
 		    // TODO: Eliminate the re-fetching of flowEntryId,
@@ -477,6 +325,9 @@
 		    }
 		}
 
+		log.debug("MEASUREMENT: Found {} Flow Entries to delete",
+			  deleteFlowEntries.size());
+
 		//
 		// Delete all entries marked for deletion
 		//
@@ -514,15 +365,11 @@
 		// Fetch and recompute the Shortest Path for those
 		// Flow Paths this controller is responsible for.
 		//
-
-		/*
-		 * TODO: For now, the computation of the reconciliation is
-		 * commented-out.
-		 */
 		topoRouteService.prepareShortestPathTopo();
 		Iterable<IFlowPath> allFlowPaths = conn.utils().getAllFlowPaths(conn);
 		HashSet<IFlowPath> flowObjSet = new HashSet<IFlowPath>();
 		for (IFlowPath flowPathObj : allFlowPaths) {
+		    counterAllFlowPaths++;
 		    if (flowPathObj == null)
 			continue;
 		    String dataPathSummaryStr = flowPathObj.getDataPathSummary();
@@ -553,6 +400,21 @@
 		    Port dstPort = new Port(dstPortShort);
 		    SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
 		    SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
+
+		    //
+		    // Use the source DPID as a heuristic to decide
+		    // which controller is responsible for maintaining the
+		    // shortest path.
+		    // NOTE: This heuristic is error-prone: if the switch
+		    // goes away and no controller is responsible for that
+		    // switch, then the original Flow Path is not cleaned-up
+		    //
+		    IOFSwitch mySwitch = mySwitches.get(srcDpid.value());
+		    if (mySwitch == null)
+			continue;	// Ignore: not my responsibility
+
+		    counterMyFlowPaths++;
+
 		    //
 		    // NOTE: Using here the regular getShortestPath() method
 		    // won't work here, because that method calls internally
@@ -576,22 +438,12 @@
 		    if (dataPathSummaryStr.equals(newDataPathSummaryStr))
 			continue;	// Nothing changed
 
-		    //
-		    // Use the source DPID as a heuristic to decide
-		    // which controller is responsible for maintaining the
-		    // shortest path.
-		    // NOTE: This heuristic is error-prone: if the switch
-		    // goes away and no controller is responsible for that
-		    // switch, then the original Flow Path is not cleaned-up
-		    //
-		    IOFSwitch mySwitch = mySwitches.get(srcDpid.value());
-		    if (mySwitch == null)
-			continue;	// Ignore: not my responsibility
-
 		    log.debug("RECONCILE: Need to Reconcile Shortest Path for FlowID {}",
 			      flowId.toString());
 		    flowObjSet.add(flowPathObj);
 		}
+		log.debug("MEASUREMENT: Found {} Flows to reconcile",
+			  flowObjSet.size());
 		reconcileFlows(flowObjSet);
 		topoRouteService.dropShortestPathTopo();
 
@@ -604,19 +456,14 @@
 			(double)estimatedTime / 1000000000 + " sec";
 		    log.debug(logMsg);
 		}
+
+		long estimatedTime = System.nanoTime() - startTime;
+		double rate = (estimatedTime > 0)? ((double)counterAllFlowPaths * 1000000000) / estimatedTime: 0.0;
+		String logMsg = "MEASUREMENT: Processed AllFlowEntries: " + counterAllFlowEntries + " MyNotUpdatedFlowEntries: " + counterMyNotUpdatedFlowEntries + " AllFlowPaths: " + counterAllFlowPaths + " MyFlowPaths: " + counterMyFlowPaths + " in " + (double)estimatedTime / 1000000000 + " sec: " + rate + " paths/s";
+		log.debug(logMsg);
 	    }
 	};
 
-    /*
-    final ScheduledFuture<?> measureShortestPathHandle =
-	measureShortestPathScheduler.scheduleAtFixedRate(measureShortestPath, 10, 10, TimeUnit.SECONDS);
-    */
-
-    /*
-    final ScheduledFuture<?> measureMapReaderHandle =
-	measureMapReaderScheduler.scheduleAtFixedRate(measureMapReader, 10, 10, TimeUnit.SECONDS);
-    */
-
     final ScheduledFuture<?> mapReaderHandle =
 	mapReaderScheduler.scheduleAtFixedRate(mapReader, 3, 3, TimeUnit.SECONDS);
 
@@ -720,9 +567,12 @@
     @Override
     public boolean addFlow(FlowPath flowPath, FlowId flowId,
 			   String dataPathSummaryStr) {
+	/*
+	 * TODO: Commented-out for now
 	if (flowPath.flowId().value() == measurementFlowId) {
 	    modifiedMeasurementFlowTime = System.nanoTime();
 	}
+	*/
 
 	//
 	// Assign the FlowEntry IDs
@@ -937,9 +787,12 @@
      */
     @Override
     public boolean deleteFlow(FlowId flowId) {
+	/*
+	 * TODO: Commented-out for now
 	if (flowId.value() == measurementFlowId) {
 	    modifiedMeasurementFlowTime = System.nanoTime();
 	}
+	*/
 
 	IFlowPath flowObj = null;
 	//
diff --git a/src/main/java/net/floodlightcontroller/linkdiscovery/internal/LinkDiscoveryManager.java b/src/main/java/net/floodlightcontroller/linkdiscovery/internal/LinkDiscoveryManager.java
index 11643fa..634f7eb 100644
--- a/src/main/java/net/floodlightcontroller/linkdiscovery/internal/LinkDiscoveryManager.java
+++ b/src/main/java/net/floodlightcontroller/linkdiscovery/internal/LinkDiscoveryManager.java
@@ -82,10 +82,7 @@
 import net.floodlightcontroller.topology.NodePortTuple;
 import net.floodlightcontroller.util.EventHistory;
 import net.floodlightcontroller.util.EventHistory.EvAction;
-
 import net.onrc.onos.registry.controller.IControllerRegistryService;
-import net.onrc.onos.registry.controller.IControllerRegistryService.ControlChangeCallback;
-import net.onrc.onos.registry.controller.RegistryException;
 
 import org.openflow.protocol.OFMessage;
 import org.openflow.protocol.OFPacketIn;
@@ -130,6 +127,12 @@
 IStorageSourceListener, ILinkDiscoveryService,
 IFloodlightModule, IInfoProvider, IHAListener {
     protected static Logger log = LoggerFactory.getLogger(LinkDiscoveryManager.class);
+    
+    protected enum NetworkMapOperation {
+    	NONE,
+    	INSERT,
+    	UPDATE
+    }
 
     // Names of table/fields for links in the storage API
     private static final String LINK_TABLE_NAME = "controller_link";
@@ -183,13 +186,13 @@
     // Link discovery task details.
     protected SingletonTask discoveryTask;
     protected final int DISCOVERY_TASK_INTERVAL = 1; 
-    protected final int LINK_TIMEOUT = 5; // decreased timeout as part of LLDP process from 35 secs
-    protected final int LLDP_TO_ALL_INTERVAL = 2 ; //decreased from 15 seconds.
+    protected final int LINK_TIMEOUT = 35; // original 35 secs, aggressive 5 secs
+    protected final int LLDP_TO_ALL_INTERVAL = 15 ; //original 15 seconds, aggressive 2 secs.
     protected long lldpClock = 0;
     // This value is intentionally kept higher than LLDP_TO_ALL_INTERVAL.
     // If we want to identify link failures faster, we could decrease this
     // value to a small number, say 1 or 2 sec.
-    protected final int LLDP_TO_KNOWN_INTERVAL= 2; // LLDP frequency for known links from 20 secs
+    protected final int LLDP_TO_KNOWN_INTERVAL= 20; // LLDP frequency for known links
 
     protected LLDPTLV controllerTLV;
     protected ReentrantReadWriteLock lock;
@@ -1025,6 +1028,7 @@
 
         NodePortTuple srcNpt, dstNpt;
         boolean linkChanged = false;
+        NetworkMapOperation operation = NetworkMapOperation.NONE;
 
         lock.writeLock().lock();
         try {
@@ -1073,7 +1077,8 @@
                 writeLinkToStorage(lt, newInfo);
                 
                 // Write link to network map
-                linkStore.update(lt, newInfo, DM_OPERATION.INSERT);
+                operation = NetworkMapOperation.INSERT;
+                //linkStore.update(lt, newInfo, DM_OPERATION.INSERT);
                 
                 updateOperation = UpdateOperation.LINK_UPDATED;
                 linkChanged = true;
@@ -1132,7 +1137,8 @@
                 writeLinkToStorage(lt, newInfo);
 
                 // Write link to network map
-                linkStore.update(lt, newInfo, DM_OPERATION.UPDATE);
+                operation = NetworkMapOperation.UPDATE;
+                //linkStore.update(lt, newInfo, DM_OPERATION.UPDATE);
                 
                 if (linkChanged) {
                     updateOperation = getUpdateOperation(newInfo.getSrcPortState(),
@@ -1162,6 +1168,18 @@
         } finally {
             lock.writeLock().unlock();
         }
+        
+        switch (operation){
+        case INSERT:
+        	linkStore.update(lt, newInfo, DM_OPERATION.INSERT);
+        	break;
+        case UPDATE:
+        	linkStore.update(lt, newInfo, DM_OPERATION.UPDATE);
+        	break;
+        case NONE:
+        default:
+        	break;
+        }
 
         return linkChanged;
     }
diff --git a/src/main/java/net/onrc/onos/util/GraphDBUtils.java b/src/main/java/net/onrc/onos/util/GraphDBUtils.java
index a3d106c..b0ca23b 100644
--- a/src/main/java/net/onrc/onos/util/GraphDBUtils.java
+++ b/src/main/java/net/onrc/onos/util/GraphDBUtils.java
@@ -57,11 +57,12 @@
 	@Override
 	public IPortObject searchPort(GraphDBConnection conn, String dpid, short number) {
 		ISwitchObject sw = searchSwitch(conn, dpid);
-		GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>();
+		return sw != null ? sw.getPort(number): null;
+	/*	GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>();
 		pipe.start(sw.asVertex());
 	    pipe.out("on").has("number", number);
 	    FramedVertexIterable<IPortObject> r = new FramedVertexIterable<IPortObject>(conn.getFramedGraph(), (Iterable) pipe, IPortObject.class);
-	    return r.iterator().hasNext() ? r.iterator().next() : null;		
+	    return r.iterator().hasNext() ? r.iterator().next() : null;		*/
 	}
 
 	@Override