You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by mu...@apache.org on 2012/06/29 02:21:14 UTC

[2/3] moving out OVS code to plugins/network-elements/ovs

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4109415/plugins/network-elements/ovs/src/com/cloud/network/ovs/OvsTunnelManagerImpl.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/ovs/src/com/cloud/network/ovs/OvsTunnelManagerImpl.java b/plugins/network-elements/ovs/src/com/cloud/network/ovs/OvsTunnelManagerImpl.java
new file mode 100644
index 0000000..834f878
--- /dev/null
+++ b/plugins/network-elements/ovs/src/com/cloud/network/ovs/OvsTunnelManagerImpl.java
@@ -0,0 +1,512 @@
+// Copyright 2012 Citrix Systems, Inc. Licensed under the
+// Apache License, Version 2.0 (the "License"); you may not use this
+// file except in compliance with the License.  Citrix Systems, Inc.
+// reserves all rights not expressly granted by the License.
+// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+// 
+// Automatically generated by addcopyright.py at 04/03/2012
+package com.cloud.network.ovs;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+
+import javax.ejb.Local;
+import javax.naming.ConfigurationException;
+import javax.persistence.EntityExistsException;
+
+import org.apache.log4j.Logger;
+
+import com.cloud.agent.AgentManager;
+import com.cloud.agent.api.Answer;
+import com.cloud.agent.api.Command;
+import com.cloud.agent.manager.Commands;
+import com.cloud.configuration.Config;
+import com.cloud.configuration.dao.ConfigurationDao;
+import com.cloud.deploy.DeployDestination;
+import com.cloud.exception.AgentUnavailableException;
+import com.cloud.exception.OperationTimedoutException;
+import com.cloud.host.Host;
+import com.cloud.host.HostVO;
+import com.cloud.host.dao.HostDao;
+import com.cloud.hypervisor.Hypervisor.HypervisorType;
+import com.cloud.network.Network;
+import com.cloud.network.Networks.TrafficType;
+import com.cloud.network.PhysicalNetworkTrafficType;
+import com.cloud.network.dao.PhysicalNetworkTrafficTypeDao;
+import com.cloud.network.ovs.dao.OvsTunnelInterfaceDao;
+import com.cloud.network.ovs.dao.OvsTunnelInterfaceVO;
+import com.cloud.network.ovs.dao.OvsTunnelNetworkDao;
+import com.cloud.network.ovs.dao.OvsTunnelNetworkVO;
+import com.cloud.utils.component.Inject;
+import com.cloud.utils.concurrency.NamedThreadFactory;
+import com.cloud.utils.db.DB;
+import com.cloud.utils.exception.CloudRuntimeException;
+import com.cloud.vm.DomainRouterVO;
+import com.cloud.vm.UserVmVO;
+import com.cloud.vm.VMInstanceVO;
+import com.cloud.vm.VirtualMachine;
+import com.cloud.vm.VirtualMachine.State;
+import com.cloud.vm.VirtualMachineProfile;
+import com.cloud.vm.dao.DomainRouterDao;
+import com.cloud.vm.dao.NicDao;
+import com.cloud.vm.dao.UserVmDao;
+
+@Local(value={OvsTunnelManager.class})
+public class OvsTunnelManagerImpl implements OvsTunnelManager {
+	public static final Logger s_logger = 
+			Logger.getLogger(OvsTunnelManagerImpl.class.getName());
+	
+	String _name;
+	boolean _isEnabled;
+	ScheduledExecutorService _executorPool;
+    ScheduledExecutorService _cleanupExecutor;
+    
+	@Inject ConfigurationDao _configDao;
+	@Inject NicDao _nicDao;
+	@Inject HostDao _hostDao;
+	@Inject PhysicalNetworkTrafficTypeDao _physNetTTDao;
+	@Inject UserVmDao _userVmDao;
+	@Inject DomainRouterDao _routerDao;
+	@Inject OvsTunnelNetworkDao _tunnelNetworkDao;
+	@Inject OvsTunnelInterfaceDao _tunnelInterfaceDao;
+	@Inject AgentManager _agentMgr;
+	
+	@Override
+	public boolean configure(String name, Map<String, Object> params)
+			throws ConfigurationException {
+		_name = name;
+		_isEnabled = Boolean.parseBoolean(_configDao.getValue(Config.OvsTunnelNetwork.key()));
+		
+		if (_isEnabled) {
+			_executorPool = Executors.newScheduledThreadPool(10, new NamedThreadFactory("OVS"));
+			_cleanupExecutor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("OVS-Cleanup"));
+		}
+		
+		return true;
+	}
+
+	@DB
+	protected OvsTunnelNetworkVO createTunnelRecord(long from, long to,
+													long networkId, int key) {
+		OvsTunnelNetworkVO ta = null;
+		try {
+			ta = new OvsTunnelNetworkVO(from, to, key, networkId);
+			OvsTunnelNetworkVO lock = 
+					_tunnelNetworkDao.acquireInLockTable(Long.valueOf(1));
+			if (lock == null) {
+			    s_logger.warn("Cannot lock table ovs_tunnel_account");
+			    return null;
+			}
+			_tunnelNetworkDao.persist(ta);
+			_tunnelNetworkDao.releaseFromLockTable(lock.getId());
+		} catch (EntityExistsException e) {
+			s_logger.debug("A record for the tunnel from " + from + 
+						   " to " + to + " already exists");
+		}
+		return ta;
+	}
+
+	@DB
+	protected OvsTunnelInterfaceVO createInterfaceRecord(String ip, 
+			String netmask,String mac,long hostId, String label) {
+		OvsTunnelInterfaceVO ti = null;
+		try {
+			ti = new OvsTunnelInterfaceVO(ip, netmask, mac, hostId, label);
+			//TODO: Is locking really necessary here?
+			OvsTunnelInterfaceVO lock =
+					_tunnelInterfaceDao.acquireInLockTable(Long.valueOf(1));
+			if (lock == null) {
+			    s_logger.warn("Cannot lock table ovs_tunnel_account");
+			    return null;
+			}
+			_tunnelInterfaceDao.persist(ti);
+			_tunnelInterfaceDao.releaseFromLockTable(lock.getId());
+		} catch (EntityExistsException e) {
+			s_logger.debug("A record for the interface for network " + label +
+					       " on host id " + hostId + " already exists");
+		}
+		return ti;
+	}
+	
+	private String handleFetchInterfaceAnswer(Answer[] answers, Long hostId){
+		OvsFetchInterfaceAnswer ans = (OvsFetchInterfaceAnswer) answers[0];
+		if (ans.getResult()) {
+			if (ans.getIp() != null && 
+				!("".equals(ans.getIp()))) {
+				OvsTunnelInterfaceVO ti =
+						createInterfaceRecord(ans.getIp(), ans.getNetmask(),
+											  ans.getMac(), hostId, ans.getLabel());
+				return ti.getIp();
+			}
+		}
+		// Fetch interface failed!
+	    s_logger.warn("Unable to fetch the IP address for the GRE tunnel endpoint" +
+	    			  ans.getDetails());
+		return null;
+	}
+
+	private void handleCreateTunnelAnswer(Answer[] answers){
+		OvsCreateTunnelAnswer r = (OvsCreateTunnelAnswer) answers[0];
+		String s = String.format(
+				"(hostIP:%1$s, remoteIP:%2$s, bridge:%3$s," +
+				"greKey:%4$s, portName:%5$s)",
+				r.getFromIp(), r.getToIp(), r.getBridge(),
+				r.getKey(), r.getInPortName());
+		Long from = r.getFrom();
+		Long to = r.getTo();
+		long networkId = r.getNetworkId();
+		OvsTunnelNetworkVO tunnel = _tunnelNetworkDao.getByFromToNetwork(from, to, networkId);
+		if (tunnel == null) {
+            throw new CloudRuntimeException(
+            		String.format("Unable find tunnelNetwork record" +
+            					  "(from=%1$s,to=%2$s, account=%3$s",
+            					  from, to, networkId));
+		}
+		if (!r.getResult()) {
+		    tunnel.setState("FAILED");
+			s_logger.warn("Create GRE tunnel failed due to " +
+					r.getDetails() + s);
+		} else {
+		    tunnel.setState("SUCCESS");
+		    tunnel.setPortName(r.getInPortName());
+		    s_logger.warn("Create GRE tunnel " +
+		    		r.getDetails() + s);
+		}
+		_tunnelNetworkDao.update(tunnel.getId(), tunnel);
+	}
+	
+	private String getGreEndpointIP(Host host, Network nw) throws
+		AgentUnavailableException, OperationTimedoutException {
+		String endpointIp = null;
+		// Fetch fefault name for network label from configuration
+		String physNetLabel = _configDao.getValue(Config.OvsTunnelNetworkDefaultLabel.key());
+        Long physNetId = nw.getPhysicalNetworkId();
+        PhysicalNetworkTrafficType physNetTT = 
+        		_physNetTTDao.findBy(physNetId, TrafficType.Guest);
+        HypervisorType hvType = host.getHypervisorType();
+        
+        switch (hvType) {
+        	case XenServer:
+        		String label = physNetTT.getXenNetworkLabel();
+        		if ((label!=null) && (!label.equals(""))) {
+        			physNetLabel = label; 
+        		}
+        		break;
+        	default:
+        		throw new CloudRuntimeException("Hypervisor " +
+        				hvType.toString() +
+        				" unsupported by OVS Tunnel Manager");
+        }
+		
+        // Try to fetch GRE endpoint IP address for cloud db
+        // If not found, then find it on the hypervisor            
+        OvsTunnelInterfaceVO tunnelIface =
+        		_tunnelInterfaceDao.getByHostAndLabel(host.getId(),
+        											  physNetLabel);
+        if (tunnelIface == null) {
+            //Now find and fetch configuration for physical interface
+        	//for network with label on target host
+			Commands fetchIfaceCmds =
+					new Commands(new OvsFetchInterfaceCommand(physNetLabel));
+			s_logger.debug("Ask host " + host.getId() + 
+						   " to retrieve interface for phy net with label:" +
+						   physNetLabel);
+			Answer[] fetchIfaceAnswers = _agentMgr.send(host.getId(),
+														fetchIfaceCmds);
+            //And finally save it for future use
+			endpointIp = handleFetchInterfaceAnswer(fetchIfaceAnswers,
+													host.getId());
+        } else {
+        	endpointIp = tunnelIface.getIp();
+        }
+        return endpointIp;
+	}
+	
+	private int getGreKey(Network network) {
+		int key = 0;
+		try {
+			//The GRE key is actually in the host part of the URI
+			String keyStr = network.getBroadcastUri().getHost();
+    		// The key is most certainly and int.
+    		// So we feel quite safe in converting it into a string			
+    		key = Integer.valueOf(keyStr);
+    		return key;
+		} catch (NumberFormatException e) {
+			s_logger.debug("Well well, how did '" + key + 
+					       "' end up in the broadcast URI for the network?");
+			throw new CloudRuntimeException(
+					String.format("Invalid GRE key parsed from" +
+							"network broadcast URI (%s)",
+							network.getBroadcastUri().toString()));
+		}
+	}
+	
+	@DB
+    protected void CheckAndCreateTunnel(VirtualMachine instance,
+    									Network nw, DeployDestination dest) {
+		if (!_isEnabled) {
+			return;
+		}
+		
+		s_logger.debug("Creating tunnels with OVS tunnel manager");
+		if (instance.getType() != VirtualMachine.Type.User
+				&& instance.getType() != VirtualMachine.Type.DomainRouter) {
+			s_logger.debug("Will not work if you're not" +
+						   "an instance or a virtual router");
+			return;
+		}
+		
+		long hostId = dest.getHost().getId();
+		int key = getGreKey(nw);
+		// Find active VMs with a NIC on the target network
+		List<UserVmVO> vms = _userVmDao.listByNetworkIdAndStates(nw.getId(),
+							  State.Running, State.Starting,
+							  State.Stopping, State.Unknown, State.Migrating);
+		// Find routers for the network
+		List<DomainRouterVO> routers = _routerDao.findByNetwork(nw.getId());
+		List<VMInstanceVO>ins = new ArrayList<VMInstanceVO>();
+		if (vms != null) {
+			ins.addAll(vms);
+		}
+		if (routers.size() != 0) {
+			ins.addAll(routers);
+		}
+		List<Long> toHostIds = new ArrayList<Long>();
+		List<Long> fromHostIds = new ArrayList<Long>();
+        for (VMInstanceVO v : ins) {
+            Long rh = v.getHostId();
+            if (rh == null || rh.longValue() == hostId) {
+                continue;
+            }
+            OvsTunnelNetworkVO ta =
+            		_tunnelNetworkDao.getByFromToNetwork(hostId,
+            				rh.longValue(), nw.getId());
+            // Try and create the tunnel even if a previous attempt failed
+            if (ta == null || ta.getState().equals("FAILED")) {
+            	s_logger.debug("Attempting to create tunnel from:" +
+            			hostId + " to:" + rh.longValue());
+            	if (ta == null) {
+            		this.createTunnelRecord(hostId, rh.longValue(),
+            				nw.getId(), key);
+            	}
+                if (!toHostIds.contains(rh)) {
+                    toHostIds.add(rh);
+                } 
+            }
+
+            ta = _tunnelNetworkDao.getByFromToNetwork(rh.longValue(),
+            		hostId, nw.getId());
+            // Try and create the tunnel even if a previous attempt failed            
+            if (ta == null || ta.getState().equals("FAILED")) {
+            	s_logger.debug("Attempting to create tunnel from:" +
+            			rh.longValue() + " to:" + hostId);
+            	if (ta == null) {
+            		this.createTunnelRecord(rh.longValue(), hostId,
+            				nw.getId(), key);
+            	} 
+                if (!fromHostIds.contains(rh)) {
+                    fromHostIds.add(rh);
+                }
+            }
+        }
+		//TODO: Should we propagate the exception here?
+        try {
+            String myIp = getGreEndpointIP(dest.getHost(), nw);
+            if (myIp == null)
+            	throw new GreTunnelException("Unable to retrieve the source " +
+            								 "endpoint for the GRE tunnel." +
+            								 "Failure is on host:" + dest.getHost().getId());
+            boolean noHost = true;
+			for (Long i : toHostIds) {
+				HostVO rHost = _hostDao.findById(i);
+				String otherIp = getGreEndpointIP(rHost, nw);
+	            if (otherIp == null)
+	            	throw new GreTunnelException("Unable to retrieve the remote " +
+	            								 "endpoint for the GRE tunnel." +
+	            								 "Failure is on host:" + rHost.getId());
+				Commands cmds = new Commands(
+						new OvsCreateTunnelCommand(otherIp, key, 
+								Long.valueOf(hostId), i, nw.getId(), myIp));
+				s_logger.debug("Ask host " + hostId + 
+						" to create gre tunnel to " + i);
+				Answer[] answers = _agentMgr.send(hostId, cmds);
+				handleCreateTunnelAnswer(answers);
+				noHost = false;
+			}
+			
+			for (Long i : fromHostIds) {
+			    HostVO rHost = _hostDao.findById(i);
+			    String otherIp = getGreEndpointIP(rHost, nw);
+				Commands cmds = new Commands(
+				        new OvsCreateTunnelCommand(myIp, key, i,
+				        						   Long.valueOf(hostId),
+				        		                   nw.getId(), otherIp));
+				s_logger.debug("Ask host " + i +
+						" to create gre tunnel to " + hostId);
+				Answer[] answers = _agentMgr.send(i, cmds);
+				handleCreateTunnelAnswer(answers);
+				noHost = false;
+			}
+			// If no tunnels have been configured, perform the bridge setup anyway
+			// This will ensure VIF rules will be triggered
+			if (noHost) {
+				Commands cmds = new Commands(
+						new OvsSetupBridgeCommand(key, hostId, nw.getId()));
+				s_logger.debug("Ask host " + hostId + 
+						" to configure bridge for network:" + nw.getId());
+				Answer[] answers = _agentMgr.send(hostId, cmds);
+				handleSetupBridgeAnswer(answers);
+			}
+		} catch (Exception e) {
+		    // I really thing we should do a better handling of these exceptions
+			s_logger.warn("Ovs Tunnel network created tunnel failed", e);
+		}	
+	}
+	
+	@Override
+	public boolean start() {
+		return true;
+	}
+
+	@Override
+	public boolean stop() {
+		return true;
+	}
+
+	@Override
+	public String getName() {
+		return _name;
+	}
+
+	@Override
+	public boolean isOvsTunnelEnabled() {
+		return _isEnabled;
+	}
+
+    @Override
+    public void VmCheckAndCreateTunnel(
+    		VirtualMachineProfile<? extends VirtualMachine> vm,
+    		Network nw, DeployDestination dest) {
+        CheckAndCreateTunnel(vm.getVirtualMachine(), nw, dest);    
+    }
+
+    @DB
+    private void handleDestroyTunnelAnswer(Answer ans, long from,
+    									   long to, long network_id) {
+        if (ans.getResult()) {
+            OvsTunnelNetworkVO lock = _tunnelNetworkDao.acquireInLockTable(Long.valueOf(1));
+            if (lock == null) {
+                s_logger.warn(String.format("failed to lock" +
+                		"ovs_tunnel_account, remove record of " + 
+                         "tunnel(from=%1$s, to=%2$s account=%3$s) failed",
+                         from, to, network_id));
+                return;
+            }
+
+            _tunnelNetworkDao.removeByFromToNetwork(from, to, network_id);
+            _tunnelNetworkDao.releaseFromLockTable(lock.getId());
+            
+            s_logger.debug(String.format("Destroy tunnel(account:%1$s," +
+            		"from:%2$s, to:%3$s) successful",
+            		network_id, from, to)); 
+        } else {
+            s_logger.debug(String.format("Destroy tunnel(account:%1$s," +
+            		"from:%2$s, to:%3$s) failed",
+            		network_id, from, to));
+        }
+    }
+
+    @DB
+    private void handleDestroyBridgeAnswer(Answer ans,
+    		long host_id, long network_id) {
+        
+        if (ans.getResult()) {
+            OvsTunnelNetworkVO lock =
+            		_tunnelNetworkDao.acquireInLockTable(Long.valueOf(1));
+            if (lock == null) {
+                s_logger.warn("failed to lock ovs_tunnel_network," +
+                		"remove record");
+                return;
+            }
+
+            _tunnelNetworkDao.removeByFromNetwork(host_id, network_id);
+            _tunnelNetworkDao.releaseFromLockTable(lock.getId());
+            
+            s_logger.debug(String.format("Destroy bridge for" +
+            		"network %1$s successful", network_id)); 
+        } else {
+        	s_logger.debug(String.format("Destroy bridge for" +
+        			"network %1$s failed", network_id));        
+        }
+    }
+
+    private void handleSetupBridgeAnswer(Answer[] answers) {
+    	//TODO: Add some error management here?
+    	s_logger.debug("Placeholder for something more meanginful to come");
+    }
+
+    @Override
+    public void CheckAndDestroyTunnel(VirtualMachine vm, Network nw) {
+    	if (!_isEnabled) {
+            return;
+        }
+        
+        List<UserVmVO> userVms = _userVmDao.listByAccountIdAndHostId(
+        		vm.getAccountId(), vm.getHostId());
+        if (vm.getType() == VirtualMachine.Type.User) {
+            if (userVms.size() > 1) {
+                return;
+            }
+            
+            List<DomainRouterVO> routers =
+            		_routerDao.findByNetwork(nw.getId());
+            for (DomainRouterVO router : routers) {
+                if (router.getHostId() == vm.getHostId()) {
+                	return;
+                }
+            }
+        } else if (vm.getType() == VirtualMachine.Type.DomainRouter &&
+        		userVms.size() != 0) {
+                return;
+        }
+        try {
+            /* Now we are last one on host, destroy the bridge with all 
+             * the tunnels for this network  */
+        	int key = getGreKey(nw);
+            Command cmd = new OvsDestroyBridgeCommand(nw.getId(), key);
+            s_logger.debug("Destroying bridge for network " + nw.getId() +
+            		" on host:" + vm.getHostId());
+            Answer ans = _agentMgr.send(vm.getHostId(), cmd);
+            handleDestroyBridgeAnswer(ans, vm.getHostId(), nw.getId());
+            
+            /* Then ask hosts have peer tunnel with me to destroy them */
+            List<OvsTunnelNetworkVO> peers = 
+            		_tunnelNetworkDao.listByToNetwork(vm.getHostId(),
+            				nw.getId());
+            for (OvsTunnelNetworkVO p : peers) {
+            	// If the tunnel was not successfully created don't bother to remove it
+            	if (p.getState().equals("SUCCESS")) {
+	                cmd = new OvsDestroyTunnelCommand(p.getNetworkId(), key,
+	                		p.getPortName());
+	                s_logger.debug("Destroying tunnel to " + vm.getHostId() + 
+	                		" from " + p.getFrom());
+	                ans = _agentMgr.send(p.getFrom(), cmd);
+	                handleDestroyTunnelAnswer(ans, p.getFrom(),
+	                		p.getTo(), p.getNetworkId());
+            	}
+            }
+        } catch (Exception e) {
+            s_logger.warn(String.format("Destroy tunnel(account:%1$s," +
+            		"hostId:%2$s) failed", vm.getAccountId(), vm.getHostId()), e);
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4109415/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceDao.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceDao.java b/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceDao.java
new file mode 100644
index 0000000..3298ca6
--- /dev/null
+++ b/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceDao.java
@@ -0,0 +1,32 @@
+/**
+ *  Copyright (C) 2010 Cloud.com, Inc.  All rights reserved.
+ * 
+ * This software is licensed under the GNU General Public License v3 or later.
+ * 
+ * It is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or any later version.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * 
+ */
+
+package com.cloud.network.ovs.dao;
+
+import java.util.List;
+
+import com.cloud.utils.db.GenericDao;
+
+public interface OvsTunnelInterfaceDao extends
+		GenericDao<OvsTunnelInterfaceVO, Long> {
+	
+	OvsTunnelInterfaceVO getByHostAndLabel(long hostId, String label);
+
+	List<OvsTunnelInterfaceVO> listByLabel(String label);
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4109415/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceDaoImpl.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceDaoImpl.java b/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceDaoImpl.java
new file mode 100644
index 0000000..5adf63d
--- /dev/null
+++ b/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceDaoImpl.java
@@ -0,0 +1,65 @@
+/**
+ *  Copyright (C) 2010 Cloud.com, Inc.  All rights reserved.
+ * 
+ * This software is licensed under the GNU General Public License v3 or later.
+ * 
+ * It is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or any later version.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * 
+ */
+
+package com.cloud.network.ovs.dao;
+
+import java.util.List;
+
+import javax.ejb.Local;
+
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.SearchCriteria.Op;
+
+@Local(value = { OvsTunnelInterfaceDao.class })
+public class OvsTunnelInterfaceDaoImpl extends
+		GenericDaoBase<OvsTunnelInterfaceVO, Long> implements OvsTunnelInterfaceDao {
+
+	protected final SearchBuilder<OvsTunnelInterfaceVO> hostAndLabelSearch;
+	protected final SearchBuilder<OvsTunnelInterfaceVO> labelSearch;
+	
+	public OvsTunnelInterfaceDaoImpl() {
+		hostAndLabelSearch = createSearchBuilder();
+		hostAndLabelSearch.and("host_id", hostAndLabelSearch.entity().getHostId(), Op.EQ);
+		hostAndLabelSearch.and("label", hostAndLabelSearch.entity().getLabel(), Op.EQ);
+		hostAndLabelSearch.done();
+		
+		labelSearch = createSearchBuilder();
+		labelSearch.and("label", labelSearch.entity().getLabel(), Op.EQ);
+		labelSearch.done();
+		
+	}
+	
+	@Override
+	public OvsTunnelInterfaceVO getByHostAndLabel(long hostId, String label) {
+		SearchCriteria<OvsTunnelInterfaceVO> sc = hostAndLabelSearch.create();
+        sc.setParameters("host_id", hostId);
+        sc.setParameters("label", label);
+		return findOneBy(sc);
+	}
+
+    @Override
+    public List<OvsTunnelInterfaceVO> listByLabel(String label) {
+        SearchCriteria<OvsTunnelInterfaceVO> sc = labelSearch.create();
+        sc.setParameters("label", label);
+        return listBy(sc);
+    }
+
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4109415/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceVO.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceVO.java b/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceVO.java
new file mode 100644
index 0000000..0b2699d
--- /dev/null
+++ b/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceVO.java
@@ -0,0 +1,111 @@
+/**
+ *  Copyright (C) 2010 Cloud.com, Inc.  All rights reserved.
+ * 
+ * This software is licensed under the GNU General Public License v3 or later.
+ * 
+ * It is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or any later version.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * 
+ */
+
+package com.cloud.network.ovs.dao;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity
+@Table(name=("ovs_tunnel_interface"))
+public class OvsTunnelInterfaceVO {
+	@Id
+	@GeneratedValue(strategy = GenerationType.IDENTITY)
+	@Column(name = "id")
+	private long id;
+
+	@Column(name = "ip")
+	private String ip;
+
+	@Column(name = "netmask")
+	private String netmask;
+
+	@Column(name = "mac")
+	private String mac;
+
+	@Column(name = "host_id")
+	private long hostId;
+
+	@Column(name = "label")
+    private String label;
+	
+	public OvsTunnelInterfaceVO() {
+		
+	}
+	
+	public OvsTunnelInterfaceVO(String ip, String netmask, String mac, long hostId, String label) {
+		this.ip = ip;
+		this.netmask = netmask;
+		this.mac = mac;
+		this.hostId = hostId;
+		this.label = label;
+	}
+
+	public long getId() {
+		return id;
+	}
+
+	public void setId(long id) {
+		this.id = id;
+	}
+
+	public String getIp() {
+		return ip;
+	}
+
+	public void setIp(String ip) {
+		this.ip = ip;
+	}
+
+	public String getNetmask() {
+		return netmask;
+	}
+
+	public void setNetmask(String netmask) {
+		this.netmask = netmask;
+	}
+
+	public String getMac() {
+		return mac;
+	}
+
+	public void setMac(String mac) {
+		this.mac = mac;
+	}
+
+	public long getHostId() {
+		return hostId;
+	}
+
+	public void setHostId(long hostId) {
+		this.hostId = hostId;
+	}
+
+	public String getLabel() {
+		return label;
+	}
+
+	public void setLabel(String label) {
+		this.label = label;
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4109415/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelNetworkDao.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelNetworkDao.java b/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelNetworkDao.java
new file mode 100644
index 0000000..4b0b61c
--- /dev/null
+++ b/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelNetworkDao.java
@@ -0,0 +1,31 @@
+/**
+ *  Copyright (C) 2010 Cloud.com, Inc.  All rights reserved.
+ * 
+ * This software is licensed under the GNU General Public License v3 or later.
+ * 
+ * It is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or any later version.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * 
+ */
+
+package com.cloud.network.ovs.dao;
+
+import java.util.List;
+
+import com.cloud.utils.db.GenericDao;
+
+public interface OvsTunnelNetworkDao extends
+		GenericDao<OvsTunnelNetworkVO, Long> {
+	OvsTunnelNetworkVO getByFromToNetwork(long from, long to, long networkId);
+	void removeByFromNetwork(long from, long networkId);
+	void removeByFromToNetwork(long from, long to, long networkId);
+	List<OvsTunnelNetworkVO> listByToNetwork(long to, long networkId);
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4109415/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelNetworkDaoImpl.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelNetworkDaoImpl.java b/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelNetworkDaoImpl.java
new file mode 100644
index 0000000..3b979de
--- /dev/null
+++ b/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelNetworkDaoImpl.java
@@ -0,0 +1,91 @@
+/**
+ *  Copyright (C) 2010 Cloud.com, Inc.  All rights reserved.
+ * 
+ * This software is licensed under the GNU General Public License v3 or later.
+ * 
+ * It is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or any later version.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * 
+ */
+
+package com.cloud.network.ovs.dao;
+
+import java.util.List;
+
+import javax.ejb.Local;
+
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.SearchCriteria.Op;
+
+@Local(value = { OvsTunnelNetworkDao.class })
+public class OvsTunnelNetworkDaoImpl extends
+		GenericDaoBase<OvsTunnelNetworkVO, Long> implements OvsTunnelNetworkDao {
+
+	protected final SearchBuilder<OvsTunnelNetworkVO> fromToNetworkSearch;
+	protected final SearchBuilder<OvsTunnelNetworkVO> fromNetworkSearch;
+	protected final SearchBuilder<OvsTunnelNetworkVO> toNetworkSearch;
+	
+	public OvsTunnelNetworkDaoImpl() {
+		fromToNetworkSearch = createSearchBuilder();
+		fromToNetworkSearch.and("from", fromToNetworkSearch.entity().getFrom(), Op.EQ);
+		fromToNetworkSearch.and("to", fromToNetworkSearch.entity().getTo(), Op.EQ);
+		fromToNetworkSearch.and("network_id", fromToNetworkSearch.entity().getNetworkId(), Op.EQ);
+		fromToNetworkSearch.done();
+		
+		fromNetworkSearch = createSearchBuilder();
+		fromNetworkSearch.and("from", fromNetworkSearch.entity().getFrom(), Op.EQ);
+		fromNetworkSearch.and("network_id", fromNetworkSearch.entity().getNetworkId(), Op.EQ);
+		fromNetworkSearch.done();
+		
+		toNetworkSearch = createSearchBuilder();
+		toNetworkSearch.and("to", toNetworkSearch.entity().getTo(), Op.EQ);
+		toNetworkSearch.and("network_id", toNetworkSearch.entity().getNetworkId(), Op.EQ);
+		toNetworkSearch.done();
+	}
+	
+	@Override
+	public OvsTunnelNetworkVO getByFromToNetwork(long from, long to,
+			long networkId) {
+		SearchCriteria<OvsTunnelNetworkVO> sc = fromToNetworkSearch.create();
+        sc.setParameters("from", from);
+        sc.setParameters("to", to);
+        sc.setParameters("network_id", networkId);
+		return findOneBy(sc);
+	}
+
+    @Override
+    public void removeByFromNetwork(long from, long networkId) {
+        SearchCriteria<OvsTunnelNetworkVO> sc = fromNetworkSearch.create();
+        sc.setParameters("from", from);
+        sc.setParameters("network_id", networkId);
+        remove(sc);
+    }
+
+    @Override
+    public List<OvsTunnelNetworkVO> listByToNetwork(long to, long networkId) {
+        SearchCriteria<OvsTunnelNetworkVO> sc = toNetworkSearch.create();
+        sc.setParameters("to", to);
+        sc.setParameters("network_id", networkId);
+        return listBy(sc);
+    }
+
+    @Override
+    public void removeByFromToNetwork(long from, long to, long networkId) {
+        SearchCriteria<OvsTunnelNetworkVO> sc = fromToNetworkSearch.create();
+        sc.setParameters("from", from);
+        sc.setParameters("to", to);
+        sc.setParameters("network_id", networkId);
+        remove(sc);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4109415/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelNetworkVO.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelNetworkVO.java b/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelNetworkVO.java
new file mode 100644
index 0000000..ad793c8
--- /dev/null
+++ b/plugins/network-elements/ovs/src/com/cloud/network/ovs/dao/OvsTunnelNetworkVO.java
@@ -0,0 +1,106 @@
+/**
+ *  Copyright (C) 2010 Cloud.com, Inc.  All rights reserved.
+ * 
+ * This software is licensed under the GNU General Public License v3 or later.
+ * 
+ * It is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or any later version.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * 
+ */
+
+package com.cloud.network.ovs.dao;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity
+@Table(name=("ovs_tunnel_network"))
+public class OvsTunnelNetworkVO {
+	@Id
+	@GeneratedValue(strategy = GenerationType.IDENTITY)
+	@Column(name = "id")
+	private long id;
+
+	@Column(name = "from")
+	private long from;
+
+	@Column(name = "to")
+	private long to;
+
+	@Column(name = "key")
+	private int key;
+	
+	@Column(name = "network_id")
+	private long networkId;
+	
+	@Column(name = "port_name")
+    private String portName;
+	
+	@Column(name = "state")
+	private String state;
+	
+	public OvsTunnelNetworkVO() {
+		
+	}
+	
+	public OvsTunnelNetworkVO(long from, long to, int key, long networkId) {
+		this.from = from;
+		this.to = to;
+		this.key = key;
+		this.networkId = networkId;
+		this.portName = "[]";
+		this.state = "FAILED";
+	}
+	
+	public void setKey(int key) {
+		this.key = key;
+	}
+	
+	public long getFrom() {
+		return from;
+	}
+	
+	public long getTo() {
+		return to;
+	}
+	
+	public int getKey() {
+		return key;
+	}
+	
+	public long getId() {
+		return id;
+	}
+	
+	public long getNetworkId() {
+		return networkId;
+	}
+	
+	public String getState() {
+	    return state;
+	}
+	
+	public void setState(String state) {
+	    this.state = state;
+	}
+	
+	public void setPortName(String name) {
+	    this.portName = name;
+	}
+	
+	public String getPortName() {
+	    return portName;
+	}
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4109415/server/src/com/cloud/configuration/DefaultComponentLibrary.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/configuration/DefaultComponentLibrary.java b/server/src/com/cloud/configuration/DefaultComponentLibrary.java
index 61b6861..ca0b406 100755
--- a/server/src/com/cloud/configuration/DefaultComponentLibrary.java
+++ b/server/src/com/cloud/configuration/DefaultComponentLibrary.java
@@ -99,9 +99,6 @@ import com.cloud.network.firewall.FirewallManagerImpl;
 import com.cloud.network.lb.ElasticLoadBalancerManagerImpl;
 import com.cloud.network.lb.LoadBalancingRulesManagerImpl;
 import com.cloud.network.lb.dao.ElasticLbVmMapDaoImpl;
-import com.cloud.network.ovs.OvsTunnelManagerImpl;
-import com.cloud.network.ovs.dao.OvsTunnelInterfaceDaoImpl;
-import com.cloud.network.ovs.dao.OvsTunnelNetworkDaoImpl;
 import com.cloud.network.router.VirtualNetworkApplianceManagerImpl;
 import com.cloud.network.rules.RulesManagerImpl;
 import com.cloud.network.rules.dao.PortForwardingRulesDaoImpl;
@@ -286,8 +283,6 @@ public class DefaultComponentLibrary extends ComponentLibraryBase implements Com
         addDao("UsageEventDao", UsageEventDaoImpl.class);
         addDao("ClusterDetailsDao", ClusterDetailsDaoImpl.class);
         addDao("UserVmDetailsDao", UserVmDetailsDaoImpl.class);
-        addDao("OvsTunnelInterfaceDao", OvsTunnelInterfaceDaoImpl.class);
-        addDao("OvsTunnelAccountDao", OvsTunnelNetworkDaoImpl.class);
         addDao("StoragePoolWorkDao", StoragePoolWorkDaoImpl.class);
         addDao("HostTagsDao", HostTagsDaoImpl.class);
         addDao("NetworkDomainDao", NetworkDomainDaoImpl.class);
@@ -360,7 +355,6 @@ public class DefaultComponentLibrary extends ComponentLibraryBase implements Com
         addManager("LoadBalancingRulesManager", LoadBalancingRulesManagerImpl.class);
         addManager("RulesManager", RulesManagerImpl.class);
         addManager("RemoteAccessVpnManager", RemoteAccessVpnManagerImpl.class);
-        addManager("OvsTunnelManager", OvsTunnelManagerImpl.class);
         addManager("Capacity Manager", CapacityManagerImpl.class);
         addManager("VirtualMachineManager", ClusteredVirtualMachineManagerImpl.class);
         addManager("HypervisorGuruManager", HypervisorGuruManagerImpl.class);

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4109415/server/src/com/cloud/network/element/OvsElement.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/element/OvsElement.java b/server/src/com/cloud/network/element/OvsElement.java
deleted file mode 100644
index 9a73667..0000000
--- a/server/src/com/cloud/network/element/OvsElement.java
+++ /dev/null
@@ -1,132 +0,0 @@
-// Copyright 2012 Citrix Systems, Inc. Licensed under the
-// Apache License, Version 2.0 (the "License"); you may not use this
-// file except in compliance with the License.  Citrix Systems, Inc.
-// reserves all rights not expressly granted by the License.
-// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-// 
-// Automatically generated by addcopyright.py at 04/03/2012
-package com.cloud.network.element;
-
-import java.util.List;
-import java.util.Map;
-
-import javax.ejb.Local;
-
-import com.cloud.deploy.DeployDestination;
-import com.cloud.exception.ConcurrentOperationException;
-import com.cloud.exception.InsufficientCapacityException;
-import com.cloud.exception.ResourceUnavailableException;
-import com.cloud.network.Network;
-import com.cloud.network.Network.Capability;
-import com.cloud.network.Network.Provider;
-import com.cloud.network.Network.Service;
-import com.cloud.network.Networks;
-import com.cloud.network.PhysicalNetworkServiceProvider;
-import com.cloud.network.ovs.OvsTunnelManager;
-import com.cloud.offering.NetworkOffering;
-import com.cloud.utils.component.AdapterBase;
-import com.cloud.utils.component.Inject;
-import com.cloud.vm.NicProfile;
-import com.cloud.vm.ReservationContext;
-import com.cloud.vm.VirtualMachine;
-import com.cloud.vm.VirtualMachineProfile;
-
-@Local(value = NetworkElement.class)
-public class OvsElement extends AdapterBase implements NetworkElement {
-    @Inject
-    OvsTunnelManager _ovsTunnelMgr;
-
-    @Override
-    public boolean destroy(Network network)
-            throws ConcurrentOperationException, ResourceUnavailableException {
-        return true;
-    }
-
-    @Override
-    public Map<Service, Map<Capability, String>> getCapabilities() {
-        return null;
-    }
-
-    @Override
-    public Provider getProvider() {
-        return null;
-    }
-
-    @Override
-    public boolean implement(Network network, NetworkOffering offering,
-            DeployDestination dest, ReservationContext context)
-            throws ConcurrentOperationException, ResourceUnavailableException,
-            InsufficientCapacityException {
-        //Consider actually implementing the network here
-    	return true;
-    }
-
-    @Override
-    public boolean prepare(Network network, NicProfile nic,
-            VirtualMachineProfile<? extends VirtualMachine> vm,
-            DeployDestination dest, ReservationContext context)
-            throws ConcurrentOperationException, ResourceUnavailableException,
-            InsufficientCapacityException {
-        if (nic.getBroadcastType() != Networks.BroadcastDomainType.Vswitch) {
-            return true;
-        }
-
-        if (nic.getTrafficType() != Networks.TrafficType.Guest) {
-            return true;
-        }
-
-        _ovsTunnelMgr.VmCheckAndCreateTunnel(vm, network, dest);
-        //_ovsTunnelMgr.applyDefaultFlow(vm.getVirtualMachine(), dest);
-
-        return true;
-    }
-
-    @Override
-    public boolean release(Network network, NicProfile nic,
-            VirtualMachineProfile<? extends VirtualMachine> vm,
-            ReservationContext context) throws ConcurrentOperationException,
-            ResourceUnavailableException {
-        if (nic.getBroadcastType() != Networks.BroadcastDomainType.Vswitch) {
-            return true;
-        }
-
-        if (nic.getTrafficType() != Networks.TrafficType.Guest) {
-            return true;
-        }
-
-        _ovsTunnelMgr.CheckAndDestroyTunnel(vm.getVirtualMachine(), network);
-        return true;
-    }
-
-    @Override
-    public boolean shutdown(Network network, ReservationContext context, boolean cleanup)
-            throws ConcurrentOperationException, ResourceUnavailableException {
-        return true;
-    }
-
-    @Override
-    public boolean isReady(PhysicalNetworkServiceProvider provider) {
-    	return true;
-    }
-
-    @Override
-    public boolean shutdownProviderInstances(PhysicalNetworkServiceProvider provider, ReservationContext context)
-            throws ConcurrentOperationException, ResourceUnavailableException {
-        return true;
-    }
-
-    @Override
-    public boolean canEnableIndividualServices() {
-        return false;
-    }
-
-    @Override
-    public boolean verifyServicesCombination(List<String> services) {
-        return true;
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4109415/server/src/com/cloud/network/guru/ExternalGuestNetworkGuru.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/guru/ExternalGuestNetworkGuru.java b/server/src/com/cloud/network/guru/ExternalGuestNetworkGuru.java
index 6c381f2..b887fc6 100644
--- a/server/src/com/cloud/network/guru/ExternalGuestNetworkGuru.java
+++ b/server/src/com/cloud/network/guru/ExternalGuestNetworkGuru.java
@@ -35,7 +35,6 @@ import com.cloud.network.Networks.BroadcastDomainType;
 import com.cloud.network.PhysicalNetworkVO;
 import com.cloud.network.dao.NetworkDao;
 import com.cloud.network.dao.PhysicalNetworkDao;
-import com.cloud.network.ovs.OvsTunnelManager;
 import com.cloud.network.rules.PortForwardingRuleVO;
 import com.cloud.network.rules.dao.PortForwardingRulesDao;
 import com.cloud.offering.NetworkOffering;
@@ -64,12 +63,13 @@ public class ExternalGuestNetworkGuru extends GuestNetworkGuru {
     DataCenterDao _zoneDao;
     @Inject
     PortForwardingRulesDao _pfRulesDao;
-    @Inject
-    OvsTunnelManager _tunnelMgr;
 
+    //FIXME: why there is dependency on Ovs tunnel manager.
+    
     @Override
     public Network design(NetworkOffering offering, DeploymentPlan plan, Network userSpecified, Account owner) {
-        if (_tunnelMgr.isOvsTunnelEnabled()) {
+    	
+        if (Boolean.parseBoolean(_configDao.getValue(Config.OvsTunnelNetwork.key()))) {
             return null;
         }
 
@@ -88,7 +88,7 @@ public class ExternalGuestNetworkGuru extends GuestNetworkGuru {
     public Network implement(Network config, NetworkOffering offering, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException {
         assert (config.getState() == State.Implementing) : "Why are we implementing " + config;
 
-        if (_tunnelMgr.isOvsTunnelEnabled()) {
+        if (Boolean.parseBoolean(_configDao.getValue(Config.OvsTunnelNetwork.key()))) {
             return null;
         }
         
@@ -171,7 +171,8 @@ public class ExternalGuestNetworkGuru extends GuestNetworkGuru {
         
         NicProfile profile = super.allocate(config, nic, vm);
 
-        if (_tunnelMgr.isOvsTunnelEnabled()) {
+    	boolean _isEnabled = Boolean.parseBoolean(_configDao.getValue(Config.OvsTunnelNetwork.key()));
+        if (_isEnabled) {
             return null;
         }
 
@@ -190,7 +191,7 @@ public class ExternalGuestNetworkGuru extends GuestNetworkGuru {
     public void deallocate(Network config, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm) {
         super.deallocate(config, nic, vm);
 
-        if (_tunnelMgr.isOvsTunnelEnabled()) {
+        if (Boolean.parseBoolean(_configDao.getValue(Config.OvsTunnelNetwork.key()))) {
             return;
         }
         
@@ -207,7 +208,8 @@ public class ExternalGuestNetworkGuru extends GuestNetworkGuru {
     public void reserve(NicProfile nic, Network config, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context)
             throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException {
         assert (nic.getReservationStrategy() == ReservationStrategy.Start) : "What can I do for nics that are not allocated at start? ";
-        if (_tunnelMgr.isOvsTunnelEnabled()) {
+    	boolean _isEnabled = Boolean.parseBoolean(_configDao.getValue(Config.OvsTunnelNetwork.key()));
+        if (_isEnabled) {
             return;
         }
         
@@ -241,7 +243,8 @@ public class ExternalGuestNetworkGuru extends GuestNetworkGuru {
 
     @Override
     public boolean release(NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm, String reservationId) {
-        if (_tunnelMgr.isOvsTunnelEnabled()) {
+
+        if (Boolean.parseBoolean(_configDao.getValue(Config.OvsTunnelNetwork.key()))) {
             return true;
         }
 

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4109415/server/src/com/cloud/network/guru/OvsGuestNetworkGuru.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/guru/OvsGuestNetworkGuru.java b/server/src/com/cloud/network/guru/OvsGuestNetworkGuru.java
deleted file mode 100644
index d031fee..0000000
--- a/server/src/com/cloud/network/guru/OvsGuestNetworkGuru.java
+++ /dev/null
@@ -1,91 +0,0 @@
-// Copyright 2012 Citrix Systems, Inc. Licensed under the
-// Apache License, Version 2.0 (the "License"); you may not use this
-// file except in compliance with the License.  Citrix Systems, Inc.
-// reserves all rights not expressly granted by the License.
-// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-// 
-// Automatically generated by addcopyright.py at 04/03/2012
-package com.cloud.network.guru;
-
-import javax.ejb.Local;
-
-import org.apache.log4j.Logger;
-
-import com.cloud.dc.DataCenter;
-import com.cloud.deploy.DeployDestination;
-import com.cloud.deploy.DeploymentPlan;
-import com.cloud.event.EventTypes;
-import com.cloud.event.EventUtils;
-import com.cloud.event.EventVO;
-import com.cloud.exception.InsufficientAddressCapacityException;
-import com.cloud.exception.InsufficientVirtualNetworkCapcityException;
-import com.cloud.network.Network;
-import com.cloud.network.NetworkManager;
-import com.cloud.network.NetworkVO;
-import com.cloud.network.ovs.OvsTunnelManager;
-import com.cloud.offering.NetworkOffering;
-import com.cloud.user.Account;
-import com.cloud.user.UserContext;
-import com.cloud.utils.component.Inject;
-import com.cloud.vm.Nic.ReservationStrategy;
-import com.cloud.vm.NicProfile;
-import com.cloud.vm.ReservationContext;
-import com.cloud.vm.VirtualMachine;
-import com.cloud.vm.VirtualMachineProfile;
-import com.cloud.network.Networks.BroadcastDomainType;
-import com.cloud.network.Network.State;
-
-@Local(value=NetworkGuru.class)
-public class OvsGuestNetworkGuru extends GuestNetworkGuru {
-	private static final Logger s_logger = Logger.getLogger(OvsGuestNetworkGuru.class);
-	
-	@Inject NetworkManager _externalNetworkManager;
-	@Inject OvsTunnelManager _ovsTunnelMgr;
-	
-	@Override
-    public Network design(NetworkOffering offering, DeploymentPlan plan, Network userSpecified, Account owner) {
-      
-		if (!_ovsTunnelMgr.isOvsTunnelEnabled()) {
-			return null;
-		}
-		
-        NetworkVO config = (NetworkVO) super.design(offering, plan, userSpecified, owner); 
-        if (config == null) {
-        	return null;
-        }
-        
-        config.setBroadcastDomainType(BroadcastDomainType.Vswitch);
-        
-        return config;
-	}
-	
-    protected void allocateVnet(Network network, NetworkVO implemented, long dcId,
-    		long physicalNetworkId, String reservationId) throws InsufficientVirtualNetworkCapcityException {
-        if (network.getBroadcastUri() == null) {
-            String vnet = _dcDao.allocateVnet(dcId, physicalNetworkId, network.getAccountId(), reservationId);
-            if (vnet == null) {
-                throw new InsufficientVirtualNetworkCapcityException("Unable to allocate vnet as a part of network " + network + " implement ", DataCenter.class, dcId);
-            }
-            implemented.setBroadcastUri(BroadcastDomainType.Vswitch.toUri(vnet));
-            EventUtils.saveEvent(UserContext.current().getCallerUserId(), network.getAccountId(), EventVO.LEVEL_INFO, EventTypes.EVENT_ZONE_VLAN_ASSIGN, "Assigned Zone Vlan: "+vnet+ " Network Id: "+network.getId(), 0);
-        } else {
-            implemented.setBroadcastUri(network.getBroadcastUri());
-        }
-    }
-	
-	@Override
-	public Network implement(Network config, NetworkOffering offering, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException {
-		 assert (config.getState() == State.Implementing) : "Why are we implementing " + config;
-		 if (!_ovsTunnelMgr.isOvsTunnelEnabled()) {
-			 return null;
-		 }
-		 NetworkVO implemented = (NetworkVO)super.implement(config, offering, dest, context);		 
-         return implemented;
-	}
-	
-}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4109415/server/src/com/cloud/network/ovs/GreTunnelException.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/ovs/GreTunnelException.java b/server/src/com/cloud/network/ovs/GreTunnelException.java
deleted file mode 100644
index a364501..0000000
--- a/server/src/com/cloud/network/ovs/GreTunnelException.java
+++ /dev/null
@@ -1,19 +0,0 @@
-// Copyright 2012 Citrix Systems, Inc. Licensed under the
-// Apache License, Version 2.0 (the "License"); you may not use this
-// file except in compliance with the License.  Citrix Systems, Inc.
-// reserves all rights not expressly granted by the License.
-// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-// 
-// Automatically generated by addcopyright.py at 04/03/2012
-package com.cloud.network.ovs;
-
-public class GreTunnelException extends Exception {
-	public GreTunnelException(String msg) {
-		super(msg);
-	}
-}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4109415/server/src/com/cloud/network/ovs/OvsTunnelManager.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/ovs/OvsTunnelManager.java b/server/src/com/cloud/network/ovs/OvsTunnelManager.java
deleted file mode 100644
index 5b1e1bf..0000000
--- a/server/src/com/cloud/network/ovs/OvsTunnelManager.java
+++ /dev/null
@@ -1,30 +0,0 @@
-// Copyright 2012 Citrix Systems, Inc. Licensed under the
-// Apache License, Version 2.0 (the "License"); you may not use this
-// file except in compliance with the License.  Citrix Systems, Inc.
-// reserves all rights not expressly granted by the License.
-// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-// 
-// Automatically generated by addcopyright.py at 04/03/2012
-package com.cloud.network.ovs;
-
-import com.cloud.deploy.DeployDestination;
-import com.cloud.network.Network;
-import com.cloud.utils.component.Manager;
-import com.cloud.vm.VirtualMachine;
-import com.cloud.vm.VirtualMachineProfile;
-
-public interface OvsTunnelManager extends Manager {
-	
-	boolean isOvsTunnelEnabled();
-
-    public void VmCheckAndCreateTunnel(VirtualMachineProfile<? extends VirtualMachine> vm,
-    		Network nw, DeployDestination dest);
-    
-    public void CheckAndDestroyTunnel(VirtualMachine vm, Network nw);
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4109415/server/src/com/cloud/network/ovs/OvsTunnelManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/ovs/OvsTunnelManagerImpl.java b/server/src/com/cloud/network/ovs/OvsTunnelManagerImpl.java
deleted file mode 100644
index 834f878..0000000
--- a/server/src/com/cloud/network/ovs/OvsTunnelManagerImpl.java
+++ /dev/null
@@ -1,512 +0,0 @@
-// Copyright 2012 Citrix Systems, Inc. Licensed under the
-// Apache License, Version 2.0 (the "License"); you may not use this
-// file except in compliance with the License.  Citrix Systems, Inc.
-// reserves all rights not expressly granted by the License.
-// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-// 
-// Automatically generated by addcopyright.py at 04/03/2012
-package com.cloud.network.ovs;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-
-import javax.ejb.Local;
-import javax.naming.ConfigurationException;
-import javax.persistence.EntityExistsException;
-
-import org.apache.log4j.Logger;
-
-import com.cloud.agent.AgentManager;
-import com.cloud.agent.api.Answer;
-import com.cloud.agent.api.Command;
-import com.cloud.agent.manager.Commands;
-import com.cloud.configuration.Config;
-import com.cloud.configuration.dao.ConfigurationDao;
-import com.cloud.deploy.DeployDestination;
-import com.cloud.exception.AgentUnavailableException;
-import com.cloud.exception.OperationTimedoutException;
-import com.cloud.host.Host;
-import com.cloud.host.HostVO;
-import com.cloud.host.dao.HostDao;
-import com.cloud.hypervisor.Hypervisor.HypervisorType;
-import com.cloud.network.Network;
-import com.cloud.network.Networks.TrafficType;
-import com.cloud.network.PhysicalNetworkTrafficType;
-import com.cloud.network.dao.PhysicalNetworkTrafficTypeDao;
-import com.cloud.network.ovs.dao.OvsTunnelInterfaceDao;
-import com.cloud.network.ovs.dao.OvsTunnelInterfaceVO;
-import com.cloud.network.ovs.dao.OvsTunnelNetworkDao;
-import com.cloud.network.ovs.dao.OvsTunnelNetworkVO;
-import com.cloud.utils.component.Inject;
-import com.cloud.utils.concurrency.NamedThreadFactory;
-import com.cloud.utils.db.DB;
-import com.cloud.utils.exception.CloudRuntimeException;
-import com.cloud.vm.DomainRouterVO;
-import com.cloud.vm.UserVmVO;
-import com.cloud.vm.VMInstanceVO;
-import com.cloud.vm.VirtualMachine;
-import com.cloud.vm.VirtualMachine.State;
-import com.cloud.vm.VirtualMachineProfile;
-import com.cloud.vm.dao.DomainRouterDao;
-import com.cloud.vm.dao.NicDao;
-import com.cloud.vm.dao.UserVmDao;
-
-@Local(value={OvsTunnelManager.class})
-public class OvsTunnelManagerImpl implements OvsTunnelManager {
-	public static final Logger s_logger = 
-			Logger.getLogger(OvsTunnelManagerImpl.class.getName());
-	
-	String _name;
-	boolean _isEnabled;
-	ScheduledExecutorService _executorPool;
-    ScheduledExecutorService _cleanupExecutor;
-    
-	@Inject ConfigurationDao _configDao;
-	@Inject NicDao _nicDao;
-	@Inject HostDao _hostDao;
-	@Inject PhysicalNetworkTrafficTypeDao _physNetTTDao;
-	@Inject UserVmDao _userVmDao;
-	@Inject DomainRouterDao _routerDao;
-	@Inject OvsTunnelNetworkDao _tunnelNetworkDao;
-	@Inject OvsTunnelInterfaceDao _tunnelInterfaceDao;
-	@Inject AgentManager _agentMgr;
-	
-	@Override
-	public boolean configure(String name, Map<String, Object> params)
-			throws ConfigurationException {
-		_name = name;
-		_isEnabled = Boolean.parseBoolean(_configDao.getValue(Config.OvsTunnelNetwork.key()));
-		
-		if (_isEnabled) {
-			_executorPool = Executors.newScheduledThreadPool(10, new NamedThreadFactory("OVS"));
-			_cleanupExecutor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("OVS-Cleanup"));
-		}
-		
-		return true;
-	}
-
-	@DB
-	protected OvsTunnelNetworkVO createTunnelRecord(long from, long to,
-													long networkId, int key) {
-		OvsTunnelNetworkVO ta = null;
-		try {
-			ta = new OvsTunnelNetworkVO(from, to, key, networkId);
-			OvsTunnelNetworkVO lock = 
-					_tunnelNetworkDao.acquireInLockTable(Long.valueOf(1));
-			if (lock == null) {
-			    s_logger.warn("Cannot lock table ovs_tunnel_account");
-			    return null;
-			}
-			_tunnelNetworkDao.persist(ta);
-			_tunnelNetworkDao.releaseFromLockTable(lock.getId());
-		} catch (EntityExistsException e) {
-			s_logger.debug("A record for the tunnel from " + from + 
-						   " to " + to + " already exists");
-		}
-		return ta;
-	}
-
-	@DB
-	protected OvsTunnelInterfaceVO createInterfaceRecord(String ip, 
-			String netmask,String mac,long hostId, String label) {
-		OvsTunnelInterfaceVO ti = null;
-		try {
-			ti = new OvsTunnelInterfaceVO(ip, netmask, mac, hostId, label);
-			//TODO: Is locking really necessary here?
-			OvsTunnelInterfaceVO lock =
-					_tunnelInterfaceDao.acquireInLockTable(Long.valueOf(1));
-			if (lock == null) {
-			    s_logger.warn("Cannot lock table ovs_tunnel_account");
-			    return null;
-			}
-			_tunnelInterfaceDao.persist(ti);
-			_tunnelInterfaceDao.releaseFromLockTable(lock.getId());
-		} catch (EntityExistsException e) {
-			s_logger.debug("A record for the interface for network " + label +
-					       " on host id " + hostId + " already exists");
-		}
-		return ti;
-	}
-	
-	private String handleFetchInterfaceAnswer(Answer[] answers, Long hostId){
-		OvsFetchInterfaceAnswer ans = (OvsFetchInterfaceAnswer) answers[0];
-		if (ans.getResult()) {
-			if (ans.getIp() != null && 
-				!("".equals(ans.getIp()))) {
-				OvsTunnelInterfaceVO ti =
-						createInterfaceRecord(ans.getIp(), ans.getNetmask(),
-											  ans.getMac(), hostId, ans.getLabel());
-				return ti.getIp();
-			}
-		}
-		// Fetch interface failed!
-	    s_logger.warn("Unable to fetch the IP address for the GRE tunnel endpoint" +
-	    			  ans.getDetails());
-		return null;
-	}
-
-	private void handleCreateTunnelAnswer(Answer[] answers){
-		OvsCreateTunnelAnswer r = (OvsCreateTunnelAnswer) answers[0];
-		String s = String.format(
-				"(hostIP:%1$s, remoteIP:%2$s, bridge:%3$s," +
-				"greKey:%4$s, portName:%5$s)",
-				r.getFromIp(), r.getToIp(), r.getBridge(),
-				r.getKey(), r.getInPortName());
-		Long from = r.getFrom();
-		Long to = r.getTo();
-		long networkId = r.getNetworkId();
-		OvsTunnelNetworkVO tunnel = _tunnelNetworkDao.getByFromToNetwork(from, to, networkId);
-		if (tunnel == null) {
-            throw new CloudRuntimeException(
-            		String.format("Unable find tunnelNetwork record" +
-            					  "(from=%1$s,to=%2$s, account=%3$s",
-            					  from, to, networkId));
-		}
-		if (!r.getResult()) {
-		    tunnel.setState("FAILED");
-			s_logger.warn("Create GRE tunnel failed due to " +
-					r.getDetails() + s);
-		} else {
-		    tunnel.setState("SUCCESS");
-		    tunnel.setPortName(r.getInPortName());
-		    s_logger.warn("Create GRE tunnel " +
-		    		r.getDetails() + s);
-		}
-		_tunnelNetworkDao.update(tunnel.getId(), tunnel);
-	}
-	
-	private String getGreEndpointIP(Host host, Network nw) throws
-		AgentUnavailableException, OperationTimedoutException {
-		String endpointIp = null;
-		// Fetch fefault name for network label from configuration
-		String physNetLabel = _configDao.getValue(Config.OvsTunnelNetworkDefaultLabel.key());
-        Long physNetId = nw.getPhysicalNetworkId();
-        PhysicalNetworkTrafficType physNetTT = 
-        		_physNetTTDao.findBy(physNetId, TrafficType.Guest);
-        HypervisorType hvType = host.getHypervisorType();
-        
-        switch (hvType) {
-        	case XenServer:
-        		String label = physNetTT.getXenNetworkLabel();
-        		if ((label!=null) && (!label.equals(""))) {
-        			physNetLabel = label; 
-        		}
-        		break;
-        	default:
-        		throw new CloudRuntimeException("Hypervisor " +
-        				hvType.toString() +
-        				" unsupported by OVS Tunnel Manager");
-        }
-		
-        // Try to fetch GRE endpoint IP address for cloud db
-        // If not found, then find it on the hypervisor            
-        OvsTunnelInterfaceVO tunnelIface =
-        		_tunnelInterfaceDao.getByHostAndLabel(host.getId(),
-        											  physNetLabel);
-        if (tunnelIface == null) {
-            //Now find and fetch configuration for physical interface
-        	//for network with label on target host
-			Commands fetchIfaceCmds =
-					new Commands(new OvsFetchInterfaceCommand(physNetLabel));
-			s_logger.debug("Ask host " + host.getId() + 
-						   " to retrieve interface for phy net with label:" +
-						   physNetLabel);
-			Answer[] fetchIfaceAnswers = _agentMgr.send(host.getId(),
-														fetchIfaceCmds);
-            //And finally save it for future use
-			endpointIp = handleFetchInterfaceAnswer(fetchIfaceAnswers,
-													host.getId());
-        } else {
-        	endpointIp = tunnelIface.getIp();
-        }
-        return endpointIp;
-	}
-	
-	private int getGreKey(Network network) {
-		int key = 0;
-		try {
-			//The GRE key is actually in the host part of the URI
-			String keyStr = network.getBroadcastUri().getHost();
-    		// The key is most certainly and int.
-    		// So we feel quite safe in converting it into a string			
-    		key = Integer.valueOf(keyStr);
-    		return key;
-		} catch (NumberFormatException e) {
-			s_logger.debug("Well well, how did '" + key + 
-					       "' end up in the broadcast URI for the network?");
-			throw new CloudRuntimeException(
-					String.format("Invalid GRE key parsed from" +
-							"network broadcast URI (%s)",
-							network.getBroadcastUri().toString()));
-		}
-	}
-	
-	@DB
-    protected void CheckAndCreateTunnel(VirtualMachine instance,
-    									Network nw, DeployDestination dest) {
-		if (!_isEnabled) {
-			return;
-		}
-		
-		s_logger.debug("Creating tunnels with OVS tunnel manager");
-		if (instance.getType() != VirtualMachine.Type.User
-				&& instance.getType() != VirtualMachine.Type.DomainRouter) {
-			s_logger.debug("Will not work if you're not" +
-						   "an instance or a virtual router");
-			return;
-		}
-		
-		long hostId = dest.getHost().getId();
-		int key = getGreKey(nw);
-		// Find active VMs with a NIC on the target network
-		List<UserVmVO> vms = _userVmDao.listByNetworkIdAndStates(nw.getId(),
-							  State.Running, State.Starting,
-							  State.Stopping, State.Unknown, State.Migrating);
-		// Find routers for the network
-		List<DomainRouterVO> routers = _routerDao.findByNetwork(nw.getId());
-		List<VMInstanceVO>ins = new ArrayList<VMInstanceVO>();
-		if (vms != null) {
-			ins.addAll(vms);
-		}
-		if (routers.size() != 0) {
-			ins.addAll(routers);
-		}
-		List<Long> toHostIds = new ArrayList<Long>();
-		List<Long> fromHostIds = new ArrayList<Long>();
-        for (VMInstanceVO v : ins) {
-            Long rh = v.getHostId();
-            if (rh == null || rh.longValue() == hostId) {
-                continue;
-            }
-            OvsTunnelNetworkVO ta =
-            		_tunnelNetworkDao.getByFromToNetwork(hostId,
-            				rh.longValue(), nw.getId());
-            // Try and create the tunnel even if a previous attempt failed
-            if (ta == null || ta.getState().equals("FAILED")) {
-            	s_logger.debug("Attempting to create tunnel from:" +
-            			hostId + " to:" + rh.longValue());
-            	if (ta == null) {
-            		this.createTunnelRecord(hostId, rh.longValue(),
-            				nw.getId(), key);
-            	}
-                if (!toHostIds.contains(rh)) {
-                    toHostIds.add(rh);
-                } 
-            }
-
-            ta = _tunnelNetworkDao.getByFromToNetwork(rh.longValue(),
-            		hostId, nw.getId());
-            // Try and create the tunnel even if a previous attempt failed            
-            if (ta == null || ta.getState().equals("FAILED")) {
-            	s_logger.debug("Attempting to create tunnel from:" +
-            			rh.longValue() + " to:" + hostId);
-            	if (ta == null) {
-            		this.createTunnelRecord(rh.longValue(), hostId,
-            				nw.getId(), key);
-            	} 
-                if (!fromHostIds.contains(rh)) {
-                    fromHostIds.add(rh);
-                }
-            }
-        }
-		//TODO: Should we propagate the exception here?
-        try {
-            String myIp = getGreEndpointIP(dest.getHost(), nw);
-            if (myIp == null)
-            	throw new GreTunnelException("Unable to retrieve the source " +
-            								 "endpoint for the GRE tunnel." +
-            								 "Failure is on host:" + dest.getHost().getId());
-            boolean noHost = true;
-			for (Long i : toHostIds) {
-				HostVO rHost = _hostDao.findById(i);
-				String otherIp = getGreEndpointIP(rHost, nw);
-	            if (otherIp == null)
-	            	throw new GreTunnelException("Unable to retrieve the remote " +
-	            								 "endpoint for the GRE tunnel." +
-	            								 "Failure is on host:" + rHost.getId());
-				Commands cmds = new Commands(
-						new OvsCreateTunnelCommand(otherIp, key, 
-								Long.valueOf(hostId), i, nw.getId(), myIp));
-				s_logger.debug("Ask host " + hostId + 
-						" to create gre tunnel to " + i);
-				Answer[] answers = _agentMgr.send(hostId, cmds);
-				handleCreateTunnelAnswer(answers);
-				noHost = false;
-			}
-			
-			for (Long i : fromHostIds) {
-			    HostVO rHost = _hostDao.findById(i);
-			    String otherIp = getGreEndpointIP(rHost, nw);
-				Commands cmds = new Commands(
-				        new OvsCreateTunnelCommand(myIp, key, i,
-				        						   Long.valueOf(hostId),
-				        		                   nw.getId(), otherIp));
-				s_logger.debug("Ask host " + i +
-						" to create gre tunnel to " + hostId);
-				Answer[] answers = _agentMgr.send(i, cmds);
-				handleCreateTunnelAnswer(answers);
-				noHost = false;
-			}
-			// If no tunnels have been configured, perform the bridge setup anyway
-			// This will ensure VIF rules will be triggered
-			if (noHost) {
-				Commands cmds = new Commands(
-						new OvsSetupBridgeCommand(key, hostId, nw.getId()));
-				s_logger.debug("Ask host " + hostId + 
-						" to configure bridge for network:" + nw.getId());
-				Answer[] answers = _agentMgr.send(hostId, cmds);
-				handleSetupBridgeAnswer(answers);
-			}
-		} catch (Exception e) {
-		    // I really thing we should do a better handling of these exceptions
-			s_logger.warn("Ovs Tunnel network created tunnel failed", e);
-		}	
-	}
-	
-	@Override
-	public boolean start() {
-		return true;
-	}
-
-	@Override
-	public boolean stop() {
-		return true;
-	}
-
-	@Override
-	public String getName() {
-		return _name;
-	}
-
-	@Override
-	public boolean isOvsTunnelEnabled() {
-		return _isEnabled;
-	}
-
-    @Override
-    public void VmCheckAndCreateTunnel(
-    		VirtualMachineProfile<? extends VirtualMachine> vm,
-    		Network nw, DeployDestination dest) {
-        CheckAndCreateTunnel(vm.getVirtualMachine(), nw, dest);    
-    }
-
-    @DB
-    private void handleDestroyTunnelAnswer(Answer ans, long from,
-    									   long to, long network_id) {
-        if (ans.getResult()) {
-            OvsTunnelNetworkVO lock = _tunnelNetworkDao.acquireInLockTable(Long.valueOf(1));
-            if (lock == null) {
-                s_logger.warn(String.format("failed to lock" +
-                		"ovs_tunnel_account, remove record of " + 
-                         "tunnel(from=%1$s, to=%2$s account=%3$s) failed",
-                         from, to, network_id));
-                return;
-            }
-
-            _tunnelNetworkDao.removeByFromToNetwork(from, to, network_id);
-            _tunnelNetworkDao.releaseFromLockTable(lock.getId());
-            
-            s_logger.debug(String.format("Destroy tunnel(account:%1$s," +
-            		"from:%2$s, to:%3$s) successful",
-            		network_id, from, to)); 
-        } else {
-            s_logger.debug(String.format("Destroy tunnel(account:%1$s," +
-            		"from:%2$s, to:%3$s) failed",
-            		network_id, from, to));
-        }
-    }
-
-    @DB
-    private void handleDestroyBridgeAnswer(Answer ans,
-    		long host_id, long network_id) {
-        
-        if (ans.getResult()) {
-            OvsTunnelNetworkVO lock =
-            		_tunnelNetworkDao.acquireInLockTable(Long.valueOf(1));
-            if (lock == null) {
-                s_logger.warn("failed to lock ovs_tunnel_network," +
-                		"remove record");
-                return;
-            }
-
-            _tunnelNetworkDao.removeByFromNetwork(host_id, network_id);
-            _tunnelNetworkDao.releaseFromLockTable(lock.getId());
-            
-            s_logger.debug(String.format("Destroy bridge for" +
-            		"network %1$s successful", network_id)); 
-        } else {
-        	s_logger.debug(String.format("Destroy bridge for" +
-        			"network %1$s failed", network_id));        
-        }
-    }
-
-    private void handleSetupBridgeAnswer(Answer[] answers) {
-    	//TODO: Add some error management here?
-    	s_logger.debug("Placeholder for something more meanginful to come");
-    }
-
-    @Override
-    public void CheckAndDestroyTunnel(VirtualMachine vm, Network nw) {
-    	if (!_isEnabled) {
-            return;
-        }
-        
-        List<UserVmVO> userVms = _userVmDao.listByAccountIdAndHostId(
-        		vm.getAccountId(), vm.getHostId());
-        if (vm.getType() == VirtualMachine.Type.User) {
-            if (userVms.size() > 1) {
-                return;
-            }
-            
-            List<DomainRouterVO> routers =
-            		_routerDao.findByNetwork(nw.getId());
-            for (DomainRouterVO router : routers) {
-                if (router.getHostId() == vm.getHostId()) {
-                	return;
-                }
-            }
-        } else if (vm.getType() == VirtualMachine.Type.DomainRouter &&
-        		userVms.size() != 0) {
-                return;
-        }
-        try {
-            /* Now we are last one on host, destroy the bridge with all 
-             * the tunnels for this network  */
-        	int key = getGreKey(nw);
-            Command cmd = new OvsDestroyBridgeCommand(nw.getId(), key);
-            s_logger.debug("Destroying bridge for network " + nw.getId() +
-            		" on host:" + vm.getHostId());
-            Answer ans = _agentMgr.send(vm.getHostId(), cmd);
-            handleDestroyBridgeAnswer(ans, vm.getHostId(), nw.getId());
-            
-            /* Then ask hosts have peer tunnel with me to destroy them */
-            List<OvsTunnelNetworkVO> peers = 
-            		_tunnelNetworkDao.listByToNetwork(vm.getHostId(),
-            				nw.getId());
-            for (OvsTunnelNetworkVO p : peers) {
-            	// If the tunnel was not successfully created don't bother to remove it
-            	if (p.getState().equals("SUCCESS")) {
-	                cmd = new OvsDestroyTunnelCommand(p.getNetworkId(), key,
-	                		p.getPortName());
-	                s_logger.debug("Destroying tunnel to " + vm.getHostId() + 
-	                		" from " + p.getFrom());
-	                ans = _agentMgr.send(p.getFrom(), cmd);
-	                handleDestroyTunnelAnswer(ans, p.getFrom(),
-	                		p.getTo(), p.getNetworkId());
-            	}
-            }
-        } catch (Exception e) {
-            s_logger.warn(String.format("Destroy tunnel(account:%1$s," +
-            		"hostId:%2$s) failed", vm.getAccountId(), vm.getHostId()), e);
-        }
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4109415/server/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceDao.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceDao.java b/server/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceDao.java
deleted file mode 100644
index 3298ca6..0000000
--- a/server/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceDao.java
+++ /dev/null
@@ -1,32 +0,0 @@
-/**
- *  Copyright (C) 2010 Cloud.com, Inc.  All rights reserved.
- * 
- * This software is licensed under the GNU General Public License v3 or later.
- * 
- * It is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or any later version.
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- * 
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- * 
- */
-
-package com.cloud.network.ovs.dao;
-
-import java.util.List;
-
-import com.cloud.utils.db.GenericDao;
-
-public interface OvsTunnelInterfaceDao extends
-		GenericDao<OvsTunnelInterfaceVO, Long> {
-	
-	OvsTunnelInterfaceVO getByHostAndLabel(long hostId, String label);
-
-	List<OvsTunnelInterfaceVO> listByLabel(String label);
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4109415/server/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceDaoImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceDaoImpl.java b/server/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceDaoImpl.java
deleted file mode 100644
index 5adf63d..0000000
--- a/server/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceDaoImpl.java
+++ /dev/null
@@ -1,65 +0,0 @@
-/**
- *  Copyright (C) 2010 Cloud.com, Inc.  All rights reserved.
- * 
- * This software is licensed under the GNU General Public License v3 or later.
- * 
- * It is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or any later version.
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- * 
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- * 
- */
-
-package com.cloud.network.ovs.dao;
-
-import java.util.List;
-
-import javax.ejb.Local;
-
-import com.cloud.utils.db.GenericDaoBase;
-import com.cloud.utils.db.SearchBuilder;
-import com.cloud.utils.db.SearchCriteria;
-import com.cloud.utils.db.SearchCriteria.Op;
-
-@Local(value = { OvsTunnelInterfaceDao.class })
-public class OvsTunnelInterfaceDaoImpl extends
-		GenericDaoBase<OvsTunnelInterfaceVO, Long> implements OvsTunnelInterfaceDao {
-
-	protected final SearchBuilder<OvsTunnelInterfaceVO> hostAndLabelSearch;
-	protected final SearchBuilder<OvsTunnelInterfaceVO> labelSearch;
-	
-	public OvsTunnelInterfaceDaoImpl() {
-		hostAndLabelSearch = createSearchBuilder();
-		hostAndLabelSearch.and("host_id", hostAndLabelSearch.entity().getHostId(), Op.EQ);
-		hostAndLabelSearch.and("label", hostAndLabelSearch.entity().getLabel(), Op.EQ);
-		hostAndLabelSearch.done();
-		
-		labelSearch = createSearchBuilder();
-		labelSearch.and("label", labelSearch.entity().getLabel(), Op.EQ);
-		labelSearch.done();
-		
-	}
-	
-	@Override
-	public OvsTunnelInterfaceVO getByHostAndLabel(long hostId, String label) {
-		SearchCriteria<OvsTunnelInterfaceVO> sc = hostAndLabelSearch.create();
-        sc.setParameters("host_id", hostId);
-        sc.setParameters("label", label);
-		return findOneBy(sc);
-	}
-
-    @Override
-    public List<OvsTunnelInterfaceVO> listByLabel(String label) {
-        SearchCriteria<OvsTunnelInterfaceVO> sc = labelSearch.create();
-        sc.setParameters("label", label);
-        return listBy(sc);
-    }
-
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4109415/server/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceVO.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceVO.java b/server/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceVO.java
deleted file mode 100644
index 0b2699d..0000000
--- a/server/src/com/cloud/network/ovs/dao/OvsTunnelInterfaceVO.java
+++ /dev/null
@@ -1,111 +0,0 @@
-/**
- *  Copyright (C) 2010 Cloud.com, Inc.  All rights reserved.
- * 
- * This software is licensed under the GNU General Public License v3 or later.
- * 
- * It is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or any later version.
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- * 
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- * 
- */
-
-package com.cloud.network.ovs.dao;
-
-import javax.persistence.Column;
-import javax.persistence.Entity;
-import javax.persistence.GeneratedValue;
-import javax.persistence.GenerationType;
-import javax.persistence.Id;
-import javax.persistence.Table;
-
-@Entity
-@Table(name=("ovs_tunnel_interface"))
-public class OvsTunnelInterfaceVO {
-	@Id
-	@GeneratedValue(strategy = GenerationType.IDENTITY)
-	@Column(name = "id")
-	private long id;
-
-	@Column(name = "ip")
-	private String ip;
-
-	@Column(name = "netmask")
-	private String netmask;
-
-	@Column(name = "mac")
-	private String mac;
-
-	@Column(name = "host_id")
-	private long hostId;
-
-	@Column(name = "label")
-    private String label;
-	
-	public OvsTunnelInterfaceVO() {
-		
-	}
-	
-	public OvsTunnelInterfaceVO(String ip, String netmask, String mac, long hostId, String label) {
-		this.ip = ip;
-		this.netmask = netmask;
-		this.mac = mac;
-		this.hostId = hostId;
-		this.label = label;
-	}
-
-	public long getId() {
-		return id;
-	}
-
-	public void setId(long id) {
-		this.id = id;
-	}
-
-	public String getIp() {
-		return ip;
-	}
-
-	public void setIp(String ip) {
-		this.ip = ip;
-	}
-
-	public String getNetmask() {
-		return netmask;
-	}
-
-	public void setNetmask(String netmask) {
-		this.netmask = netmask;
-	}
-
-	public String getMac() {
-		return mac;
-	}
-
-	public void setMac(String mac) {
-		this.mac = mac;
-	}
-
-	public long getHostId() {
-		return hostId;
-	}
-
-	public void setHostId(long hostId) {
-		this.hostId = hostId;
-	}
-
-	public String getLabel() {
-		return label;
-	}
-
-	public void setLabel(String label) {
-		this.label = label;
-	}
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4109415/server/src/com/cloud/network/ovs/dao/OvsTunnelNetworkDao.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/ovs/dao/OvsTunnelNetworkDao.java b/server/src/com/cloud/network/ovs/dao/OvsTunnelNetworkDao.java
deleted file mode 100644
index 4b0b61c..0000000
--- a/server/src/com/cloud/network/ovs/dao/OvsTunnelNetworkDao.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/**
- *  Copyright (C) 2010 Cloud.com, Inc.  All rights reserved.
- * 
- * This software is licensed under the GNU General Public License v3 or later.
- * 
- * It is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or any later version.
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- * 
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- * 
- */
-
-package com.cloud.network.ovs.dao;
-
-import java.util.List;
-
-import com.cloud.utils.db.GenericDao;
-
-public interface OvsTunnelNetworkDao extends
-		GenericDao<OvsTunnelNetworkVO, Long> {
-	OvsTunnelNetworkVO getByFromToNetwork(long from, long to, long networkId);
-	void removeByFromNetwork(long from, long networkId);
-	void removeByFromToNetwork(long from, long to, long networkId);
-	List<OvsTunnelNetworkVO> listByToNetwork(long to, long networkId);
-}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4109415/server/src/com/cloud/network/ovs/dao/OvsTunnelNetworkDaoImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/ovs/dao/OvsTunnelNetworkDaoImpl.java b/server/src/com/cloud/network/ovs/dao/OvsTunnelNetworkDaoImpl.java
deleted file mode 100644
index 3b979de..0000000
--- a/server/src/com/cloud/network/ovs/dao/OvsTunnelNetworkDaoImpl.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/**
- *  Copyright (C) 2010 Cloud.com, Inc.  All rights reserved.
- * 
- * This software is licensed under the GNU General Public License v3 or later.
- * 
- * It is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or any later version.
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- * 
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- * 
- */
-
-package com.cloud.network.ovs.dao;
-
-import java.util.List;
-
-import javax.ejb.Local;
-
-import com.cloud.utils.db.GenericDaoBase;
-import com.cloud.utils.db.SearchBuilder;
-import com.cloud.utils.db.SearchCriteria;
-import com.cloud.utils.db.SearchCriteria.Op;
-
-@Local(value = { OvsTunnelNetworkDao.class })
-public class OvsTunnelNetworkDaoImpl extends
-		GenericDaoBase<OvsTunnelNetworkVO, Long> implements OvsTunnelNetworkDao {
-
-	protected final SearchBuilder<OvsTunnelNetworkVO> fromToNetworkSearch;
-	protected final SearchBuilder<OvsTunnelNetworkVO> fromNetworkSearch;
-	protected final SearchBuilder<OvsTunnelNetworkVO> toNetworkSearch;
-	
-	public OvsTunnelNetworkDaoImpl() {
-		fromToNetworkSearch = createSearchBuilder();
-		fromToNetworkSearch.and("from", fromToNetworkSearch.entity().getFrom(), Op.EQ);
-		fromToNetworkSearch.and("to", fromToNetworkSearch.entity().getTo(), Op.EQ);
-		fromToNetworkSearch.and("network_id", fromToNetworkSearch.entity().getNetworkId(), Op.EQ);
-		fromToNetworkSearch.done();
-		
-		fromNetworkSearch = createSearchBuilder();
-		fromNetworkSearch.and("from", fromNetworkSearch.entity().getFrom(), Op.EQ);
-		fromNetworkSearch.and("network_id", fromNetworkSearch.entity().getNetworkId(), Op.EQ);
-		fromNetworkSearch.done();
-		
-		toNetworkSearch = createSearchBuilder();
-		toNetworkSearch.and("to", toNetworkSearch.entity().getTo(), Op.EQ);
-		toNetworkSearch.and("network_id", toNetworkSearch.entity().getNetworkId(), Op.EQ);
-		toNetworkSearch.done();
-	}
-	
-	@Override
-	public OvsTunnelNetworkVO getByFromToNetwork(long from, long to,
-			long networkId) {
-		SearchCriteria<OvsTunnelNetworkVO> sc = fromToNetworkSearch.create();
-        sc.setParameters("from", from);
-        sc.setParameters("to", to);
-        sc.setParameters("network_id", networkId);
-		return findOneBy(sc);
-	}
-
-    @Override
-    public void removeByFromNetwork(long from, long networkId) {
-        SearchCriteria<OvsTunnelNetworkVO> sc = fromNetworkSearch.create();
-        sc.setParameters("from", from);
-        sc.setParameters("network_id", networkId);
-        remove(sc);
-    }
-
-    @Override
-    public List<OvsTunnelNetworkVO> listByToNetwork(long to, long networkId) {
-        SearchCriteria<OvsTunnelNetworkVO> sc = toNetworkSearch.create();
-        sc.setParameters("to", to);
-        sc.setParameters("network_id", networkId);
-        return listBy(sc);
-    }
-
-    @Override
-    public void removeByFromToNetwork(long from, long to, long networkId) {
-        SearchCriteria<OvsTunnelNetworkVO> sc = fromToNetworkSearch.create();
-        sc.setParameters("from", from);
-        sc.setParameters("to", to);
-        sc.setParameters("network_id", networkId);
-        remove(sc);
-    }
-
-}