brooklyn.location.docker.strategy.affinity.DockerAffinityRuleStrategy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of brooklyn-clocker-docker Show documentation
Show all versions of brooklyn-clocker-docker Show documentation
Clocker Brooklyn entities and locations for Docker integration.
/*
* Copyright 2014-2015 by Cloudsoft Corporation Limited
*
* Licensed 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 brooklyn.location.docker.strategy.affinity;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import brooklyn.entity.Entity;
import brooklyn.entity.basic.EntityPredicates;
import brooklyn.entity.container.docker.DockerHost;
import brooklyn.location.docker.DockerHostLocation;
import brooklyn.location.docker.strategy.AbstractDockerPlacementStrategy;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
/**
* Docker host selection strategy using affinity rules to filter available hosts.
*/
public class DockerAffinityRuleStrategy extends AbstractDockerPlacementStrategy {
private static final Logger LOG = LoggerFactory.getLogger(DockerAffinityRuleStrategy.class);
@Override
public List filterLocations(List locations, Entity entity) {
List available = Lists.newArrayList();
// Select hosts that satisfy the affinity rules
for (DockerHostLocation machine : locations) {
Optional> entityRules = Optional.fromNullable(entity.config().get(DockerHost.DOCKER_HOST_AFFINITY_RULES));
Optional> hostRules = Optional.fromNullable(machine.getOwner().config().get(DockerHost.DOCKER_HOST_AFFINITY_RULES));
Optional> infrastructureRules = Optional.fromNullable(machine.getOwner().getInfrastructure().config().get(DockerHost.DOCKER_HOST_AFFINITY_RULES));
Iterable combined = Iterables.concat(Optional.presentInstances(ImmutableList.of(entityRules, hostRules, infrastructureRules)));
AffinityRules rules = AffinityRules.rulesFor(entity).parse(combined);
Iterable entities = getBrooklynManagementContext().getEntityManager().findEntities(EntityPredicates.locationsIncludes(machine));
if (Iterables.isEmpty(entities)) {
if (rules.allowEmptyLocations()) {
available.add(machine);
}
} else {
Iterable filtered = Iterables.filter(entities, rules);
if (Iterables.size(filtered) == Iterables.size(entities)) {
available.add(machine);
}
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("Available Docker hosts: {}", Iterables.toString(available));
}
return available;
}
}