<?xml version="1.0" encoding="UTF-8"?>
<project name="PEtALS integration - Moving container" default="all" basedir=".">

   <!-- ******************************************************************* -->
   <taskdef resource="net/sf/antcontrib/antlib.xml" />
   <taskdef resource="org/ow2/petals/ant/antlib.xml" />

   <import>
      <javaresource name="org/ow2/petals/integration/ant/petals-esb.xml" classpath="${maven_plugin_classpath}" />
   </import>
   <import>
      <javaresource name="org/ow2/petals/integration/ant/petals-esb-cli.xml" classpath="${maven_plugin_classpath}" />
   </import>
   <import>
      <javaresource name="org/ow2/petals/integration/ant/petals-registry.xml" classpath="${maven_plugin_classpath}" />
   </import>
   <import>
      <javaresource name="org/ow2/petals/integration/ant/petals-registry-cli.xml"
                    classpath="${maven_plugin_classpath}" />
   </import>

   <basename property="petalsRegistryDirName" file="${org.ow2.petals:petals-registry-overlay-zip:zip}" suffix=".zip" />
   <basename property="petalsCliDirName" file="${org.ow2.petals:petals-cli-distrib-zip:zip}" suffix=".zip" />
   <basename property="petalsRegistryCliDirName" file="${org.ow2.petals:petals-registry-cli-zip:zip}" suffix=".zip" />

   <property name="petals.cli.home"
             value="${project.build.directory}/${project.artifactId}/petals-cli/${petalsCliDirName}" />
   <property name="petals.registry-cli.home"
             value="${project.build.directory}/${project.artifactId}/petals-registry-cli/${petalsRegistryCliDirName}" />

   <property name="registry.target.member.home"
             value="${project.build.directory}/${project.artifactId}/registry-target" />

   <scriptdef name="generate-domain-name" language="javascript">
      <attribute name="property" />
      <![CDATA[
       importClass( java.util.UUID );

       project.setProperty( attributes.get( "property" ), "PEtALS" + UUID.randomUUID() );
       ]]>
   </scriptdef>

   <property name="initial-topology.home" value="${project.build.directory}/${project.artifactId}/initial-topology" />
   <property name="resources.home" value="${project.build.directory}/${project.artifactId}/resources" />

   <property name="target-topology.home" value="${project.build.directory}/${project.artifactId}/target-topology" />
   <property name="target-topology.domain-name" value="Target-PEtALS" />
   <property name="target-topology.registry-port" value="7910" />
   <property name="target-topology.registry-jmx-port" value="7750" />
   <property name="target-topology.container-1.home" value="${target-topology.home}/container-1" />
   <property name="target-topology.container-1.name" value="target-container-0" />
   <property name="target-topology.container-1.passphrase" value="target-petals-passphrase-0" />
   <property name="target-topology.container-1.jmx-port" value="7700" />
   <property name="target-topology.container-1.registry-port" value="7910" />
   <property name="target-topology.container-1.se-rmi-port" value="1099" />
   <property name="target-topology.container-2.home" value="${target-topology.home}/container-2" />
   <property name="target-topology.container-2.name" value="target-container-1" />
   <property name="target-topology.container-2.passphrase" value="target-petals-passphrase-1" />
   <property name="target-topology.container-2.jmx-port" value="7701" />
   <property name="target-topology.container-1.registry-port" value="7911" />
   <property name="target-topology.container-2.se-rmi-port" value="1199" />

   <generate-domain-name property="initial-topology.standalone-container-1.domain-name" />
   <property name="initial-topology.standalone-container-1.home" value="${initial-topology.home}/container-0" />
   <property name="initial-topology.standalone-container-1.name" value="initial-container-0" />
   <property name="initial-topology.standalone-container-1.jmx-port" value="7702" />
   <property name="initial-topology.standalone-container-1.nio-port" value="7802" />
   <property name="initial-topology.standalone-container-1.registry-port" value="7902" />
   <property name="initial-topology.standalone-container-1.se-rmi-port" value="1299" />
   <property name="initial-topology.standalone-container-2.home" value="${initial-topology.home}/container-1" />
   <property name="initial-topology.standalone-container-2.name" value="initial-container-1" />
   <property name="initial-topology.standalone-container-2.jmx-port" value="7703" />
   <property name="initial-topology.standalone-container-2.nio-port" value="7803" />
   <property name="initial-topology.standalone-container-2.registry-port" value="7903" />
   <property name="initial-topology.standalone-container-2.se-rmi-port" value="1399" />
   <property name="initial-topology.standalone-container-3.home" value="${initial-topology.home}/container-2" />
   <property name="initial-topology.standalone-container-3.name" value="initial-container-2" />
   <property name="initial-topology.standalone-container-3.jmx-port" value="7704" />
   <property name="initial-topology.standalone-container-3.nio-port" value="7804" />
   <property name="initial-topology.standalone-container-3.registry-port" value="7904" />
   <property name="initial-topology.standalone-container-3.se-rmi-port" value="1499" />
   <property name="initial-topology.standalone-container-4.home" value="${initial-topology.home}/container-3" />
   <property name="initial-topology.standalone-container-4.name" value="initial-container-3" />
   <property name="initial-topology.standalone-container-4.jmx-port" value="7705" />
   <property name="initial-topology.standalone-container-4.nio-port" value="7805" />
   <property name="initial-topology.standalone-container-4.registry-port" value="7905" />
   <property name="initial-topology.standalone-container-4.se-rmi-port" value="1599" />
   <property name="initial-topology.standalone-container-5.home" value="${initial-topology.home}/container-4" />
   <property name="initial-topology.standalone-container-5.name" value="initial-container-4" />
   <property name="initial-topology.standalone-container-5.jmx-port" value="7706" />
   <property name="initial-topology.standalone-container-5.nio-port" value="7806" />
   <property name="initial-topology.standalone-container-5.registry-port" value="7906" />
   <property name="initial-topology.standalone-container-5.se-rmi-port" value="1699" />
   <property name="initial-topology.standalone-container-6.home" value="${initial-topology.home}/container-5" />
   <property name="initial-topology.standalone-container-6.name" value="initial-container-5" />
   <property name="initial-topology.standalone-container-6.jmx-port" value="7707" />
   <property name="initial-topology.standalone-container-6.nio-port" value="7807" />
   <property name="initial-topology.standalone-container-6.registry-port" value="7907" />
   <property name="initial-topology.standalone-container-6.se-rmi-port" value="1799" />

   <!-- Set this property to 'true' to maximize the parallelization of Ant tasks. Set it to 'false' for debug purpose -->
   <property name="maximize-parallelization" value="true" />
   <property name="standalon-container.nb" value="6" />

   <target name="all" depends="clean">
      <echo message="All task finished!" />
   </target>

   <target name="launchContainers" depends="killAllContainers, killAllRegistryMembers, launch-TargetRegistryOverlay">
      <parallel>
         <sequential>
            <echo message="Launching Petals ESB container '${target-topology.container-1.name}' of target topology..." />
            <launchContainer topology="${project.build.directory}/${project.artifactId}/resources/topologies/target-topology.xml"
                             container.home="${target-topology.container-1.home}"
                             container.name="${target-topology.container-1.name}"
                             container.passphrase="${target-topology.container-1.passphrase}"
                             container.jmx-port="${target-topology.container-1.jmx-port}"
                             container.registry-port="${target-topology.container-1.registry-port}" />
         </sequential>
         <sequential>
            <echo message="Launching Petals ESB container '${target-topology.container-2.name}' of target topology..." />
            <launchContainer topology="${project.build.directory}/${project.artifactId}/resources/topologies/target-topology.xml"
                             container.home="${target-topology.container-2.home}"
                             container.name="${target-topology.container-2.name}"
                             container.passphrase="${target-topology.container-2.passphrase}"
                             container.jmx-port="${target-topology.container-2.jmx-port}"
                             container.registry-port="${target-topology.container-2.registry-port}" />
         </sequential>
         <for list="1,2,3,4,5,6"
              param="initial-topology.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <echo message="Launching Petals ESB container '${initial-topology.standalone-container-@{initial-topology.counter}.name}' of initial topology..." />
               <launchContainer container.home="${initial-topology.standalone-container-@{initial-topology.counter}.home}"
                                container.name="${initial-topology.standalone-container-@{initial-topology.counter}.name}"
                                container.jmx-port="${initial-topology.standalone-container-@{initial-topology.counter}.jmx-port}"
                                container.nio-port="${initial-topology.standalone-container-@{initial-topology.counter}.nio-port}"
                                container.registry-port="${initial-topology.standalone-container-@{initial-topology.counter}.registry-port}" />
            </sequential>
         </for>
      </parallel>
   </target>

   <target name="launch-TargetRegistryOverlay" depends="killAllRegistryMembers">
      <copy file="${project.build.directory}/${project.artifactId}/resources/topologies/target-cluster.xml"
            tofile="${registry.target.member.home}/${petalsRegistryDirName}/conf/cluster.xml"
            overwrite="true" />
      <replaceregexp file="${registry.target.member.home}/${petalsRegistryDirName}/conf/member.properties"
                     match="petals.registry.overlay.member.local-identifier.*"
                     replace="petals.registry.overlay.member.local-identifier=target-member"
                     byline="true" />
      <configureRegistryMember cluster-definition="${project.build.directory}/${project.artifactId}/resources/topologies/target-cluster.xml"
                               home="${registry.target.member.home}/${petalsRegistryDirName}"
                               name="target-member"
                               port="${target-topology.registry-port}"
                               jmx.port="${target-topology.registry-jmx-port}" />
      <startRegistryMember home="${registry.target.member.home}/${petalsRegistryDirName}"
                           name="target-member"
                           port="${target-topology.registry-port}" />
   </target>

   <macrodef name="launchContainer">
      <attribute name="container.home" />
      <attribute name="container.name" />
      <attribute name="container.jmx-port" default="7700" />
      <!-- Both following parameters are required for containers of the target topology -->
      <attribute name="container.passphrase" default="petals" />
      <attribute name="topology" default="" />
      <!-- Both following parameters are required for standalone containers of the initial topology -->
      <attribute name="container.nio-port" default="7800" />
      <attribute name="container.registry-port" default="7900" />
      <sequential>

         <!-- Configure the container -->
         <if>
            <length string="@{topology}" trim="true" length="0" />
            <then>
               <!-- Update the default topology file -->
               <replaceregexp file="@{container.home}/conf/topology.xml"
                              match='mode="static"'
                              replace='mode="dynamic"'
                              byline="true" />
               <replaceregexp file="@{container.home}/conf/topology.xml"
                              match='&lt;tns:container name="sample-0"&gt;'
                              replace='&lt;tns:container name="@{container.name}"&gt;'
                              byline="true" />
               <replaceregexp file="@{container.home}/conf/topology.xml"
                              match='&lt;tns:rmi-port&gt;7700&lt;/tns:rmi-port&gt;'
                              replace='&lt;tns:rmi-port&gt;@{container.jmx-port}&lt;/tns:rmi-port&gt;'
                              byline="true" />
               <replaceregexp file="@{container.home}/conf/topology.xml"
                              match='&lt;tns:tcp-port&gt;7800&lt;/tns:tcp-port&gt;'
                              replace='&lt;tns:tcp-port&gt;@{container.nio-port}&lt;/tns:tcp-port&gt;'
                              byline="true" />
               <replaceregexp file="@{container.home}/conf/topology.xml"
                              match='&lt;registry:overlay-member&gt;localhost&lt;/registry:overlay-member&gt;'
                              replace='&lt;registry:overlay-member port="@{container.registry-port}"&gt;localhost&lt;/registry:overlay-member&gt;'
                              byline="true" />

               <replaceregexp file="@{container.home}/conf/server.properties"
                              match="#petals.embedded-registry-overlay.port.*"
                              replace="petals.embedded-registry-overlay.port=@{container.registry-port}"
                              byline="true" />
            </then>
            <else>
               <copy file="@{topology}" tofile="@{container.home}/conf/topology.xml" overwrite="true" />
            </else>
         </if>

         <replaceregexp file="@{container.home}/conf/server.properties"
                        match="petals.container.name.*"
                        replace="petals.container.name=@{container.name}"
                        byline="true" />
         <replaceregexp file="@{container.home}/conf/server.properties"
                        match="petals.topology.passphrase.*"
                        replace="petals.topology.passphrase=@{container.passphrase}"
                        byline="true" />
         <replaceregexp file="@{container.home}/conf/server.properties"
                        match="#petals.artifact-repository=.*"
                        replace="petals.artifact-repository=false"
                        byline="true" />
         <replaceregexp file="@{container.home}/conf/server.properties"
                        match="#petals.ws-api=.*"
                        replace="petals.ws-api=false"
                        byline="true" />

         <replaceregexp file="@{container.home}/conf/env.sh" match=" -Xmx1024m " replace=" -Xmx256m " byline="true" />

         <replaceregexp file="@{container.home}/conf/loggers.properties"
                        match="org.ow2.petals.log.handler.PetalsFileHandler.level.*"
                        replace="org.ow2.petals.log.handler.PetalsFileHandler.level=FINEST"
                        byline="true" />
         <replaceregexp file="@{container.home}/conf/loggers.properties"
                        match="#Petals.Communication.TopologyService.level.*"
                        replace="Petals.Communication.TopologyService.level=FINEST"
                        byline="true" />
         <replaceregexp file="@{container.home}/conf/loggers.properties"
                        match="#Petals.System.Registry.level.*"
                        replace="Petals.System.Registry.level=FINEST"
                        byline="true" />
         <replaceregexp file="@{container.home}/conf/loggers.properties"
                        match="#Petals.System.Extensions.PreExtensionsManager.level.*"
                        replace="Petals.System.Extensions.PreExtensionsManager.level=FINEST"
                        byline="true" />

         <startContainer home="@{container.home}" name="@{container.name}" jmx.port="@{container.jmx-port}" />
      </sequential>
   </macrodef>

   <macrodef name="local-stop-container">
      <attribute name="home" />
      <attribute name="name" />
      <sequential>
         <echo message="Stopping Petals ESB container '@{name}'..." />
         <stopContainer name="@{name}" home="@{home}" />
      </sequential>
   </macrodef>

   <target name="stopTargetRegistry">
      <stopRegistryMember name="sample-member-0" home="${registry.target.member.home}/${petalsRegistryDirName}" />
   </target>

   <!-- Prepare environment of components and services -->
   <target name="prepare" depends="launchContainers">
      <echo message="Generates RMI client cfg files" />
      <parallel>
         <for list="1,2,3,4,5,6"
              param="initial-topology.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <echo file="${initial-topology.standalone-container-@{initial-topology.counter}.home}/rmi_consumer.properties"
                     append="false">
                  rmiContext=RMIComponentContext_CLI_moving_new_container_to_an_existing_topology_${initial-topology.standalone-container-@{initial-topology.counter}.name}
                  rmiPort=${initial-topology.standalone-container-@{initial-topology.counter}.se-rmi-port}
               </echo>
            </sequential>
         </for>
         <echo file="${target-topology.container-1.home}/rmi_consumer.properties" append="false">
            rmiContext=RMIComponentContext_CLI_moving_new_container_to_an_existing_topology_${target-topology.container-1.name}
            rmiPort=${target-topology.container-1.se-rmi-port}
         </echo>
         <echo file="${target-topology.container-2.home}/rmi_consumer.properties" append="false">
            rmiContext=RMIComponentContext_CLI_moving_new_container_to_an_existing_topology_${target-topology.container-2.name}
            rmiPort=${target-topology.container-2.se-rmi-port}
         </echo>
      </parallel>
   </target>

   <!-- Deploy components and services -->
   <target name="deploy" depends="prepare">
      <echo message="Deploying components..." />
      <parallel>
         <for list="1,2,3,4,5,6"
              param="initial-topology.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <echo message="Deploying artifacts on container '${initial-topology.standalone-container-@{initial-topology.counter}.name}' ..." />
               <deploy-a-container container-name="${initial-topology.standalone-container-@{initial-topology.counter}.name}"
                                   container-home="${initial-topology.standalone-container-@{initial-topology.counter}.home}"
                                   jmx.port="${initial-topology.standalone-container-@{initial-topology.counter}.jmx-port}"
                                   rmi.port="${initial-topology.standalone-container-@{initial-topology.counter}.se-rmi-port}"
                                   service-endpoint-property="initial-topology.standalone-container-@{initial-topology.counter}.endpoint" />
               <echo message="Deployed endpoint for container '${initial-topology.standalone-container-@{initial-topology.counter}.name}': ${initial-topology.standalone-container-@{initial-topology.counter}.endpoint}" />
            </sequential>
         </for>
         <sequential>
            <echo message="Deploying artifacts on container '${target-topology.container-1.name}' ..." />
            <deploy-a-container container-name="${target-topology.container-1.name}"
                                container-home="${target-topology.container-1.home}"
                                jmx.port="${target-topology.container-1.jmx-port}"
                                rmi.port="${target-topology.container-1.se-rmi-port}"
                                service-endpoint-property="target-topology.container-1.endpoint" />
            <echo message="Deployed endpoint for container '${target-topology.container-1.name}': ${target-topology.container-1.endpoint}" />
         </sequential>
         <sequential>
            <echo message="Deploying artifacts on container '${target-topology.container-2.name}' ..." />
            <deploy-a-container container-name="${target-topology.container-2.name}"
                                container-home="${target-topology.container-2.home}"
                                jmx.port="${target-topology.container-2.jmx-port}"
                                rmi.port="${target-topology.container-2.se-rmi-port}"
                                service-endpoint-property="target-topology.container-2.endpoint" />
            <echo message="Deployed endpoint for container '${target-topology.container-2.name}': ${target-topology.container-2.endpoint}" />
         </sequential>
      </parallel>

      <echo message="Deployment finished !" />

      <!-- As endpoints are deployed, we can generate RMI client files for invocations -->
      <replace file="${resources.home}/rmi_client/invokes.target.containers.xml">
         <replacefilter token="@ENDPOINT_TARGETCONTAINER_0@" value="${target-topology.container-1.endpoint}" />
         <replacefilter token="@ENDPOINT_TARGETCONTAINER_1@" value="${target-topology.container-2.endpoint}" />
         <replacefilter token="@CONTAINER_NAME_0@" value="${target-topology.container-1.name}" />
         <replacefilter token="@CONTAINER_NAME_1@" value="${target-topology.container-2.name}" />
      </replace>
      <replace file="${resources.home}/rmi_client/invokes.target.containers.fails.xml">
         <replacefilter token="@ENDPOINT_TARGETCONTAINER_0@" value="${target-topology.container-1.endpoint}" />
         <replacefilter token="@ENDPOINT_TARGETCONTAINER_1@" value="${target-topology.container-2.endpoint}" />
         <replacefilter token="@CONTAINER_NAME_0@" value="${target-topology.container-1.name}" />
         <replacefilter token="@CONTAINER_NAME_1@" value="${target-topology.container-2.name}" />
      </replace>
      <replace file="${resources.home}/rmi_client/invokes.containers-moved.xml">
         <replacefilter token="@ENDPOINT_INITIALCONTAINER_0@"
                        value="${initial-topology.standalone-container-1.endpoint}" />
         <replacefilter token="@ENDPOINT_INITIALCONTAINER_1@"
                        value="${initial-topology.standalone-container-2.endpoint}" />
         <replacefilter token="@ENDPOINT_INITIALCONTAINER_2@"
                        value="${initial-topology.standalone-container-3.endpoint}" />
         <replacefilter token="@ENDPOINT_INITIALCONTAINER_3@"
                        value="${initial-topology.standalone-container-4.endpoint}" />
         <replacefilter token="@ENDPOINT_INITIALCONTAINER_4@"
                        value="${initial-topology.standalone-container-5.endpoint}" />
         <replacefilter token="@ENDPOINT_INITIALCONTAINER_5@"
                        value="${initial-topology.standalone-container-6.endpoint}" />
         <replacefilter token="@CONTAINER_NAME_0@" value="${initial-topology.standalone-container-1.name}" />
         <replacefilter token="@CONTAINER_NAME_1@" value="${initial-topology.standalone-container-2.name}" />
         <replacefilter token="@CONTAINER_NAME_2@" value="${initial-topology.standalone-container-3.name}" />
         <replacefilter token="@CONTAINER_NAME_3@" value="${initial-topology.standalone-container-4.name}" />
         <replacefilter token="@CONTAINER_NAME_4@" value="${initial-topology.standalone-container-5.name}" />
         <replacefilter token="@CONTAINER_NAME_5@" value="${initial-topology.standalone-container-6.name}" />
      </replace>
      <for list="2,3,4,5,6"
           param="container.counter"
           parallel="${maximize-parallelization}"
           threadCount="${standalon-container.nb}">
         <sequential>
            <copy file="${resources.home}/rmi_client/invokes.initial-topology.container-detached-TEMPLATE.xml"
                  tofile="${resources.home}/rmi_client/invokes.initial-topology.container-detached-@{container.counter}.xml" />
            <replace file="${resources.home}/rmi_client/invokes.initial-topology.container-detached-@{container.counter}.xml">
               <replacefilter token="@ENDPOINT_CONTAINER@"
                              value="${initial-topology.standalone-container-@{container.counter}.endpoint}" />
               <replacefilter token="@CONTAINER_NAME@"
                              value="${initial-topology.standalone-container-@{container.counter}.name}" />
            </replace>
         </sequential>
      </for>

      <for list="1,2,3,4,5,6"
           param="container.counter"
           parallel="${maximize-parallelization}"
           threadCount="${standalon-container.nb}">
         <sequential>
            <copy file="${resources.home}/rmi_client/invokes.my-self-TEMPLATE.xml"
                  tofile="${initial-topology.standalone-container-@{container.counter}.home}/invokes.my-self.xml" />
            <replace file="${initial-topology.standalone-container-@{container.counter}.home}/invokes.my-self.xml">
               <replacefilter token="@ENDPOINT_CONTAINER@"
                              value="${initial-topology.standalone-container-@{container.counter}.endpoint}" />
               <replacefilter token="@CONTAINER_NAME@"
                              value="${initial-topology.standalone-container-@{container.counter}.name}" />
            </replace>
         </sequential>
      </for>
      <copy file="${resources.home}/rmi_client/invokes.my-self-TEMPLATE.xml"
            tofile="${target-topology.container-1.home}/invokes.my-self.xml" />
      <replace file="${target-topology.container-1.home}/invokes.my-self.xml">
         <replacefilter token="@ENDPOINT_CONTAINER@" value="${target-topology.container-1.endpoint}" />
         <replacefilter token="@CONTAINER_NAME@" value="${target-topology.container-1.name}" />
      </replace>
      <copy file="${resources.home}/rmi_client/invokes.my-self-TEMPLATE.xml"
            tofile="${target-topology.container-2.home}/invokes.my-self.xml" />
      <replace file="${target-topology.container-2.home}/invokes.my-self.xml">
         <replacefilter token="@ENDPOINT_CONTAINER@" value="${target-topology.container-2.endpoint}" />
         <replacefilter token="@CONTAINER_NAME@" value="${target-topology.container-2.name}" />
      </replace>

      <replace file="${resources.home}/rmi_client/invokes.remaining-containers.from-others.xml">
         <replacefilter token="@ENDPOINT_CONTAINER_0@" value="${target-topology.container-1.endpoint}" />
         <replacefilter token="@CONTAINER_NAME_0@" value="${target-topology.container-1.name}" />
         <replacefilter token="@ENDPOINT_CONTAINER_1@" value="${initial-topology.standalone-container-1.endpoint}" />
         <replacefilter token="@CONTAINER_NAME_1@" value="${initial-topology.standalone-container-1.name}" />
      </replace>

   </target>

   <macrodef name="deploy-a-container">
      <attribute name="container-name" />
      <attribute name="container-home" />
      <attribute name="jmx.host" default="localhost" />
      <attribute name="jmx.port" default="7700" />
      <attribute name="jmx.user" default="petals" />
      <attribute name="jmx.pwd" default="petals" />
      <attribute name="rmi.port" />
      <attribute name="service-endpoint-property" />
      <sequential>
         <echo message="container-name=@{container-name}" file="@{container-home}/conf/se-pojo.properties" />
         <jbi-install-component file="${org.ow2.petals:petals-se-pojo:jbi-component}"
                                host="@{jmx.host}"
                                port="@{jmx.port}"
                                username="@{jmx.user}"
                                password="@{jmx.pwd}">
            <param name="propertiesFile" value="@{container-home}/conf/se-pojo.properties" />
         </jbi-install-component>
         <jbi-start-component name="petals-se-pojo"
                              host="@{jmx.host}"
                              port="@{jmx.port}"
                              username="@{jmx.user}"
                              password="@{jmx.pwd}" />

         <jbi-install-component file="${org.ow2.petals:petals-se-rmi:jbi-component}"
                                host="@{jmx.host}"
                                port="@{jmx.port}"
                                username="@{jmx.user}"
                                password="@{jmx.pwd}">
            <param name="rmiPort" value="@{rmi.port}" />
            <param name="componentContextName"
                   value="RMIComponentContext_CLI_moving_new_container_to_an_existing_topology_@{container-name}" />
         </jbi-install-component>
         <jbi-start-component name="petals-se-rmi"
                              host="@{jmx.host}"
                              port="@{jmx.port}"
                              username="@{jmx.user}"
                              password="@{jmx.pwd}" />

         <jbi-deploy-service-assembly file="${org.ow2.petals.integration.test:sa-POJO-HelloService:jbi-service-assembly}"
                                      host="@{jmx.host}"
                                      port="@{jmx.port}"
                                      username="@{jmx.user}"
                                      password="@{jmx.pwd}" />
         <jbi-start-service-assembly name="sa-POJO-HelloService"
                                     host="@{jmx.host}"
                                     port="@{jmx.port}"
                                     username="@{jmx.user}"
                                     password="@{jmx.pwd}" />

         <!-- Retrieve the service endpoint located on the current container -->
         <echo message="Retrieving the service endpoint located on the container '@{container-name}' ..." />
         <local name="tempfile" />
         <local name="return-code" />
         <tempfile property="tempfile" prefix="petals-cli" suffix=".out" />
         <echo message="Petals CLI output dumped in: ${tempfile}" />
         <exec executable="./petals-cli.sh"
               dir="${petals.cli.home}/bin"
               output="${tempfile}"
               resultproperty="return-code">
            <arg value="-d" />
            <arg value="-h" />
            <arg value="@{jmx.host}" />
            <arg value="-n" />
            <arg value="@{jmx.port}" />
            <arg value="-u" />
            <arg value="@{jmx.user}" />
            <arg value="-p" />
            <arg value="@{jmx.pwd}" />
            <arg value="-c" />
            <arg value="--" />
            <arg value="registry-list" />
            <arg value="--output-endpoints" />
            <arg value="-c" />
            <arg value="@{container-name}" />
         </exec>
         <!-- Check that the Petals CLI command execution is succeded -->
         <fail message="Petals CLI command fails">
            <condition>
               <not>
                  <equals arg1="0" arg2="${return-code}" />
               </not>
            </condition>
         </fail>
         <local name="sa.endpoint.line" />
         <loadresource property="sa.endpoint.line">
            <file file="${tempfile}" />
         </loadresource>
         <propertyregex property="@{service-endpoint-property}"
                        input="${sa.endpoint.line}"
                        regexp="(.*): @{container-name},petals-se-pojo.*"
                        select="\1" />
         <echo message="The service endpoint located on the container '@{container-name}' is ${@{service-endpoint-property}}..." />
      </sequential>
   </macrodef>

   <!-- 
	  Run tests:
	     0) The Petals ESB environment:
	         - 6 standalone topologies, just configured to be dynamic,
	         - A target topology with one domain containing:
	             - A dedicated registry overlay,
	             - 2 Petals ESB nodes
	     1) Start the test environement described below.
	     2) Move concurrently all stand-alone containers into the target domain using both containers of the target domain, using Petals CLI.
	        For each standalone container attached, check that:
	         - the Petals CLI command execution is succeded,
	         - all containers moved are known by all containers of the target domain, in their point of view,
	         - all containers of the target domain are known by all containers moved in their point of view,
	         - all containers moved are known by all other containers moved in their point of view,
	         - all endpoints of the container moved are propagated to the containers of the target domain, in their point of view,
	         - all endpoints of the containers of the target domain are propagated to the containers moved, in their point of view,
	         - all endpoints of the containers moved are propagated to the containers moved, in their point of view,
	         - from the container moved, an invocation of a service located on an initial container of the target domain succeeds,
	         - from the initial container moved, an invocation of a service located on an initial container of the target domain succeeds,
	         - the Fractal component associated to the Petals Registry Overlay cluster of the initial stand-alone container is stopped.
	     3) For each container moved, restart it:
	         - It is restarted in the target domain
	         - It is known by other containers of the target topology
	         - the Fractal component associated to the Petals Registry Overlay cluster of the initial stand-alone container is not started.
	     4) Detach concurrently 6 containers of the target topology (one of containers of target topology + 5 containers previously moved), using Petals CLI.
	        For each container detached, check that:
            - the Petals CLI command execution is succeded,
            - the container detached are now standalone containers (does not known any other container, have an embedded registry started)
            - all remaining containers of the domain does not known the container detached in their point of view,
            - all endpoints of the container detached are no more registered on all remaining containers of the domain, in their point of view,
            - all endpoints of remaining containers of the domain are no more registered on the detached container, in its point of view,
            - from a remaining container of the topology, an invocation of a service located on the detached container fails.
            - from a remaining container of the topology, an invocation of a service located on the other remaining container of the topology succeeds.
            - from the detached container, an invocation of a service located on the detached container succeeds.
         5) For each container detached, restart it:
            - When the container is stopped, no more data associated to it remains into the registry overlay
            - It is restarted in its domain
            - It is not known by other containers of the  topology
         6) Restart the remaining containers of the topology:
            - It is restarted in its domain
            - It is known by other containers of the initial topology
            - It is not known by the container detached
         7) Attach and detach containers concurrently
	 -->
   <target name="run"
           depends="deploy, concurrent-attachments-detachments, move-container, restart-containers-moved, detach-container, restart-container-detached, restart-remaining-containers, concurrent-attachments-detachments">
      <echo message="---------  Integration test completed  ----------" />
   </target>

   <!-- 2) Move the stand-alone container into the target domain, using Petals CLI -->
   <target name="move-container">
      <echo message="Launching integration tests moving a container from a stand-alone domain to another domain composed of two nodes..." />
      <echo message="---------  Integration test started  ----------" />

      <echo message="*****************************************************************************************************************************" />
      <echo message="*** 2) Move the stand-alone containers into the target domain, using Petals CLI from both containers of the target domain ***" />
      <echo message="*****************************************************************************************************************************" />
      <echo message="Moving standalone containers to the target topology ..." />
      <parallel>
         <!-- Using the target container #0 -->
         <for list="1,2,3" param="initial-topology.counter" parallel="true">
            <sequential>
               <cli.attach-container container-to-attach.jmx-port="${initial-topology.standalone-container-@{initial-topology.counter}.jmx-port}"
                                     target-domain-name="${target-topology.domain-name}"
                                     target-container.name="target-container-0"
                                     target-container.passphrase="${target-topology.container-1.passphrase}"
                                     target-container.jmx-port="${target-topology.container-1.jmx-port}" />
            </sequential>
         </for>
         <!-- Using the target container #1 -->
         <for list="4,5,6" param="initial-topology.counter" parallel="true">
            <sequential>
               <cli.attach-container container-to-attach.jmx-port="${initial-topology.standalone-container-@{initial-topology.counter}.jmx-port}"
                                     target-domain-name="${target-topology.domain-name}"
                                     target-container.name="target-container-1"
                                     target-container.passphrase="${target-topology.container-2.passphrase}"
                                     target-container.jmx-port="${target-topology.container-2.jmx-port}" />
            </sequential>
         </for>
      </parallel>

      <!-- We wait 5s that all modifications are propagated to all containers -->
      <sleep seconds="5" />

      <!-- Check that the containers moved are known by the containers of the target topology, in their point of view -->
      <for list="1,2" param="target-topology.counter" parallel="${maximize-parallelization}">
         <sequential>
            <echo message="Checking that all containers moved are known by the container '${target-topology.container-@{target-topology.counter}.name}' of the target topology, in its point of view ..." />
            <cli.assert-containers-known-by-container container-names-to-check="${initial-topology.standalone-container-1.name},${initial-topology.standalone-container-2.name},${initial-topology.standalone-container-3.name},${initial-topology.standalone-container-4.name},${initial-topology.standalone-container-5.name},${initial-topology.standalone-container-6.name}"
                                                      from-container.jmx-port="${target-topology.container-@{target-topology.counter}.jmx-port}"
                                                      container-names-filter="initial-container-[012345]" />
         </sequential>
      </for>

      <!-- Check that all containers of the target domain are known by each container moved in its point of view -->
      <for list="1,2,3,4,5,6"
           param="initial-topology.counter"
           parallel="${maximize-parallelization}"
           threadCount="${standalon-container.nb}">
         <sequential>
            <echo message="Checking that all containers of the target domain are known by the container moved '${initial-topology.standalone-container-@{initial-topology.counter}.name}' in its point of view ..." />
            <cli.assert-containers-known-by-container container-names-to-check="${target-topology.container-1.name},${target-topology.container-2.name}"
                                                      from-container.jmx-port="${initial-topology.standalone-container-@{initial-topology.counter}.jmx-port}"
                                                      container-names-filter="target-container-[01]" />
         </sequential>
      </for>

      <!-- Check in the point of view of a moved container that all other containers moved are declared -->
      <for list="1,2,3,4,5,6"
           param="initial-topology.counter"
           parallel="${maximize-parallelization}"
           threadCount="${standalon-container.nb}">
         <sequential>
            <echo message="Checking that all containers moved are known by the container moved '${initial-topology.standalone-container-@{initial-topology.counter}.name}' in its point of view ..." />
            <cli.assert-containers-known-by-container container-names-to-check="${initial-topology.standalone-container-1.name},${initial-topology.standalone-container-2.name},${initial-topology.standalone-container-3.name},${initial-topology.standalone-container-4.name},${initial-topology.standalone-container-5.name},${initial-topology.standalone-container-6.name}"
                                                      from-container.jmx-port="${initial-topology.standalone-container-@{initial-topology.counter}.jmx-port}"
                                                      container-names-filter="initial-container-[012345]" />
         </sequential>
      </for>

      <!-- Check that all endpoints of all containers are propagated to all other containers, in their point of view -->
      <echo message="Check that all endpoints of all containers are propagated to all other containers, in their point of view ..." />
      <for list="1,2" param="target-topology.counter" parallel="${maximize-parallelization}">
         <sequential>
            <for list="1,2,3,4,5,6"
                 param="initial-topology.counter"
                 parallel="${maximize-parallelization}"
                 threadCount="${standalon-container.nb}">
               <sequential>
                  <!-- Endpoint of moved containers are propagated to target containers, in their point of view -->
                  <cli.assert-enpoints-activated endpoint-names="${initial-topology.standalone-container-@{initial-topology.counter}.endpoint}"
                                                 container-name="${initial-topology.standalone-container-@{initial-topology.counter}.name}"
                                                 pov-container.jmx-port="${target-topology.container-@{target-topology.counter}.jmx-port}" />
                  <!-- Endpoint of target containers are propagated to moved containers, in their point of view -->
                  <cli.assert-enpoints-activated endpoint-names="${target-topology.container-@{target-topology.counter}.endpoint}"
                                                 container-name="${target-topology.container-@{target-topology.counter}.name}"
                                                 pov-container.jmx-port="${initial-topology.standalone-container-@{initial-topology.counter}.jmx-port}" />
               </sequential>
            </for>
         </sequential>
      </for>
      <!-- Endpoint of moved containers are propagated to other moved containers, in their point of view -->
      <for list="1,2,3,4,5,6"
           param="initial-topology.counter"
           parallel="${maximize-parallelization}"
           threadCount="${standalon-container.nb}">
         <sequential>
            <for list="1,2,3,4,5,6" param="other-initial-topology.counter" parallel="${maximize-parallelization}">
               <sequential>
                  <cli.assert-enpoints-activated endpoint-names="${initial-topology.standalone-container-@{other-initial-topology.counter}.endpoint}"
                                                 container-name="${initial-topology.standalone-container-@{other-initial-topology.counter}.name}"
                                                 pov-container.jmx-port="${initial-topology.standalone-container-@{initial-topology.counter}.jmx-port}" />
               </sequential>
            </for>
         </sequential>
      </for>

      <!-- Check that, from the containers moved, invocations of services located on containers of the target domain succeed -->
      <echo message="Checking that, from the containers moved, invocations of services located on containers of the target domain succeed ..." />
      <for list="1,2,3,4,5,6"
           param="initial-topology.counter"
           parallel="${maximize-parallelization}"
           threadCount="${standalon-container.nb}">
         <sequential>
            <invoke-rmi-client rmi-consumer-file="${initial-topology.standalone-container-@{initial-topology.counter}.home}/rmi_consumer.properties"
                               invocations-file="${resources.home}/rmi_client/invokes.target.containers.xml" />
         </sequential>
      </for>

      <!-- Check that, from each container of the target topology, an invocation of service located on containers moved succeeds -->
      <echo message="Checking that, from each container of the target topology, an invocation of service located on containers moved succeeds ..." />
      <for list="1,2" param="target-topology.counter" parallel="${maximize-parallelization}">
         <sequential>
            <invoke-rmi-client rmi-consumer-file="${target-topology.container-@{target-topology.counter}.home}/rmi_consumer.properties"
                               invocations-file="${resources.home}/rmi_client/invokes.containers-moved.xml" />
         </sequential>
      </for>

      <!-- Check that, the Fractal component associated to the Petals Registry Overlay cluster of the initial stand-alone container is stopped -->
      <echo message="Checking that, the Fractal component associated to the Petals Registry Overlay cluster of the initial stand-alone container is stopped ..." />
      <fail message="Petals Registry Overlay cluster of the initial stand-alone container not stopped">
         <condition>
            <or>
               <socket server="localhost" port="${initial-topology.standalone-container-1.registry-port}" />
               <socket server="localhost" port="${initial-topology.standalone-container-2.registry-port}" />
               <socket server="localhost" port="${initial-topology.standalone-container-3.registry-port}" />
               <socket server="localhost" port="${initial-topology.standalone-container-4.registry-port}" />
               <socket server="localhost" port="${initial-topology.standalone-container-5.registry-port}" />
               <socket server="localhost" port="${initial-topology.standalone-container-6.registry-port}" />
            </or>
         </condition>
      </fail>

   </target>

   <!-- 4) Restart the containers moved -->
   <target name="restart-containers-moved">
      <echo message="**************************************" />
      <echo message="*** 3) Restart the containers moved ***" />
      <echo message="**************************************" />
      <for list="1,2,3,4,5,6"
           param="initial-topology.counter"
           parallel="${maximize-parallelization}"
           threadCount="${standalon-container.nb}">
         <sequential>
            <local-stop-container name="${initial-topology.standalone-container-@{initial-topology.counter}.name}"
                                  home="${initial-topology.standalone-container-@{initial-topology.counter}.home}" />
            <startContainer home="${initial-topology.standalone-container-@{initial-topology.counter}.home}"
                            name="${initial-topology.standalone-container-@{initial-topology.counter}.name}"
                            jmx.port="${initial-topology.standalone-container-@{initial-topology.counter}.jmx-port}" />
         </sequential>
      </for>

      <!-- Check that each container moved is restarted in the right domain -->
      <for list="1,2,3,4,5,6"
           param="initial-topology.counter"
           parallel="${maximize-parallelization}"
           threadCount="${standalon-container.nb}">
         <sequential>
            <echo message="Checking that the container moved and restarted '${initial-topology.standalone-container-@{initial-topology.counter}.name}' is in the right domain '${target-topology.domain-name}'" />
            <cli.assert-container-in-domain domain-name="${target-topology.domain-name}"
                                            container.jmx-port="${initial-topology.standalone-container-@{initial-topology.counter}.jmx-port}" />
         </sequential>
      </for>

      <!-- Check that all containers of the target domain are known by the container moved in its point of view -->
      <echo message="Checking that all containers of the target domain are known by the container moved in its point of view ..." />
      <cli.assert-containers-known-by-container container-names-to-check="${target-topology.container-1.name},${target-topology.container-2.name}"
                                                from-container.jmx-port="${initial-topology.standalone-container-1.jmx-port}"
                                                container-names-filter="target-container-[01]" />

      <!-- Check that, the Fractal component associated to the Petals Registry Overlay cluster of the initial stand-alone container is not started -->
      <for list="1,2,3,4,5,6"
           param="initial-topology.counter"
           parallel="${maximize-parallelization}"
           threadCount="${standalon-container.nb}">
         <sequential>
            <echo message="Checking that, the Fractal component associated to the Petals Registry Overlay cluster of the initial stand-alone container '${initial-topology.standalone-container-@{initial-topology.counter}.name}' is not started ..." />
            <fail message="Petals Registry Overlay cluster of the initial stand-alone container '${initial-topology.standalone-container-@{initial-topology.counter}.name}' is started">
               <condition>
                  <socket server="localhost"
                          port="${initial-topology.standalone-container-@{initial-topology.counter}.registry-port}" />
               </condition>
            </fail>
         </sequential>
      </for>
   </target>

   <!-- 4) Detach the containers, using Petals CLI -->
   <target name="detach-container">
      <echo message="********************************************************************" />
      <echo message="*** 4) Detach 6 containers of the topology, using Petals CLI ***" />
      <echo message="********************************************************************" />
      <echo message="Detaching containers ..." />
      <parallel>
         <cli.detach-container container-to-detach.jmx-port="${target-topology.container-2.jmx-port}" />
         <cli.detach-container container-to-detach.jmx-port="${initial-topology.standalone-container-2.jmx-port}" />
         <cli.detach-container container-to-detach.jmx-port="${initial-topology.standalone-container-3.jmx-port}" />
         <cli.detach-container container-to-detach.jmx-port="${initial-topology.standalone-container-4.jmx-port}" />
         <cli.detach-container container-to-detach.jmx-port="${initial-topology.standalone-container-5.jmx-port}" />
         <cli.detach-container container-to-detach.jmx-port="${initial-topology.standalone-container-6.jmx-port}" />
      </parallel>

      <!-- We wait 5s that all modifications are propagated to all containers -->
      <sleep seconds="5" />

      <!-- Check that the containers detached are not known by the remaing containers, in their point of view -->
      <echo message="Checking that the containers detached are not known by the remaining containers, in their point of view ..." />
      <parallel>
         <!-- On one remaining container -->
         <for list="2,3,4,5,6"
              param="container.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <cli.assert-container-not-known-by-container unknown-container-name="${initial-topology.standalone-container-@{container.counter}.name}"
                                                            from-container.jmx-port="${target-topology.container-1.jmx-port}" />
            </sequential>
         </for>
         <cli.assert-container-not-known-by-container unknown-container-name="${target-topology.container-2.name}"
                                                      from-container.jmx-port="${target-topology.container-1.jmx-port}" />

         <!-- On other remaining container -->
         <for list="2,3,4,5,6"
              param="container.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <cli.assert-container-not-known-by-container unknown-container-name="${initial-topology.standalone-container-@{container.counter}.name}"
                                                            from-container.jmx-port="${initial-topology.standalone-container-1.jmx-port}" />
            </sequential>
         </for>
         <cli.assert-container-not-known-by-container unknown-container-name="${target-topology.container-2.name}"
                                                      from-container.jmx-port="${initial-topology.standalone-container-1.jmx-port}" />
      </parallel>

      <!-- Check that detached containers are now standalone containers -->
      <echo message="Checking that the containers detached are now standalone containers ..." />
      <parallel>
         <for list="2,3,4,5,6"
              param="container.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <!-- A detached container uses keep the same registry overlay servers, but a different topology instance than before to be detached -->
               <cli.assert-container-standalone container-name="${initial-topology.standalone-container-@{container.counter}.name}"
                                                jmx-port="${initial-topology.standalone-container-@{container.counter}.jmx-port}" />
            </sequential>
         </for>
         <!-- A detached container uses keep the same registry overlay servers, but a different topology instance than before to be detached -->
         <cli.assert-container-standalone container-name="${target-topology.container-2.name}"
                                          jmx-port="${target-topology.container-2.jmx-port}" />
      </parallel>

      <!-- Check that all endpoints of the container detached are no more registered on all remaining containers -->
      <echo message="Checking that all endpoints of the containers detached are no more registered on all remaining containers ..." />
      <parallel>
         <!-- On one remaining container -->
         <for list="2,3,4,5,6"
              param="container.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <cli.assert-enpoints-not-activated endpoint-names="${initial-topology.standalone-container-@{other-initial-topology.counter}.endpoint}"
                                                  pov-container.jmx-port="${target-topology.container-1.jmx-port}" />
            </sequential>
         </for>
         <cli.assert-container-not-known-by-container unknown-container-name="${target-topology.container-2.endpoint}"
                                                      from-container.jmx-port="${target-topology.container-1.jmx-port}" />

         <!-- On other remaining container -->
         <for list="2,3,4,5,6"
              param="container.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <cli.assert-enpoints-not-activated endpoint-names="${initial-topology.standalone-container-@{other-initial-topology.counter}.endpoint}"
                                                  pov-container.jmx-port="${initial-topology.standalone-container-1.jmx-port}" />
            </sequential>
         </for>
         <cli.assert-container-not-known-by-container unknown-container-name="${target-topology.container-2.endpoint}"
                                                      from-container.jmx-port="${initial-topology.standalone-container-1.jmx-port}" />
      </parallel>

      <!-- Check that all endpoints of the remaining containers are no more registered on the containers detached -->
      <echo message="Checking that all endpoints of the remaining containers are no more registered on the containers detached ..." />
      <parallel>
         <for list="2,3,4,5,6"
              param="container.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <cli.assert-enpoints-not-activated endpoint-names="${initial-topology.standalone-container-1.endpoint}"
                                                  pov-container.jmx-port="${initial-topology.standalone-container-@{container.counter}.jmx-port}" />
               <cli.assert-enpoints-not-activated endpoint-names="${target-topology.container-1.endpoint}"
                                                  pov-container.jmx-port="${initial-topology.standalone-container-@{container.counter}.jmx-port}" />
            </sequential>
         </for>
         <cli.assert-enpoints-not-activated endpoint-names="${initial-topology.standalone-container-1.endpoint}"
                                            pov-container.jmx-port="${target-topology.container-2.jmx-port}" />
         <cli.assert-enpoints-not-activated endpoint-names="${target-topology.container-1.endpoint}"
                                            pov-container.jmx-port="${target-topology.container-2.jmx-port}" />
      </parallel>

      <!-- Check that, from remaining containers of the topology, an invocation of a service located on each detached container fails. -->
      <echo message="Checking that, from remaining containers of the topology, an invocation of a service located on each detached container fails ..." />
      <parallel>
         <for list="2,3,4,5,6"
              param="container.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <invoke-rmi-client rmi-consumer-file="${initial-topology.standalone-container-1.home}/rmi_consumer.properties"
                                  invocations-file="${resources.home}/rmi_client/invokes.initial-topology.container-detached-@{container.counter}.xml" />
            </sequential>
         </for>
         <for list="2,3,4,5,6"
              param="container.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <invoke-rmi-client rmi-consumer-file="${target-topology.container-1.home}/rmi_consumer.properties"
                                  invocations-file="${resources.home}/rmi_client/invokes.initial-topology.container-detached-@{container.counter}.xml" />
            </sequential>
         </for>
      </parallel>

      <!-- Check that, from each container detached, an invocation of a service located on a remaining containers of the topology fails,
		     and an invocation of itself succeeds -->
      <echo message="Checking that, from each the container detached, an invocation of a service located on a remaining container of the topology fails, and an invocation of itself succeeds ..." />
      <parallel>
         <for list="2,3,4,5,6"
              param="container.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <invoke-rmi-client rmi-consumer-file="${initial-topology.standalone-container-@{container.counter}.home}/rmi_consumer.properties"
                                  invocations-file="${resources.home}/rmi_client/invokes.remaining-containers.from-others.xml" />
               <invoke-rmi-client rmi-consumer-file="${initial-topology.standalone-container-@{container.counter}.home}/rmi_consumer.properties"
                                  invocations-file="${initial-topology.standalone-container-@{container.counter}.home}/invokes.my-self.xml" />
            </sequential>
         </for>
         <invoke-rmi-client rmi-consumer-file="${target-topology.container-2.home}/rmi_consumer.properties"
                            invocations-file="${resources.home}/rmi_client/invokes.remaining-containers.from-others.xml" />
         <invoke-rmi-client rmi-consumer-file="${target-topology.container-2.home}/rmi_consumer.properties"
                            invocations-file="${target-topology.container-2.home}/invokes.my-self.xml" />
      </parallel>
   </target>

   <!-- 5) Restart the container detached -->
   <target name="restart-container-detached">
      <echo message="*****************************************" />
      <echo message="*** 5) Restart the containers detached ***" />
      <echo message="*****************************************" />
      <echo message="Restarting detached containers ..." />
      <!-- Before to stop, we retrieve the new domain name of containers detached -->
      <parallel>
         <for list="2,3,4,5,6"
              param="container.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <xmlproperty file="${initial-topology.standalone-container-@{container.counter}.home}/data/work/topology.xml"
                            prefix="initial-topology.standalone-container-@{container.counter}.new-topo"
                            collapseAttributes="true" />
               <property name="initial-topology.standalone-container-@{container.counter}.new-topo.registration-name"
                         value="${initial-topology.standalone-container-@{container.counter}.new-topo.topology.domain.name}">
               </property>
               <echo message="The topology of the detached container '${initial-topology.standalone-container-@{container.counter}.name}' is registered with the name: ${initial-topology.standalone-container-@{container.counter}.new-topo.registration-name}" />
               <registry-cli.topology-is-registered port="${target-topology.registry-port}"
                                                    group="target-registry-overlay"
                                                    password="target-s3cr3t"
                                                    topology="${initial-topology.standalone-container-@{container.counter}.new-topo.registration-name}" />

               <local-stop-container name="${initial-topology.standalone-container-@{container.counter}.name}"
                                     home="${initial-topology.standalone-container-@{container.counter}.home}" />

               <echo message="Using Petals Registry CLI, check that the topology of the stopped detached container '${initial-topology.standalone-container-@{container.counter}.name}' is no more registered" />
               <registry-cli.topology-is-not-registered port="${target-topology.registry-port}"
                                                        group="target-registry-overlay"
                                                        password="target-s3cr3t"
                                                        topology="${initial-topology.standalone-container-@{container.counter}.new-topo.registration-name}" />

               <startContainer home="${initial-topology.standalone-container-@{container.counter}.home}"
                               name="${initial-topology.standalone-container-@{container.counter}.name}"
                               jmx.port="${initial-topology.standalone-container-@{container.counter}.jmx-port}" />
            </sequential>
         </for>
         <sequential>
            <xmlproperty file="${target-topology.container-2.home}/data/work/topology.xml"
                         prefix="target-topology.container-2.new-topo"
                         collapseAttributes="true" />
            <property name="target-topology.container-2.new-topo.registration-name"
                      value="${target-topology.container-2.new-topo.topology.domain.name}">
            </property>
            <echo message="The topology of the detached container '${target-topology.container-2.name}' is registered with the name: ${target-topology.container-2.new-topo.registration-name}" />
            <registry-cli.topology-is-registered port="${target-topology.registry-port}"
                                                 group="target-registry-overlay"
                                                 password="target-s3cr3t"
                                                 topology="${target-topology.container-2.new-topo.registration-name}" />

            <local-stop-container name="${target-topology.container-2.name}"
                                  home="${target-topology.container-2.home}" />

            <echo message="Using Petals Registry CLI, check that the topology of the stopped detached container '${target-topology.container-2.name}' is no more registered" />
            <registry-cli.topology-is-not-registered port="${target-topology.registry-port}"
                                                     group="target-registry-overlay"
                                                     password="target-s3cr3t"
                                                     topology="${target-topology.container-2.new-topo.registration-name}" />

            <startContainer home="${target-topology.container-2.home}"
                            name="${target-topology.container-2.name}"
                            jmx.port="${target-topology.container-2.jmx-port}" />
         </sequential>
      </parallel>

      <!-- Check that the containers detached are restarted in their domain -->
      <echo message="Checking that the containers detached are restarted in their domain ..." />
      <for list="2,3,4,5,6"
           param="container.counter"
           parallel="${maximize-parallelization}"
           threadCount="${standalon-container.nb}">
         <sequential>
            <cli.assert-container-in-domain domain-name="${initial-topology.standalone-container-@{container.counter}.new-topo.registration-name}"
                                            container.jmx-port="${initial-topology.standalone-container-@{container.counter}.jmx-port}" />
         </sequential>
      </for>
      <cli.assert-container-in-domain domain-name="${target-topology.container-2.new-topo.registration-name}"
                                      container.jmx-port="${target-topology.container-2.jmx-port}" />

      <!-- Check that the containers detached are not known by the remaining containers of the topology -->
      <echo message="Checking that the containers detached are not known by the remaining containers, in their point of view ..." />
      <parallel>
         <!-- On one remaining container -->
         <for list="2,3,4,5,6"
              param="container.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <cli.assert-container-not-known-by-container unknown-container-name="${initial-topology.standalone-container-@{container.counter}.name}"
                                                            from-container.jmx-port="${target-topology.container-1.jmx-port}" />
            </sequential>
         </for>
         <cli.assert-container-not-known-by-container unknown-container-name="${target-topology.container-2.name}"
                                                      from-container.jmx-port="${target-topology.container-1.jmx-port}" />

         <!-- On other remaining container -->
         <for list="2,3,4,5,6"
              param="container.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <cli.assert-container-not-known-by-container unknown-container-name="${initial-topology.standalone-container-@{container.counter}.name}"
                                                            from-container.jmx-port="${initial-topology.standalone-container-1.jmx-port}" />
            </sequential>
         </for>
         <cli.assert-container-not-known-by-container unknown-container-name="${target-topology.container-2.name}"
                                                      from-container.jmx-port="${initial-topology.standalone-container-1.jmx-port}" />
      </parallel>
   </target>

   <!-- 6) Restart the remaining containers of the topology -->
   <target name="restart-remaining-containers">
      <echo message="***********************************************************" />
      <echo message="*** 6) Restart the remaining containers of the topology ***" />
      <echo message="***********************************************************" />
      <parallel>
         <sequential>
            <local-stop-container name="${target-topology.container-1.name}"
                                  home="${target-topology.container-1.home}" />
            <startContainer home="${target-topology.container-1.home}"
                            name="${target-topology.container-1.name}"
                            jmx.port="${target-topology.container-1.jmx-port}" />
         </sequential>
         <sequential>
            <local-stop-container name="${initial-topology.standalone-container-1.name}"
                                  home="${initial-topology.standalone-container-1.home}" />
            <startContainer home="${initial-topology.standalone-container-1.home}"
                            name="${initial-topology.standalone-container-1.name}"
                            jmx.port="${initial-topology.standalone-container-1.jmx-port}" />
         </sequential>
      </parallel>

      <!-- Check that the restarted containers are restarted in the same domain: the target topology domain -->
      <echo message="Checking that the initial container is restarted in the right domain ..." />
      <parallel>
         <cli.assert-container-in-domain domain-name="${target-topology.domain-name}"
                                         container.jmx-port="${initial-topology.standalone-container-1.jmx-port}" />
         <cli.assert-container-in-domain domain-name="${target-topology.domain-name}"
                                         container.jmx-port="${target-topology.container-1.jmx-port}" />
      </parallel>

      <!-- Check that remaining containers are known by themselves -->
      <echo message="Checking that remaining containers are known by themselves ..." />
      <parallel>
         <cli.assert-containers-known-by-container container-names-to-check="${initial-topology.standalone-container-1.name}"
                                                   from-container.jmx-port="${target-topology.container-1.jmx-port}" />
         <cli.assert-containers-known-by-container container-names-to-check="${target-topology.container-1.name}"
                                                   from-container.jmx-port="${initial-topology.standalone-container-1.jmx-port}" />
      </parallel>

      <!-- Check that remaining containers are not known by the containers detached -->
      <echo message="Checking that remaining containers are not known by the containers detached ..." />
      <parallel>
         <!-- For one remaining container -->
         <for list="2,3,4,5,6"
              param="container.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <cli.assert-container-not-known-by-container unknown-container-name="${target-topology.container-1.name}"
                                                            from-container.jmx-port="${initial-topology.standalone-container-@{container.counter}.jmx-port}" />
            </sequential>
         </for>
         <cli.assert-container-not-known-by-container unknown-container-name="${target-topology.container-1.name}"
                                                      from-container.jmx-port="${target-topology.container-2.jmx-port}" />

         <!-- For other remaining container -->
         <for list="2,3,4,5,6"
              param="container.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <cli.assert-container-not-known-by-container unknown-container-name="${initial-topology.standalone-container-1.name}"
                                                            from-container.jmx-port="${initial-topology.standalone-container-@{container.counter}.jmx-port}" />
            </sequential>
         </for>
         <cli.assert-container-not-known-by-container unknown-container-name="${initial-topology.standalone-container-1.name}"
                                                      from-container.jmx-port="${target-topology.container-2.jmx-port}" />
      </parallel>

   </target>

   <!-- 7) Concurrent attachments and detachments -->
   <target name="concurrent-attachments-detachments">
      <echo message="*************************************************" />
      <echo message="*** 7) Concurrent attachments and detachments ***" />
      <echo message="*************************************************" />

      <!-- TODO: replace <sequential> by <parallel> -->
      <sequential>
         <for list="1,2,3"
              param="container-id"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <!-- Attachment/Detachment of 'initial-topology.standalone-container-[123]' using 'target-topology.container-1'-->
               <for list="0,1,2,3,4,5,6,7,8,9" parallel="false" param="counter">
                  <sequential>
                     <echo message="Iteration #@{counter}: Attach container '${initial-topology.standalone-container-@{container-id}.name}'" />
                     <cli.attach-container container-to-attach.jmx-port="${initial-topology.standalone-container-@{container-id}.jmx-port}"
                                           target-domain-name="${target-topology.domain-name}"
                                           target-container.name="${target-topology.container-1.name}"
                                           target-container.passphrase="${target-topology.container-1.passphrase}"
                                           target-container.jmx-port="${target-topology.container-1.jmx-port}" />
                     <!-- We wait 5s that all modifications are propagated to all containers -->
                     <sleep seconds="5" />

                     <echo message="Iteration #@{counter}: Detach container '${initial-topology.standalone-container-@{container-id}.name}'" />
                     <cli.detach-container container-to-detach.jmx-port="${initial-topology.standalone-container-@{container-id}.jmx-port}" />
                     <!-- We wait 5s that all modifications are propagated to all containers -->
                     <sleep seconds="5" />
                  </sequential>
               </for>
            </sequential>
         </for>

         <for list="4,5,6"
              param="container-id"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <!-- Attachment/Detachment of 'initial-topology.standalone-container-[456]' using 'target-topology.container-2'-->
               <for list="0,1,2,3,4,5,6,7,8,9" parallel="false" param="counter">
                  <sequential>
                     <echo message="Iteration #@{counter}: Attach container '${initial-topology.standalone-container-@{container-id}.name}'" />
                     <cli.attach-container container-to-attach.jmx-port="${initial-topology.standalone-container-@{container-id}.jmx-port}"
                                           target-domain-name="${target-topology.domain-name}"
                        target-container.name="${target-topology.container-2.name}"
                        target-container.passphrase="${target-topology.container-2.passphrase}"
                        target-container.jmx-port="${target-topology.container-2.jmx-port}" />
                     <!-- We wait 5s that all modifications are propagated to all containers -->
                     <sleep seconds="5" />

                     <echo message="Iteration #@{counter}: Detach container '${initial-topology.standalone-container-@{container-id}.name}'" />
                     <cli.detach-container container-to-detach.jmx-port="${initial-topology.standalone-container-@{container-id}.jmx-port}" />
                     <!-- We wait 5s that all modifications are propagated to all containers -->
                     <sleep seconds="5" />
                  </sequential>
               </for>
            </sequential>
         </for>
      </sequential>

      <!-- Check that the containers detached are not known by the remaing containers, in their point of view -->
      <echo message="Checking that the containers detached are not known by the remaining containers, in their point of view ..." />
      <parallel>
         <!-- On one remaining container -->
         <for list="1,2,3,4,5,6"
              param="container.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <cli.assert-container-not-known-by-container unknown-container-name="${initial-topology.standalone-container-@{container.counter}.name}"
                                                            from-container.jmx-port="${target-topology.container-1.jmx-port}" />
            </sequential>
         </for>

         <!-- On other remaining container -->
         <for list="1,2,3,4,5,6"
              param="container.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <cli.assert-container-not-known-by-container unknown-container-name="${initial-topology.standalone-container-@{container.counter}.name}"
                                                            from-container.jmx-port="${target-topology.container-2.jmx-port}" />
            </sequential>
         </for>
      </parallel>

      <!-- Check that, from remaining containers of the topology, an invocation of a service located on each detached container fails. -->
      <echo message="Checking that, from remaining containers of the topology, an invocation of a service located on each detached container fails ..." />
      <parallel>
         <for list="1,2,3,4,5,6"
              param="container.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <invoke-rmi-client rmi-consumer-file="${target-topology.container-1.home}/rmi_consumer.properties"
                                  invocations-file="${resources.home}/rmi_client/invokes.initial-topology.container-detached-@{container.counter}.xml" />
            </sequential>
         </for>
         <for list="1,2,3,4,5,6"
              param="container.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <invoke-rmi-client rmi-consumer-file="${target-topology.container-2.home}/rmi_consumer.properties"
                                  invocations-file="${resources.home}/rmi_client/invokes.initial-topology.container-detached-@{container.counter}.xml" />
            </sequential>
         </for>
      </parallel>

      <!-- Check that, from each container detached, an invocation of a service located on a remaining containers of the topology fails,
		     and an invocation of itself succeeds -->
      <echo message="Checking that, from each the container detached, an invocation of a service located on a remaining container of the topology fails, and an invocation of itself succeeds ..." />
      <parallel>
         <for list="1,2,3,4,5,6"
              param="container.counter"
              parallel="${maximize-parallelization}"
              threadCount="${standalon-container.nb}">
            <sequential>
               <invoke-rmi-client rmi-consumer-file="${initial-topology.standalone-container-@{container.counter}.home}/rmi_consumer.properties"
                                  invocations-file="${resources.home}/rmi_client/invokes.target.containers.fails.xml" />
            </sequential>
         </for>
      </parallel>
   </target>

   <!-- Clean the test working area -->
   <target name="clean" depends="run">
      <local-stop-container name="${initial-topology.standalone-container-1.name}"
                            home="${initial-topology.standalone-container-1.home}" />
      <local-stop-container name="${initial-topology.standalone-container-2.name}"
                            home="${initial-topology.standalone-container-2.home}" />
      <local-stop-container name="${initial-topology.standalone-container-3.name}"
                            home="${initial-topology.standalone-container-3.home}" />
      <local-stop-container name="${initial-topology.standalone-container-4.name}"
                            home="${initial-topology.standalone-container-4.home}" />
      <local-stop-container name="${initial-topology.standalone-container-5.name}"
                            home="${initial-topology.standalone-container-5.home}" />
      <local-stop-container name="${initial-topology.standalone-container-6.name}"
                            home="${initial-topology.standalone-container-6.home}" />
      <local-stop-container name="${target-topology.container-1.name}" home="${target-topology.container-1.home}" />
      <local-stop-container name="${target-topology.container-2.name}" home="${target-topology.container-2.home}" />
      <antcall target="stopTargetRegistry" />
   </target>

   <macrodef name="invoke-rmi-client">
      <attribute name="rmi-consumer-file" />
      <attribute name="invocations-file" />
      <sequential>
         <local name="return-code" />
         <java classname="com.ebmwebsoucing.integration.client.rmi.RMIClient"
               fork="true"
               resultproperty="return-code"
               classpath="${maven.runtime.classpath}">
            <arg value="-integration" />
            <arg value="@{invocations-file}" />
            <arg value="-properties" />
            <arg value="@{rmi-consumer-file}" />
         </java>

         <fail message="Fail to invoke RMI client tests '@{invocations-file}' from '@{rmi-consumer-file}'">
            <condition>
               <not>
                  <equals arg1="0" arg2="${return-code}" />
               </not>
            </condition>
         </fail>
      </sequential>
   </macrodef>

</project>