/*
 * Copyright 2012-2013 by Cloudsoft Corp.
 *
 * 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.demo;

import java.util.List;

import brooklyn.entity.basic.AbstractApplication;
import brooklyn.entity.basic.Entities;
import brooklyn.entity.basic.StartableApplication;
import brooklyn.entity.nosql.cassandra.CassandraCluster;
import brooklyn.entity.nosql.cassandra.CassandraNode;
import brooklyn.entity.proxying.EntitySpec;
import brooklyn.launcher.BrooklynLauncher;
import brooklyn.policy.PolicySpec;
import brooklyn.policy.ha.ServiceFailureDetector;
import brooklyn.policy.ha.ServiceReplacer;
import brooklyn.policy.ha.ServiceRestarter;
import brooklyn.util.CommandLineUtil;

import com.google.common.collect.Lists;

public class HighAvailabilityCassandraCluster extends AbstractApplication {

    public static final String DEFAULT_LOCATION_SPEC = "aws-ec2:us-east-1";
    
    @Override
    public void init() {
        addChild(EntitySpec.create(CassandraCluster.class)
                .configure(CassandraCluster.CLUSTER_NAME, "Brooklyn")
                .configure(CassandraCluster.INITIAL_SIZE, 6)
                .configure(CassandraCluster.ENABLE_AVAILABILITY_ZONES, true)
                .configure(CassandraCluster.NUM_AVAILABILITY_ZONES, 3)
                //See https://github.com/brooklyncentral/brooklyn/issues/973
                //.configure(CassandraCluster.AVAILABILITY_ZONE_NAMES, ImmutableList.of("us-east-1b", "us-east-1c", "us-east-1e"))
                .configure(CassandraCluster.ENDPOINT_SNITCH_NAME, "GossipingPropertyFileSnitch")
                .configure(CassandraCluster.MEMBER_SPEC, EntitySpec.create(CassandraNode.class)
                        .policy(PolicySpec.create(ServiceFailureDetector.class))
                        .policy(PolicySpec.create(ServiceRestarter.class)
                                .configure(ServiceRestarter.FAILURE_SENSOR_TO_MONITOR, ServiceFailureDetector.ENTITY_FAILED)))
                .policy(PolicySpec.create(ServiceReplacer.class)
                        .configure(ServiceReplacer.FAILURE_SENSOR_TO_MONITOR, ServiceRestarter.ENTITY_RESTART_FAILED)));
    }
    
    public static void main(String[] argv) {
        List<String> args = Lists.newArrayList(argv);
        String port =  CommandLineUtil.getCommandLineOption(args, "--port", "8081+");
        String location = CommandLineUtil.getCommandLineOption(args, "--location", DEFAULT_LOCATION_SPEC);
        
        BrooklynLauncher launcher = BrooklynLauncher.newInstance()
                 .application(EntitySpec.create(StartableApplication.class, HighAvailabilityCassandraCluster.class)
                         .displayName("Cassandra"))
                 .webconsolePort(port)
                 .location(location)
                 .start();
             
        Entities.dumpInfo(launcher.getApplications());
    }
}

@@ -644,16 +644,163 @@
 .List;%0A%0A
+import brooklyn.catalog.Catalog;%0Aimport brooklyn.catalog.CatalogConfig;%0Aimport brooklyn.config.ConfigKey;%0Aimport brooklyn.entity.basic.ConfigKeys;%0A
 import b
@@ -1381,16 +1381,147 @@
 Lists;%0A%0A
+@Catalog(name=%22HA Cassandra Cluster Application%22, description=%22Deploy a Cassandra cluster with a High Availability architecture.%22)%0A
 public c
@@ -1665,16 +1665,552 @@
 ast-1%22;%0A
+ %0A    @CatalogConfig(label=%22Number of Availability Zones%22, priority=1)%0A    public static final ConfigKey%3CInteger%3E NUM_AVAILABILITY_ZONES = ConfigKeys.newConfigKey(%0A        %22cassandra.cluster.numAvailabilityZones%22, %22Number of availability zones to spread the cluster across%22, 3); %0A    %0A    @CatalogConfig(label=%22Initial Cluster Size%22, priority=2)%0A    public static final ConfigKey%3CInteger%3E CASSANDRA_CLUSTER_SIZE = ConfigKeys.newConfigKey(%0A        %22cassandra.cluster.initialSize%22, %22Initial size of the Cassandra cluster%22, 6);      %0A    %0A
     %0A   
@@ -2432,17 +2432,49 @@
 L_SIZE, 
-6
+getConfig(CASSANDRA_CLUSTER_SIZE)
 )%0A      
@@ -2612,17 +2612,49 @@
 _ZONES, 
-3
+getConfig(NUM_AVAILABILITY_ZONES)
 )%0A      

Update HighAvailabilityCassandraCluster.java