package org.safehaus.subutai.impl.manager.builder;


import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.safehaus.subutai.api.container.ContainerManager;
import org.safehaus.subutai.api.manager.helper.Environment;
import org.safehaus.subutai.api.manager.helper.EnvironmentBlueprint;
import org.safehaus.subutai.api.manager.helper.NodeGroup;
import org.safehaus.subutai.api.manager.helper.PlacementStrategyENUM;
import org.safehaus.subutai.impl.manager.exception.EnvironmentBuildException;
import org.safehaus.subutai.impl.manager.exception.EnvironmentInstanceDestroyException;
import org.safehaus.subutai.shared.protocol.Agent;


/**
 * Created by bahadyr on 6/23/14.
 */
public class EnvironmentBuilder {


    public Environment build( final EnvironmentBlueprint blueprint, ContainerManager containerManager )
            throws EnvironmentBuildException {
        Environment environment = new Environment();
        environment.setName( blueprint.getName() );
        for ( NodeGroup nodeGroup : blueprint.getNodeGroups() ) {
            PlacementStrategyENUM e1 = nodeGroup.getPlacementStrategyENUM();
            Collection<Agent> hosts = new HashSet<Agent>();
            int nodeCount = 3;

            Set<Agent> agentList =
                    containerManager.clone( environment.getUuid(), nodeGroup.getTemplateName(), nodeCount, hosts, e1 );

            environment.getAgents().addAll( agentList );
        }

        return environment;
    }


    public void destroy( final Environment environment ) throws EnvironmentInstanceDestroyException {
        //TODO destroy environment code goes here
        //        for ( EnvironmentNodeGroup nodeGroup : environment.getEnvironmentNodeGroups() ) {
        //            nodeGroupBuilder.destroy( nodeGroup );
        //        }
    }
}

@@ -123,17 +123,16 @@
 il.Set;%0A
-%0A
 import o
@@ -183,16 +183,79 @@
 anager;%0A
+import org.safehaus.subutai.api.lxcmanager.LxcCreateException;%0A
 import o
@@ -1323,18 +1323,17 @@
 gentList
- =
+;
 %0A       
@@ -1336,24 +1336,31 @@
         
+ try %7B%0A
         
  contain
@@ -1351,16 +1351,35 @@
         
+        agentList =
  contain
@@ -1394,17 +1394,16 @@
 r.clone(
- 
 environm
@@ -1466,19 +1466,145 @@
 osts, e1
- 
 );
+%0A            %7D catch(LxcCreateException ex) %7B%0A                throw new EnvironmentBuildException(ex.toString());%0A            %7D
 %0A%0A      

fix exception handling for environment manager