/**
* 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.api.commands;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.cloud.api.BaseCmd;
import com.cloud.api.ServerApiException;
import com.cloud.async.AsyncJobVO;
import com.cloud.dc.DataCenterVO;
import com.cloud.domain.DomainVO;
import com.cloud.host.HostVO;
import com.cloud.storage.GuestOS;
import com.cloud.storage.VMTemplateHostVO;
import com.cloud.storage.VMTemplateVO;
import com.cloud.storage.VMTemplateStorageResourceAssoc.Status;
import com.cloud.storage.dao.VMTemplateDao.TemplateFilter;
import com.cloud.user.Account;
import com.cloud.utils.Pair;
public class ListIsosCmd extends BaseCmd {
public static final Logger s_logger = Logger.getLogger(ListIsosCmd.class.getName());
private static final String s_name = "listisosresponse";
private static final List> s_properties = new ArrayList>();
static {
s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT, Boolean.FALSE));
s_properties.add(new Pair(BaseCmd.Properties.DOMAIN_ID, Boolean.FALSE));
s_properties.add(new Pair(BaseCmd.Properties.ID, Boolean.FALSE));
s_properties.add(new Pair(BaseCmd.Properties.NAME, Boolean.FALSE));
s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE));
s_properties.add(new Pair(BaseCmd.Properties.IS_PUBLIC, Boolean.FALSE));
s_properties.add(new Pair(BaseCmd.Properties.IS_READY, Boolean.FALSE));
s_properties.add(new Pair(BaseCmd.Properties.ISO_FILTER, Boolean.FALSE));
s_properties.add(new Pair(BaseCmd.Properties.BOOTABLE, Boolean.FALSE));
s_properties.add(new Pair(BaseCmd.Properties.KEYWORD, Boolean.FALSE));
s_properties.add(new Pair(BaseCmd.Properties.PAGE, Boolean.FALSE));
s_properties.add(new Pair(BaseCmd.Properties.PAGESIZE, Boolean.FALSE));
s_properties.add(new Pair(BaseCmd.Properties.ZONE_ID, Boolean.FALSE));
}
@Override
public String getName() {
return s_name;
}
@Override
public List> getProperties() {
return s_properties;
}
@Override
public List> execute(Map params) {
String accountName = (String)params.get(BaseCmd.Properties.ACCOUNT.getName());
Long domainId = (Long)params.get(BaseCmd.Properties.DOMAIN_ID.getName());
Long id = (Long) params.get(BaseCmd.Properties.ID.getName());
String name = (String) params.get(BaseCmd.Properties.NAME.getName());
Account account = (Account) params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
Integer page = (Integer)params.get(BaseCmd.Properties.PAGE.getName());
Integer pageSize = (Integer)params.get(BaseCmd.Properties.PAGESIZE.getName());
String isoFilterString = (String) params.get(BaseCmd.Properties.ISO_FILTER.getName());
Boolean bootable = (Boolean)params.get(BaseCmd.Properties.BOOTABLE.getName());
String keyword = (String)params.get(BaseCmd.Properties.KEYWORD.getName());
Long zoneId = (Long)params.get(BaseCmd.Properties.ZONE_ID.getName());
boolean isAdmin = false;
TemplateFilter isoFilter;
try {
if (isoFilterString == null) {
isoFilter = TemplateFilter.selfexecutable;
} else {
isoFilter = TemplateFilter.valueOf(isoFilterString);
}
} catch (IllegalArgumentException e) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Please specify a valid template filter.");
}
Long accountId = null;
if ((account == null) || (account.getType() == Account.ACCOUNT_TYPE_ADMIN)) {
isAdmin = true;
// validate domainId before proceeding
if (domainId != null) {
if ((account != null) && !getManagementServer().isChildDomain(account.getDomainId(), domainId)) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid domain id (" + domainId + ") given, unable to list events.");
}
if (accountName != null) {
Account userAccount = getManagementServer().findAccountByName(accountName, domainId);
if (userAccount != null) {
accountId = userAccount.getId();
} else {
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to find account " + accountName + " in domain " + domainId);
}
}
} else {
domainId = ((account == null) ? DomainVO.ROOT_DOMAIN : account.getDomainId());
}
} else {
accountId = account.getId();
accountName = account.getAccountName();
domainId = account.getDomainId();
}
Long startIndex = Long.valueOf(0);
int pageSizeNum = 50;
if (pageSize != null) {
pageSizeNum = pageSize.intValue();
}
if (page != null) {
int pageNum = page.intValue();
if (pageNum > 0) {
startIndex = Long.valueOf(pageSizeNum * (pageNum-1));
}
}
boolean onlyReady = (isoFilter == TemplateFilter.featured) ||
(isoFilter == TemplateFilter.selfexecutable) ||
(isoFilter == TemplateFilter.sharedexecutable) ||
(isoFilter == TemplateFilter.executable && accountId != null) ||
(isoFilter == TemplateFilter.community);
List isos = null;
try {
isos = getManagementServer().listTemplates(id, name, keyword, isoFilter, true, bootable, accountId, pageSize, startIndex, zoneId);
} catch (Exception e) {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, e.getMessage());
}
int numTags = 0;
Map> isoHostsMap = new HashMap>();
for (VMTemplateVO iso : isos) {
List isoHosts = getManagementServer().listTemplateHostBy(iso.getId(), zoneId);
if (iso.getName().equals("xs-tools.iso")) {
List xstoolsZones = new ArrayList();
// the xs-tools.iso is a special case since it will be available on every computing host in the zone and we want to return it once per zone
List xstoolsHosts = new ArrayList();
for (VMTemplateHostVO isoHost : isoHosts) {
HostVO host = getManagementServer().getHostBy(isoHost.getHostId());
if (!xstoolsZones.contains(Long.valueOf(host.getDataCenterId()))) {
xstoolsZones.add(Long.valueOf(host.getDataCenterId()));
xstoolsHosts.add(isoHost);
}
}
isoHostsMap.put(iso.getId(), xstoolsHosts);
numTags += xstoolsHosts.size();
} else {
isoHostsMap.put(iso.getId(), isoHosts);
numTags += isoHosts.size();
}
}
List