/** * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. * * This software is licensed under the GNU General Public License v3 or later. * * It is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ 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 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.SecondaryStorageVmVO; import com.cloud.vm.VirtualMachine.State; @Local(value={SecondaryStorageVmDao.class}) public class SecondaryStorageVmDaoImpl extends GenericDaoBase implements SecondaryStorageVmDao { private static final Logger s_logger = Logger.getLogger(SecondaryStorageVmDaoImpl.class); protected SearchBuilder DataCenterStatusSearch; protected SearchBuilder StateSearch; protected SearchBuilder HostSearch; protected SearchBuilder LastHostSearch; protected SearchBuilder HostUpSearch; protected SearchBuilder ZoneSearch; protected SearchBuilder StateChangeSearch; 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.done(); StateSearch = createSearchBuilder(); StateSearch.and("states", StateSearch.entity().getState(), SearchCriteria.Op.IN); StateSearch.done(); HostSearch = createSearchBuilder(); HostSearch.and("host", HostSearch.entity().getHostId(), SearchCriteria.Op.EQ); HostSearch.done(); LastHostSearch = createSearchBuilder(); LastHostSearch.and("lastHost", LastHostSearch.entity().getLastHostId(), SearchCriteria.Op.EQ); LastHostSearch.and("state", LastHostSearch.entity().getState(), 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.done(); ZoneSearch = createSearchBuilder(); ZoneSearch.and("zone", ZoneSearch.entity().getDataCenterId(), 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.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); boolean result = super.remove(id); txn.commit(); return result; } @Override public List getSecStorageVmListInStates(long dataCenterId, State... states) { SearchCriteria sc = DataCenterStatusSearch.create(); sc.setParameters("states", (Object[])states); sc.setParameters("dc", dataCenterId); return listBy(sc); } @Override public List getSecStorageVmListInStates(State... states) { SearchCriteria sc = StateSearch.create(); sc.setParameters("states", (Object[])states); return listBy(sc); } @Override public List listByHostId(long hostId) { SearchCriteria sc = HostSearch.create(); sc.setParameters("host", hostId); return listBy(sc); } @Override public List listUpByHostId(long hostId) { SearchCriteria sc = HostUpSearch.create(); sc.setParameters("host", hostId); sc.setParameters("states", new Object[] {State.Destroyed, State.Stopped, State.Expunging}); return listBy(sc); } @Override public List getRunningSecStorageVmListByMsid(long msid) { List l = new ArrayList(); Transaction txn = Transaction.currentTxn();; PreparedStatement pstmt = null; try { pstmt = txn.prepareAutoCloseStatement( "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=?"); pstmt.setLong(1, msid); ResultSet rs = pstmt.executeQuery(); while(rs.next()) { l.add(rs.getLong(1)); } } catch (SQLException e) { } catch (Throwable e) { } return l; } @Override public List listByZoneId(long zoneId) { SearchCriteria sc = ZoneSearch.create(); sc.setParameters("zone", zoneId); return listBy(sc); } @Override public List listByLastHostId(long hostId) { SearchCriteria sc = LastHostSearch.create(); sc.setParameters("lastHost", hostId); sc.setParameters("state", State.Stopped); return listBy(sc); } }