You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by al...@apache.org on 2013/05/08 00:01:51 UTC
[35/72] [abbrv] [partial] Moved most of the VOs and DAOs from server
package into engine-schema as well
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d015fb35/engine/schema/src/com/cloud/storage/dao/SnapshotScheduleDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/storage/dao/SnapshotScheduleDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/SnapshotScheduleDaoImpl.java
new file mode 100644
index 0000000..c01644e
--- /dev/null
+++ b/engine/schema/src/com/cloud/storage/dao/SnapshotScheduleDaoImpl.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.storage.dao;
+
+import java.util.Date;
+import java.util.List;
+
+import javax.ejb.Local;
+
+import org.springframework.stereotype.Component;
+
+import com.cloud.storage.Snapshot;
+import com.cloud.storage.SnapshotScheduleVO;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+
+@Component
+@Local (value={SnapshotScheduleDao.class})
+public class SnapshotScheduleDaoImpl extends GenericDaoBase<SnapshotScheduleVO, Long> implements SnapshotScheduleDao {
+ protected final SearchBuilder<SnapshotScheduleVO> executableSchedulesSearch;
+ protected final SearchBuilder<SnapshotScheduleVO> coincidingSchedulesSearch;
+ private final SearchBuilder<SnapshotScheduleVO> VolumeIdSearch;
+ private final SearchBuilder<SnapshotScheduleVO> VolumeIdPolicyIdSearch;
+
+
+ protected SnapshotScheduleDaoImpl() {
+
+ executableSchedulesSearch = createSearchBuilder();
+ executableSchedulesSearch.and("scheduledTimestamp", executableSchedulesSearch.entity().getScheduledTimestamp(), SearchCriteria.Op.LT);
+ executableSchedulesSearch.and("asyncJobId", executableSchedulesSearch.entity().getAsyncJobId(), SearchCriteria.Op.NULL);
+ executableSchedulesSearch.done();
+
+ coincidingSchedulesSearch = createSearchBuilder();
+ coincidingSchedulesSearch.and("volumeId", coincidingSchedulesSearch.entity().getVolumeId(), SearchCriteria.Op.EQ);
+ coincidingSchedulesSearch.and("scheduledTimestamp", coincidingSchedulesSearch.entity().getScheduledTimestamp(), SearchCriteria.Op.LT);
+ coincidingSchedulesSearch.and("asyncJobId", coincidingSchedulesSearch.entity().getAsyncJobId(), SearchCriteria.Op.NULL);
+ coincidingSchedulesSearch.done();
+
+ VolumeIdSearch = createSearchBuilder();
+ VolumeIdSearch.and("volumeId", VolumeIdSearch.entity().getVolumeId(), SearchCriteria.Op.EQ);
+ VolumeIdSearch.done();
+
+ VolumeIdPolicyIdSearch = createSearchBuilder();
+ VolumeIdPolicyIdSearch.and("volumeId", VolumeIdPolicyIdSearch.entity().getVolumeId(), SearchCriteria.Op.EQ);
+ VolumeIdPolicyIdSearch.and("policyId", VolumeIdPolicyIdSearch.entity().getPolicyId(), SearchCriteria.Op.EQ);
+ VolumeIdPolicyIdSearch.done();
+
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public List<SnapshotScheduleVO> getCoincidingSnapshotSchedules(long volumeId, Date date) {
+ SearchCriteria<SnapshotScheduleVO> sc = coincidingSchedulesSearch.create();
+ sc.setParameters("volumeId", volumeId);
+ sc.setParameters("scheduledTimestamp", date);
+ // Don't return manual snapshots. They will be executed through another code path.
+ sc.addAnd("policyId", SearchCriteria.Op.NEQ, 1L);
+ return listBy(sc);
+ }
+
+
+ @Override
+ public SnapshotScheduleVO findOneByVolume(long volumeId) {
+ SearchCriteria<SnapshotScheduleVO> sc = VolumeIdSearch.create();
+ sc.setParameters("volumeId", volumeId);
+ return findOneBy(sc);
+ }
+
+
+ @Override
+ public SnapshotScheduleVO findOneByVolumePolicy(long volumeId, long policyId) {
+ SearchCriteria<SnapshotScheduleVO> sc = VolumeIdPolicyIdSearch.create();
+ sc.setParameters("volumeId", volumeId);
+ sc.setParameters("policyId", policyId);
+ return findOneBy(sc);
+ }
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public List<SnapshotScheduleVO> getSchedulesToExecute(Date currentTimestamp) {
+ SearchCriteria<SnapshotScheduleVO> sc = executableSchedulesSearch.create();
+ sc.setParameters("scheduledTimestamp", currentTimestamp);
+ return listBy(sc);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public SnapshotScheduleVO getCurrentSchedule(Long volumeId, Long policyId, boolean executing) {
+ assert volumeId != null;
+ SearchCriteria<SnapshotScheduleVO> sc = createSearchCriteria();
+ SearchCriteria.Op op = executing ? SearchCriteria.Op.NNULL : SearchCriteria.Op.NULL;
+ sc.addAnd("volumeId", SearchCriteria.Op.EQ, volumeId);
+ if (policyId != null) {
+ sc.addAnd("policyId", SearchCriteria.Op.EQ, policyId);
+ if (policyId != Snapshot.MANUAL_POLICY_ID) {
+ // manual policies aren't scheduled by the snapshot poller, so don't look for the jobId here
+ sc.addAnd("asyncJobId", op);
+ }
+ } else {
+ sc.addAnd("asyncJobId", op);
+ }
+
+ List<SnapshotScheduleVO> snapshotSchedules = listBy(sc);
+ // This will return only one schedule because of a DB uniqueness constraint.
+ assert (snapshotSchedules.size() <= 1);
+ if (snapshotSchedules.isEmpty()) {
+ return null;
+ }
+ else {
+ return snapshotSchedules.get(0);
+ }
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d015fb35/engine/schema/src/com/cloud/storage/dao/StoragePoolDetailsDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/storage/dao/StoragePoolDetailsDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/StoragePoolDetailsDaoImpl.java
new file mode 100644
index 0000000..38b5253
--- /dev/null
+++ b/engine/schema/src/com/cloud/storage/dao/StoragePoolDetailsDaoImpl.java
@@ -0,0 +1,85 @@
+// 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.storage.dao;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.ejb.Local;
+
+import org.apache.cloudstack.storage.datastore.db.StoragePoolDetailVO;
+import org.apache.cloudstack.storage.datastore.db.StoragePoolDetailsDao;
+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.Transaction;
+
+@Component
+@Local(value=StoragePoolDetailsDao.class)
+public class StoragePoolDetailsDaoImpl extends GenericDaoBase<StoragePoolDetailVO, Long> implements StoragePoolDetailsDao {
+
+ protected final SearchBuilder<StoragePoolDetailVO> PoolSearch;
+
+ protected StoragePoolDetailsDaoImpl() {
+ super();
+ PoolSearch = createSearchBuilder();
+ PoolSearch.and("pool", PoolSearch.entity().getPoolId(), SearchCriteria.Op.EQ);
+ PoolSearch.and("name", PoolSearch.entity().getName(), SearchCriteria.Op.EQ);
+ PoolSearch.done();
+ }
+
+ @Override
+ public void update(long poolId, Map<String, String> details) {
+ Transaction txn = Transaction.currentTxn();
+ SearchCriteria<StoragePoolDetailVO> sc = PoolSearch.create();
+ sc.setParameters("pool", poolId);
+
+ txn.start();
+ expunge(sc);
+ for (Map.Entry<String, String> entry : details.entrySet()) {
+ StoragePoolDetailVO detail = new StoragePoolDetailVO(poolId, entry.getKey(), entry.getValue());
+ persist(detail);
+ }
+ txn.commit();
+ }
+
+ @Override
+ public Map<String, String> getDetails(long poolId) {
+ SearchCriteria<StoragePoolDetailVO> sc = PoolSearch.create();
+ sc.setParameters("pool", poolId);
+
+ List<StoragePoolDetailVO> details = listBy(sc);
+ Map<String, String> detailsMap = new HashMap<String, String>();
+ for (StoragePoolDetailVO detail : details) {
+ detailsMap.put(detail.getName(), detail.getValue());
+ }
+
+ return detailsMap;
+ }
+
+ @Override
+ public StoragePoolDetailVO findDetail(long poolId, String name) {
+ SearchCriteria<StoragePoolDetailVO> sc = PoolSearch.create();
+ sc.setParameters("pool", poolId);
+ sc.setParameters("name", name);
+
+ return findOneIncludingRemovedBy(sc);
+ }
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d015fb35/engine/schema/src/com/cloud/storage/dao/StoragePoolHostDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/storage/dao/StoragePoolHostDao.java b/engine/schema/src/com/cloud/storage/dao/StoragePoolHostDao.java
new file mode 100644
index 0000000..8dd10a7
--- /dev/null
+++ b/engine/schema/src/com/cloud/storage/dao/StoragePoolHostDao.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.storage.dao;
+
+import java.util.List;
+
+import com.cloud.host.Status;
+import com.cloud.storage.StoragePoolHostVO;
+import com.cloud.utils.Pair;
+import com.cloud.utils.db.GenericDao;
+
+public interface StoragePoolHostDao extends GenericDao<StoragePoolHostVO, Long> {
+ public List<StoragePoolHostVO> listByPoolId(long id);
+
+ public List<StoragePoolHostVO> listByHostIdIncludingRemoved(long hostId);
+
+ public StoragePoolHostVO findByPoolHost(long poolId, long hostId);
+
+ List<StoragePoolHostVO> listByHostStatus(long poolId, Status hostStatus);
+
+ List<Pair<Long, Integer>> getDatacenterStoragePoolHostInfo(long dcId, boolean sharedOnly);
+
+ public void deletePrimaryRecordsForHost(long hostId);
+
+ public void deleteStoragePoolHostDetails(long hostId, long poolId);
+
+ List<StoragePoolHostVO> listByHostId(long hostId);
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d015fb35/engine/schema/src/com/cloud/storage/dao/StoragePoolHostDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/storage/dao/StoragePoolHostDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/StoragePoolHostDaoImpl.java
new file mode 100644
index 0000000..4f509d1
--- /dev/null
+++ b/engine/schema/src/com/cloud/storage/dao/StoragePoolHostDaoImpl.java
@@ -0,0 +1,184 @@
+// 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.storage.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.host.Status;
+import com.cloud.storage.StoragePoolHostVO;
+import com.cloud.utils.Pair;
+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 = { StoragePoolHostDao.class })
+public class StoragePoolHostDaoImpl extends GenericDaoBase<StoragePoolHostVO, Long> implements StoragePoolHostDao {
+ public static final Logger s_logger = Logger.getLogger(StoragePoolHostDaoImpl.class.getName());
+
+ protected final SearchBuilder<StoragePoolHostVO> PoolSearch;
+ protected final SearchBuilder<StoragePoolHostVO> HostSearch;
+ protected final SearchBuilder<StoragePoolHostVO> PoolHostSearch;
+
+ protected static final String HOST_FOR_POOL_SEARCH = "SELECT * FROM storage_pool_host_ref ph, host h where ph.host_id = h.id and ph.pool_id=? and h.status=? ";
+
+ protected static final String STORAGE_POOL_HOST_INFO = "SELECT p.data_center_id, count(ph.host_id) " + " FROM storage_pool p, storage_pool_host_ref ph "
+ + " WHERE p.id = ph.pool_id AND p.data_center_id = ? " + " GROUP by p.data_center_id";
+
+ protected static final String SHARED_STORAGE_POOL_HOST_INFO = "SELECT p.data_center_id, count(ph.host_id) " + " FROM storage_pool p, storage_pool_host_ref ph "
+ + " WHERE p.id = ph.pool_id AND p.data_center_id = ? " + " AND p.pool_type NOT IN ('LVM', 'Filesystem')" + " GROUP by p.data_center_id";
+
+ protected static final String DELETE_PRIMARY_RECORDS = "DELETE " + "FROM storage_pool_host_ref " + "WHERE host_id = ?";
+
+ public StoragePoolHostDaoImpl() {
+ PoolSearch = createSearchBuilder();
+ PoolSearch.and("pool_id", PoolSearch.entity().getPoolId(), SearchCriteria.Op.EQ);
+ PoolSearch.done();
+
+ HostSearch = createSearchBuilder();
+ HostSearch.and("host_id", HostSearch.entity().getHostId(), SearchCriteria.Op.EQ);
+ HostSearch.done();
+
+ PoolHostSearch = createSearchBuilder();
+ PoolHostSearch.and("pool_id", PoolHostSearch.entity().getPoolId(), SearchCriteria.Op.EQ);
+ PoolHostSearch.and("host_id", PoolHostSearch.entity().getHostId(), SearchCriteria.Op.EQ);
+ PoolHostSearch.done();
+
+ }
+
+ @Override
+ public List<StoragePoolHostVO> listByPoolId(long id) {
+ SearchCriteria<StoragePoolHostVO> sc = PoolSearch.create();
+ sc.setParameters("pool_id", id);
+ return listIncludingRemovedBy(sc);
+ }
+
+ @Override
+ public List<StoragePoolHostVO> listByHostIdIncludingRemoved(long hostId) {
+ SearchCriteria<StoragePoolHostVO> sc = HostSearch.create();
+ sc.setParameters("host_id", hostId);
+ return listIncludingRemovedBy(sc);
+ }
+
+ @Override
+ public List<StoragePoolHostVO> listByHostId(long hostId) {
+ SearchCriteria<StoragePoolHostVO> sc = HostSearch.create();
+ sc.setParameters("host_id", hostId);
+ return listBy(sc);
+ }
+
+ @Override
+ public StoragePoolHostVO findByPoolHost(long poolId, long hostId) {
+ SearchCriteria<StoragePoolHostVO> sc = PoolHostSearch.create();
+ sc.setParameters("pool_id", poolId);
+ sc.setParameters("host_id", hostId);
+ return findOneIncludingRemovedBy(sc);
+ }
+
+ @Override
+ public List<StoragePoolHostVO> listByHostStatus(long poolId, Status hostStatus) {
+ Transaction txn = Transaction.currentTxn();
+ PreparedStatement pstmt = null;
+ List<StoragePoolHostVO> result = new ArrayList<StoragePoolHostVO>();
+ ResultSet rs = null;
+ try {
+ String sql = HOST_FOR_POOL_SEARCH;
+ pstmt = txn.prepareStatement(sql);
+
+ pstmt.setLong(1, poolId);
+ pstmt.setString(2, hostStatus.toString());
+ rs = pstmt.executeQuery();
+ while (rs.next()) {
+ // result.add(toEntityBean(rs, false)); TODO: this is buggy in GenericDaoBase for hand constructed queries
+ long id = rs.getLong(1); // ID column
+ result.add(findById(id));
+ }
+ } catch (Exception e) {
+ s_logger.warn("Exception: ", e);
+ } finally {
+ try {
+ if (rs != null) {
+ rs.close();
+ }
+ if (pstmt != null) {
+ pstmt.close();
+ }
+ } catch (SQLException e) {
+ }
+ }
+ return result;
+
+ }
+
+ @Override
+ public List<Pair<Long, Integer>> getDatacenterStoragePoolHostInfo(long dcId, boolean sharedOnly) {
+ ArrayList<Pair<Long, Integer>> l = new ArrayList<Pair<Long, Integer>>();
+ String sql = sharedOnly ? SHARED_STORAGE_POOL_HOST_INFO : STORAGE_POOL_HOST_INFO;
+ Transaction txn = Transaction.currentTxn();
+ ;
+ PreparedStatement pstmt = null;
+ try {
+ pstmt = txn.prepareAutoCloseStatement(sql);
+ pstmt.setLong(1, dcId);
+
+ ResultSet rs = pstmt.executeQuery();
+ while (rs.next()) {
+ l.add(new Pair<Long, Integer>(rs.getLong(1), rs.getInt(2)));
+ }
+ } catch (SQLException e) {
+ } catch (Throwable e) {
+ }
+ return l;
+ }
+
+ /**
+ * This method deletes the primary records from the host
+ *
+ * @param hostId
+ * -- id of the host
+ */
+ @Override
+ public void deletePrimaryRecordsForHost(long hostId) {
+ SearchCriteria<StoragePoolHostVO> sc = HostSearch.create();
+ sc.setParameters("host_id", hostId);
+ Transaction txn = Transaction.currentTxn();
+ txn.start();
+ remove(sc);
+ txn.commit();
+ }
+
+ @Override
+ public void deleteStoragePoolHostDetails(long hostId, long poolId) {
+ SearchCriteria<StoragePoolHostVO> sc = PoolHostSearch.create();
+ sc.setParameters("host_id", hostId);
+ sc.setParameters("pool_id", poolId);
+ Transaction txn = Transaction.currentTxn();
+ txn.start();
+ remove(sc);
+ txn.commit();
+ }
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d015fb35/engine/schema/src/com/cloud/storage/dao/StoragePoolWorkDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/storage/dao/StoragePoolWorkDao.java b/engine/schema/src/com/cloud/storage/dao/StoragePoolWorkDao.java
new file mode 100644
index 0000000..c05abf5
--- /dev/null
+++ b/engine/schema/src/com/cloud/storage/dao/StoragePoolWorkDao.java
@@ -0,0 +1,38 @@
+// 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.storage.dao;
+
+import java.util.List;
+
+import com.cloud.storage.StoragePoolWorkVO;
+import com.cloud.utils.db.GenericDao;
+/**
+ * Data Access Object for storage_pool table
+ */
+public interface StoragePoolWorkDao extends GenericDao<StoragePoolWorkVO, Long> {
+
+ List<StoragePoolWorkVO> listPendingWorkForPrepareForMaintenanceByPoolId(long poolId);
+
+ List<StoragePoolWorkVO> listPendingWorkForCancelMaintenanceByPoolId(long poolId);
+
+ StoragePoolWorkVO findByPoolIdAndVmId(long poolId, long vmId);
+
+ void removePendingJobsOnMsRestart(long msId, long poolId);
+
+ List<Long> searchForPoolIdsForPendingWorkJobs(long msId);
+
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d015fb35/engine/schema/src/com/cloud/storage/dao/StoragePoolWorkDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/storage/dao/StoragePoolWorkDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/StoragePoolWorkDaoImpl.java
new file mode 100644
index 0000000..360a814
--- /dev/null
+++ b/engine/schema/src/com/cloud/storage/dao/StoragePoolWorkDaoImpl.java
@@ -0,0 +1,136 @@
+// 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.storage.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.springframework.stereotype.Component;
+
+import com.cloud.storage.StoragePoolWorkVO;
+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;
+import com.cloud.utils.exception.CloudRuntimeException;
+
+@Component
+@Local(value={StoragePoolWorkDao.class}) @DB(txn=false)
+public class StoragePoolWorkDaoImpl extends GenericDaoBase<StoragePoolWorkVO, Long> implements StoragePoolWorkDao {
+
+ protected final SearchBuilder<StoragePoolWorkVO> PendingWorkForPrepareForMaintenanceSearch;
+ protected final SearchBuilder<StoragePoolWorkVO> PendingWorkForCancelMaintenanceSearch;
+ protected final SearchBuilder<StoragePoolWorkVO> PoolAndVmIdSearch;
+ protected final SearchBuilder<StoragePoolWorkVO> PendingJobsForDeadMs;
+
+ private final String FindPoolIds = "SELECT distinct storage_pool_work.pool_id FROM storage_pool_work WHERE mgmt_server_id = ?";
+
+ protected StoragePoolWorkDaoImpl() {
+ PendingWorkForPrepareForMaintenanceSearch = createSearchBuilder();
+ PendingWorkForPrepareForMaintenanceSearch.and("poolId", PendingWorkForPrepareForMaintenanceSearch.entity().getPoolId(), SearchCriteria.Op.EQ);
+ PendingWorkForPrepareForMaintenanceSearch.and("stoppedForMaintenance", PendingWorkForPrepareForMaintenanceSearch.entity().isStoppedForMaintenance(), SearchCriteria.Op.EQ);
+ PendingWorkForPrepareForMaintenanceSearch.and("startedAfterMaintenance", PendingWorkForPrepareForMaintenanceSearch.entity().isStartedAfterMaintenance(), SearchCriteria.Op.EQ);
+ PendingWorkForPrepareForMaintenanceSearch.done();
+
+ PendingWorkForCancelMaintenanceSearch = createSearchBuilder();
+ PendingWorkForCancelMaintenanceSearch.and("poolId", PendingWorkForCancelMaintenanceSearch.entity().getPoolId(), SearchCriteria.Op.EQ);
+ PendingWorkForCancelMaintenanceSearch.and("stoppedForMaintenance", PendingWorkForCancelMaintenanceSearch.entity().isStoppedForMaintenance(), SearchCriteria.Op.EQ);
+ PendingWorkForCancelMaintenanceSearch.and("startedAfterMaintenance", PendingWorkForCancelMaintenanceSearch.entity().isStartedAfterMaintenance(), SearchCriteria.Op.EQ);
+ PendingWorkForCancelMaintenanceSearch.done();
+
+ PoolAndVmIdSearch = createSearchBuilder();
+ PoolAndVmIdSearch.and("poolId", PoolAndVmIdSearch.entity().getPoolId(), SearchCriteria.Op.EQ);
+ PoolAndVmIdSearch.and("vmId", PoolAndVmIdSearch.entity().getVmId(), SearchCriteria.Op.EQ);
+ PoolAndVmIdSearch.done();
+
+ PendingJobsForDeadMs = createSearchBuilder();
+ PendingJobsForDeadMs.and("managementServerId", PendingJobsForDeadMs.entity().getManagementServerId(), SearchCriteria.Op.EQ);
+ PendingJobsForDeadMs.and("poolId", PendingJobsForDeadMs.entity().getPoolId(), SearchCriteria.Op.EQ);
+ PendingJobsForDeadMs.and("stoppedForMaintenance", PendingJobsForDeadMs.entity().isStoppedForMaintenance(), SearchCriteria.Op.EQ);
+ PendingJobsForDeadMs.and("startedAfterMaintenance", PendingJobsForDeadMs.entity().isStartedAfterMaintenance(), SearchCriteria.Op.EQ);
+ PendingJobsForDeadMs.done();
+
+ }
+
+ @Override
+ public List<StoragePoolWorkVO> listPendingWorkForPrepareForMaintenanceByPoolId(long poolId) {
+ SearchCriteria<StoragePoolWorkVO> sc = PendingWorkForPrepareForMaintenanceSearch.create();
+ sc.setParameters("poolId", poolId);
+ sc.setParameters("stoppedForMaintenance", false);
+ sc.setParameters("startedAfterMaintenance", false);
+ return listBy(sc);
+ }
+
+ @Override
+ public List<StoragePoolWorkVO> listPendingWorkForCancelMaintenanceByPoolId(long poolId) {
+ SearchCriteria<StoragePoolWorkVO> sc = PendingWorkForCancelMaintenanceSearch.create();
+ sc.setParameters("poolId", poolId);
+ sc.setParameters("stoppedForMaintenance", true);
+ sc.setParameters("startedAfterMaintenance", false);
+ return listBy(sc);
+ }
+
+ @Override
+ public StoragePoolWorkVO findByPoolIdAndVmId(long poolId, long vmId) {
+ SearchCriteria<StoragePoolWorkVO> sc = PoolAndVmIdSearch.create();
+ sc.setParameters("poolId", poolId);
+ sc.setParameters("vmId", vmId);
+ return listBy(sc).get(0);
+ }
+
+ @Override
+ public void removePendingJobsOnMsRestart(long msId, long poolId) {
+ //hung jobs are those which are stopped, but never started
+ SearchCriteria<StoragePoolWorkVO> sc = PendingJobsForDeadMs.create();
+ sc.setParameters("managementServerId", msId);
+ sc.setParameters("poolId", poolId);
+ sc.setParameters("stoppedForMaintenance", true);
+ sc.setParameters("startedAfterMaintenance", false);
+ remove(sc);
+ }
+
+ @Override
+ @DB
+ public List<Long> searchForPoolIdsForPendingWorkJobs(long msId){
+
+ StringBuilder sql = new StringBuilder(FindPoolIds);
+
+ Transaction txn = Transaction.currentTxn();
+ PreparedStatement pstmt = null;
+ try {
+ pstmt = txn.prepareAutoCloseStatement(sql.toString());
+ pstmt.setLong(1, msId);
+
+ ResultSet rs = pstmt.executeQuery();
+ List<Long> poolIds = new ArrayList<Long>();
+
+ while (rs.next()) {
+ poolIds.add(rs.getLong("pool_id"));
+ }
+ return poolIds;
+ } catch (SQLException e) {
+ throw new CloudRuntimeException("Unable to execute " + pstmt.toString(), e);
+ }
+
+ }
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d015fb35/engine/schema/src/com/cloud/storage/dao/SwiftDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/storage/dao/SwiftDao.java b/engine/schema/src/com/cloud/storage/dao/SwiftDao.java
new file mode 100644
index 0000000..72c5219
--- /dev/null
+++ b/engine/schema/src/com/cloud/storage/dao/SwiftDao.java
@@ -0,0 +1,31 @@
+// 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.storage.dao;
+
+import com.cloud.agent.api.to.SwiftTO;
+import com.cloud.storage.SwiftVO;
+import com.cloud.utils.db.GenericDao;
+
+/**
+ *
+ *
+ */
+
+public interface SwiftDao extends GenericDao<SwiftVO, Long> {
+
+ SwiftTO getSwiftTO(Long swiftId);
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d015fb35/engine/schema/src/com/cloud/storage/dao/SwiftDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/storage/dao/SwiftDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/SwiftDaoImpl.java
new file mode 100644
index 0000000..19e5a85
--- /dev/null
+++ b/engine/schema/src/com/cloud/storage/dao/SwiftDaoImpl.java
@@ -0,0 +1,58 @@
+// 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.storage.dao;
+
+import java.util.Collections;
+import java.util.List;
+
+import javax.ejb.Local;
+
+import org.apache.log4j.Logger;
+import org.springframework.stereotype.Component;
+
+import com.cloud.agent.api.to.SwiftTO;
+import com.cloud.storage.SwiftVO;
+import com.cloud.utils.db.GenericDaoBase;
+
+/**
+ *
+ *
+ */
+@Component
+@Local (value={SwiftDao.class})
+public class SwiftDaoImpl extends GenericDaoBase<SwiftVO, Long> implements SwiftDao {
+ public static final Logger s_logger = Logger.getLogger(SwiftDaoImpl.class.getName());
+
+ @Override
+ public SwiftTO getSwiftTO(Long swiftId) {
+ if (swiftId != null) {
+ SwiftVO swift = findById(swiftId);
+ if (swift != null) {
+ return swift.toSwiftTO();
+ }
+ return null;
+ }
+
+ List<SwiftVO> swiftVOs = listAll();
+ if (swiftVOs == null || swiftVOs.size() < 1) {
+ return null;
+ } else {
+ Collections.shuffle(swiftVOs);
+ return swiftVOs.get(0).toSwiftTO();
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d015fb35/engine/schema/src/com/cloud/storage/dao/UploadDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/storage/dao/UploadDao.java b/engine/schema/src/com/cloud/storage/dao/UploadDao.java
new file mode 100755
index 0000000..06336ae
--- /dev/null
+++ b/engine/schema/src/com/cloud/storage/dao/UploadDao.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.storage.dao;
+
+import java.util.List;
+
+import com.cloud.storage.UploadVO;
+import com.cloud.storage.Upload.Status;
+import com.cloud.storage.Upload.Type;
+import com.cloud.storage.Upload.Mode;
+import com.cloud.utils.db.GenericDao;
+
+public interface UploadDao extends GenericDao<UploadVO, Long> {
+
+ List<UploadVO> listByTypeUploadStatus(long typeId, Type type,
+ Status uploadState);
+
+ List<UploadVO> listByHostAndUploadStatus(long sserverId, Status uploadInProgress);
+
+ List<UploadVO> listByModeAndStatus(Mode mode, Status uploadState);
+
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d015fb35/engine/schema/src/com/cloud/storage/dao/UploadDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/storage/dao/UploadDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/UploadDaoImpl.java
new file mode 100755
index 0000000..31fad43
--- /dev/null
+++ b/engine/schema/src/com/cloud/storage/dao/UploadDaoImpl.java
@@ -0,0 +1,92 @@
+// 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.storage.dao;
+import java.util.List;
+import javax.ejb.Local;
+
+import org.apache.log4j.Logger;
+import org.springframework.stereotype.Component;
+
+import com.cloud.storage.UploadVO;
+import com.cloud.storage.Upload.Status;
+import com.cloud.storage.Upload.Mode;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+
+@Component
+@Local(value={UploadDao.class})
+public class UploadDaoImpl extends GenericDaoBase<UploadVO, Long> implements UploadDao {
+ public static final Logger s_logger = Logger.getLogger(UploadDaoImpl.class.getName());
+ protected final SearchBuilder<UploadVO> typeUploadStatusSearch;
+ protected final SearchBuilder<UploadVO> typeHostAndUploadStatusSearch;
+ protected final SearchBuilder<UploadVO> typeModeAndStatusSearch;
+
+ protected static final String UPDATE_UPLOAD_INFO =
+ "UPDATE upload SET upload_state = ?, upload_pct= ?, last_updated = ? "
+ + ", upload_error_str = ?, upload_job_id = ? "
+ + "WHERE host_id = ? and type_id = ? and type = ?";
+
+ protected static final String UPLOADS_STATE_DC=
+ "SELECT * FROM upload t, host h where t.host_id = h.id and h.data_center_id=? "
+ + " and t.type_id=? and t.upload_state = ?" ;
+
+
+ public UploadDaoImpl() {
+ typeUploadStatusSearch = createSearchBuilder();
+ typeUploadStatusSearch.and("type_id", typeUploadStatusSearch.entity().getTypeId(), SearchCriteria.Op.EQ);
+ typeUploadStatusSearch.and("upload_state", typeUploadStatusSearch.entity().getUploadState(), SearchCriteria.Op.EQ);
+ typeUploadStatusSearch.and("type", typeUploadStatusSearch.entity().getType(), SearchCriteria.Op.EQ);
+ typeUploadStatusSearch.done();
+
+ typeHostAndUploadStatusSearch = createSearchBuilder();
+ typeHostAndUploadStatusSearch.and("host_id", typeHostAndUploadStatusSearch.entity().getHostId(), SearchCriteria.Op.EQ);
+ typeHostAndUploadStatusSearch.and("upload_state", typeHostAndUploadStatusSearch.entity().getUploadState(), SearchCriteria.Op.EQ);
+ typeHostAndUploadStatusSearch.done();
+
+ typeModeAndStatusSearch = createSearchBuilder();
+ typeModeAndStatusSearch.and("mode", typeModeAndStatusSearch.entity().getMode(), SearchCriteria.Op.EQ);
+ typeModeAndStatusSearch.and("upload_state", typeModeAndStatusSearch.entity().getUploadState(), SearchCriteria.Op.EQ);
+ typeModeAndStatusSearch.done();
+
+ }
+
+ @Override
+ public List<UploadVO> listByTypeUploadStatus(long typeId, UploadVO.Type type, UploadVO.Status uploadState) {
+ SearchCriteria<UploadVO> sc = typeUploadStatusSearch.create();
+ sc.setParameters("type_id", typeId);
+ sc.setParameters("type", type);
+ sc.setParameters("upload_state", uploadState.toString());
+ return listBy(sc);
+ }
+
+ @Override
+ public List<UploadVO> listByHostAndUploadStatus(long sserverId, Status uploadState){
+ SearchCriteria<UploadVO> sc = typeHostAndUploadStatusSearch.create();
+ sc.setParameters("host_id", sserverId);
+ sc.setParameters("upload_state", uploadState.toString());
+ return listBy(sc);
+ }
+
+ @Override
+ public List<UploadVO> listByModeAndStatus(Mode mode, Status uploadState){
+ SearchCriteria<UploadVO> sc = typeModeAndStatusSearch.create();
+ sc.setParameters("mode", mode.toString());
+ sc.setParameters("upload_state", uploadState.toString());
+ return listBy(sc);
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d015fb35/engine/schema/src/com/cloud/storage/dao/VMTemplateDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/storage/dao/VMTemplateDao.java b/engine/schema/src/com/cloud/storage/dao/VMTemplateDao.java
new file mode 100755
index 0000000..8520757
--- /dev/null
+++ b/engine/schema/src/com/cloud/storage/dao/VMTemplateDao.java
@@ -0,0 +1,84 @@
+// 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.storage.dao;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.cloudstack.engine.subsystem.api.storage.TemplateEvent;
+import org.apache.cloudstack.engine.subsystem.api.storage.TemplateState;
+
+import com.cloud.domain.DomainVO;
+import com.cloud.hypervisor.Hypervisor.HypervisorType;
+import com.cloud.projects.Project.ListProjectResourcesCriteria;
+import com.cloud.storage.VMTemplateVO;
+import com.cloud.template.VirtualMachineTemplate.TemplateFilter;
+import com.cloud.user.Account;
+import com.cloud.utils.Pair;
+import com.cloud.utils.db.GenericDao;
+import com.cloud.utils.fsm.StateDao;
+
+/*
+ * Data Access Object for vm_templates table
+ */
+public interface VMTemplateDao extends GenericDao<VMTemplateVO, Long>, StateDao<TemplateState, TemplateEvent, VMTemplateVO> {
+
+
+ public List<VMTemplateVO> listByPublic();
+ public VMTemplateVO findByName(String templateName);
+ public VMTemplateVO findByTemplateName(String templateName);
+
+ //public void update(VMTemplateVO template);
+
+
+ public List<VMTemplateVO> listAllSystemVMTemplates();
+
+ public List<VMTemplateVO> listDefaultBuiltinTemplates();
+ public String getRoutingTemplateUniqueName();
+ public List<VMTemplateVO> findIsosByIdAndPath(Long domainId, Long accountId, String path);
+ public List<VMTemplateVO> listReadyTemplates();
+ public List<VMTemplateVO> listByAccountId(long accountId);
+ public Set<Pair<Long, Long>> searchTemplates(String name, String keyword, TemplateFilter templateFilter, boolean isIso,
+ List<HypervisorType> hypers, Boolean bootable, DomainVO domain, Long pageSize, Long startIndex, Long zoneId,
+ HypervisorType hyperType, boolean onlyReady, boolean showDomr, List<Account> permittedAccounts, Account caller,
+ ListProjectResourcesCriteria listProjectResourcesCriteria, Map<String, String> tags, String zoneType);
+
+ public Set<Pair<Long, Long>> searchSwiftTemplates(String name, String keyword, TemplateFilter templateFilter,
+ boolean isIso, List<HypervisorType> hypers, Boolean bootable, DomainVO domain, Long pageSize, Long startIndex,
+ Long zoneId, HypervisorType hyperType, boolean onlyReady, boolean showDomr, List<Account> permittedAccounts, Account caller, Map<String, String> tags);
+
+ public Set<Pair<Long, Long>> searchS3Templates(String name, String keyword, TemplateFilter templateFilter,
+ boolean isIso, List<HypervisorType> hypers, Boolean bootable, DomainVO domain, Long pageSize, Long startIndex,
+ Long zoneId, HypervisorType hyperType, boolean onlyReady, boolean showDomr, List<Account> permittedAccounts, Account caller, Map<String, String> tags);
+
+ public long addTemplateToZone(VMTemplateVO tmplt, long zoneId);
+ public List<VMTemplateVO> listAllInZone(long dataCenterId);
+
+ public List<VMTemplateVO> listByHypervisorType(List<HypervisorType> hyperTypes);
+ public List<VMTemplateVO> publicIsoSearch(Boolean bootable, boolean listRemoved, Map<String, String> tags);
+ public List<VMTemplateVO> userIsoSearch(boolean listRemoved);
+ VMTemplateVO findSystemVMTemplate(long zoneId);
+ VMTemplateVO findSystemVMTemplate(long zoneId, HypervisorType hType);
+
+ VMTemplateVO findRoutingTemplate(HypervisorType type, String templateName);
+ List<Long> listPrivateTemplatesByHost(Long hostId);
+ public Long countTemplatesForAccount(long accountId);
+
+ List<VMTemplateVO> findTemplatesToSyncToS3();
+
+}