cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From alena1...@apache.org
Subject [22/90] [abbrv] [partial] Moved most of the VOs and DAOs from server package into engine-schema as well
Date Wed, 08 May 2013 20:47:48 GMT
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/vm/dao/DomainRouterDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/vm/dao/DomainRouterDao.java b/engine/schema/src/com/cloud/vm/dao/DomainRouterDao.java
new file mode 100755
index 0000000..95d1ea6
--- /dev/null
+++ b/engine/schema/src/com/cloud/vm/dao/DomainRouterDao.java
@@ -0,0 +1,149 @@
+// 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.vm.dao;
+
+import java.util.List;
+
+import com.cloud.network.Network;
+import com.cloud.network.router.VirtualRouter;
+import com.cloud.network.router.VirtualRouter.Role;
+import com.cloud.utils.db.GenericDao;
+import com.cloud.vm.DomainRouterVO;
+import com.cloud.vm.VirtualMachine.State;
+
+/**
+ *
+ *  DomainRouterDao implements
+ */
+public interface DomainRouterDao extends GenericDao<DomainRouterVO, Long> {
+    /**
+     * gets the DomainRouterVO by user id and data center
+     * @Param dcId data center Id.
+     * @return list of DomainRouterVO
+     */
+    public List<DomainRouterVO> listByDataCenter(long dcId);
+    
+    /**
+     * gets the DomainRouterVO by account id and data center
+     * @param account id of the user.
+     * @Param dcId data center Id.
+     * @return DomainRouterVO
+     */
+    public List<DomainRouterVO> findBy(long accountId, long dcId);
+    
+    /**
+     * gets the DomainRouterVO by user id.
+     * @param userId id of the user.
+     * @Param dcId data center Id.
+     * @return list of DomainRouterVO
+     */
+    public List<DomainRouterVO> listBy(long userId);
+    
+    /**
+     * list virtual machine routers by host id.  pass in null to get all
+     * virtual machine routers.
+     * @param hostId id of the host.  null if to get all.
+     * @return list of DomainRouterVO
+     */
+    public List<DomainRouterVO> listByHostId(Long hostId);
+    public List<DomainRouterVO> listByLastHostId(Long hostId);
+
+    /**
+     * list virtual machine routers by pod id.  pass in null to get all
+     * virtual machine routers.
+     * @param podId id of the pod.  null if to get all.
+     * @return list of DomainRouterVO
+     */
+    public List<DomainRouterVO> listByPodId(Long podId);
+
+    /**
+     * list virtual machine routers by pod id.  pass in null to get all
+     * virtual machine routers.
+     * @param podId id of the pod.  null if to get all.
+     * @param state state of the domain router. null if to get all.
+     * @return list of DomainRouterVO
+     */
+    public List<DomainRouterVO> listByPodIdAndStates(Long podId, State... states);
+
+    /**
+     * list virtual machine routers by host id.
+     * pass in null to get all
+     * virtual machine routers.
+     * @param hostId id of the host.  null if to get all.
+     * @return list of DomainRouterVO
+     */
+    public List<DomainRouterVO> listIsolatedByHostId(Long hostId);
+    
+	/**
+	 * Find the list of domain routers for a domain
+	 * @param id
+	 * @return
+	 */
+	public List<DomainRouterVO> listByDomain(Long id);
+
+	List<DomainRouterVO> findBy(long accountId, long dcId, Role role);
+	
+	List<DomainRouterVO> findByNetwork(long networkId);
+
+    List<DomainRouterVO> listActive(long networkId);
+
+    /**
+     * List domain routers by state and network type which reside on Host managed by the specified management server
+     * @return
+     */    
+    List<DomainRouterVO> listByStateAndNetworkType(State state, Network.GuestType type, long mgmtSrvrId);
+    
+    List<DomainRouterVO> findByNetworkOutsideThePod(long networkId, long podId, State state, Role role);
+    
+    List<DomainRouterVO> listByNetworkAndPodAndRole(long networkId, long podId, Role role);
+    
+    List<DomainRouterVO> listByNetworkAndRole(long networkId, Role role);
+    
+    List<DomainRouterVO> listByElementId(long elementId);
+    
+    /**
+     * Persists the domain router instance + creates the reference to the guest network (if not null)
+     * @param guestNetworks TODO
+     * @return
+     */    
+    DomainRouterVO persist(DomainRouterVO router, List<Network> guestNetworks);
+
+    /**
+     * @param routerId
+     * @return
+     */
+    List<Long> getRouterNetworks(long routerId);
+
+    /**
+     * @param vpcId
+     * @return
+     */
+    List<DomainRouterVO> listByVpcId(long vpcId);
+
+    /**
+     * @param routerId
+     * @param guestNetwork
+     */
+    void addRouterToGuestNetwork(VirtualRouter router, Network guestNetwork);
+
+    /**
+     * @param routerId
+     * @param guestNetworkId
+     */
+    void removeRouterFromGuestNetwork(long routerId, long guestNetworkId);
+    
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/vm/dao/DomainRouterDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/vm/dao/DomainRouterDaoImpl.java b/engine/schema/src/com/cloud/vm/dao/DomainRouterDaoImpl.java
new file mode 100755
index 0000000..391fa58
--- /dev/null
+++ b/engine/schema/src/com/cloud/vm/dao/DomainRouterDaoImpl.java
@@ -0,0 +1,358 @@
+// 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.vm.dao;
+
+import java.util.List;
+
+import javax.annotation.PostConstruct;
+import javax.ejb.Local;
+import javax.inject.Inject;
+
+import org.springframework.stereotype.Component;
+
+import com.cloud.host.HostVO;
+import com.cloud.host.dao.HostDao;
+import com.cloud.host.dao.HostDaoImpl;
+import com.cloud.network.Network;
+import com.cloud.network.dao.RouterNetworkDao;
+import com.cloud.network.dao.RouterNetworkDaoImpl;
+import com.cloud.network.dao.RouterNetworkVO;
+import com.cloud.network.router.VirtualRouter;
+import com.cloud.network.router.VirtualRouter.Role;
+import com.cloud.offering.NetworkOffering;
+import com.cloud.offerings.dao.NetworkOfferingDao;
+import com.cloud.offerings.dao.NetworkOfferingDaoImpl;
+import com.cloud.user.UserStatisticsVO;
+import com.cloud.user.dao.UserStatisticsDao;
+import com.cloud.user.dao.UserStatisticsDaoImpl;
+import com.cloud.utils.db.DB;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.JoinBuilder.JoinType;
+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;
+import com.cloud.utils.db.UpdateBuilder;
+import com.cloud.vm.DomainRouterVO;
+import com.cloud.vm.VirtualMachine;
+import com.cloud.vm.VirtualMachine.State;
+
+@Component
+@Local(value = { DomainRouterDao.class })
+@DB
+public class DomainRouterDaoImpl extends GenericDaoBase<DomainRouterVO, Long> implements DomainRouterDao {
+
+    protected SearchBuilder<DomainRouterVO> AllFieldsSearch;
+    protected SearchBuilder<DomainRouterVO> IdNetworkIdStatesSearch;
+    protected SearchBuilder<DomainRouterVO> HostUpSearch;
+    protected SearchBuilder<DomainRouterVO> StateNetworkTypeSearch;
+    protected SearchBuilder<DomainRouterVO> OutsidePodSearch;
+    @Inject HostDao _hostsDao;
+    @Inject RouterNetworkDao _routerNetworkDao;
+    @Inject UserStatisticsDao _userStatsDao;
+    @Inject NetworkOfferingDao _offDao;
+    protected SearchBuilder<DomainRouterVO> VpcSearch;
+    
+    public DomainRouterDaoImpl() {
+    }
+
+    @PostConstruct
+    protected void init() {
+        AllFieldsSearch = createSearchBuilder();
+        AllFieldsSearch.and("dc", AllFieldsSearch.entity().getDataCenterId(), Op.EQ);
+        AllFieldsSearch.and("account", AllFieldsSearch.entity().getAccountId(), Op.EQ);
+        AllFieldsSearch.and("role", AllFieldsSearch.entity().getRole(), Op.EQ);
+        AllFieldsSearch.and("domainId", AllFieldsSearch.entity().getDomainId(), Op.EQ);
+        AllFieldsSearch.and("host", AllFieldsSearch.entity().getHostId(), Op.EQ);
+        AllFieldsSearch.and("lastHost", AllFieldsSearch.entity().getLastHostId(), Op.EQ);
+        AllFieldsSearch.and("state", AllFieldsSearch.entity().getState(), Op.EQ);
+        AllFieldsSearch.and("states", AllFieldsSearch.entity().getState(), Op.IN);
+        SearchBuilder<RouterNetworkVO> joinRouterNetwork = _routerNetworkDao.createSearchBuilder();
+        joinRouterNetwork.and("networkId", joinRouterNetwork.entity().getNetworkId(), Op.EQ);
+        AllFieldsSearch.join("networkRouter", joinRouterNetwork, joinRouterNetwork.entity().getRouterId(), AllFieldsSearch.entity().getId(), JoinType.INNER);
+        AllFieldsSearch.and("podId", AllFieldsSearch.entity().getPodIdToDeployIn(), Op.EQ);
+        AllFieldsSearch.and("elementId", AllFieldsSearch.entity().getElementId(), Op.EQ);
+        AllFieldsSearch.and("vpcId", AllFieldsSearch.entity().getVpcId(), Op.EQ);
+        AllFieldsSearch.done();
+        
+        VpcSearch = createSearchBuilder();
+        VpcSearch.and("role", VpcSearch.entity().getRole(), Op.EQ);
+        VpcSearch.and("vpcId", VpcSearch.entity().getVpcId(), Op.EQ);
+        VpcSearch.done();
+
+        IdNetworkIdStatesSearch = createSearchBuilder();
+        IdNetworkIdStatesSearch.and("id", IdNetworkIdStatesSearch.entity().getId(), Op.EQ);
+        SearchBuilder<RouterNetworkVO> joinRouterNetwork1 = _routerNetworkDao.createSearchBuilder();
+        joinRouterNetwork1.and("networkId", joinRouterNetwork1.entity().getNetworkId(), Op.EQ);
+        IdNetworkIdStatesSearch.join("networkRouter", joinRouterNetwork1, joinRouterNetwork1.entity().getRouterId(), IdNetworkIdStatesSearch.entity().getId(), JoinType.INNER);
+        IdNetworkIdStatesSearch.and("states", IdNetworkIdStatesSearch.entity().getState(), Op.IN);
+        IdNetworkIdStatesSearch.done();
+
+        HostUpSearch = createSearchBuilder();
+        HostUpSearch.and("host", HostUpSearch.entity().getHostId(), Op.EQ);
+        HostUpSearch.and("states", HostUpSearch.entity().getState(), Op.NIN);
+        SearchBuilder<RouterNetworkVO> joinRouterNetwork3 = _routerNetworkDao.createSearchBuilder();
+        joinRouterNetwork3.and("networkId", joinRouterNetwork3.entity().getNetworkId(), Op.EQ);
+        joinRouterNetwork3.and("type", joinRouterNetwork3.entity().getGuestType(), Op.EQ);
+        HostUpSearch.join("networkRouter", joinRouterNetwork3, joinRouterNetwork3.entity().getRouterId(), HostUpSearch.entity().getId(), JoinType.INNER);
+        HostUpSearch.done();
+          
+        StateNetworkTypeSearch = createSearchBuilder();
+        StateNetworkTypeSearch.and("state", StateNetworkTypeSearch.entity().getState(), Op.EQ);
+        SearchBuilder<RouterNetworkVO> joinRouterNetwork4 = _routerNetworkDao.createSearchBuilder();
+        joinRouterNetwork4.and("networkId", joinRouterNetwork4.entity().getNetworkId(), Op.EQ);
+        joinRouterNetwork4.and("type", joinRouterNetwork4.entity().getGuestType(), Op.EQ);
+        StateNetworkTypeSearch.join("networkRouter", joinRouterNetwork4, joinRouterNetwork4.entity().getRouterId(), StateNetworkTypeSearch.entity().getId(), JoinType.INNER);
+        
+        SearchBuilder<HostVO> joinHost = _hostsDao.createSearchBuilder();
+        joinHost.and("mgmtServerId", joinHost.entity().getManagementServerId(), Op.EQ);
+        StateNetworkTypeSearch.join("host", joinHost, joinHost.entity().getId(), 
+                StateNetworkTypeSearch.entity().getHostId(), JoinType.INNER);
+        StateNetworkTypeSearch.done();
+        
+        
+        OutsidePodSearch = createSearchBuilder();
+        SearchBuilder<RouterNetworkVO> joinRouterNetwork2 = _routerNetworkDao.createSearchBuilder();
+        joinRouterNetwork2.and("networkId", joinRouterNetwork2.entity().getNetworkId(), Op.EQ);
+        OutsidePodSearch.join("networkRouter", joinRouterNetwork2, joinRouterNetwork2.entity().getRouterId(), 
+                OutsidePodSearch.entity().getId(), JoinType.INNER);
+        OutsidePodSearch.and("podId", OutsidePodSearch.entity().getPodIdToDeployIn(), Op.NEQ);
+        OutsidePodSearch.and("state", OutsidePodSearch.entity().getState(), Op.EQ);
+        OutsidePodSearch.and("role", OutsidePodSearch.entity().getRole(), Op.EQ);
+        OutsidePodSearch.done();
+
+    }
+
+    @Override
+    public boolean remove(Long id) {
+        Transaction txn = Transaction.currentTxn();
+        txn.start();
+        DomainRouterVO router = createForUpdate();
+        router.setPublicIpAddress(null);
+        UpdateBuilder ub = getUpdateBuilder(router);
+        ub.set(router, "state", State.Destroyed);
+        update(id, ub, router);
+
+        boolean result = super.remove(id);
+        txn.commit();
+        return result;
+    }
+
+    @Override
+    public List<DomainRouterVO> listByDataCenter(long dcId) {
+        SearchCriteria<DomainRouterVO> sc = AllFieldsSearch.create();
+        sc.setParameters("dc", dcId);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<DomainRouterVO> findBy(long accountId, long dcId) {
+        SearchCriteria<DomainRouterVO> sc = AllFieldsSearch.create();
+        sc.setParameters("account", accountId);
+        sc.setParameters("dc", dcId);
+        sc.setParameters("role", Role.VIRTUAL_ROUTER);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<DomainRouterVO> findBy(long accountId, long dcId, Role role) {
+        SearchCriteria<DomainRouterVO> sc = AllFieldsSearch.create();
+        sc.setParameters("account", accountId);
+        sc.setParameters("dc", dcId);
+        sc.setParameters("role", role);
+        return listBy(sc);
+    }
+
+
+    @Override
+    public List<DomainRouterVO> listBy(long accountId) {
+        SearchCriteria<DomainRouterVO> sc = AllFieldsSearch.create();
+        sc.setParameters("account", accountId);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<DomainRouterVO> listByHostId(Long hostId) {
+        SearchCriteria<DomainRouterVO> sc = AllFieldsSearch.create();
+        sc.setParameters("host", hostId);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<DomainRouterVO> listByPodId(Long podId) {
+        SearchCriteria<DomainRouterVO> sc = AllFieldsSearch.create();
+        sc.setParameters("podId", podId);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<DomainRouterVO> listByPodIdAndStates(Long podId, State... states) {
+        SearchCriteria<DomainRouterVO> sc = AllFieldsSearch.create();
+        sc.setParameters("podId", podId);
+        sc.setParameters("states", (Object[]) states);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<DomainRouterVO> listIsolatedByHostId(Long hostId) {
+        SearchCriteria<DomainRouterVO> sc = HostUpSearch.create();
+        if (hostId != null) {
+            sc.setParameters("host", hostId);
+        }
+        sc.setJoinParameters("networkRouter", "type", Network.GuestType.Isolated);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<DomainRouterVO> listByDomain(Long domainId) {
+        SearchCriteria<DomainRouterVO> sc = AllFieldsSearch.create();
+        sc.setParameters("domainId", domainId);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<DomainRouterVO> findByNetwork(long networkId) {
+        SearchCriteria<DomainRouterVO> sc = AllFieldsSearch.create();
+        sc.setJoinParameters("networkRouter", "networkId", networkId);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<DomainRouterVO> listByLastHostId(Long hostId) {
+        SearchCriteria<DomainRouterVO> sc = AllFieldsSearch.create();
+        sc.setParameters("lastHost", hostId);
+        sc.setParameters("state", State.Stopped);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<DomainRouterVO> listActive(long networkId) {
+        SearchCriteria<DomainRouterVO> sc = IdNetworkIdStatesSearch.create();
+        sc.setJoinParameters("networkRouter", "networkId", networkId);
+        sc.setParameters("states", State.Running, State.Migrating, State.Stopping, State.Starting);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<DomainRouterVO> listByStateAndNetworkType(State state, Network.GuestType type, long mgmtSrvrId) {
+        SearchCriteria<DomainRouterVO> sc = StateNetworkTypeSearch.create();
+        sc.setParameters("state", state);
+        sc.setJoinParameters("networkRouter", "type", type);
+        sc.setJoinParameters("host", "mgmtServerId", mgmtSrvrId);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<DomainRouterVO> findByNetworkOutsideThePod(long networkId, long podId, State state, Role role) {
+        SearchCriteria<DomainRouterVO> sc = OutsidePodSearch.create();
+        sc.setJoinParameters("networkRouter", "networkId", networkId);
+        sc.setParameters("podId", podId);
+        sc.setParameters("state", state);
+        sc.setParameters("role", role);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<DomainRouterVO> listByNetworkAndPodAndRole(long networkId, long podId, Role role) {
+        SearchCriteria<DomainRouterVO> sc = AllFieldsSearch.create();
+        sc.setJoinParameters("networkRouter", "networkId", networkId);
+        sc.setParameters("podId", podId);
+        sc.setParameters("role", role);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<DomainRouterVO> listByNetworkAndRole(long networkId, Role role) {
+        SearchCriteria<DomainRouterVO> sc = AllFieldsSearch.create();
+        sc.setJoinParameters("networkRouter", "networkId", networkId);
+        sc.setParameters("role", role);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<DomainRouterVO> listByElementId(long elementId) {
+        SearchCriteria<DomainRouterVO> sc = AllFieldsSearch.create();
+        sc.setParameters("elementId", elementId);
+        return listBy(sc);
+    }
+    
+    @Override
+    @DB
+    public DomainRouterVO persist(DomainRouterVO router, List<Network> guestNetworks) {
+        Transaction txn = Transaction.currentTxn();
+        txn.start();
+
+        // 1) create network
+        DomainRouterVO newRouter = super.persist(router);
+        
+        if (guestNetworks != null && !guestNetworks.isEmpty()) {
+            // 2) add router to the network
+            for (Network guestNetwork : guestNetworks) {
+                addRouterToGuestNetwork(router, guestNetwork);  
+            }
+        }
+       
+        txn.commit();
+        return newRouter;
+    }
+    
+    @Override
+    @DB
+    public void addRouterToGuestNetwork(VirtualRouter router, Network guestNetwork) {
+        if (_routerNetworkDao.findByRouterAndNetwork(router.getId(), guestNetwork.getId()) == null) {
+            NetworkOffering off = _offDao.findById(guestNetwork.getNetworkOfferingId());
+            if (!(off.getName().equalsIgnoreCase(NetworkOffering.SystemPrivateGatewayNetworkOffering))) {
+                Transaction txn = Transaction.currentTxn();
+                txn.start();
+                //1) add router to network
+                RouterNetworkVO routerNtwkMap = new RouterNetworkVO(router.getId(), guestNetwork.getId(), guestNetwork.getGuestType());
+                _routerNetworkDao.persist(routerNtwkMap);
+                //2) create user stats entry for the network
+                UserStatisticsVO stats = _userStatsDao.findBy(router.getAccountId(), router.getDataCenterId(), 
+                        guestNetwork.getId(), null, router.getId(), router.getType().toString());
+                if (stats == null) {
+                    stats = new UserStatisticsVO(router.getAccountId(), router.getDataCenterId(), null, router.getId(),
+                            router.getType().toString(), guestNetwork.getId());
+                    _userStatsDao.persist(stats);
+                }
+                txn.commit();
+            }
+        }  
+    }
+    
+    @Override
+    public void removeRouterFromGuestNetwork(long routerId, long guestNetworkId) {
+        RouterNetworkVO routerNtwkMap = _routerNetworkDao.findByRouterAndNetwork(routerId, guestNetworkId);
+        if (routerNtwkMap != null) {
+            _routerNetworkDao.remove(routerNtwkMap.getId());
+        }
+    }
+    
+    @Override
+    public List<Long> getRouterNetworks(long routerId) {
+        return _routerNetworkDao.getRouterNetworks(routerId);
+    }
+    
+    @Override
+    public List<DomainRouterVO> listByVpcId(long vpcId) {
+        SearchCriteria<DomainRouterVO> sc = VpcSearch.create();
+        sc.setParameters("vpcId", vpcId);
+        sc.setParameters("role", Role.VIRTUAL_ROUTER);
+        return listBy(sc);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/vm/dao/InstanceGroupDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/vm/dao/InstanceGroupDao.java b/engine/schema/src/com/cloud/vm/dao/InstanceGroupDao.java
new file mode 100644
index 0000000..681a893
--- /dev/null
+++ b/engine/schema/src/com/cloud/vm/dao/InstanceGroupDao.java
@@ -0,0 +1,36 @@
+// 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.vm.dao;
+
+import java.util.List;
+
+import com.cloud.utils.db.GenericDao;
+import com.cloud.vm.InstanceGroupVO;
+
+public interface InstanceGroupDao extends GenericDao<InstanceGroupVO, Long>{
+	List<InstanceGroupVO> listByAccountId(long id);
+	boolean isNameInUse(Long accountId, String name);
+    InstanceGroupVO findByAccountAndName(Long accountId, String name);
+    
+    /**
+     * Updates name for the vm
+     * @param id - group id
+     * @param name
+     */
+    void updateVmGroup(long id, String name);
+
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/vm/dao/InstanceGroupDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/vm/dao/InstanceGroupDaoImpl.java b/engine/schema/src/com/cloud/vm/dao/InstanceGroupDaoImpl.java
new file mode 100644
index 0000000..0ce127e
--- /dev/null
+++ b/engine/schema/src/com/cloud/vm/dao/InstanceGroupDaoImpl.java
@@ -0,0 +1,80 @@
+// 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.vm.dao;
+
+import java.util.List;
+
+import javax.ejb.Local;
+
+import org.springframework.stereotype.Component;
+
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.vm.InstanceGroupVO;
+
+@Component
+@Local (value={InstanceGroupDao.class})
+public class InstanceGroupDaoImpl extends GenericDaoBase<InstanceGroupVO, Long> implements InstanceGroupDao{
+	private SearchBuilder<InstanceGroupVO> AccountIdNameSearch;
+	protected final SearchBuilder<InstanceGroupVO> AccountSearch;
+	
+	protected InstanceGroupDaoImpl() {
+        AccountSearch = createSearchBuilder();
+        AccountSearch.and("account", AccountSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
+        AccountSearch.done();
+        
+        AccountIdNameSearch = createSearchBuilder();
+        AccountIdNameSearch.and("accountId", AccountIdNameSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
+        AccountIdNameSearch.and("groupName", AccountIdNameSearch.entity().getName(), SearchCriteria.Op.EQ);
+        AccountIdNameSearch.done();
+        
+	}
+	
+    @Override
+    public boolean isNameInUse(Long accountId, String name) {
+        SearchCriteria<InstanceGroupVO> sc = createSearchCriteria();
+        sc.addAnd("name", SearchCriteria.Op.EQ, name);
+        if (accountId != null) {
+            sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId);
+        }
+        List<InstanceGroupVO> vmGroups = listBy(sc);
+        return ((vmGroups != null) && !vmGroups.isEmpty());
+    }
+    
+	@Override
+	public InstanceGroupVO findByAccountAndName(Long accountId, String name) {
+		SearchCriteria<InstanceGroupVO> sc = AccountIdNameSearch.create();
+		sc.setParameters("accountId", accountId);
+		sc.setParameters("groupName", name);
+		return findOneBy(sc);
+	}
+	
+    @Override
+    public void updateVmGroup(long id, String name) {
+        InstanceGroupVO vo = createForUpdate();
+        vo.setName(name);
+        update(id, vo);
+    }
+    
+    @Override
+    public List<InstanceGroupVO> listByAccountId(long id) {
+        SearchCriteria<InstanceGroupVO> sc = AccountSearch.create();
+        sc.setParameters("account", id);
+        return listBy(sc);
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/vm/dao/InstanceGroupVMMapDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/vm/dao/InstanceGroupVMMapDao.java b/engine/schema/src/com/cloud/vm/dao/InstanceGroupVMMapDao.java
new file mode 100644
index 0000000..28a0f53
--- /dev/null
+++ b/engine/schema/src/com/cloud/vm/dao/InstanceGroupVMMapDao.java
@@ -0,0 +1,28 @@
+// 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.vm.dao;
+
+import java.util.List;
+
+import com.cloud.utils.db.GenericDao;
+import com.cloud.vm.InstanceGroupVMMapVO;
+
+public interface InstanceGroupVMMapDao extends GenericDao<InstanceGroupVMMapVO, Long>{
+	List<InstanceGroupVMMapVO> listByInstanceId(long instanceId);
+	List<InstanceGroupVMMapVO> listByGroupId(long groupId);
+	InstanceGroupVMMapVO findByVmIdGroupId(long instanceId, long groupId);
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/vm/dao/InstanceGroupVMMapDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/vm/dao/InstanceGroupVMMapDaoImpl.java b/engine/schema/src/com/cloud/vm/dao/InstanceGroupVMMapDaoImpl.java
new file mode 100644
index 0000000..a33cc84
--- /dev/null
+++ b/engine/schema/src/com/cloud/vm/dao/InstanceGroupVMMapDaoImpl.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.vm.dao;
+
+import java.util.List;
+
+import javax.ejb.Local;
+
+import org.springframework.stereotype.Component;
+
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.vm.InstanceGroupVMMapVO;
+
+@Component
+@Local(value={InstanceGroupVMMapDao.class})
+public class InstanceGroupVMMapDaoImpl extends GenericDaoBase<InstanceGroupVMMapVO, Long> implements InstanceGroupVMMapDao{
+	
+	private SearchBuilder<InstanceGroupVMMapVO> ListByVmId;
+	private SearchBuilder<InstanceGroupVMMapVO> ListByGroupId;
+    private SearchBuilder<InstanceGroupVMMapVO> ListByVmIdGroupId;
+	
+	protected InstanceGroupVMMapDaoImpl() {
+		ListByVmId  = createSearchBuilder();
+		ListByVmId.and("instanceId", ListByVmId.entity().getInstanceId(), SearchCriteria.Op.EQ);
+		ListByVmId.done();
+		
+		ListByGroupId  = createSearchBuilder();
+		ListByGroupId.and("groupId", ListByGroupId.entity().getGroupId(), SearchCriteria.Op.EQ);
+		ListByGroupId.done();
+		
+        ListByVmIdGroupId  = createSearchBuilder();
+        ListByVmIdGroupId.and("instanceId", ListByVmIdGroupId.entity().getInstanceId(), SearchCriteria.Op.EQ);
+        ListByVmIdGroupId.and("groupId", ListByVmIdGroupId.entity().getGroupId(), SearchCriteria.Op.EQ);
+        ListByVmIdGroupId.done();
+	}
+	
+    @Override
+    public List<InstanceGroupVMMapVO> listByInstanceId(long vmId) {
+        SearchCriteria<InstanceGroupVMMapVO> sc = ListByVmId.create();
+        sc.setParameters("instanceId", vmId);
+        return listBy(sc);
+    }
+    
+    @Override
+    public List<InstanceGroupVMMapVO> listByGroupId(long groupId) {
+        SearchCriteria<InstanceGroupVMMapVO> sc = ListByGroupId.create();
+        sc.setParameters("groupId", groupId);
+        return listBy(sc);
+    }
+    
+	@Override
+	public InstanceGroupVMMapVO findByVmIdGroupId(long instanceId, long groupId) {
+        SearchCriteria<InstanceGroupVMMapVO> sc = ListByVmIdGroupId.create();
+        sc.setParameters("groupId", groupId);
+        sc.setParameters("instanceId", instanceId);
+		return findOneBy(sc);
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/vm/dao/NicDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/vm/dao/NicDao.java b/engine/schema/src/com/cloud/vm/dao/NicDao.java
new file mode 100644
index 0000000..67c1e07
--- /dev/null
+++ b/engine/schema/src/com/cloud/vm/dao/NicDao.java
@@ -0,0 +1,69 @@
+// 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.vm.dao;
+
+import java.util.List;
+
+import com.cloud.utils.db.GenericDao;
+import com.cloud.vm.NicVO;
+import com.cloud.vm.VirtualMachine;
+
+public interface NicDao extends GenericDao<NicVO, Long> {
+    List<NicVO> listByVmId(long instanceId);
+    
+    List<String> listIpAddressInNetwork(long networkConfigId);
+    List<NicVO> listByVmIdIncludingRemoved(long instanceId);
+    
+    List<NicVO> listByNetworkId(long networkId);
+    
+    NicVO findByInstanceIdAndNetworkId(long networkId, long instanceId);
+    
+    NicVO findByInstanceIdAndNetworkIdIncludingRemoved(long networkId, long instanceId);
+    
+    NicVO findByNetworkIdTypeAndGateway(long networkId, VirtualMachine.Type vmType, String gateway);
+
+    void removeNicsForInstance(long instanceId);
+    
+    NicVO findByNetworkIdAndType(long networkId, VirtualMachine.Type vmType);
+    
+    NicVO findByIp4AddressAndNetworkId(String ip4Address, long networkId);
+    
+    NicVO findDefaultNicForVM(long instanceId);
+
+    /**
+     * @param networkId
+     * @param instanceId
+     * @return
+     */
+    NicVO findNonReleasedByInstanceIdAndNetworkId(long networkId, long instanceId);
+    
+    String getIpAddress(long networkId, long instanceId);
+    
+    int countNics(long instanceId);
+    
+    NicVO findByNetworkIdInstanceIdAndBroadcastUri(long networkId, long instanceId, String broadcastUri);
+    
+    NicVO findByIp4AddressAndNetworkIdAndInstanceId(long networkId, long instanceId, String ip4Address);
+
+    List<NicVO> listByVmIdAndNicId(Long vmId, Long nicId);
+
+    NicVO findByIp4AddressAndVmId(String ip4Address, long instance);
+    
+    List<NicVO> listPlaceholderNicsByNetworkId(long networkId);
+    
+    List<NicVO> listPlaceholderNicsByNetworkIdAndVmType(long networkId, VirtualMachine.Type vmType);
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/vm/dao/NicDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/vm/dao/NicDaoImpl.java b/engine/schema/src/com/cloud/vm/dao/NicDaoImpl.java
new file mode 100644
index 0000000..c70d194
--- /dev/null
+++ b/engine/schema/src/com/cloud/vm/dao/NicDaoImpl.java
@@ -0,0 +1,241 @@
+// 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.vm.dao;
+
+import java.util.List;
+
+import javax.ejb.Local;
+
+import org.springframework.stereotype.Component;
+
+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.Func;
+import com.cloud.utils.db.SearchCriteria.Op;
+import com.cloud.vm.Nic;
+import com.cloud.vm.Nic.State;
+import com.cloud.vm.NicVO;
+import com.cloud.vm.VirtualMachine;
+
+@Component
+@Local(value=NicDao.class)
+public class NicDaoImpl extends GenericDaoBase<NicVO, Long> implements NicDao {
+    private final SearchBuilder<NicVO> AllFieldsSearch;
+    private final GenericSearchBuilder<NicVO, String> IpSearch;
+    private final SearchBuilder<NicVO> NonReleasedSearch;
+    final GenericSearchBuilder<NicVO, Integer> CountBy;
+
+    
+    public NicDaoImpl() {
+        super();
+        
+        AllFieldsSearch = createSearchBuilder();
+        AllFieldsSearch.and("instance", AllFieldsSearch.entity().getInstanceId(), Op.EQ);
+        AllFieldsSearch.and("network", AllFieldsSearch.entity().getNetworkId(), Op.EQ);
+        AllFieldsSearch.and("gateway", AllFieldsSearch.entity().getGateway(), Op.EQ);
+        AllFieldsSearch.and("vmType", AllFieldsSearch.entity().getVmType(), Op.EQ);
+        AllFieldsSearch.and("address", AllFieldsSearch.entity().getIp4Address(), Op.EQ);
+        AllFieldsSearch.and("isDefault", AllFieldsSearch.entity().isDefaultNic(), Op.EQ);
+        AllFieldsSearch.and("broadcastUri", AllFieldsSearch.entity().getBroadcastUri(), Op.EQ);
+        AllFieldsSearch.and("secondaryip", AllFieldsSearch.entity().getSecondaryIp(), Op.EQ);
+        AllFieldsSearch.and("nicid", AllFieldsSearch.entity().getId(), Op.EQ);
+        AllFieldsSearch.done();
+
+        IpSearch = createSearchBuilder(String.class);
+        IpSearch.select(null, Func.DISTINCT, IpSearch.entity().getIp4Address());
+        IpSearch.and("network", IpSearch.entity().getNetworkId(), Op.EQ);
+        IpSearch.and("address", IpSearch.entity().getIp4Address(), Op.NNULL);
+        IpSearch.done();
+        
+        NonReleasedSearch = createSearchBuilder();
+        NonReleasedSearch.and("instance", NonReleasedSearch.entity().getInstanceId(), Op.EQ);
+        NonReleasedSearch.and("network", NonReleasedSearch.entity().getNetworkId(), Op.EQ);
+        NonReleasedSearch.and("state", NonReleasedSearch.entity().getState(), Op.NOTIN);
+        NonReleasedSearch.done();
+        
+        CountBy = createSearchBuilder(Integer.class);
+        CountBy.select(null, Func.COUNT, CountBy.entity().getId());
+        CountBy.and("vmId", CountBy.entity().getInstanceId(), Op.EQ);
+        CountBy.and("removed", CountBy.entity().getRemoved(), Op.NULL);
+        CountBy.done();
+    }
+    
+    @Override
+    public void removeNicsForInstance(long instanceId) {
+        SearchCriteria<NicVO> sc = AllFieldsSearch.create();
+        sc.setParameters("instance", instanceId);
+        remove(sc);
+    }
+    
+    @Override
+    public List<NicVO> listByVmId(long instanceId) {
+        SearchCriteria<NicVO> sc = AllFieldsSearch.create();
+        sc.setParameters("instance", instanceId);
+        return listBy(sc);
+    }
+    
+    @Override
+    public List<NicVO> listByVmIdIncludingRemoved(long instanceId) {
+        SearchCriteria<NicVO> sc = AllFieldsSearch.create();
+        sc.setParameters("instance", instanceId);
+        return listIncludingRemovedBy(sc);
+    }
+    
+    
+    @Override
+    public List<String> listIpAddressInNetwork(long networkId) {
+        SearchCriteria<String> sc = IpSearch.create();
+        sc.setParameters("network", networkId);
+        return customSearch(sc, null);
+    }
+    
+    @Override
+    public List<NicVO> listByNetworkId(long networkId) {
+        SearchCriteria<NicVO> sc = AllFieldsSearch.create();
+        sc.setParameters("network", networkId);
+        return listBy(sc);
+    }
+    
+    @Override
+    public NicVO findByInstanceIdAndNetworkId(long networkId, long instanceId) {
+        SearchCriteria<NicVO> sc = AllFieldsSearch.create();
+        sc.setParameters("network", networkId);
+        sc.setParameters("instance", instanceId);
+        return findOneBy(sc);
+    }
+    
+    @Override
+    public NicVO findByInstanceIdAndNetworkIdIncludingRemoved(long networkId, long instanceId) {
+        SearchCriteria<NicVO> sc = createSearchCriteria();
+        sc.addAnd("networkId", SearchCriteria.Op.EQ, networkId);
+        sc.addAnd("instanceId", SearchCriteria.Op.EQ, instanceId);
+        return findOneIncludingRemovedBy(sc);
+    }
+    
+    @Override
+    public NicVO findByNetworkIdAndType(long networkId, VirtualMachine.Type vmType) {
+        SearchCriteria<NicVO> sc = AllFieldsSearch.create();
+        sc.setParameters("network", networkId);
+        sc.setParameters("vmType", vmType);
+        return findOneBy(sc);
+    }
+    
+    @Override
+    public NicVO findByNetworkIdTypeAndGateway(long networkId, VirtualMachine.Type vmType, String gateway) {
+        SearchCriteria<NicVO> sc = AllFieldsSearch.create();
+        sc.setParameters("network", networkId);
+        sc.setParameters("vmType", vmType);
+        sc.setParameters("gateway", gateway);
+        return findOneBy(sc);
+    }
+    
+    @Override
+    public NicVO findByIp4AddressAndNetworkId(String ip4Address, long networkId) {
+        SearchCriteria<NicVO> sc = AllFieldsSearch.create();
+        sc.setParameters("address", ip4Address);
+        sc.setParameters("network", networkId);
+        return findOneBy(sc);
+    }
+    
+    @Override
+    public NicVO findDefaultNicForVM(long instanceId) {
+        SearchCriteria<NicVO> sc = AllFieldsSearch.create();
+        sc.setParameters("instance", instanceId);
+        sc.setParameters("isDefault", 1);
+        return findOneBy(sc);
+    }
+    
+    @Override
+    public NicVO findNonReleasedByInstanceIdAndNetworkId(long networkId, long instanceId) {
+        SearchCriteria<NicVO> sc = NonReleasedSearch.create();
+        sc.setParameters("network", networkId);
+        sc.setParameters("instance", instanceId);
+        sc.setParameters("state", State.Releasing, Nic.State.Deallocating);
+        return findOneBy(sc);
+    }
+    
+    @Override
+    public String getIpAddress(long networkId, long instanceId) {
+        SearchCriteria<NicVO> sc = AllFieldsSearch.create();
+        sc.setParameters("network", networkId);
+        sc.setParameters("instance", instanceId);
+        return findOneBy(sc).getIp4Address();
+    }
+
+    @Override
+    public int countNics(long instanceId) {
+        SearchCriteria<Integer> sc = CountBy.create();
+        sc.setParameters("vmId", instanceId);
+        List<Integer> results = customSearch(sc, null);
+        return results.get(0);
+    }
+
+
+    @Override
+    public NicVO findByNetworkIdInstanceIdAndBroadcastUri(long networkId, long instanceId, String broadcastUri) {
+        SearchCriteria<NicVO> sc = AllFieldsSearch.create();
+        sc.setParameters("network", networkId);
+        sc.setParameters("instance", instanceId);
+        sc.setParameters("broadcastUri", broadcastUri);
+        return findOneBy(sc);
+    }
+    
+    @Override
+    public NicVO findByIp4AddressAndNetworkIdAndInstanceId(long networkId, long instanceId, String ip4Address) {
+        SearchCriteria<NicVO> sc = AllFieldsSearch.create();
+        sc.setParameters("network", networkId);
+        sc.setParameters("instance", instanceId);
+        sc.setParameters("address", ip4Address);
+        return findOneBy(sc);
+    }
+
+    @Override
+    public List<NicVO> listByVmIdAndNicId(Long vmId, Long nicId) {
+        SearchCriteria<NicVO> sc = AllFieldsSearch.create();
+        sc.setParameters("instance", vmId);
+        sc.setParameters("nicid", nicId);
+        return listBy(sc);
+    }
+
+    @Override
+    public NicVO findByIp4AddressAndVmId(String ip4Address, long instance) {
+        SearchCriteria<NicVO> sc = AllFieldsSearch.create();
+        sc.setParameters("address", ip4Address);
+        sc.setParameters("instance", instance);
+        return findOneBy(sc);
+    }
+
+    @Override
+    public List<NicVO> listPlaceholderNicsByNetworkId(long networkId) {
+        SearchCriteria<NicVO> sc = AllFieldsSearch.create();
+        sc.setParameters("network", networkId);
+        sc.setParameters("strategy", Nic.ReservationStrategy.PlaceHolder.toString());
+        return listBy(sc);
+    }
+
+    @Override
+    public List<NicVO> listPlaceholderNicsByNetworkIdAndVmType(long networkId, VirtualMachine.Type vmType) {
+        SearchCriteria<NicVO> sc = AllFieldsSearch.create();
+        sc.setParameters("network", networkId);
+        sc.setParameters("strategy", Nic.ReservationStrategy.PlaceHolder.toString());
+        sc.setParameters("vmType", vmType);
+        return listBy(sc);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/vm/dao/NicSecondaryIpDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/vm/dao/NicSecondaryIpDao.java b/engine/schema/src/com/cloud/vm/dao/NicSecondaryIpDao.java
new file mode 100644
index 0000000..da96df4
--- /dev/null
+++ b/engine/schema/src/com/cloud/vm/dao/NicSecondaryIpDao.java
@@ -0,0 +1,53 @@
+// 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.vm.dao;
+
+import java.util.List;
+import com.cloud.utils.db.GenericDao;
+
+public interface NicSecondaryIpDao extends GenericDao<NicSecondaryIpVO, Long> {
+    List<NicSecondaryIpVO> listByVmId(long instanceId);
+
+    List<String> listSecondaryIpAddressInNetwork(long networkConfigId);
+    List<NicSecondaryIpVO> listByNetworkId(long networkId);
+
+    NicSecondaryIpVO findByInstanceIdAndNetworkId(long networkId, long instanceId);
+
+    //    void removeNicsForInstance(long instanceId);
+    //    void removeSecondaryIpForNic(long nicId);
+
+    NicSecondaryIpVO findByIp4AddressAndNetworkId(String ip4Address, long networkId);
+
+    /**
+     * @param networkId
+     * @param instanceId
+     * @return
+     */
+
+    List<NicSecondaryIpVO> getSecondaryIpAddressesForVm(long vmId);
+
+    List<NicSecondaryIpVO> listByNicId(long nicId);
+
+    List<NicSecondaryIpVO> listByNicIdAndVmid(long nicId, long vmId);
+
+    NicSecondaryIpVO findByIp4AddressAndNicId(String ip4Address, long nicId);
+
+    NicSecondaryIpVO findByIp4AddressAndNetworkIdAndInstanceId(long networkId,
+            Long vmId, String vmIp);
+
+    List<String> getSecondaryIpAddressesForNic(long nicId);
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/vm/dao/NicSecondaryIpDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/vm/dao/NicSecondaryIpDaoImpl.java b/engine/schema/src/com/cloud/vm/dao/NicSecondaryIpDaoImpl.java
new file mode 100644
index 0000000..3befaf7
--- /dev/null
+++ b/engine/schema/src/com/cloud/vm/dao/NicSecondaryIpDaoImpl.java
@@ -0,0 +1,138 @@
+// 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.vm.dao;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.ejb.Local;
+
+import org.springframework.stereotype.Component;
+
+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.Func;
+import com.cloud.utils.db.SearchCriteria.Op;
+
+@Component
+@Local(value=NicSecondaryIpDao.class)
+public class NicSecondaryIpDaoImpl extends GenericDaoBase<NicSecondaryIpVO, Long> implements NicSecondaryIpDao {
+    private final SearchBuilder<NicSecondaryIpVO> AllFieldsSearch;
+    private final GenericSearchBuilder<NicSecondaryIpVO, String> IpSearch;
+
+    protected NicSecondaryIpDaoImpl() {
+        super();
+        AllFieldsSearch = createSearchBuilder();
+        AllFieldsSearch.and("instanceId", AllFieldsSearch.entity().getVmId(), Op.EQ);
+        AllFieldsSearch.and("network", AllFieldsSearch.entity().getNetworkId(), Op.EQ);
+        AllFieldsSearch.and("address", AllFieldsSearch.entity().getIp4Address(), Op.EQ);
+        AllFieldsSearch.and("nicId", AllFieldsSearch.entity().getNicId(), Op.EQ);
+        AllFieldsSearch.done();
+
+        IpSearch = createSearchBuilder(String.class);
+        IpSearch.select(null, Func.DISTINCT, IpSearch.entity().getIp4Address());
+        IpSearch.and("network", IpSearch.entity().getNetworkId(), Op.EQ);
+        IpSearch.and("address", IpSearch.entity().getIp4Address(), Op.NNULL);
+        IpSearch.done();
+    }
+
+    @Override
+    public List<NicSecondaryIpVO> listByVmId(long instanceId) {
+        SearchCriteria<NicSecondaryIpVO> sc = AllFieldsSearch.create();
+        sc.setParameters("instanceId", instanceId);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<NicSecondaryIpVO> listByNicId(long nicId) {
+        SearchCriteria<NicSecondaryIpVO> sc = AllFieldsSearch.create();
+        sc.setParameters("nicId", nicId);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<String> listSecondaryIpAddressInNetwork(long networkId) {
+        SearchCriteria<String> sc = IpSearch.create();
+        sc.setParameters("network", networkId);
+        return customSearch(sc, null);
+    }
+
+    @Override
+    public List<NicSecondaryIpVO> listByNetworkId(long networkId) {
+        SearchCriteria<NicSecondaryIpVO> sc = AllFieldsSearch.create();
+        sc.setParameters("network", networkId);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<NicSecondaryIpVO> listByNicIdAndVmid(long nicId, long vmId) {
+        SearchCriteria<NicSecondaryIpVO> sc = AllFieldsSearch.create();
+        sc.setParameters("nicId", nicId);
+        sc.setParameters("instanceId", vmId);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<NicSecondaryIpVO> getSecondaryIpAddressesForVm(long vmId) {
+        SearchCriteria<NicSecondaryIpVO> sc = AllFieldsSearch.create();
+        sc.setParameters("instanceId", vmId);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<String> getSecondaryIpAddressesForNic(long nicId) {
+        SearchCriteria<NicSecondaryIpVO> sc = AllFieldsSearch.create();
+        sc.setParameters("nicId", nicId);
+        List<NicSecondaryIpVO> results = search(sc, null);
+        List<String> ips = new ArrayList<String>(results.size());
+        for (NicSecondaryIpVO result : results) {
+            ips.add(result.getIp4Address());
+        }
+        return ips;
+    }
+
+    @Override
+    public NicSecondaryIpVO findByInstanceIdAndNetworkId(long networkId, long instanceId) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public NicSecondaryIpVO findByIp4AddressAndNetworkId(String ip4Address, long networkId) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+    @Override
+    public NicSecondaryIpVO findByIp4AddressAndNicId(String ip4Address, long nicId) {
+        SearchCriteria<NicSecondaryIpVO> sc = AllFieldsSearch.create();
+        sc.setParameters("address", ip4Address);
+        sc.setParameters("nicId", nicId);
+        return findOneBy(sc);
+    }
+
+    @Override
+    public NicSecondaryIpVO findByIp4AddressAndNetworkIdAndInstanceId(
+            long networkId, Long vmId, String vmIp) {
+        SearchCriteria<NicSecondaryIpVO> sc = AllFieldsSearch.create();
+        sc.setParameters("network", networkId);
+        sc.setParameters("instanceId", vmId);
+        sc.setParameters("address", vmIp);
+        return findOneBy(sc);
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/vm/dao/NicSecondaryIpVO.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/vm/dao/NicSecondaryIpVO.java b/engine/schema/src/com/cloud/vm/dao/NicSecondaryIpVO.java
new file mode 100644
index 0000000..770e188
--- /dev/null
+++ b/engine/schema/src/com/cloud/vm/dao/NicSecondaryIpVO.java
@@ -0,0 +1,160 @@
+// 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.vm.dao;
+
+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 com.cloud.utils.db.GenericDao;
+import com.cloud.vm.NicSecondaryIp;
+
+@Entity
+@Table(name = "nic_secondary_ips")
+public class NicSecondaryIpVO implements NicSecondaryIp {
+
+    public NicSecondaryIpVO(Long nicId, String ipaddr, Long vmId,
+            Long accountId, Long domainId, Long networkId) {
+        this.nicId = nicId;
+        this.vmId = vmId;
+        this.ip4Address = ipaddr;
+        this.accountId = accountId;
+        this.domainId = domainId;
+        this.networkId = networkId;
+    }
+
+    protected NicSecondaryIpVO() {
+    }
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Column(name = "id")
+    long id;
+
+    @Column(name = "nicId")
+    long nicId;
+
+    @Column(name="domain_id", updatable=false)
+    long domainId;
+
+    @Column(name="account_id", updatable=false)
+    private Long accountId;
+
+    @Column(name = "ip4_address")
+    String ip4Address;
+
+    @Column(name = "ip6_address")
+    String ip6Address;
+
+    @Column(name = "network_id")
+    long networkId;
+
+    @Column(name = GenericDao.CREATED_COLUMN)
+    Date created;
+
+    @Column(name = "uuid")
+    String uuid = UUID.randomUUID().toString();
+
+    @Column(name = "vmId")
+    Long vmId;
+
+    public long getId() {
+        return id;
+    }
+
+    public void setId(long id) {
+        this.id = id;
+    }
+
+    public long getNicId() {
+        return nicId;
+    }
+
+    public void setNicId(long nicId) {
+        this.nicId = nicId;
+    }
+
+    public long getDomainId() {
+        return domainId;
+    }
+
+    public void setDomainId(Long domainId) {
+        this.domainId = domainId;
+    }
+
+    public long getAccountId() {
+        return accountId;
+    }
+
+    public void setAccountId(Long accountId) {
+        this.accountId = accountId;
+    }
+
+    public String getIp4Address() {
+        return ip4Address;
+    }
+
+    public void setIp4Address(String ip4Address) {
+        this.ip4Address = ip4Address;
+    }
+
+    public String getIp6Address() {
+        return ip6Address;
+    }
+
+    public void setIp6Address(String ip6Address) {
+        this.ip6Address = ip6Address;
+    }
+
+    public long getNetworkId() {
+        return networkId;
+    }
+
+    public void setNetworkId(long networkId) {
+        this.networkId = networkId;
+    }
+
+    public Date getCreated() {
+        return created;
+    }
+
+    public void setCreated(Date created) {
+        this.created = created;
+    }
+
+    public String getUuid() {
+        return uuid;
+    }
+
+    public void setUuid(String uuid) {
+        this.uuid = uuid;
+    }
+
+    public long getVmId() {
+        return vmId;
+    }
+
+    public void setVmId(Long vmId) {
+        this.vmId = vmId;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/vm/dao/SecondaryStorageVmDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/vm/dao/SecondaryStorageVmDao.java b/engine/schema/src/com/cloud/vm/dao/SecondaryStorageVmDao.java
new file mode 100644
index 0000000..857b637
--- /dev/null
+++ b/engine/schema/src/com/cloud/vm/dao/SecondaryStorageVmDao.java
@@ -0,0 +1,42 @@
+// 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.vm.dao;
+
+import java.util.List;
+
+import com.cloud.utils.db.GenericDao;
+import com.cloud.vm.SecondaryStorageVm;
+import com.cloud.vm.SecondaryStorageVmVO;
+import com.cloud.vm.VirtualMachine.State;
+
+public interface SecondaryStorageVmDao extends GenericDao<SecondaryStorageVmVO, Long> {
+	
+    public List<SecondaryStorageVmVO> getSecStorageVmListInStates(SecondaryStorageVm.Role role, long dataCenterId, State... states);
+    public List<SecondaryStorageVmVO> getSecStorageVmListInStates(SecondaryStorageVm.Role role, State... states);
+    
+    public List<SecondaryStorageVmVO> listByHostId(SecondaryStorageVm.Role role, long hostId);
+    public List<SecondaryStorageVmVO> listByLastHostId(SecondaryStorageVm.Role role, long hostId);
+    
+    public List<SecondaryStorageVmVO> listUpByHostId(SecondaryStorageVm.Role role, long hostId);
+    
+    public List<SecondaryStorageVmVO> listByZoneId(SecondaryStorageVm.Role role, long zoneId);
+    
+    public List<Long> getRunningSecStorageVmListByMsid(SecondaryStorageVm.Role role, long msid);
+    
+    public List<Long> listRunningSecStorageOrderByLoad(SecondaryStorageVm.Role role, long zoneId);
+    SecondaryStorageVmVO findByInstanceName(String instanceName);
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/vm/dao/SecondaryStorageVmDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/vm/dao/SecondaryStorageVmDaoImpl.java b/engine/schema/src/com/cloud/vm/dao/SecondaryStorageVmDaoImpl.java
new file mode 100644
index 0000000..f802a90
--- /dev/null
+++ b/engine/schema/src/com/cloud/vm/dao/SecondaryStorageVmDaoImpl.java
@@ -0,0 +1,269 @@
+// 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.vm.dao;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.ejb.Local;
+
+import org.apache.log4j.Logger;
+import org.springframework.stereotype.Component;
+
+import com.cloud.utils.db.Attribute;
+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.db.UpdateBuilder;
+import com.cloud.vm.SecondaryStorageVm;
+import com.cloud.vm.SecondaryStorageVmVO;
+import com.cloud.vm.VirtualMachine.State;
+
+@Component
+@Local(value={SecondaryStorageVmDao.class})
+public class SecondaryStorageVmDaoImpl extends GenericDaoBase<SecondaryStorageVmVO, Long> implements SecondaryStorageVmDao {
+    private static final Logger s_logger = Logger.getLogger(SecondaryStorageVmDaoImpl.class);
+    
+    protected SearchBuilder<SecondaryStorageVmVO> DataCenterStatusSearch;
+    protected SearchBuilder<SecondaryStorageVmVO> StateSearch;
+    protected SearchBuilder<SecondaryStorageVmVO> HostSearch;
+    protected SearchBuilder<SecondaryStorageVmVO> LastHostSearch;
+    protected SearchBuilder<SecondaryStorageVmVO> HostUpSearch;
+    protected SearchBuilder<SecondaryStorageVmVO> ZoneSearch;
+    protected SearchBuilder<SecondaryStorageVmVO> StateChangeSearch;
+    protected SearchBuilder<SecondaryStorageVmVO> InstanceSearch;
+    
+    protected final Attribute _updateTimeAttr;
+    
+    public SecondaryStorageVmDaoImpl() {
+        DataCenterStatusSearch = createSearchBuilder();
+        DataCenterStatusSearch.and("dc", DataCenterStatusSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
+        DataCenterStatusSearch.and("states", DataCenterStatusSearch.entity().getState(), SearchCriteria.Op.IN);
+        DataCenterStatusSearch.and("role", DataCenterStatusSearch.entity().getRole(), SearchCriteria.Op.EQ);
+        DataCenterStatusSearch.done();
+        
+        StateSearch = createSearchBuilder();
+        StateSearch.and("states", StateSearch.entity().getState(), SearchCriteria.Op.IN);
+        StateSearch.and("role", StateSearch.entity().getRole(), SearchCriteria.Op.EQ);
+        StateSearch.done();
+        
+        HostSearch = createSearchBuilder();
+        HostSearch.and("host", HostSearch.entity().getHostId(), SearchCriteria.Op.EQ);
+        HostSearch.and("role", HostSearch.entity().getRole(), SearchCriteria.Op.EQ);
+        HostSearch.done();
+        
+        InstanceSearch = createSearchBuilder();
+        InstanceSearch.and("instanceName", InstanceSearch.entity().getInstanceName(), SearchCriteria.Op.EQ);
+        InstanceSearch.done();
+        
+        LastHostSearch = createSearchBuilder();
+        LastHostSearch.and("lastHost", LastHostSearch.entity().getLastHostId(), SearchCriteria.Op.EQ);
+        LastHostSearch.and("state", LastHostSearch.entity().getState(), SearchCriteria.Op.EQ);
+        LastHostSearch.and("role", LastHostSearch.entity().getRole(), SearchCriteria.Op.EQ);
+        LastHostSearch.done();
+        
+        HostUpSearch = createSearchBuilder();
+        HostUpSearch.and("host", HostUpSearch.entity().getHostId(), SearchCriteria.Op.EQ);
+        HostUpSearch.and("states", HostUpSearch.entity().getState(), SearchCriteria.Op.NIN);
+        HostUpSearch.and("role", HostUpSearch.entity().getRole(), SearchCriteria.Op.EQ);
+        HostUpSearch.done();
+        
+        ZoneSearch = createSearchBuilder();
+        ZoneSearch.and("zone", ZoneSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
+        ZoneSearch.and("role", ZoneSearch.entity().getRole(), SearchCriteria.Op.EQ);
+        ZoneSearch.done();
+        
+        StateChangeSearch = createSearchBuilder();
+        StateChangeSearch.and("id", StateChangeSearch.entity().getId(), SearchCriteria.Op.EQ);
+        StateChangeSearch.and("states", StateChangeSearch.entity().getState(), SearchCriteria.Op.EQ);
+        StateChangeSearch.and("host", StateChangeSearch.entity().getHostId(), SearchCriteria.Op.EQ);
+        StateChangeSearch.and("update", StateChangeSearch.entity().getUpdated(), SearchCriteria.Op.EQ);
+        StateChangeSearch.and("role", StateChangeSearch.entity().getUpdated(), SearchCriteria.Op.EQ);
+        StateChangeSearch.done();
+        
+        _updateTimeAttr = _allAttributes.get("updateTime");
+        assert _updateTimeAttr != null : "Couldn't get this updateTime attribute";
+    }
+    
+    @Override
+    public boolean remove(Long id) {
+        Transaction txn = Transaction.currentTxn();
+        txn.start();
+        SecondaryStorageVmVO proxy = createForUpdate();
+        proxy.setPublicIpAddress(null);
+        proxy.setPrivateIpAddress(null);
+        
+        UpdateBuilder ub = getUpdateBuilder(proxy);
+        ub.set(proxy, "state", State.Destroyed);
+        ub.set(proxy, "privateIpAddress", null);
+        update(id, ub, proxy);
+        
+        boolean result = super.remove(id);
+        txn.commit();
+        return result;
+    }
+    
+    @Override
+    public List<SecondaryStorageVmVO> getSecStorageVmListInStates(SecondaryStorageVm.Role role, long dataCenterId, State... states) {
+        SearchCriteria<SecondaryStorageVmVO> sc = DataCenterStatusSearch.create();
+        sc.setParameters("states", (Object[])states);
+        sc.setParameters("dc", dataCenterId);
+        if(role != null) {
+            sc.setParameters("role", role);
+        }
+        return listBy(sc);
+    }
+
+    @Override
+    public List<SecondaryStorageVmVO> getSecStorageVmListInStates(SecondaryStorageVm.Role role, State... states) {
+        SearchCriteria<SecondaryStorageVmVO> sc = StateSearch.create();
+        sc.setParameters("states", (Object[])states);
+        if(role != null) {
+            sc.setParameters("role", role);
+        }
+        
+        return listBy(sc);
+    }
+    
+    @Override
+    public List<SecondaryStorageVmVO> listByHostId(SecondaryStorageVm.Role role, long hostId) {
+        SearchCriteria<SecondaryStorageVmVO> sc = HostSearch.create();
+        sc.setParameters("host", hostId);
+        if(role != null) {
+            sc.setParameters("role", role);
+        }
+        return listBy(sc);
+    }
+    
+    @Override
+    public List<SecondaryStorageVmVO> listUpByHostId(SecondaryStorageVm.Role role, long hostId) {
+        SearchCriteria<SecondaryStorageVmVO> sc = HostUpSearch.create();
+        sc.setParameters("host", hostId);
+        sc.setParameters("states", new Object[] {State.Destroyed, State.Stopped, State.Expunging});
+        if(role != null) {
+            sc.setParameters("role", role);
+        }
+        return listBy(sc);
+    }
+    
+    @Override
+    public List<Long> getRunningSecStorageVmListByMsid(SecondaryStorageVm.Role role, long msid) {
+    	List<Long> l = new ArrayList<Long>();
+        Transaction txn = Transaction.currentTxn();;
+        PreparedStatement pstmt = null;
+        try {
+        	String sql;
+        	if(role == null) {
+                sql = "SELECT s.id FROM secondary_storage_vm s, vm_instance v, host h " +
+        			"WHERE s.id=v.id AND v.state='Running' AND v.host_id=h.id AND h.mgmt_server_id=?";
+            } else {
+                sql = "SELECT s.id FROM secondary_storage_vm s, vm_instance v, host h " +
+    				"WHERE s.id=v.id AND v.state='Running' AND s.role=? AND v.host_id=h.id AND h.mgmt_server_id=?";
+            }
+        	
+            pstmt = txn.prepareAutoCloseStatement(sql);
+            
+            if(role == null) {
+            	pstmt.setLong(1, msid);
+            } else {
+            	pstmt.setString(1, role.toString());
+            	pstmt.setLong(2, msid);
+            }
+            
+            ResultSet rs = pstmt.executeQuery();
+            while(rs.next()) {
+            	l.add(rs.getLong(1));
+            }
+        } catch (SQLException e) {
+        } catch (Throwable e) {
+        }
+        return l;
+    }
+    
+
+    @Override
+    public SecondaryStorageVmVO findByInstanceName(String instanceName) {
+        SearchCriteria<SecondaryStorageVmVO> sc = InstanceSearch.create();
+        sc.setParameters("instanceName", instanceName);
+        List<SecondaryStorageVmVO> list = listBy(sc);
+        if( list == null || list.size() == 0 ) {
+            return null;
+        } else {
+            return list.get(0);
+        }
+    }
+
+	@Override
+	public List<SecondaryStorageVmVO> listByZoneId(SecondaryStorageVm.Role role, long zoneId) {
+		SearchCriteria<SecondaryStorageVmVO> sc = ZoneSearch.create();
+        sc.setParameters("zone", zoneId);
+        if(role != null) {
+            sc.setParameters("role", role);
+        }
+        return listBy(sc);
+	}
+
+	@Override
+	public List<SecondaryStorageVmVO> listByLastHostId(SecondaryStorageVm.Role role, long hostId) {
+		SearchCriteria<SecondaryStorageVmVO> sc = LastHostSearch.create();
+		sc.setParameters("lastHost", hostId);
+		sc.setParameters("state", State.Stopped);
+        if(role != null) {
+            sc.setParameters("role", role);
+        }
+		
+		return listBy(sc);
+	}
+	
+	@Override
+    public List<Long> listRunningSecStorageOrderByLoad(SecondaryStorageVm.Role role, long zoneId) {
+    	
+    	List<Long> l = new ArrayList<Long>();
+        Transaction txn = Transaction.currentTxn();;
+        PreparedStatement pstmt = null;
+        try {
+        	String sql;
+        	if(role == null) {
+                sql = "SELECT s.id, count(l.id) as count FROM secondary_storage_vm s INNER JOIN vm_instance v ON s.id=v.id LEFT JOIN cmd_exec_log l ON s.id=l.instance_id WHERE v.state='Running' AND v.data_center_id=? GROUP BY s.id ORDER BY count";
+            } else {
+                sql = "SELECT s.id, count(l.id) as count FROM secondary_storage_vm s INNER JOIN vm_instance v ON s.id=v.id LEFT JOIN cmd_exec_log l ON s.id=l.instance_id WHERE v.state='Running' AND v.data_center_id=? AND s.role=? GROUP BY s.id ORDER BY count";
+            }
+        	
+            pstmt = txn.prepareAutoCloseStatement(sql);
+            
+            if(role == null) {
+            	pstmt.setLong(1, zoneId);
+            } else {
+            	pstmt.setLong(1, zoneId);
+            	pstmt.setString(2, role.toString());
+            }
+            
+            ResultSet rs = pstmt.executeQuery();
+            while(rs.next()) {
+            	l.add(rs.getLong(1));
+            }
+        } catch (SQLException e) {
+        	s_logger.error("Unexpected exception ", e);
+        }
+        
+        return l;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/vm/dao/UserVmCloneSettingDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/vm/dao/UserVmCloneSettingDao.java b/engine/schema/src/com/cloud/vm/dao/UserVmCloneSettingDao.java
new file mode 100644
index 0000000..44a1bf3
--- /dev/null
+++ b/engine/schema/src/com/cloud/vm/dao/UserVmCloneSettingDao.java
@@ -0,0 +1,37 @@
+// 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.vm.dao;
+
+import java.util.List;
+
+import com.cloud.utils.db.GenericDao;
+import com.cloud.vm.UserVmCloneSettingVO;
+
+public interface UserVmCloneSettingDao extends GenericDao<UserVmCloneSettingVO, Long> {
+
+    /*
+     * Returns a User VM clone type record by vm id.
+     */
+    UserVmCloneSettingVO findByVmId(long id);
+
+    /*
+     * Returns a list of VMs by clone type.
+     * cloneType can be full/linked.
+     */
+    List<UserVmCloneSettingVO> listByCloneType(String cloneType);
+
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/vm/dao/UserVmCloneSettingDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/vm/dao/UserVmCloneSettingDaoImpl.java b/engine/schema/src/com/cloud/vm/dao/UserVmCloneSettingDaoImpl.java
new file mode 100644
index 0000000..174f283
--- /dev/null
+++ b/engine/schema/src/com/cloud/vm/dao/UserVmCloneSettingDaoImpl.java
@@ -0,0 +1,74 @@
+// 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.vm.dao;
+
+
+import java.util.List;
+
+import javax.annotation.PostConstruct;
+import javax.ejb.Local;
+
+import org.apache.log4j.Logger;
+import org.springframework.stereotype.Component;
+
+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;
+import com.cloud.vm.UserVmCloneSettingVO;
+import com.cloud.utils.db.DB;
+
+
+@Component
+@Local(value= { UserVmCloneSettingDao.class })
+@DB(txn = false)
+public class UserVmCloneSettingDaoImpl extends GenericDaoBase<UserVmCloneSettingVO, Long> implements UserVmCloneSettingDao {
+    public static final Logger s_logger = Logger.getLogger(UserVmCloneSettingDaoImpl.class);
+
+    protected SearchBuilder<UserVmCloneSettingVO> vmIdSearch;
+    protected SearchBuilder<UserVmCloneSettingVO> cloneTypeSearch;
+
+    public UserVmCloneSettingDaoImpl() {
+    }
+
+    @PostConstruct
+    public void init() {
+        // Initialize the search builders.
+        vmIdSearch = createSearchBuilder();
+        vmIdSearch.and("vmId", vmIdSearch.entity().getCloneType(), Op.EQ);
+        vmIdSearch.done();
+
+        cloneTypeSearch = createSearchBuilder();
+        cloneTypeSearch.and("cloneType", cloneTypeSearch.entity().getCloneType(), Op.EQ);
+        cloneTypeSearch.done();
+    }
+
+    @Override
+    public UserVmCloneSettingVO findByVmId(long vmId) {
+        SearchCriteria<UserVmCloneSettingVO> sc = vmIdSearch.create();
+        sc.setParameters("vmId", vmId);
+        return findOneBy(sc);
+    }
+
+    @Override
+    public List<UserVmCloneSettingVO> listByCloneType(String cloneType) {
+        SearchCriteria<UserVmCloneSettingVO> sc = cloneTypeSearch.create();
+        sc.setParameters("cloneType", cloneType);
+        return search(sc, null);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/vm/dao/UserVmDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/vm/dao/UserVmDao.java b/engine/schema/src/com/cloud/vm/dao/UserVmDao.java
new file mode 100755
index 0000000..81d13cd
--- /dev/null
+++ b/engine/schema/src/com/cloud/vm/dao/UserVmDao.java
@@ -0,0 +1,76 @@
+// 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.vm.dao;
+
+import java.util.Date;
+import java.util.Hashtable;
+import java.util.List;
+
+import com.cloud.utils.db.GenericDao;
+import com.cloud.vm.UserVmVO;
+import com.cloud.vm.VirtualMachine.State;
+
+public interface UserVmDao extends GenericDao<UserVmVO, Long> {
+    List<UserVmVO> listByAccountId(long id);
+
+    List<UserVmVO> listByAccountAndPod(long accountId, long podId);
+    List<UserVmVO> listByAccountAndDataCenter(long accountId, long dcId);
+    List<UserVmVO> listByHostId(Long hostId);
+    List<UserVmVO> listByLastHostId(Long hostId);
+    List<UserVmVO> listUpByHostId(Long hostId);
+
+    /**
+     * Updates display name and group for vm; enables/disables ha
+     * @param id vm id.
+     * @param displan name and enable for ha
+     * @param userData updates the userData of the vm
+     */
+    void updateVM(long id, String displayName, boolean enable, Long osTypeId, String userData);
+    
+    List<UserVmVO> findDestroyedVms(Date date);
+
+	/**
+	 * List running VMs on the specified host
+	 * @param id
+	 * @return
+	 */
+	public List<UserVmVO> listRunningByHostId(long hostId);
+
+	/**
+	 * List user vm instances with virtualized networking (i.e. not direct attached networking) for the given account and datacenter
+	 * @param accountId will search for vm instances belonging to this account
+	 * @param dcId will search for vm instances in this zone
+	 * @return the list of vm instances owned by the account in the given data center that have virtualized networking (not direct attached networking)
+	 */
+	List<UserVmVO> listVirtualNetworkInstancesByAcctAndZone(long accountId, long dcId, long networkId);
+	
+	List<UserVmVO> listByNetworkIdAndStates(long networkId, State... states);
+	
+	List<UserVmVO> listByAccountIdAndHostId(long accountId, long hostId);
+	
+	void loadDetails(UserVmVO vm);
+
+	void saveDetails(UserVmVO vm);
+	
+	List<Long> listPodIdsHavingVmsforAccount(long zoneId, long accountId);	
+    public Long countAllocatedVMsForAccount(long accountId);
+
+    Hashtable<Long, UserVmData> listVmDetails(Hashtable<Long, UserVmData> userVmData);
+
+    List<UserVmVO> listByIsoId(Long isoId);
+   
+}


Mime
View raw message