/**
* Copyright (c) 2009 - 2012 Red Hat, Inc.
*
* This software is licensed to you under the GNU General Public License,
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
* implied, including the implied warranties of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
* along with this software; if not, see
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* Red Hat trademarks are not licensed under GPLv2. No permission is
* granted to use or replicate Red Hat trademarks that are incorporated
* in this software or its documentation.
*/
package org.candlepin.controller;
import org.candlepin.audit.Event;
import org.candlepin.audit.EventFactory;
import org.candlepin.audit.EventSink;
import org.candlepin.common.exceptions.BadRequestException;
import org.candlepin.common.exceptions.ForbiddenException;
import org.candlepin.model.Consumer;
import org.candlepin.model.ConsumerCurator;
import org.candlepin.model.Entitlement;
import org.candlepin.model.Owner;
import org.candlepin.model.Pool;
import org.candlepin.model.PoolQuantity;
import org.candlepin.policy.EntitlementRefusedException;
import org.candlepin.policy.js.entitlement.EntitlementRulesTranslator;
import org.candlepin.resource.dto.AutobindData;
import com.google.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xnap.commons.i18n.I18n;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
/**
* entitler
*/
public class Entitler {
private static Logger log = LoggerFactory.getLogger(Entitler.class);
private PoolManager poolManager;
private I18n i18n;
private EventFactory evtFactory;
private EventSink sink;
private ConsumerCurator consumerCurator;
private EntitlementRulesTranslator messageTranslator;
@Inject
public Entitler(PoolManager pm, ConsumerCurator cc, I18n i18n,
EventFactory evtFactory, EventSink sink,
EntitlementRulesTranslator messageTranslator) {
this.poolManager = pm;
this.i18n = i18n;
this.evtFactory = evtFactory;
this.sink = sink;
this.consumerCurator = cc;
this.messageTranslator = messageTranslator;
}
public List<Entitlement> bindByPool(String poolId, String consumeruuid,
Integer quantity) {
Consumer c = consumerCurator.findByUuid(consumeruuid);
return bindByPool(poolId, c, quantity);
}
public List<Entitlement> bindByPool(String poolId, Consumer consumer,
Integer quantity) {
Pool pool = poolManager.find(poolId);
List<Entitlement> entitlementList = new LinkedList<Entitlement>();
if (pool == null) {
throw new BadRequestException(i18n.tr(
"Subscription pool {0} does not exist.", poolId));
}
if (log.isDebugEnabled()) {
log.debug("pool: id[" + pool.getId() + "], consumed[" +
pool.getConsumed() + "], qty [" + pool.getQuantity() + "]");
}
// Attempt to create an entitlement:
entitlementList.add(createEntitlementByPool(consumer, pool, quantity));
return entitlementList;
}
private Entitlement createEntitlementByPool(Consumer consumer, Pool pool,
Integer quantity) {
// Attempt to create an entitlement:
try {
Entitlement e = poolManager.entitleByPool(consumer, pool, quantity);
log.debug("Created entitlement: " + e);
return e;
}
catch (EntitlementRefusedException e) {
// TODO: Could be multiple errors, but we'll just report the first one for now
throw new ForbiddenException(messageTranslator.poolErrorToMessage(pool,
e.getResult().getErrors().get(0)));
}
}
public void adjustEntitlementQuantity(Consumer consumer, Entitlement ent,
Integer quantity) {
// Attempt to adjust an entitlement:
try {
poolManager.adjustEntitlementQuantity(consumer, ent, quantity);
}
catch (EntitlementRefusedException e) {
// TODO: Could be multiple errors, but we'll just report the first one for now:
throw new ForbiddenException(messageTranslator.entitlementErrorToMessage(ent,
e.getResult().getErrors().get(0)));
}
}
public List<Entitlement> bindByProducts(String[] productIds,
String consumeruuid, Date entitleDate, Collection<String> fromPools) {
Consumer c = consumerCurator.findByUuid(consumeruuid);
AutobindData data = AutobindData.create(c).on(entitleDate)
.forProducts(productIds).withPools(fromPools);
return bindByProducts(data);
}
/**
* Entitles the given Consumer to the given Product. Will seek out pools
* which provide access to this product, either directly or as a child, and
* select the best one based on a call to the rules engine.
*
* @param data AutobindData encapsulating data required for an autobind request
* @return List of Entitlements
*/
public List<Entitlement> bindByProducts(AutobindData data) {
return bindByProducts(data, false);
}
/**
*
* Force option is used to heal entire org
*
* @param data AutobindData encapsulating data required for an autobind request
* @param force heal host even if it has autoheal disabled
* @return List of Entitlements
*/
public List<Entitlement> bindByProducts(AutobindData data, boolean force) {
Consumer consumer = data.getConsumer();
// If the consumer is a guest, and has a host, try to heal the host first
if (consumer.hasFact("virt.uuid")) {
String guestUuid = consumer.getFact("virt.uuid");
Consumer host = consumerCurator.getHost(guestUuid, consumer.getOwner());
if (host != null && (force || host.isAutoheal())) {
log.info("Attempting to heal host machine with UUID " +
host.getUuid() + " for guest with UUID " + consumer.getUuid());
try {
List<Entitlement> hostEntitlements =
poolManager.entitleByProductsForHost(consumer, host,
data.getOnDate(), data.getPossiblePools());
log.debug("Granted host {} entitlements", hostEntitlements.size());
sendEvents(hostEntitlements);
}
catch (Exception e) {
//log and continue, this should NEVER block
log.debug("Healing failed for host UUID " + host.getUuid() +
" with message: " + e.getMessage());
}
// Consumer is stale at this point.
consumer = consumerCurator.getConsumer(consumer.getUuid());
}
}
// Attempt to create entitlements:
try {
// the pools are only used to bind the guest
List<Entitlement> entitlements = poolManager.entitleByProducts(data);
log.debug("Created entitlements: " + entitlements);
return entitlements;
}
catch (EntitlementRefusedException e) {
// TODO: Could be multiple errors, but we'll just report the first one for now
String productId = "Unknown Product";
if (data.getProductIds().length > 0) {
productId = data.getProductIds()[0];
}
throw new ForbiddenException(messageTranslator.productErrorToMessage(productId,
e.getResult().getErrors().get(0)));
}
}
/**
* Entitles the given Consumer to the given Product. Will seek out pools
* which provide access to this product, either directly or as a child, and
* select the best one based on a call to the rules engine.
*
* @param consumer The consumer being entitled.
* @return List of Entitlements
*/
public List<PoolQuantity> getDryRun(Consumer consumer,
String serviceLevelOverride) {
List<PoolQuantity> result = new ArrayList<PoolQuantity>();
try {
Owner owner = consumer.getOwner();
result = poolManager.getBestPools(
consumer, null, null, owner, serviceLevelOverride, null);
log.debug("Created Pool Quantity list: " + result);
}
catch (EntitlementRefusedException e) {
// If we catch an exception we will just return an empty list
// The dry run just reports that an autobind will have no pools
// We will debug log the message, but returning does not seem to add
// to the process
if (log.isDebugEnabled()) {
String message = e.getResult().getErrors().get(0).getResourceKey();
log.debug("consumer dry-run " + consumer.getUuid() + ": " + message);
}
}
return result;
}
public void sendEvents(List<Entitlement> entitlements) {
if (entitlements != null) {
for (Entitlement e : entitlements) {
Event event = evtFactory.entitlementCreated(e);
sink.queueEvent(event);
}
}
}
}