/*
 * Copyright 2014-2016 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;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;

import org.apache.brooklyn.api.entity.Entity;
import org.apache.brooklyn.api.entity.Group;
import org.apache.brooklyn.core.entity.EntityPredicates;

import brooklyn.location.docker.DockerHostLocation;

/**
 * Strategy that requires the hostname of the Docker location to match a particular regexp.
 */
public class GroupPlacementStrategy extends BasicDockerPlacementStrategy {

    private static final Logger LOG = LoggerFactory.getLogger(GroupPlacementStrategy.class);

    @Override
    public boolean apply(DockerHostLocation input) {
        List<Entity> deployed = input.getDockerContainerList();
        Entity parent = entity.getParent();
        String applicationId = entity.getApplicationId();
        Iterable<Entity> sameApplication = Iterables.filter(deployed, EntityPredicates.applicationIdEqualTo(applicationId));
        if (Iterables.isEmpty(sameApplication)) {
            // There are no entites from the same app here
            return true;
        } else {
            Iterable<Entity> sameParent = Iterables.filter(sameApplication, EntityPredicates.isChildOf(parent));
            if (Iterables.isEmpty(sameParent)) {
                // There are entites from the same app, but different parent here
                return false;
            } else {
                // The entites here have the same parent
                return true;
            }
        }
    }
}

@@ -740,49 +740,8 @@
 y;%0A%0A
-import com.google.common.base.Predicate;%0A
 impo
@@ -858,24 +858,75 @@
 lyn.
-api.entity.Group
+config.ConfigKey;%0Aimport org.apache.brooklyn.core.config.ConfigKeys
 ;%0Aim
@@ -979,16 +979,72 @@
 dicates;
+%0Aimport org.apache.brooklyn.util.core.flags.SetFromFlag;
 %0A%0Aimport
@@ -1124,72 +1124,154 @@
 res 
-the hostname of the Docker location to match a particular regexp
+entities with the same parent to use the same host.%0A * Can be configured to require exclusive use of the host with the%0A * %7B@code exclusive%7D option
 .%0A *
@@ -1442,16 +1442,337 @@
 lass);%0A%0A
+    @SetFromFlag(%22requireEmpty%22)%0A    public static final ConfigKey%3CBoolean%3E REQUIRE_EXCLUSIVE = ConfigKeys.newBooleanConfigKey(%0A            %22docker.constraint.exclusive%22,%0A            %22Whether the Docker host must be exclusive to this application; by default other applications can co-exist%22,%0A            Boolean.FALSE);%0A%0A
     @Ove
@@ -1826,24 +1826,92 @@
 on input) %7B%0A
+        boolean requireExclusive = config().get(REQUIRE_EXCLUSIVE);%0A
         List
@@ -2205,104 +2205,368 @@
 if (
-Iterables.isEmpty(sameApplication)) %7B%0A            // There are n
+requireExclusive && Iterables.size(deployed) %3E Iterables.size(sameApplication)) %7B%0A            LOG.debug(%22Found entities not in %7B%7D; required exclusive. Reject: %7B%7D%22, applicationId, input);%0A            return false;%0A        %7D%0A        if (Iterables.isEmpty(sameApplication)) %7B%0A            LOG.debug(%22N
 o entit
+i
 es 
-from the same app here
+present from %7B%7D. Accept: %7B%7D%22, applicationId, input);
 %0A   
@@ -2786,34 +2786,37 @@
     
-// There are
+LOG.debug(%22All
  entit
+i
 es from 
 the 
@@ -2815,47 +2815,69 @@
 rom 
-the same app, but different parent here
+%7B%7D have different parent. Reject: %7B%7D%22, applicationId, input);
 %0A   
@@ -2944,35 +2944,43 @@
     
-// The
+LOG.debug(%22All
  entit
+i
 es 
-here have th
+from %7B%7D hav
 e sa
@@ -2988,16 +2988,53 @@
 e parent
+. Accept: %7B%7D%22, applicationId, input);
 %0A       

Add exclusive flag to group strategy