cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ahu...@apache.org
Subject [46/56] [abbrv] [partial] Moved most of the VOs and DAOs from server package into engine-schema as well
Date Mon, 06 May 2013 23:35:26 GMT
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/dc/dao/DataCenterVnetDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/dao/DataCenterVnetDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/DataCenterVnetDaoImpl.java
new file mode 100755
index 0000000..e97f2c6
--- /dev/null
+++ b/engine/schema/src/com/cloud/dc/dao/DataCenterVnetDaoImpl.java
@@ -0,0 +1,318 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with 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.
+package com.cloud.dc.dao;
+
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+import javax.inject.Inject;
+import javax.naming.ConfigurationException;
+
+import com.cloud.exception.InvalidParameterValueException;
+import org.springframework.stereotype.Component;
+
+import com.cloud.dc.DataCenterVnetVO;
+import com.cloud.network.dao.AccountGuestVlanMapDao;
+import com.cloud.network.dao.AccountGuestVlanMapVO;
+import com.cloud.utils.db.DB;
+import com.cloud.utils.db.GenericDao;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.GenericSearchBuilder;
+import com.cloud.utils.db.JoinBuilder;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.SearchCriteria.Func;
+import com.cloud.utils.db.SearchCriteria.Op;
+import com.cloud.utils.db.Transaction;
+import com.cloud.utils.exception.CloudRuntimeException;
+
+/**
+ * DataCenterVnetDaoImpl maintains the one-to-many relationship between
+ * data center/physical_network and the vnet that appears within the physical network.
+ */
+@Component
+@DB(txn=false)
+public class DataCenterVnetDaoImpl extends GenericDaoBase<DataCenterVnetVO, Long> implements DataCenterVnetDao {
+
+    private final SearchBuilder<DataCenterVnetVO> FreeVnetSearch;
+    private final SearchBuilder<DataCenterVnetVO> FreeDedicatedVnetSearch;
+    private final SearchBuilder<DataCenterVnetVO> VnetDcSearch;
+    private final SearchBuilder<DataCenterVnetVO> VnetDcSearchAllocated;
+    private final SearchBuilder<DataCenterVnetVO> DcSearchAllocated;
+    private final SearchBuilder<DataCenterVnetVO> DcSearchAllocatedInRange;
+    private final GenericSearchBuilder<DataCenterVnetVO, Integer> countZoneVlans;
+    private final GenericSearchBuilder<DataCenterVnetVO, Integer> countAllocatedZoneVlans;
+    private final SearchBuilder<DataCenterVnetVO> SearchRange;
+    private final SearchBuilder<DataCenterVnetVO> DedicatedGuestVlanRangeSearch;
+    private final GenericSearchBuilder<DataCenterVnetVO, Integer> countVnetsAllocatedToAccount;
+    protected GenericSearchBuilder<DataCenterVnetVO, Integer> countVnetsDedicatedToAccount;
+    protected SearchBuilder<AccountGuestVlanMapVO> AccountGuestVlanMapSearch;
+
+    @Inject protected AccountGuestVlanMapDao _accountGuestVlanMapDao;
+    
+    public List<DataCenterVnetVO> listAllocatedVnets(long physicalNetworkId) {
+        SearchCriteria<DataCenterVnetVO> sc = DcSearchAllocated.create();
+        sc.setParameters("physicalNetworkId", physicalNetworkId);       
+        return listBy(sc);
+    }
+
+    public List<DataCenterVnetVO> listAllocatedVnetsInRange(long dcId, long physicalNetworkId, Integer start, Integer end) {
+        SearchCriteria<DataCenterVnetVO> sc = DcSearchAllocatedInRange.create();
+        sc.setParameters("dc",dcId);
+        sc.setParameters("physicalNetworkId", physicalNetworkId);
+        sc.setParameters("vnetRange", start.toString(), end.toString());
+        return listBy(sc);
+    }
+
+    public void lockRange(long dcId, long physicalNetworkId, Integer start, Integer end) {
+        SearchCriteria<DataCenterVnetVO> sc = SearchRange.create();
+        sc.setParameters("dc",dcId);
+        sc.setParameters("physicalNetworkId", physicalNetworkId);
+        sc.setParameters("vnetRange", start.toString(), end.toString());
+        lockRows(sc,null,true);
+    }
+
+    public List<DataCenterVnetVO> findVnet(long dcId, String vnet) {
+    	SearchCriteria<DataCenterVnetVO> sc = VnetDcSearch.create();;
+    	sc.setParameters("dc", dcId);
+    	sc.setParameters("vnet", vnet);
+    	return listBy(sc);
+    }
+    
+    public int countZoneVlans(long dcId, boolean onlyCountAllocated){    	
+        SearchCriteria<Integer> sc = onlyCountAllocated ?  countAllocatedZoneVlans.create() : countZoneVlans.create();
+        sc.setParameters("dc", dcId);                
+        return customSearch(sc, null).get(0);
+    }
+    
+    public List<DataCenterVnetVO> findVnet(long dcId, long physicalNetworkId, String vnet) {
+        SearchCriteria<DataCenterVnetVO> sc = VnetDcSearch.create();
+        sc.setParameters("dc", dcId);
+        sc.setParameters("physicalNetworkId", physicalNetworkId);
+        sc.setParameters("vnet", vnet);
+
+        return listBy(sc);
+    }    
+    
+    @DB
+    public void add(long dcId, long physicalNetworkId, int start, int end) {
+        String insertVnet = "INSERT INTO `cloud`.`op_dc_vnet_alloc` (vnet, data_center_id, physical_network_id) VALUES ( ?, ?, ?)";
+        
+        Transaction txn = Transaction.currentTxn();
+        try {
+            txn.start();
+            PreparedStatement stmt = txn.prepareAutoCloseStatement(insertVnet);
+            for (int i = start; i <= end; i++) {
+                stmt.setString(1, String.valueOf(i));
+                stmt.setLong(2, dcId);
+                stmt.setLong(3, physicalNetworkId);
+                stmt.addBatch();
+            }
+            stmt.executeBatch();
+            txn.commit();
+        } catch (SQLException e) {
+           if (!e.getMessage().contains("Duplicate")){
+               txn.rollback();
+               txn.close();
+               throw new CloudRuntimeException("Exception caught adding vnet ", e);
+           }
+        }
+    }
+
+    public void deleteRange(Transaction txn, long dcId, long physicalNetworkId, int start, int end) {
+        String deleteVnet = "DELETE FROM `cloud`.`op_dc_vnet_alloc` WHERE data_center_id=? AND physical_network_id=? AND taken IS NULL AND vnet BETWEEN ? AND ?";
+        try {
+            PreparedStatement stmt = txn.prepareAutoCloseStatement(deleteVnet);
+            stmt.setLong(1,dcId);
+            stmt.setLong(2,physicalNetworkId);
+            stmt.setString(3,((Integer)start).toString());
+            stmt.setString(4,((Integer)end).toString());
+            stmt.execute();
+        } catch (SQLException e) {
+            throw new CloudRuntimeException("Exception caught adding vnet ", e);
+        }
+    }
+    public void delete(long physicalNetworkId) {
+        SearchCriteria<DataCenterVnetVO> sc = VnetDcSearch.create();
+        sc.setParameters("physicalNetworkId", physicalNetworkId);
+        remove(sc);
+    }
+
+    @DB
+    public DataCenterVnetVO take(long physicalNetworkId, long accountId, String reservationId, List<Long> vlanDbIds) {
+        SearchCriteria<DataCenterVnetVO> sc;
+        if (vlanDbIds != null) {
+            sc = FreeDedicatedVnetSearch.create();
+            sc.setParameters("accountGuestVlanMapId", vlanDbIds.toArray());
+        } else {
+            sc = FreeVnetSearch.create();
+        }
+        sc.setParameters("physicalNetworkId", physicalNetworkId);
+        Date now = new Date();
+        Transaction txn = Transaction.currentTxn();
+        txn.start();
+        DataCenterVnetVO vo = lockOneRandomRow(sc, true);
+        if (vo == null) {
+            return null;
+        }
+
+        vo.setTakenAt(now);
+        vo.setAccountId(accountId);
+        vo.setReservationId(reservationId);
+        update(vo.getId(), vo);
+        txn.commit();
+        return vo;
+    }
+
+
+    public void release(String vnet, long physicalNetworkId, long accountId, String reservationId) {
+        SearchCriteria<DataCenterVnetVO> sc = VnetDcSearchAllocated.create();
+        sc.setParameters("vnet", vnet);
+        sc.setParameters("physicalNetworkId", physicalNetworkId);
+        sc.setParameters("account", accountId);
+        sc.setParameters("reservation", reservationId);
+
+        DataCenterVnetVO vo = findOneIncludingRemovedBy(sc);
+        if (vo == null) {
+            return;
+        }
+
+        vo.setTakenAt(null);
+        vo.setAccountId(null);
+        vo.setReservationId(null);
+        update(vo.getId(), vo);
+    }
+
+    @Override
+    public void releaseDedicatedGuestVlans(Long dedicatedGuestVlanRangeId) {
+        SearchCriteria<DataCenterVnetVO> sc = DedicatedGuestVlanRangeSearch.create();
+        sc.setParameters("dedicatedGuestVlanRangeId", dedicatedGuestVlanRangeId);
+        List<DataCenterVnetVO> vnets = listBy(sc);
+        for(DataCenterVnetVO vnet : vnets) {
+           vnet.setAccountGuestVlanMapId(null);
+           update(vnet.getId(), vnet);
+        }
+    }
+
+    @Override
+    public int countVnetsAllocatedToAccount(long dcId, long accountId) {
+        SearchCriteria<Integer> sc = countVnetsAllocatedToAccount.create();
+        sc.setParameters("dc", dcId);
+        sc.setParameters("accountId", accountId);
+        return customSearch(sc, null).get(0);
+    }
+
+    @Override
+    public int countVnetsDedicatedToAccount(long dcId, long accountId) {
+        SearchCriteria<Integer> sc = countVnetsDedicatedToAccount.create();
+        sc.setParameters("dc", dcId);
+        sc.setParameters("accountId", accountId);
+        return customSearch(sc, null).get(0);
+    }
+
+    @Override
+    public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
+        boolean result = super.configure(name, params);
+
+        countVnetsDedicatedToAccount = createSearchBuilder(Integer.class);
+        countVnetsDedicatedToAccount.and("dc", countVnetsDedicatedToAccount.entity().getDataCenterId(), SearchCriteria.Op.EQ);
+        countVnetsDedicatedToAccount.and("accountGuestVlanMapId", countVnetsDedicatedToAccount.entity().getAccountGuestVlanMapId(), Op.NNULL);
+        AccountGuestVlanMapSearch = _accountGuestVlanMapDao.createSearchBuilder();
+        AccountGuestVlanMapSearch.and("accountId", AccountGuestVlanMapSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
+        countVnetsDedicatedToAccount.join("AccountGuestVlanMapSearch", AccountGuestVlanMapSearch, countVnetsDedicatedToAccount.entity().getAccountGuestVlanMapId(),
+                AccountGuestVlanMapSearch.entity().getId(), JoinBuilder.JoinType.INNER);
+        countVnetsDedicatedToAccount.select(null, Func.COUNT, countVnetsDedicatedToAccount.entity().getId());
+        countVnetsDedicatedToAccount.done();
+        AccountGuestVlanMapSearch.done();
+
+        return result;
+    }
+
+    public DataCenterVnetDaoImpl() {
+    	super();
+        DcSearchAllocated = createSearchBuilder();
+        DcSearchAllocated.and("dc", DcSearchAllocated.entity().getDataCenterId(), SearchCriteria.Op.EQ);
+        DcSearchAllocated.and("physicalNetworkId", DcSearchAllocated.entity().getPhysicalNetworkId(), SearchCriteria.Op.EQ);
+        DcSearchAllocated.and("allocated", DcSearchAllocated.entity().getTakenAt(), SearchCriteria.Op.NNULL);
+        DcSearchAllocated.done();
+
+        DcSearchAllocatedInRange = createSearchBuilder();
+        DcSearchAllocatedInRange.and("dc",DcSearchAllocatedInRange.entity().getDataCenterId(), Op.EQ);
+        DcSearchAllocatedInRange.and("physicalNetworkId", DcSearchAllocatedInRange.entity().getPhysicalNetworkId(), Op.EQ);
+        DcSearchAllocatedInRange.and("allocated", DcSearchAllocatedInRange.entity().getTakenAt(), Op.NNULL);
+        DcSearchAllocatedInRange.and("vnetRange", DcSearchAllocatedInRange.entity().getVnet(), Op.BETWEEN);
+        DcSearchAllocatedInRange.done();
+
+        SearchRange = createSearchBuilder();
+        SearchRange.and("dc", SearchRange.entity().getDataCenterId(), Op.EQ);
+        SearchRange.and("physicalNetworkId", SearchRange.entity().getPhysicalNetworkId(), Op.EQ);
+        SearchRange.and("vnetRange", SearchRange.entity().getVnet(), Op.BETWEEN);
+        
+        FreeVnetSearch = createSearchBuilder();
+        FreeVnetSearch.and("dc", FreeVnetSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
+        FreeVnetSearch.and("physicalNetworkId", FreeVnetSearch.entity().getPhysicalNetworkId(), SearchCriteria.Op.EQ);
+        FreeVnetSearch.and("taken", FreeVnetSearch.entity().getTakenAt(), SearchCriteria.Op.NULL);
+        FreeVnetSearch.and("accountGuestVlanMapId", FreeVnetSearch.entity().getAccountGuestVlanMapId(), SearchCriteria.Op.NULL);
+        FreeVnetSearch.done();
+
+        FreeDedicatedVnetSearch = createSearchBuilder();
+        FreeDedicatedVnetSearch.and("dc", FreeDedicatedVnetSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
+        FreeDedicatedVnetSearch.and("physicalNetworkId", FreeDedicatedVnetSearch.entity().getPhysicalNetworkId(), SearchCriteria.Op.EQ);
+        FreeDedicatedVnetSearch.and("taken", FreeDedicatedVnetSearch.entity().getTakenAt(), SearchCriteria.Op.NULL);
+        FreeDedicatedVnetSearch.and("accountGuestVlanMapId", FreeDedicatedVnetSearch.entity().getAccountGuestVlanMapId(), SearchCriteria.Op.IN);
+        FreeDedicatedVnetSearch.done();
+        
+        VnetDcSearch = createSearchBuilder();
+        VnetDcSearch.and("vnet", VnetDcSearch.entity().getVnet(), SearchCriteria.Op.EQ);
+        VnetDcSearch.and("dc", VnetDcSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
+        VnetDcSearch.and("physicalNetworkId", VnetDcSearch.entity().getPhysicalNetworkId(), SearchCriteria.Op.EQ);
+        VnetDcSearch.done();
+        
+        countZoneVlans = createSearchBuilder(Integer.class);
+        countZoneVlans.select(null, Func.COUNT, countZoneVlans.entity().getId());
+        countZoneVlans.and("dc", countZoneVlans.entity().getDataCenterId(), Op.EQ);  
+        countZoneVlans.done();
+        
+        countAllocatedZoneVlans = createSearchBuilder(Integer.class);
+        countAllocatedZoneVlans.select(null, Func.COUNT, countAllocatedZoneVlans.entity().getId());
+        countAllocatedZoneVlans.and("dc", countAllocatedZoneVlans.entity().getDataCenterId(), Op.EQ);
+        countAllocatedZoneVlans.and("allocated", countAllocatedZoneVlans.entity().getTakenAt(), SearchCriteria.Op.NNULL);
+        countAllocatedZoneVlans.done();
+
+        VnetDcSearchAllocated = createSearchBuilder();
+        VnetDcSearchAllocated.and("vnet", VnetDcSearchAllocated.entity().getVnet(), SearchCriteria.Op.EQ);
+        VnetDcSearchAllocated.and("dc", VnetDcSearchAllocated.entity().getDataCenterId(), SearchCriteria.Op.EQ);
+        VnetDcSearchAllocated.and("physicalNetworkId", VnetDcSearchAllocated.entity().getPhysicalNetworkId(), SearchCriteria.Op.EQ);        
+        VnetDcSearchAllocated.and("taken", VnetDcSearchAllocated.entity().getTakenAt(), SearchCriteria.Op.NNULL);
+        VnetDcSearchAllocated.and("account", VnetDcSearchAllocated.entity().getAccountId(), SearchCriteria.Op.EQ);
+        VnetDcSearchAllocated.and("reservation", VnetDcSearchAllocated.entity().getReservationId(), SearchCriteria.Op.EQ);
+        VnetDcSearchAllocated.done();
+
+        DedicatedGuestVlanRangeSearch = createSearchBuilder();
+        DedicatedGuestVlanRangeSearch.and("dedicatedGuestVlanRangeId", DedicatedGuestVlanRangeSearch.entity().getAccountGuestVlanMapId(), SearchCriteria.Op.EQ);
+        DedicatedGuestVlanRangeSearch.done();
+
+        countVnetsAllocatedToAccount = createSearchBuilder(Integer.class);
+        countVnetsAllocatedToAccount.and("dc", countVnetsAllocatedToAccount.entity().getDataCenterId(), SearchCriteria.Op.EQ);
+        countVnetsAllocatedToAccount.and("accountId", countVnetsAllocatedToAccount.entity().getAccountId(), SearchCriteria.Op.EQ);
+        countVnetsAllocatedToAccount.select(null, Func.COUNT, countVnetsAllocatedToAccount.entity().getId());
+        countVnetsAllocatedToAccount.done();
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/dc/dao/DcDetailsDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/dao/DcDetailsDao.java b/engine/schema/src/com/cloud/dc/dao/DcDetailsDao.java
new file mode 100644
index 0000000..a3b72a8
--- /dev/null
+++ b/engine/schema/src/com/cloud/dc/dao/DcDetailsDao.java
@@ -0,0 +1,32 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with 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.
+package com.cloud.dc.dao;
+
+import java.util.Map;
+
+import com.cloud.dc.DcDetailVO;
+import com.cloud.utils.db.GenericDao;
+
+public interface DcDetailsDao extends GenericDao<DcDetailVO, Long> {
+    Map<String, String> findDetails(long dcId);
+    
+    void persist(long dcId, Map<String, String> details);
+    
+    DcDetailVO findDetail(long dcId, String name);
+
+	void deleteDetails(long dcId);
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/dc/dao/DcDetailsDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/dao/DcDetailsDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/DcDetailsDaoImpl.java
new file mode 100644
index 0000000..278fdb4
--- /dev/null
+++ b/engine/schema/src/com/cloud/dc/dao/DcDetailsDaoImpl.java
@@ -0,0 +1,97 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with 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.
+package com.cloud.dc.dao;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.ejb.Local;
+
+import org.springframework.stereotype.Component;
+
+import com.cloud.dc.DcDetailVO;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.Transaction;
+
+@Component
+@Local(value=DcDetailsDao.class)
+public class DcDetailsDaoImpl extends GenericDaoBase<DcDetailVO, Long> implements DcDetailsDao {
+    protected final SearchBuilder<DcDetailVO> DcSearch;
+    protected final SearchBuilder<DcDetailVO> DetailSearch;
+    
+    public DcDetailsDaoImpl() {
+        DcSearch = createSearchBuilder();
+        DcSearch.and("dcId", DcSearch.entity().getDcId(), SearchCriteria.Op.EQ);
+        DcSearch.done();
+        
+        DetailSearch = createSearchBuilder();
+        DetailSearch.and("dcId", DetailSearch.entity().getDcId(), SearchCriteria.Op.EQ);
+        DetailSearch.and("name", DetailSearch.entity().getName(), SearchCriteria.Op.EQ);
+        DetailSearch.done();
+    }
+
+    @Override
+    public DcDetailVO findDetail(long dcId, String name) {
+        SearchCriteria<DcDetailVO> sc = DetailSearch.create();
+        sc.setParameters("dcId", dcId);
+        sc.setParameters("name", name);
+        
+        return findOneIncludingRemovedBy(sc);
+    }
+
+    @Override
+    public Map<String, String> findDetails(long dcId) {
+        SearchCriteria<DcDetailVO> sc = DcSearch.create();
+        sc.setParameters("dcId", dcId);
+        
+        List<DcDetailVO> results = search(sc, null);
+        Map<String, String> details = new HashMap<String, String>(results.size());
+        for (DcDetailVO result : results) {
+            details.put(result.getName(), result.getValue());
+        }
+        return details;
+    }
+    
+    @Override
+    public void deleteDetails(long dcId) {
+        SearchCriteria sc = DcSearch.create();
+        sc.setParameters("dcId", dcId);
+        
+        List<DcDetailVO> results = search(sc, null);
+        for (DcDetailVO result : results) {
+        	remove(result.getId());
+        }
+    }
+
+    @Override
+    public void persist(long dcId, Map<String, String> details) {
+        Transaction txn = Transaction.currentTxn();
+        txn.start();
+        SearchCriteria<DcDetailVO> sc = DcSearch.create();
+        sc.setParameters("dcId", dcId);
+        expunge(sc);
+        
+        for (Map.Entry<String, String> detail : details.entrySet()) {
+            DcDetailVO vo = new DcDetailVO(dcId, detail.getKey(), detail.getValue());
+            persist(vo);
+        }
+        txn.commit();
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/dc/dao/HostPodDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/dao/HostPodDao.java b/engine/schema/src/com/cloud/dc/dao/HostPodDao.java
new file mode 100644
index 0000000..03f7155
--- /dev/null
+++ b/engine/schema/src/com/cloud/dc/dao/HostPodDao.java
@@ -0,0 +1,35 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with 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.
+package com.cloud.dc.dao;
+
+import java.util.HashMap;
+import java.util.List;
+
+import com.cloud.dc.HostPodVO;
+import com.cloud.utils.db.GenericDao;
+import com.cloud.vm.VirtualMachine;
+
+public interface HostPodDao extends GenericDao<HostPodVO, Long> {
+	public List<HostPodVO> listByDataCenterId(long id);
+
+    public HostPodVO findByName(String name, long dcId);
+	
+	public HashMap<Long, List<Object>> getCurrentPodCidrSubnets(long zoneId, long podIdToSkip);
+
+    public List<Long> listDisabledPods(long zoneId);
+
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/dc/dao/HostPodDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/dao/HostPodDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/HostPodDaoImpl.java
new file mode 100644
index 0000000..07b4ad1
--- /dev/null
+++ b/engine/schema/src/com/cloud/dc/dao/HostPodDaoImpl.java
@@ -0,0 +1,134 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with 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.
+package com.cloud.dc.dao;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+import javax.ejb.Local;
+
+import org.apache.log4j.Logger;
+import org.springframework.stereotype.Component;
+
+import com.cloud.dc.HostPodVO;
+import com.cloud.org.Grouping;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.GenericSearchBuilder;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.SearchCriteria.Op;
+import com.cloud.utils.db.Transaction;
+
+@Component
+@Local(value={HostPodDao.class})
+public class HostPodDaoImpl extends GenericDaoBase<HostPodVO, Long> implements HostPodDao {
+    private static final Logger s_logger = Logger.getLogger(HostPodDaoImpl.class);
+
+    protected SearchBuilder<HostPodVO> DataCenterAndNameSearch;
+    protected SearchBuilder<HostPodVO> DataCenterIdSearch;
+
+    public HostPodDaoImpl() {
+        DataCenterAndNameSearch = createSearchBuilder();
+        DataCenterAndNameSearch.and("dc", DataCenterAndNameSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
+        DataCenterAndNameSearch.and("name", DataCenterAndNameSearch.entity().getName(), SearchCriteria.Op.EQ);
+        DataCenterAndNameSearch.done();
+
+        DataCenterIdSearch = createSearchBuilder();
+        DataCenterIdSearch.and("dcId", DataCenterIdSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
+        DataCenterIdSearch.done();
+    }
+
+    @Override
+    public List<HostPodVO> listByDataCenterId(long id) {
+        SearchCriteria<HostPodVO> sc = DataCenterIdSearch.create();
+        sc.setParameters("dcId", id);
+
+        return listBy(sc);
+    }
+
+    @Override
+    public HostPodVO findByName(String name, long dcId) {
+        SearchCriteria<HostPodVO> sc = DataCenterAndNameSearch.create();
+        sc.setParameters("dc", dcId);
+        sc.setParameters("name", name);
+
+        return findOneBy(sc);
+    }
+
+    @Override
+    public HashMap<Long, List<Object>> getCurrentPodCidrSubnets(long zoneId, long podIdToSkip) {
+        HashMap<Long, List<Object>> currentPodCidrSubnets = new HashMap<Long, List<Object>>();
+
+        String selectSql = "SELECT id, cidr_address, cidr_size FROM host_pod_ref WHERE data_center_id=" + zoneId +" and removed IS NULL";
+        Transaction txn = Transaction.currentTxn();
+        try {
+            PreparedStatement stmt = txn.prepareAutoCloseStatement(selectSql);
+            ResultSet rs = stmt.executeQuery();
+            while (rs.next()) {
+                Long podId = rs.getLong("id");
+                if (podId.longValue() == podIdToSkip) {
+                    continue;
+                }
+                String cidrAddress = rs.getString("cidr_address");
+                long cidrSize = rs.getLong("cidr_size");
+                List<Object> cidrPair = new ArrayList<Object>();
+                cidrPair.add(0, cidrAddress);
+                cidrPair.add(1, new Long(cidrSize));
+                currentPodCidrSubnets.put(podId, cidrPair);
+            }
+        } catch (SQLException ex) {
+            s_logger.warn("DB exception " + ex.getMessage(), ex);
+            return null;
+        }
+
+        return currentPodCidrSubnets;
+    }
+
+    @Override
+    public boolean remove(Long id) {
+        Transaction txn = Transaction.currentTxn();
+        txn.start();
+        HostPodVO pod = createForUpdate();
+        pod.setName(null);
+
+        update(id, pod);
+
+        boolean result = super.remove(id);
+        txn.commit();
+        return result;
+    }
+
+    @Override
+    public List<Long> listDisabledPods(long zoneId) {
+        GenericSearchBuilder<HostPodVO, Long> podIdSearch = createSearchBuilder(Long.class);
+        podIdSearch.selectField(podIdSearch.entity().getId());
+        podIdSearch.and("dataCenterId", podIdSearch.entity().getDataCenterId(), Op.EQ);
+        podIdSearch.and("allocationState", podIdSearch.entity().getAllocationState(), Op.EQ);
+        podIdSearch.done();
+
+
+        SearchCriteria<Long> sc = podIdSearch.create();
+        sc.addAnd("dataCenterId", SearchCriteria.Op.EQ, zoneId);
+        sc.addAnd("allocationState", SearchCriteria.Op.EQ, Grouping.AllocationState.Disabled);
+        return customSearch(sc, null);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/dc/dao/PodVlanDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/dao/PodVlanDao.java b/engine/schema/src/com/cloud/dc/dao/PodVlanDao.java
new file mode 100644
index 0000000..6359dfe
--- /dev/null
+++ b/engine/schema/src/com/cloud/dc/dao/PodVlanDao.java
@@ -0,0 +1,30 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with 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.
+package com.cloud.dc.dao;
+
+import java.util.List;
+
+import com.cloud.dc.PodVlanVO;
+import com.cloud.utils.db.GenericDao;
+
+public interface PodVlanDao extends GenericDao<PodVlanVO, Long> {
+    public List<PodVlanVO> listAllocatedVnets(long podId);    
+    public void add(long podId, int start, int end);   
+    public void delete(long podId);
+    public PodVlanVO take(long podId, long accountId);
+    public void release(String vlan, long podId, long accountId);
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/dc/dao/PodVlanDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/dao/PodVlanDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/PodVlanDaoImpl.java
new file mode 100755
index 0000000..413f9ed
--- /dev/null
+++ b/engine/schema/src/com/cloud/dc/dao/PodVlanDaoImpl.java
@@ -0,0 +1,139 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with 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.
+package com.cloud.dc.dao;
+
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.Date;
+import java.util.List;
+
+import org.springframework.stereotype.Component;
+
+import com.cloud.dc.PodVlanVO;
+import com.cloud.utils.db.GenericDao;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.Transaction;
+import com.cloud.utils.exception.CloudRuntimeException;
+
+/**
+ * PodVlanDaoImpl maintains the one-to-many relationship between
+ */
+@Component
+public class PodVlanDaoImpl extends GenericDaoBase<PodVlanVO, Long> implements PodVlanDao {
+    private final SearchBuilder<PodVlanVO> FreeVlanSearch;
+    private final SearchBuilder<PodVlanVO> VlanPodSearch;
+    private final SearchBuilder<PodVlanVO> PodSearchAllocated;
+    
+    public List<PodVlanVO> listAllocatedVnets(long podId) {
+    	SearchCriteria<PodVlanVO> sc = PodSearchAllocated.create();
+    	sc.setParameters("podId", podId);
+    	return listBy(sc);
+    }
+    
+    public void add(long podId, int start, int end) {
+        String insertVnet = "INSERT INTO `cloud`.`op_pod_vlan_alloc` (vlan, pod_id) VALUES ( ?, ?)";
+        
+        Transaction txn = Transaction.currentTxn();
+        try {
+            txn.start();
+            PreparedStatement stmt = txn.prepareAutoCloseStatement(insertVnet);
+            for (int i = start; i < end; i++) {
+                stmt.setString(1, String.valueOf(i));
+                stmt.setLong(2, podId);
+                stmt.addBatch();
+            }
+            stmt.executeBatch();
+            txn.commit();
+        } catch (SQLException e) {
+            throw new CloudRuntimeException("Exception caught adding vnet ", e);
+        }
+    }
+    
+    public void delete(long podId) {
+    	String deleteVnet = "DELETE FROM `cloud`.`op_pod_vlan_alloc` WHERE pod_id = ?";
+
+        Transaction txn = Transaction.currentTxn();
+        try {
+            PreparedStatement stmt = txn.prepareAutoCloseStatement(deleteVnet);
+            stmt.setLong(1, podId);
+            stmt.executeUpdate();
+        } catch (SQLException e) {
+        	throw new CloudRuntimeException("Exception caught deleting vnet ", e);
+        }
+    }
+
+    public PodVlanVO take(long podId, long accountId) {
+        SearchCriteria<PodVlanVO> sc = FreeVlanSearch.create();
+        sc.setParameters("podId", podId);
+        Date now = new Date();
+        Transaction txn = Transaction.currentTxn();
+        try {
+            txn.start();
+            PodVlanVO vo = lockOneRandomRow(sc, true);
+            if (vo == null) {
+                return null;
+            }
+
+            vo.setTakenAt(now);
+            vo.setAccountId(accountId);
+            update(vo.getId(), vo);
+            txn.commit();
+            return vo;
+
+        } catch (Exception e) {
+            throw new CloudRuntimeException("Caught Exception ", e);
+        }
+    }
+
+    public void release(String vlan, long podId, long accountId) {
+        SearchCriteria<PodVlanVO> sc = VlanPodSearch.create();
+        sc.setParameters("vlan", vlan);
+        sc.setParameters("podId", podId);
+        sc.setParameters("account", accountId);
+
+        PodVlanVO vo = findOneIncludingRemovedBy(sc);
+        if (vo == null) {
+            return;
+        }
+
+        vo.setTakenAt(null);
+        vo.setAccountId(null);
+        update(vo.getId(), vo);
+    }
+    
+    public PodVlanDaoImpl() {
+    	super();
+    	PodSearchAllocated = createSearchBuilder();
+    	PodSearchAllocated.and("podId", PodSearchAllocated.entity().getPodId(), SearchCriteria.Op.EQ);
+    	PodSearchAllocated.and("allocated", PodSearchAllocated.entity().getTakenAt(), SearchCriteria.Op.NNULL);
+    	PodSearchAllocated.done();
+        
+        FreeVlanSearch = createSearchBuilder();
+        FreeVlanSearch.and("podId", FreeVlanSearch.entity().getPodId(), SearchCriteria.Op.EQ);
+        FreeVlanSearch.and("taken", FreeVlanSearch.entity().getTakenAt(), SearchCriteria.Op.NULL);
+        FreeVlanSearch.done();
+
+        VlanPodSearch = createSearchBuilder();
+        VlanPodSearch.and("vlan", VlanPodSearch.entity().getVlan(), SearchCriteria.Op.EQ);
+        VlanPodSearch.and("podId", VlanPodSearch.entity().getPodId(), SearchCriteria.Op.EQ);
+        VlanPodSearch.and("taken", VlanPodSearch.entity().getTakenAt(), SearchCriteria.Op.NNULL);
+        VlanPodSearch.and("account", VlanPodSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
+        VlanPodSearch.done();
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/dc/dao/PodVlanMapDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/dao/PodVlanMapDao.java b/engine/schema/src/com/cloud/dc/dao/PodVlanMapDao.java
new file mode 100644
index 0000000..841ed1b
--- /dev/null
+++ b/engine/schema/src/com/cloud/dc/dao/PodVlanMapDao.java
@@ -0,0 +1,30 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with 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.
+package com.cloud.dc.dao;
+
+import java.util.List;
+
+import com.cloud.dc.PodVlanMapVO;
+import com.cloud.utils.db.GenericDao;
+
+public interface PodVlanMapDao extends GenericDao<PodVlanMapVO, Long> {
+		
+	public List<PodVlanMapVO> listPodVlanMapsByPod(long podId);
+	public PodVlanMapVO listPodVlanMapsByVlan(long vlanDbId);
+	public PodVlanMapVO findPodVlanMap(long podId, long vlanDbId);
+	
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/dc/dao/PodVlanMapDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/dao/PodVlanMapDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/PodVlanMapDaoImpl.java
new file mode 100644
index 0000000..df7a5d9
--- /dev/null
+++ b/engine/schema/src/com/cloud/dc/dao/PodVlanMapDaoImpl.java
@@ -0,0 +1,75 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with 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.
+package com.cloud.dc.dao;
+
+import java.util.List;
+
+import javax.ejb.Local;
+
+import org.springframework.stereotype.Component;
+
+import com.cloud.dc.PodVlanMapVO;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+
+@Component
+@Local(value={PodVlanMapDao.class})
+public class PodVlanMapDaoImpl extends GenericDaoBase<PodVlanMapVO, Long> implements PodVlanMapDao {
+    
+	protected SearchBuilder<PodVlanMapVO> PodSearch;
+	protected SearchBuilder<PodVlanMapVO> VlanSearch;
+	protected SearchBuilder<PodVlanMapVO> PodVlanSearch;
+	
+	@Override
+	public List<PodVlanMapVO> listPodVlanMapsByPod(long podId) {
+		SearchCriteria<PodVlanMapVO> sc = PodSearch.create();
+    	sc.setParameters("podId", podId);
+    	return listIncludingRemovedBy(sc);
+	}
+	
+	@Override
+	public PodVlanMapVO listPodVlanMapsByVlan(long vlanDbId) {
+		SearchCriteria<PodVlanMapVO> sc = VlanSearch.create();
+    	sc.setParameters("vlanDbId", vlanDbId);
+    	return findOneBy(sc);
+	}
+	
+	@Override
+	public PodVlanMapVO findPodVlanMap(long podId, long vlanDbId) {
+		SearchCriteria<PodVlanMapVO> sc = PodVlanSearch.create();
+		sc.setParameters("podId", podId);
+		sc.setParameters("vlanDbId", vlanDbId);
+		return findOneIncludingRemovedBy(sc);
+	}
+	
+    public PodVlanMapDaoImpl() {
+    	PodSearch = createSearchBuilder();
+    	PodSearch.and("podId", PodSearch.entity().getPodId(), SearchCriteria.Op.EQ);
+        PodSearch.done();
+        
+    	VlanSearch = createSearchBuilder();
+    	VlanSearch.and("vlanDbId", VlanSearch.entity().getVlanDbId(), SearchCriteria.Op.EQ);
+        VlanSearch.done();
+        
+        PodVlanSearch = createSearchBuilder();
+        PodVlanSearch.and("podId", PodVlanSearch.entity().getPodId(), SearchCriteria.Op.EQ);
+        PodVlanSearch.and("vlanDbId", PodVlanSearch.entity().getVlanDbId(), SearchCriteria.Op.EQ);
+        PodVlanSearch.done();
+    }
+    
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDao.java b/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDao.java
new file mode 100755
index 0000000..f1fd2bd
--- /dev/null
+++ b/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDao.java
@@ -0,0 +1,32 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with 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.
+package com.cloud.dc.dao;
+
+import java.util.List;
+
+import com.cloud.dc.StorageNetworkIpAddressVO;
+import com.cloud.utils.db.GenericDao;
+
+public interface StorageNetworkIpAddressDao extends GenericDao<StorageNetworkIpAddressVO, Long> {
+	long countInUseIpByRangeId(long rangeId);
+
+	List<String> listInUseIpByRangeId(long rangeId);
+	
+	StorageNetworkIpAddressVO takeIpAddress(long rangeId);
+	
+	void releaseIpAddress(String ip);
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDaoImpl.java
new file mode 100755
index 0000000..782ee0d
--- /dev/null
+++ b/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDaoImpl.java
@@ -0,0 +1,114 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with 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.
+package com.cloud.dc.dao;
+
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+import javax.ejb.Local;
+import javax.naming.ConfigurationException;
+
+import org.springframework.stereotype.Component;
+
+import com.cloud.dc.DataCenterIpAddressVO;
+import com.cloud.dc.StorageNetworkIpAddressVO;
+import com.cloud.utils.db.DB;
+import com.cloud.utils.db.Filter;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.GenericSearchBuilder;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.SearchCriteria2;
+import com.cloud.utils.db.Transaction;
+import com.cloud.utils.db.SearchCriteria.Func;
+import com.cloud.utils.db.SearchCriteria.Op;
+
+@Component
+@Local(value={StorageNetworkIpAddressDao.class})
+@DB(txn=false)
+public class StorageNetworkIpAddressDaoImpl extends GenericDaoBase<StorageNetworkIpAddressVO, Long> implements StorageNetworkIpAddressDao {
+	protected final GenericSearchBuilder<StorageNetworkIpAddressVO, Long> countInUserIp;
+	protected final GenericSearchBuilder<StorageNetworkIpAddressVO, String> listInUseIp;
+	protected final SearchBuilder<StorageNetworkIpAddressVO> untakenIp;
+	protected final SearchBuilder<StorageNetworkIpAddressVO> ipSearch;
+	
+
+	protected StorageNetworkIpAddressDaoImpl() {
+		countInUserIp = createSearchBuilder(Long.class);
+		countInUserIp.select(null, Func.COUNT, null);
+		countInUserIp.and("rangeId", countInUserIp.entity().getRangeId(), Op.EQ);
+		countInUserIp.and("taken", countInUserIp.entity().getTakenAt(), Op.NNULL);
+		countInUserIp.done();
+		
+		listInUseIp = createSearchBuilder(String.class);
+		listInUseIp.selectField(listInUseIp.entity().getIpAddress());
+		listInUseIp.and("rangeId", listInUseIp.entity().getRangeId(), Op.EQ);
+		listInUseIp.and("taken", listInUseIp.entity().getTakenAt(), Op.NNULL);
+		listInUseIp.done();
+		
+		untakenIp = createSearchBuilder();
+		untakenIp.and("rangeId", untakenIp.entity().getRangeId(), Op.EQ);
+		untakenIp.and("taken", untakenIp.entity().getTakenAt(), Op.NULL);
+		untakenIp.done();
+		
+		ipSearch = createSearchBuilder();
+		ipSearch.and("ipAddress", ipSearch.entity().getIpAddress(), Op.EQ);
+		ipSearch.done();
+	}
+	
+	@Override
+	public long countInUseIpByRangeId(long rangeId) {
+		SearchCriteria<Long> sc = countInUserIp.create();
+		sc.setParameters("rangeId", rangeId);
+		return customSearch(sc, null).get(0);
+	}
+
+	@Override
+	public List<String> listInUseIpByRangeId(long rangeId) {
+		SearchCriteria<String> sc = listInUseIp.create();
+		sc.setParameters("rangeId", rangeId);
+		return customSearch(sc, null);
+	}
+	
+	@Override
+	@DB
+    public StorageNetworkIpAddressVO takeIpAddress(long rangeId) {
+		SearchCriteria<StorageNetworkIpAddressVO> sc = untakenIp.create();
+		sc.setParameters("rangeId", rangeId);
+        Transaction txn = Transaction.currentTxn();
+        txn.start();
+        StorageNetworkIpAddressVO ip = lockOneRandomRow(sc, true);
+        if (ip == null) {
+        	txn.rollback();
+        	return null;
+        }
+        ip.setTakenAt(new Date());
+        update(ip.getId(), ip);
+        txn.commit();
+        return ip;
+	}
+
+	@Override
+    public void releaseIpAddress(String ip) {
+		SearchCriteria<StorageNetworkIpAddressVO> sc = ipSearch.create();
+	    sc.setParameters("ipAddress", ip);
+	    StorageNetworkIpAddressVO vo = createForUpdate();
+	    vo.setTakenAt(null);
+	    update(vo, sc);
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpRangeDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpRangeDao.java b/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpRangeDao.java
new file mode 100755
index 0000000..2edd568
--- /dev/null
+++ b/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpRangeDao.java
@@ -0,0 +1,32 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with 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.
+package com.cloud.dc.dao;
+
+import java.util.List;
+
+import com.cloud.dc.StorageNetworkIpRangeVO;
+import com.cloud.utils.db.GenericDao;
+
+public interface StorageNetworkIpRangeDao extends GenericDao<StorageNetworkIpRangeVO, Long> {
+	List<StorageNetworkIpRangeVO> listByRangeId(long rangeId);
+
+	List<StorageNetworkIpRangeVO> listByPodId(long podId);
+
+	List<StorageNetworkIpRangeVO> listByDataCenterId(long dcId);
+	
+	long countRanges();
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpRangeDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpRangeDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpRangeDaoImpl.java
new file mode 100755
index 0000000..d732e6f
--- /dev/null
+++ b/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpRangeDaoImpl.java
@@ -0,0 +1,78 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with 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.
+package com.cloud.dc.dao;
+
+import java.util.List;
+import java.util.Map;
+
+import javax.ejb.Local;
+import javax.naming.ConfigurationException;
+
+import org.springframework.stereotype.Component;
+
+import com.cloud.dc.StorageNetworkIpRangeVO;
+import com.cloud.utils.db.DB;
+import com.cloud.utils.db.Filter;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.GenericSearchBuilder;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.SearchCriteria2;
+import com.cloud.utils.db.SearchCriteriaService;
+import com.cloud.utils.db.SearchCriteria.Func;
+import com.cloud.utils.db.SearchCriteria.Op;
+
+@Component
+@Local(value={StorageNetworkIpRangeDao.class})
+@DB(txn=false)
+public class StorageNetworkIpRangeDaoImpl extends GenericDaoBase<StorageNetworkIpRangeVO, Long> implements StorageNetworkIpRangeDao {
+	protected final GenericSearchBuilder<StorageNetworkIpRangeVO, Long> countRanges;
+	
+	protected StorageNetworkIpRangeDaoImpl() {
+		countRanges = createSearchBuilder(Long.class);
+		countRanges.select(null, Func.COUNT, null);
+		countRanges.done();
+	}
+	
+	@Override
+    public List<StorageNetworkIpRangeVO> listByPodId(long podId) {
+		SearchCriteriaService<StorageNetworkIpRangeVO, StorageNetworkIpRangeVO> sc = SearchCriteria2.create(StorageNetworkIpRangeVO.class);
+	    sc.addAnd(sc.getEntity().getPodId(), Op.EQ, podId);
+		return sc.list();
+    }
+
+	@Override
+    public List<StorageNetworkIpRangeVO> listByRangeId(long rangeId) {
+		SearchCriteriaService<StorageNetworkIpRangeVO, StorageNetworkIpRangeVO> sc = SearchCriteria2.create(StorageNetworkIpRangeVO.class);
+	    sc.addAnd(sc.getEntity().getId(), Op.EQ, rangeId);
+		return sc.list();
+    }
+
+	@Override
+    public List<StorageNetworkIpRangeVO> listByDataCenterId(long dcId) {
+		SearchCriteriaService<StorageNetworkIpRangeVO, StorageNetworkIpRangeVO> sc = SearchCriteria2.create(StorageNetworkIpRangeVO.class);
+	    sc.addAnd(sc.getEntity().getDataCenterId(), Op.EQ, dcId);
+		return sc.list();
+    }
+	
+	@Override
+	public long countRanges() {
+		SearchCriteria<Long> sc = countRanges.create();
+		return customSearch(sc, null).get(0);
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/dc/dao/VlanDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/dao/VlanDao.java b/engine/schema/src/com/cloud/dc/dao/VlanDao.java
new file mode 100755
index 0000000..cc82632
--- /dev/null
+++ b/engine/schema/src/com/cloud/dc/dao/VlanDao.java
@@ -0,0 +1,55 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with 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.
+package com.cloud.dc.dao;
+
+import java.util.List;
+
+import com.cloud.dc.Vlan;
+import com.cloud.dc.Vlan.VlanType;
+import com.cloud.dc.VlanVO;
+import com.cloud.utils.db.GenericDao;
+
+public interface VlanDao extends GenericDao<VlanVO, Long> {
+	
+	VlanVO findByZoneAndVlanId(long zoneId, String vlanId);
+	
+	List<VlanVO> listByZone(long zoneId);
+	
+	List<VlanVO> listByType(Vlan.VlanType vlanType);
+	
+	List<VlanVO> listByZoneAndType(long zoneId, Vlan.VlanType vlanType);
+	
+	List<VlanVO> listVlansForPod(long podId);
+	
+	List<VlanVO> listVlansForPodByType(long podId, Vlan.VlanType vlanType);
+	
+	void addToPod(long podId, long vlanDbId);
+
+	List<VlanVO> listVlansForAccountByType(Long zoneId, long accountId, VlanType vlanType);
+	
+	boolean zoneHasDirectAttachUntaggedVlans(long zoneId);
+
+	List<VlanVO> listZoneWideVlans(long zoneId, VlanType vlanType, String vlanId);
+
+	List<VlanVO> searchForZoneWideVlans(long dcId, String vlanType,String vlanId);
+	
+	List<VlanVO> listVlansByNetworkId(long networkId);
+	
+	List<VlanVO> listVlansByPhysicalNetworkId(long physicalNetworkId);
+
+        List<VlanVO> listZoneWideNonDedicatedVlans(long zoneId);
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java
new file mode 100755
index 0000000..100295b
--- /dev/null
+++ b/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java
@@ -0,0 +1,334 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with 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.
+package com.cloud.dc.dao;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import javax.ejb.Local;
+import javax.inject.Inject;
+import javax.naming.ConfigurationException;
+
+import org.springframework.stereotype.Component;
+
+import com.cloud.dc.AccountVlanMapVO;
+import com.cloud.dc.PodVlanMapVO;
+import com.cloud.dc.Vlan;
+import com.cloud.dc.Vlan.VlanType;
+import com.cloud.dc.VlanVO;
+import com.cloud.network.dao.IPAddressDao;
+import com.cloud.utils.Pair;
+import com.cloud.utils.db.DB;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.JoinBuilder;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.Transaction;
+import com.cloud.utils.exception.CloudRuntimeException;
+
+@Component
+@Local(value={VlanDao.class})
+public class VlanDaoImpl extends GenericDaoBase<VlanVO, Long> implements VlanDao {
+
+    private final String FindZoneWideVlans = "SELECT * FROM vlan WHERE data_center_id=? and vlan_type=? and vlan_id!=? and id not in (select vlan_db_id from account_vlan_map)";
+
+    protected SearchBuilder<VlanVO> ZoneVlanIdSearch;
+    protected SearchBuilder<VlanVO> ZoneSearch;
+    protected SearchBuilder<VlanVO> ZoneTypeSearch;
+    protected SearchBuilder<VlanVO> ZoneTypeAllPodsSearch;
+    protected SearchBuilder<VlanVO> ZoneTypePodSearch;
+    protected SearchBuilder<VlanVO> ZoneVlanSearch;
+    protected SearchBuilder<VlanVO> NetworkVlanSearch;
+    protected SearchBuilder<VlanVO> PhysicalNetworkVlanSearch;
+    protected SearchBuilder<VlanVO> ZoneWideNonDedicatedVlanSearch;
+
+    protected SearchBuilder<AccountVlanMapVO> AccountVlanMapSearch;
+
+    @Inject protected PodVlanMapDao _podVlanMapDao;
+    @Inject protected AccountVlanMapDao _accountVlanMapDao;
+    @Inject protected IPAddressDao _ipAddressDao;
+
+    @Override
+    public VlanVO findByZoneAndVlanId(long zoneId, String vlanId) {
+        SearchCriteria<VlanVO> sc = ZoneVlanIdSearch.create();
+        sc.setParameters("zoneId", zoneId);
+        sc.setParameters("vlanId", vlanId);
+        return findOneBy(sc);
+    }
+
+    @Override
+    public List<VlanVO> listByZone(long zoneId) {
+        SearchCriteria<VlanVO> sc = ZoneSearch.create();
+        sc.setParameters("zoneId", zoneId);
+        return listBy(sc);
+    }
+
+    public VlanDaoImpl() {
+        ZoneVlanIdSearch = createSearchBuilder();
+        ZoneVlanIdSearch.and("zoneId", ZoneVlanIdSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
+        ZoneVlanIdSearch.and("vlanId", ZoneVlanIdSearch.entity().getVlanTag(), SearchCriteria.Op.EQ);
+        ZoneVlanIdSearch.done();
+
+        ZoneSearch = createSearchBuilder();
+        ZoneSearch.and("zoneId", ZoneSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
+        ZoneSearch.done();
+
+        ZoneTypeSearch = createSearchBuilder();
+        ZoneTypeSearch.and("zoneId", ZoneTypeSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
+        ZoneTypeSearch.and("vlanType", ZoneTypeSearch.entity().getVlanType(), SearchCriteria.Op.EQ);
+        ZoneTypeSearch.done();
+
+        NetworkVlanSearch = createSearchBuilder();
+        NetworkVlanSearch.and("networkOfferingId", NetworkVlanSearch.entity().getNetworkId(), SearchCriteria.Op.EQ);
+        NetworkVlanSearch.done();
+
+        PhysicalNetworkVlanSearch = createSearchBuilder();
+        PhysicalNetworkVlanSearch.and("physicalNetworkId", PhysicalNetworkVlanSearch.entity().getPhysicalNetworkId(), SearchCriteria.Op.EQ);
+        PhysicalNetworkVlanSearch.done();
+    }
+
+    @Override
+    public List<VlanVO> listZoneWideVlans(long zoneId, VlanType vlanType, String vlanId){
+        SearchCriteria<VlanVO> sc = ZoneVlanSearch.create();
+        sc.setParameters("zoneId", zoneId);
+        sc.setParameters("vlanId", vlanId);
+        sc.setParameters("vlanType", vlanType);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<VlanVO> listByZoneAndType(long zoneId, VlanType vlanType) {
+        SearchCriteria<VlanVO> sc = ZoneTypeSearch.create();
+        sc.setParameters("zoneId", zoneId);
+        sc.setParameters("vlanType", vlanType);
+        return listBy(sc);
+    }
+
+
+    @Override
+    public List<VlanVO> listByType(VlanType vlanType) {
+        SearchCriteria<VlanVO> sc = ZoneTypeSearch.create();
+        sc.setParameters("vlanType", vlanType);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<VlanVO> listVlansForPod(long podId) {
+        //FIXME: use a join statement to improve the performance (should be minor since we expect only one or two
+        List<PodVlanMapVO> vlanMaps = _podVlanMapDao.listPodVlanMapsByPod(podId);
+        List<VlanVO> result  = new ArrayList<VlanVO>();
+        for (PodVlanMapVO pvmvo: vlanMaps) {
+            result.add(findById(pvmvo.getVlanDbId()));
+        }
+        return result;
+    }
+
+    @Override
+    public List<VlanVO> listVlansForPodByType(long podId, VlanType vlanType) {
+        //FIXME: use a join statement to improve the performance (should be minor since we expect only one or two)
+        List<PodVlanMapVO> vlanMaps = _podVlanMapDao.listPodVlanMapsByPod(podId);
+        List<VlanVO> result  = new ArrayList<VlanVO>();
+        for (PodVlanMapVO pvmvo: vlanMaps) {
+            VlanVO vlan =findById(pvmvo.getVlanDbId());
+            if (vlan.getVlanType() == vlanType) {
+                result.add(vlan);
+            }
+        }
+        return result;
+    }
+
+    @Override
+    public List<VlanVO> listVlansForAccountByType(Long zoneId, long accountId, VlanType vlanType) {
+        //FIXME: use a join statement to improve the performance (should be minor since we expect only one or two)
+        List<AccountVlanMapVO> vlanMaps = _accountVlanMapDao.listAccountVlanMapsByAccount(accountId);
+        List<VlanVO> result  = new ArrayList<VlanVO>();
+        for (AccountVlanMapVO acvmvo: vlanMaps) {
+            VlanVO vlan =findById(acvmvo.getVlanDbId());
+            if (vlan.getVlanType() == vlanType && (zoneId == null || vlan.getDataCenterId() == zoneId)) {
+                result.add(vlan);
+            }
+        }
+        return result;
+    }
+
+    @Override
+    public void addToPod(long podId, long vlanDbId) {
+        PodVlanMapVO pvmvo = new PodVlanMapVO(podId, vlanDbId);
+        _podVlanMapDao.persist(pvmvo);
+
+    }
+
+    @Override
+    public boolean configure(String name, Map<String, Object> params)
+            throws ConfigurationException {
+        boolean result = super.configure(name, params);
+        ZoneTypeAllPodsSearch = createSearchBuilder();
+        ZoneTypeAllPodsSearch.and("zoneId", ZoneTypeAllPodsSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
+        ZoneTypeAllPodsSearch.and("vlanType", ZoneTypeAllPodsSearch.entity().getVlanType(), SearchCriteria.Op.EQ);
+
+        SearchBuilder<PodVlanMapVO> PodVlanSearch = _podVlanMapDao.createSearchBuilder();
+        PodVlanSearch.and("podId", PodVlanSearch.entity().getPodId(), SearchCriteria.Op.NNULL);
+        ZoneTypeAllPodsSearch.join("vlan", PodVlanSearch, PodVlanSearch.entity().getVlanDbId(), ZoneTypeAllPodsSearch.entity().getId(), JoinBuilder.JoinType.INNER);
+
+        ZoneTypeAllPodsSearch.done();
+        PodVlanSearch.done();
+
+        ZoneTypePodSearch = createSearchBuilder();
+        ZoneTypePodSearch.and("zoneId", ZoneTypePodSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
+        ZoneTypePodSearch.and("vlanType", ZoneTypePodSearch.entity().getVlanType(), SearchCriteria.Op.EQ);
+
+        SearchBuilder<PodVlanMapVO> PodVlanSearch2 = _podVlanMapDao.createSearchBuilder();
+        PodVlanSearch2.and("podId", PodVlanSearch2.entity().getPodId(), SearchCriteria.Op.EQ);
+        ZoneTypePodSearch.join("vlan", PodVlanSearch2,  PodVlanSearch2.entity().getVlanDbId(), ZoneTypePodSearch.entity().getId(), JoinBuilder.JoinType.INNER);
+        PodVlanSearch2.done();
+        ZoneTypePodSearch.done();
+
+        ZoneWideNonDedicatedVlanSearch = createSearchBuilder();
+        ZoneWideNonDedicatedVlanSearch.and("zoneId", ZoneWideNonDedicatedVlanSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
+        AccountVlanMapSearch = _accountVlanMapDao.createSearchBuilder();
+        AccountVlanMapSearch.and("accountId", AccountVlanMapSearch.entity().getAccountId(), SearchCriteria.Op.NULL);
+        ZoneWideNonDedicatedVlanSearch.join("AccountVlanMapSearch", AccountVlanMapSearch, ZoneWideNonDedicatedVlanSearch.entity().getId(), AccountVlanMapSearch.entity().getVlanDbId(), JoinBuilder.JoinType.LEFTOUTER);
+        ZoneWideNonDedicatedVlanSearch.done();
+        AccountVlanMapSearch.done();
+
+        return result;
+    }
+
+    private VlanVO findNextVlan(long zoneId, Vlan.VlanType vlanType) {
+        List<VlanVO> allVlans = listByZoneAndType(zoneId, vlanType);
+        List<VlanVO> emptyVlans = new ArrayList<VlanVO>();
+        List<VlanVO> fullVlans = new ArrayList<VlanVO>();
+
+        // Try to find a VLAN that is partially allocated
+        for (VlanVO vlan : allVlans) {
+            long vlanDbId = vlan.getId();
+
+            int countOfAllocatedIps = _ipAddressDao.countIPs(zoneId, vlanDbId, true);
+            int countOfAllIps = _ipAddressDao.countIPs(zoneId, vlanDbId, false);
+
+            if ((countOfAllocatedIps > 0) && (countOfAllocatedIps < countOfAllIps)) {
+                return vlan;
+            } else if (countOfAllocatedIps == 0) {
+                emptyVlans.add(vlan);
+            } else if (countOfAllocatedIps == countOfAllIps) {
+                fullVlans.add(vlan);
+            }
+        }
+
+        if (emptyVlans.isEmpty()) {
+            return null;
+        }
+
+        // Try to find an empty VLAN with the same tag/subnet as a VLAN that is full
+        for (VlanVO fullVlan : fullVlans) {
+            for (VlanVO emptyVlan : emptyVlans) {
+                if (fullVlan.getVlanTag().equals(emptyVlan.getVlanTag()) && 
+                        fullVlan.getVlanGateway().equals(emptyVlan.getVlanGateway()) &&
+                        fullVlan.getVlanNetmask().equals(emptyVlan.getVlanNetmask())) {
+                    return emptyVlan;
+                }
+            }
+        }
+
+        // Return a random empty VLAN
+        return emptyVlans.get(0);
+    }
+
+    @Override
+    public boolean zoneHasDirectAttachUntaggedVlans(long zoneId) {
+        SearchCriteria<VlanVO> sc = ZoneTypeAllPodsSearch.create();
+        sc.setParameters("zoneId", zoneId);
+        sc.setParameters("vlanType", VlanType.DirectAttached);
+
+        return listIncludingRemovedBy(sc).size() > 0;
+    }
+
+
+    public Pair<String, VlanVO> assignPodDirectAttachIpAddress(long zoneId,
+            long podId, long accountId, long domainId) {
+        SearchCriteria<VlanVO> sc = ZoneTypePodSearch.create();
+        sc.setParameters("zoneId", zoneId);
+        sc.setParameters("vlanType", VlanType.DirectAttached);
+        sc.setJoinParameters("vlan", "podId", podId);
+
+        VlanVO vlan = findOneIncludingRemovedBy(sc);
+        if (vlan == null) {
+            return null;
+        }
+
+        return null;
+//    	String ipAddress = _ipAddressDao.assignIpAddress(accountId, domainId, vlan.getId(), false).getAddress();
+//    	if (ipAddress == null) {
+//    		return null;
+//    	}
+//		return new Pair<String, VlanVO>(ipAddress, vlan);
+
+    }
+
+    @Override
+    @DB
+    public List<VlanVO> searchForZoneWideVlans(long dcId, String vlanType, String vlanId){
+
+        StringBuilder sql = new StringBuilder(FindZoneWideVlans);
+
+        Transaction txn = Transaction.currentTxn();
+        PreparedStatement pstmt = null;
+        try {
+            pstmt = txn.prepareAutoCloseStatement(sql.toString());
+            pstmt.setLong(1, dcId);
+            pstmt.setString(2, vlanType);
+            pstmt.setString(3, vlanId);
+
+            ResultSet rs = pstmt.executeQuery();
+            List<VlanVO> zoneWideVlans = new ArrayList<VlanVO>();
+
+            while (rs.next()) {
+                zoneWideVlans.add(toEntityBean(rs, false));
+            }
+
+            return zoneWideVlans;
+        } catch (SQLException e) {
+            throw new CloudRuntimeException("Unable to execute " + pstmt.toString(), e);
+        }
+    }
+
+    @Override
+    public List<VlanVO> listVlansByNetworkId(long networkOfferingId) {
+        SearchCriteria<VlanVO> sc = NetworkVlanSearch.create();
+        sc.setParameters("networkOfferingId", networkOfferingId);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<VlanVO> listVlansByPhysicalNetworkId(long physicalNetworkId) {
+        SearchCriteria<VlanVO> sc = PhysicalNetworkVlanSearch.create();
+        sc.setParameters("physicalNetworkId", physicalNetworkId);
+        return listBy(sc);
+    }	
+
+    @Override
+    public List<VlanVO> listZoneWideNonDedicatedVlans(long zoneId) {
+        SearchCriteria<VlanVO> sc = ZoneWideNonDedicatedVlanSearch.create();
+        sc.setParameters("ZoneWideNonDedicatedVlanSearch", "zoneId", zoneId);
+        return listBy(sc);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/domain/DomainVO.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/domain/DomainVO.java b/engine/schema/src/com/cloud/domain/DomainVO.java
new file mode 100644
index 0000000..a87bedc
--- /dev/null
+++ b/engine/schema/src/com/cloud/domain/DomainVO.java
@@ -0,0 +1,215 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with 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.
+package com.cloud.domain;
+
+import java.util.Date;
+import java.util.UUID;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+import org.apache.cloudstack.api.InternalIdentity;
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.Identity;
+import com.cloud.utils.db.GenericDao;
+
+@Entity
+@Table(name="domain")
+public class DomainVO implements Domain {
+	public static final Logger s_logger = Logger.getLogger(DomainVO.class.getName());
+
+    @Id
+    @GeneratedValue(strategy=GenerationType.IDENTITY)
+    @Column(name="id")
+    private long id;
+
+    @Column(name="parent")
+    private Long parent = null;
+
+    @Column(name="name")
+    private String name = null;
+
+    @Column(name="owner")
+    private long accountId;
+
+    @Column(name="path")
+    private String path = null;
+
+    @Column(name="level")
+    private int level;
+
+    @Column(name=GenericDao.REMOVED_COLUMN)
+    private Date removed;
+
+    @Column(name="child_count")
+    private int childCount = 0;
+
+    @Column(name="next_child_seq")
+    private long nextChildSeq = 1L;
+
+    @Column(name="state")
+    private Domain.State state;
+
+    @Column(name="network_domain")
+    private String networkDomain;
+
+    @Column(name="uuid")
+    private String uuid;
+
+    public DomainVO() {}
+    
+    public DomainVO(String name, long owner, Long parentId, String networkDomain) {
+    	this.parent = parentId;
+        this.name = name;
+        this.accountId = owner;
+        this.path ="";
+        this.level = 0;
+        this.state = Domain.State.Active;
+        this.networkDomain = networkDomain;
+        this.uuid = UUID.randomUUID().toString();
+    }
+
+    public DomainVO(String name, long owner, Long parentId, String networkDomain, String uuid) {
+    	this.parent = parentId;
+        this.name = name;
+        this.accountId = owner;
+        this.path ="";
+        this.level = 0;
+        this.state = Domain.State.Active;
+        this.networkDomain = networkDomain;
+        this.uuid = uuid;
+    }
+    
+    @Override
+    public long getId() {
+        return id;
+    }
+
+    @Override
+    public Long getParent() {
+        return parent;
+    }
+
+    @Override
+    public void setParent(Long parent) {
+    	if(parent == null) {
+    		this.parent = DomainVO.ROOT_DOMAIN;
+    	} else {
+    		if(parent.longValue() <= DomainVO.ROOT_DOMAIN)
+    			this.parent = DomainVO.ROOT_DOMAIN;
+    		else
+    			this.parent = parent;
+    	}
+    }
+
+    @Override
+    public String getName() {
+        return name;
+    }
+
+    @Override
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    @Override
+    public long getAccountId() {
+        return accountId;
+    }
+
+    @Override
+    public Date getRemoved() {
+        return removed;
+    }
+
+    @Override
+    public String getPath() {
+    	return path;
+    }
+
+    @Override
+    public void setPath(String path) {
+    	this.path = path;
+    }
+
+    @Override
+    public int getLevel() {
+    	return level;
+    }
+
+    public void setLevel(int level) {
+    	this.level = level;
+    }
+
+    @Override
+    public int getChildCount() {
+    	return childCount;
+    }
+
+    public void setChildCount(int count) {
+    	childCount = count;
+    }
+
+    @Override
+    public long getNextChildSeq() {
+    	return nextChildSeq;
+    }
+
+    public void setNextChildSeq(long seq) {
+    	nextChildSeq = seq;
+    }
+
+    @Override
+    public Domain.State getState() {
+        return state;
+    }
+
+    @Override
+    public void setState(Domain.State state) {
+        this.state = state;
+    }
+
+    @Override
+    public String toString() {
+        return new StringBuilder("Domain:").append(id).append(path).toString();
+    }
+
+    @Override
+    public String getNetworkDomain() {
+        return networkDomain;
+    }
+
+    public void setNetworkDomain(String domainSuffix) {
+        this.networkDomain = domainSuffix;
+    }
+
+    @Override
+    public String getUuid() {
+    	return this.uuid;
+    }
+
+    public void setUuid(String uuid) {
+    	this.uuid = uuid;
+    }
+    
+}
+

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/domain/dao/DomainDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/domain/dao/DomainDao.java b/engine/schema/src/com/cloud/domain/dao/DomainDao.java
new file mode 100644
index 0000000..afeb0f4
--- /dev/null
+++ b/engine/schema/src/com/cloud/domain/dao/DomainDao.java
@@ -0,0 +1,34 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with 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.
+package com.cloud.domain.dao;
+
+import java.util.List;
+import java.util.Set;
+
+import com.cloud.domain.DomainVO;
+import com.cloud.utils.db.GenericDao;
+
+public interface DomainDao extends GenericDao<DomainVO, Long> {
+    public DomainVO create(DomainVO domain);
+    public DomainVO findDomainByPath(String domainPath);
+    public boolean isChildDomain(Long parentId, Long childId);
+	DomainVO findImmediateChildForParent(Long parentId);
+	List<DomainVO> findImmediateChildrenForParent(Long parentId);
+	List<DomainVO> findAllChildren(String path, Long parentId);
+	List<DomainVO> findInactiveDomains();
+    Set<Long> getDomainParentIds(long domainId);
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/domain/dao/DomainDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/domain/dao/DomainDaoImpl.java b/engine/schema/src/com/cloud/domain/dao/DomainDaoImpl.java
new file mode 100644
index 0000000..c30ca5e
--- /dev/null
+++ b/engine/schema/src/com/cloud/domain/dao/DomainDaoImpl.java
@@ -0,0 +1,278 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with 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.
+package com.cloud.domain.dao;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import javax.ejb.Local;
+
+import org.apache.log4j.Logger;
+import org.springframework.stereotype.Component;
+
+import com.cloud.domain.Domain;
+import com.cloud.domain.DomainVO;
+import com.cloud.utils.db.DB;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.Transaction;
+
+@Component
+@Local(value={DomainDao.class})
+public class DomainDaoImpl extends GenericDaoBase<DomainVO, Long> implements DomainDao {
+    private static final Logger s_logger = Logger.getLogger(DomainDaoImpl.class);
+    
+	protected SearchBuilder<DomainVO> DomainNameLikeSearch;
+	protected SearchBuilder<DomainVO> ParentDomainNameLikeSearch;
+	protected SearchBuilder<DomainVO> DomainPairSearch;
+	protected SearchBuilder<DomainVO> ImmediateChildDomainSearch;
+	protected SearchBuilder<DomainVO> FindAllChildrenSearch;
+	protected SearchBuilder<DomainVO> AllFieldsSearch;
+	
+	public DomainDaoImpl () {
+		DomainNameLikeSearch = createSearchBuilder();
+		DomainNameLikeSearch.and("name", DomainNameLikeSearch.entity().getName(), SearchCriteria.Op.LIKE);
+		DomainNameLikeSearch.done();
+		
+		ParentDomainNameLikeSearch = createSearchBuilder();
+		ParentDomainNameLikeSearch.and("name", ParentDomainNameLikeSearch.entity().getName(), SearchCriteria.Op.LIKE);
+		ParentDomainNameLikeSearch.and("parent", ParentDomainNameLikeSearch.entity().getName(), SearchCriteria.Op.EQ);
+		ParentDomainNameLikeSearch.done();
+
+		DomainPairSearch = createSearchBuilder();
+		DomainPairSearch.and("id", DomainPairSearch.entity().getId(), SearchCriteria.Op.IN);
+		DomainPairSearch.done();
+
+		ImmediateChildDomainSearch = createSearchBuilder();
+		ImmediateChildDomainSearch.and("parent", ImmediateChildDomainSearch.entity().getParent(), SearchCriteria.Op.EQ);
+		ImmediateChildDomainSearch.done();
+		
+		FindAllChildrenSearch = createSearchBuilder();
+		FindAllChildrenSearch.and("path", FindAllChildrenSearch.entity().getPath(), SearchCriteria.Op.LIKE);
+		FindAllChildrenSearch.and("id", FindAllChildrenSearch.entity().getId(), SearchCriteria.Op.NEQ);
+		FindAllChildrenSearch.done();
+		
+		AllFieldsSearch = createSearchBuilder();
+		AllFieldsSearch.and("name", AllFieldsSearch.entity().getName(), SearchCriteria.Op.EQ);
+		AllFieldsSearch.and("state", AllFieldsSearch.entity().getState(), SearchCriteria.Op.EQ);
+		AllFieldsSearch.and("owner", AllFieldsSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
+		AllFieldsSearch.and("path", AllFieldsSearch.entity().getPath(), SearchCriteria.Op.EQ);
+		AllFieldsSearch.and("parent", AllFieldsSearch.entity().getParent(), SearchCriteria.Op.EQ);
+		AllFieldsSearch.done();
+
+	}
+	
+    
+    private static String allocPath(DomainVO parentDomain, String name) {
+        String parentPath = parentDomain.getPath();
+        return parentPath + name + "/";
+    }
+
+    @Override
+    public synchronized DomainVO create(DomainVO domain) {
+        // make sure domain name is valid
+        String domainName = domain.getName();
+        if (domainName != null) {
+            if (domainName.contains("/")) {
+                throw new IllegalArgumentException("Domain name contains one or more invalid characters.  Please enter a name without '/' characters.");
+            }
+        } else {
+            throw new IllegalArgumentException("Domain name is null.  Please specify a valid domain name.");
+        }
+
+    	long parent = DomainVO.ROOT_DOMAIN;
+    	if(domain.getParent() != null && domain.getParent().longValue() >= DomainVO.ROOT_DOMAIN) {
+    		parent = domain.getParent().longValue();
+    	}
+    	
+    	DomainVO parentDomain = findById(parent);
+    	if(parentDomain == null) {
+            s_logger.error("Unable to load parent domain: " + parent);
+    		return null;
+    	}
+    	
+        Transaction txn = Transaction.currentTxn();
+    	try {
+    		txn.start();
+
+    		parentDomain = this.lockRow(parent, true);
+            if(parentDomain == null) {
+                s_logger.error("Unable to lock parent domain: " + parent);
+                return null;
+            }
+    		
+            domain.setPath(allocPath(parentDomain, domain.getName()));
+            domain.setLevel(parentDomain.getLevel() + 1);
+            
+            parentDomain.setNextChildSeq(parentDomain.getNextChildSeq() + 1); // FIXME:  remove sequence number?
+            parentDomain.setChildCount(parentDomain.getChildCount() + 1);
+            persist(domain);
+            update(parentDomain.getId(), parentDomain);
+            
+    		txn.commit();
+    		return domain;
+    	} catch(Exception e) {
+    		s_logger.error("Unable to create domain due to " + e.getMessage(), e);
+    		txn.rollback();
+    		return null;
+    	} 
+    }
+
+    @Override
+    @DB
+    public boolean remove(Long id) {
+        // check for any active users / domains assigned to the given domain id and don't remove the domain if there are any
+    	if (id != null && id.longValue() == DomainVO.ROOT_DOMAIN) {
+    		s_logger.error("Can not remove domain " + id + " as it is ROOT domain");
+    		return false;
+    	}
+    	
+        DomainVO domain = findById(id);
+        if(domain == null) {
+        	s_logger.error("Unable to remove domain as domain " + id + " no longer exists");
+        	return false;
+        }
+        
+        if(domain.getParent() == null) {
+        	s_logger.error("Invalid domain " + id + ", orphan?");
+        	return false;
+        }
+    	
+        String sql = "SELECT * from account where domain_id = " + id + " and removed is null";
+        String sql1 = "SELECT * from domain where parent = " + id + " and removed is null";
+
+        boolean success = false;
+        Transaction txn = Transaction.currentTxn();
+        try {
+        	txn.start();
+            DomainVO parentDomain = super.lockRow(domain.getParent(), true);
+            if(parentDomain == null) {
+                s_logger.error("Unable to load parent domain: " + domain.getParent());
+                return false;
+            }
+        	
+            PreparedStatement stmt = txn.prepareAutoCloseStatement(sql);
+            ResultSet rs = stmt.executeQuery();
+            if (rs.next()) {
+                return false;
+            }
+            stmt = txn.prepareAutoCloseStatement(sql1);
+            rs = stmt.executeQuery();
+            if (rs.next()) {
+                return false;
+            }
+            
+        	parentDomain.setChildCount(parentDomain.getChildCount() - 1);
+        	update(parentDomain.getId(), parentDomain);
+            success = super.remove(id);
+            txn.commit();
+        } catch (SQLException ex) {
+            success = false;
+            s_logger.error("error removing domain: " + id, ex);
+            txn.rollback();
+        } 
+        return success;
+    }
+
+    @Override
+    public DomainVO findDomainByPath(String domainPath) {
+        SearchCriteria<DomainVO> sc = createSearchCriteria();
+        sc.addAnd("path", SearchCriteria.Op.EQ, domainPath);
+        return findOneBy(sc);
+    }
+    
+    @Override
+    public DomainVO findImmediateChildForParent(Long parentId){
+        SearchCriteria<DomainVO> sc = ImmediateChildDomainSearch.create();
+    	sc.setParameters("parent", parentId);
+    	return (listBy(sc).size() > 0 ? listBy(sc).get(0) : null);//may need to revisit for multiple children case
+    }
+
+    @Override
+    public List<DomainVO> findImmediateChildrenForParent(Long parentId){
+        SearchCriteria<DomainVO> sc = ImmediateChildDomainSearch.create();
+    	sc.setParameters("parent", parentId);
+    	return listBy(sc);
+    }
+
+    @Override
+    public List<DomainVO> findAllChildren(String path, Long parentId){
+    	SearchCriteria<DomainVO> sc = FindAllChildrenSearch.create();
+    	sc.setParameters("path", "%"+path+"%");
+    	sc.setParameters("id", parentId);
+    	return listBy(sc);
+    }
+    
+    @Override
+    public boolean isChildDomain(Long parentId, Long childId) {
+        if ((parentId == null) || (childId == null)) {
+            return false;
+        }
+
+        if (parentId.equals(childId)) {
+            return true;
+        }
+
+        boolean result = false;
+        SearchCriteria<DomainVO> sc = DomainPairSearch.create();
+        sc.setParameters("id", parentId, childId);
+
+        List<DomainVO> domainPair = listBy(sc);
+
+        if ((domainPair != null) && (domainPair.size() == 2)) {
+            DomainVO d1 = domainPair.get(0);
+            DomainVO d2 = domainPair.get(1);
+
+            if (d1.getId() == parentId) {
+                result = d2.getPath().startsWith(d1.getPath());
+            } else {
+                result = d1.getPath().startsWith(d2.getPath());
+            }
+        }
+        return result;
+    }
+    
+    @Override
+    public List<DomainVO> findInactiveDomains() {
+        SearchCriteria<DomainVO> sc = AllFieldsSearch.create();
+        sc.setParameters("state", Domain.State.Inactive);
+        return listBy(sc);
+    }
+    
+    @Override
+    public Set<Long> getDomainParentIds(long domainId) {
+        Set<Long> parentDomains = new HashSet<Long>();
+        Domain domain = findById(domainId);
+
+        if (domain != null) {
+            parentDomains.add(domain.getId());
+
+            while (domain.getParent() != null) {
+                domain = findById(domain.getParent());
+                parentDomains.add(domain.getId());
+            }
+        }
+        
+        return parentDomains;
+    }
+    
+}


Mime
View raw message