Added proper comments and corrected formatting.

Project: http://git-wip-us.apache.org/repos/asf/stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/94cf7564
Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/94cf7564
Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/94cf7564

Branch: refs/heads/gsoc-projects-2015
Commit: 94cf75648d996955cd105aa9cc9e204500cff9ec
Parents: 5dba1b4
Author: swapnilpatilRajaram <[email protected]>
Authored: Sun Jun 14 14:41:29 2015 +0000
Committer: swapnilpatilRajaram <[email protected]>
Committed: Sun Jun 14 14:41:29 2015 +0000

----------------------------------------------------------------------
 .../apache/stratos/aws/extension/AWSHelper.java | 400 ++++++++++---------
 .../stratos/aws/extension/AWSLoadBalancer.java  | 295 +++++++-------
 2 files changed, 361 insertions(+), 334 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/94cf7564/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSHelper.java
----------------------------------------------------------------------
diff --git 
a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSHelper.java
 
b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSHelper.java
index 15467f2..16ac7ae 100644
--- 
a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSHelper.java
+++ 
b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSHelper.java
@@ -32,283 +32,305 @@ import com.amazonaws.ClientConfiguration;
 import com.amazonaws.auth.BasicAWSCredentials;
 import 
com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancingClient;
 import com.amazonaws.services.elasticloadbalancing.model.*;
-import com.amazonaws.services.opsworks.model.AttachElasticLoadBalancerRequest;
 
-
-public class AWSHelper 
-{
+public class AWSHelper {
        private String awsAccessKey;
        private String awsSecretKey;
-       private String httpProxy;
        private String availabilityZone;
 
        private BasicAWSCredentials awsCredentials;
        private ClientConfiguration clientConfiguration;
-       
+
        private static final Log log = LogFactory.getLog(AWSHelper.class);
-       
-       public AWSHelper()
-       {
+
+       public AWSHelper() {
                // Read values for awsAccessKey, awsSecretKey etc. from config 
file
                // Throw a proper exception / log warning if cant read 
credentials ?
-               
+
                awsCredentials = new BasicAWSCredentials(awsAccessKey, 
awsSecretKey);
                clientConfiguration = new ClientConfiguration();
        }
-       
-       /*
-        * Creates a load balancer and returns its DNS name.
-        * Useful when a new cluster is added.
+
+       /**
+        * Creates a load balancer and returns its DNS name. Useful when a new
+        * cluster is added.
+        * 
+        * @param name
+        * @param listeners
+        * @return DNS name of newly created load balancer
         */
-       public String createLoadBalancer(String name, List<Listener> listeners)
-       {
-               try
-               {
-                       CreateLoadBalancerRequest createLoadBalancerRequest = 
new CreateLoadBalancerRequest(name);
-                       
+       public String createLoadBalancer(String name, List<Listener> listeners) 
{
+               try {
+                       CreateLoadBalancerRequest createLoadBalancerRequest = 
new CreateLoadBalancerRequest(
+                                       name);
+
                        createLoadBalancerRequest.setListeners(listeners);
-                       
+
                        Set<String> availabilityZones = new HashSet<String>();
                        availabilityZones.add(availabilityZone);
-                       
+
                        
createLoadBalancerRequest.setAvailabilityZones(availabilityZones);
-                       
-                       AmazonElasticLoadBalancingClient lbClient = new 
AmazonElasticLoadBalancingClient(awsCredentials, clientConfiguration);
-                       
-                       CreateLoadBalancerResult clbResult = 
lbClient.createLoadBalancer(createLoadBalancerRequest);
-                       
+
+                       AmazonElasticLoadBalancingClient lbClient = new 
AmazonElasticLoadBalancingClient(
+                                       awsCredentials, clientConfiguration);
+
+                       CreateLoadBalancerResult clbResult = lbClient
+                                       
.createLoadBalancer(createLoadBalancerRequest);
+
                        return clbResult.getDNSName();
-               }
-               catch(Exception e)
-               {
+               } catch (Exception e) {
                        log.error("Could not create load balancer : " + name + 
".");
                        return null;
                }
        }
-       
-       /*
-        * Deletes a load balancer with the name provided.
-        * Useful when a cluster, with which this load balancer was associated, 
is removed.
+
+       /**
+        * Deletes the load balancer with the name provided. Useful when a 
cluster,
+        * with which this load balancer was associated, is removed.
+        * 
+        * @param loadBalancerName
         */
-       public void deleteLoadBalancer(String loadBalancerName)
-       {
-               try
-               {
+       public void deleteLoadBalancer(String loadBalancerName) {
+               try {
                        DeleteLoadBalancerRequest deleteLoadBalancerRequest = 
new DeleteLoadBalancerRequest();
                        
deleteLoadBalancerRequest.setLoadBalancerName(loadBalancerName);
-                       
-                       AmazonElasticLoadBalancingClient lbClient = new 
AmazonElasticLoadBalancingClient(awsCredentials, clientConfiguration);
-                       
+
+                       AmazonElasticLoadBalancingClient lbClient = new 
AmazonElasticLoadBalancingClient(
+                                       awsCredentials, clientConfiguration);
+
                        lbClient.deleteLoadBalancer(deleteLoadBalancerRequest);
-               }
-               catch(Exception e)
-               {
+               } catch (Exception e) {
                        log.error("Could not delete load balancer : " + 
loadBalancerName);
                }
        }
-       
-       /*
-        * Attaches provided instances to the load balancer.
-        * Useful when new instances get added to the cluster with which this 
load balancer is associated.
+
+       /**
+        * Attaches provided instances to the load balancer. Useful when new
+        * instances get added to the cluster with which this load balancer is
+        * associated.
+        * 
+        * @param loadBalancerName
+        * @param instances
         */
-       public void registerInstancesToLoadBalancer(String loadBalancerName, 
List<Instance> instances)
-       {
-               try
-               {
-                       RegisterInstancesWithLoadBalancerRequest 
registerInstancesWithLoadBalancerRequest = new 
RegisterInstancesWithLoadBalancerRequest(loadBalancerName, instances);
-               
-                       AmazonElasticLoadBalancingClient lbClient = new 
AmazonElasticLoadBalancingClient(awsCredentials, clientConfiguration);
-                       
-                       RegisterInstancesWithLoadBalancerResult result = 
lbClient.registerInstancesWithLoadBalancer(registerInstancesWithLoadBalancerRequest);
-               }
-               catch(Exception e)
-               {
-                       log.error("Could not register instances to load 
balancer " + loadBalancerName);
+       public void registerInstancesToLoadBalancer(String loadBalancerName,
+                       List<Instance> instances) {
+               try {
+                       RegisterInstancesWithLoadBalancerRequest 
registerInstancesWithLoadBalancerRequest = new 
RegisterInstancesWithLoadBalancerRequest(
+                                       loadBalancerName, instances);
+
+                       AmazonElasticLoadBalancingClient lbClient = new 
AmazonElasticLoadBalancingClient(
+                                       awsCredentials, clientConfiguration);
+
+                       RegisterInstancesWithLoadBalancerResult result = 
lbClient
+                                       
.registerInstancesWithLoadBalancer(registerInstancesWithLoadBalancerRequest);
+               } catch (Exception e) {
+                       log.error("Could not register instances to load 
balancer "
+                                       + loadBalancerName);
                }
        }
-       
-       /*
-        * Detaches provided instances from the load balancer, associated with 
some cluster.
-        * Useful when instances are removed from the cluster with which this 
load balancer is associated.
+
+       /**
+        * Detaches provided instances from the load balancer, associated with 
some
+        * cluster. Useful when instances are removed from the cluster with 
which
+        * this load balancer is associated.
+        * 
+        * @param loadBalancerName
+        * @param instances
         */
-       public void deregisterInstancesFromLoadBalancer(String 
loadBalancerName, List<Instance> instances)
-       {
-               try
-               {
-                       DeregisterInstancesFromLoadBalancerRequest 
deregisterInstancesFromLoadBalancerRequest = new 
DeregisterInstancesFromLoadBalancerRequest(loadBalancerName, instances);
-               
-                       AmazonElasticLoadBalancingClient lbClient = new 
AmazonElasticLoadBalancingClient(awsCredentials, clientConfiguration);
-                       
-                       DeregisterInstancesFromLoadBalancerResult result = 
lbClient.deregisterInstancesFromLoadBalancer(deregisterInstancesFromLoadBalancerRequest);
-               }
-               catch(Exception e)
-               {
-                       log.error("Could not de-register instances from load 
balancer " + loadBalancerName);
+       public void deregisterInstancesFromLoadBalancer(String loadBalancerName,
+                       List<Instance> instances) {
+               try {
+                       DeregisterInstancesFromLoadBalancerRequest 
deregisterInstancesFromLoadBalancerRequest = new 
DeregisterInstancesFromLoadBalancerRequest(
+                                       loadBalancerName, instances);
+
+                       AmazonElasticLoadBalancingClient lbClient = new 
AmazonElasticLoadBalancingClient(
+                                       awsCredentials, clientConfiguration);
+
+                       DeregisterInstancesFromLoadBalancerResult result = 
lbClient
+                                       
.deregisterInstancesFromLoadBalancer(deregisterInstancesFromLoadBalancerRequest);
+               } catch (Exception e) {
+                       log.error("Could not de-register instances from load 
balancer "
+                                       + loadBalancerName);
                }
        }
-       
-       /*
-        * Returns description of the Load Balancer which is helpful in 
determining instances, listeners associated with load balancer 
+
+       /**
+        * Returns description of the load balancer which is helpful in 
determining
+        * instances, listeners associated with load balancer
+        * 
+        * @param loadBalancerName
+        * @return description of the load balancer
         */
-       private LoadBalancerDescription getLoadBalancerDescription(String 
loadBalancerName)
-       {
+       private LoadBalancerDescription getLoadBalancerDescription(
+                       String loadBalancerName) {
                List<String> loadBalancers = new ArrayList<String>();
-               
+
                loadBalancers.add(loadBalancerName);
-               
-               DescribeLoadBalancersRequest describeLoadBalancersRequest = new 
DescribeLoadBalancersRequest(loadBalancers);
-
-               AmazonElasticLoadBalancingClient lbClient = new 
AmazonElasticLoadBalancingClient(awsCredentials, clientConfiguration);
-               
-               DescribeLoadBalancersResult result = 
lbClient.describeLoadBalancers(describeLoadBalancersRequest);
-               
-               if(result.getLoadBalancerDescriptions() == null || 
result.getLoadBalancerDescriptions().size() == 0)
+
+               DescribeLoadBalancersRequest describeLoadBalancersRequest = new 
DescribeLoadBalancersRequest(
+                               loadBalancers);
+
+               AmazonElasticLoadBalancingClient lbClient = new 
AmazonElasticLoadBalancingClient(
+                               awsCredentials, clientConfiguration);
+
+               DescribeLoadBalancersResult result = lbClient
+                               
.describeLoadBalancers(describeLoadBalancersRequest);
+
+               if (result.getLoadBalancerDescriptions() == null
+                               || result.getLoadBalancerDescriptions().size() 
== 0)
                        return null;
                else
                        return result.getLoadBalancerDescriptions().get(0);
        }
-       
-       /*
-        * Returns instances attached to the load balancer.
-        * Useful when deciding if all attached instances are required or some 
should be detached.
+
+       /**
+        * Returns instances attached to the load balancer. Useful when 
deciding if
+        * all attached instances are required or some should be detached.
+        * 
+        * @param loadBalancerName
+        * @return list of instances attached
         */
-       public List<Instance> getAttachedInstances(String loadBalancerName)
-       {
-               try
-               {
+       public List<Instance> getAttachedInstances(String loadBalancerName) {
+               try {
                        LoadBalancerDescription lbDescription = 
getLoadBalancerDescription(loadBalancerName);
-                       
-                       if(lbDescription == null)
-                       {
-                               log.warn("Could not find description of load 
balancer " + loadBalancerName);    
-                               return null;
+
+                       if (lbDescription == null) {
+                               log.warn("Could not find description of load 
balancer "
+                                               + loadBalancerName);
+                               return null;
                        }
-                       
+
                        return lbDescription.getInstances();
-                       
-               }
-               catch(Exception e)
-               {
-                       log.error("Could not find description of load balancer 
" + loadBalancerName);
+
+               } catch (Exception e) {
+                       log.error("Could not find description of load balancer "
+                                       + loadBalancerName);
                        return null;
                }
        }
-       
-       /*
-        * Adds listeners provided to the load balancer.
-        * Useful when service definition is changed, in particular port 
mappings. So new listeners need to be added.
+
+       /**
+        * Adds listeners provided to the load balancer. Useful when service
+        * definition is changed, in particular port mappings. So new listeners 
need
+        * to be added.
+        * 
+        * @param loadBalancerName
+        * @param listeners
         */
-       public void addListenersToLoadBalancer(String loadBalancerName, 
List<Listener> listeners)
-       {
-               if(listeners.size() == 0)
+       public void addListenersToLoadBalancer(String loadBalancerName,
+                       List<Listener> listeners) {
+               if (listeners.size() == 0)
                        return;
-               
-               try
-               {
+
+               try {
                        CreateLoadBalancerListenersRequest 
createLoadBalancerListenersRequest = new CreateLoadBalancerListenersRequest();
                        
createLoadBalancerListenersRequest.setListeners(listeners);
-                       
createLoadBalancerListenersRequest.setLoadBalancerName(loadBalancerName);
-                       
-                       AmazonElasticLoadBalancingClient lbClient = new 
AmazonElasticLoadBalancingClient(awsCredentials, clientConfiguration);
-                       
+                       createLoadBalancerListenersRequest
+                                       .setLoadBalancerName(loadBalancerName);
+
+                       AmazonElasticLoadBalancingClient lbClient = new 
AmazonElasticLoadBalancingClient(
+                                       awsCredentials, clientConfiguration);
+
                        
lbClient.createLoadBalancerListeners(createLoadBalancerListenersRequest);
-               }
-               catch(Exception e)
-               {
-                       log.error("Could not add listeners to load balancer " + 
loadBalancerName);
+               } catch (Exception e) {
+                       log.error("Could not add listeners to load balancer "
+                                       + loadBalancerName);
                }
        }
-       
-       /*
-        * Remove listeners provided from the load balancer.
-        * Useful when attached listeners are no longer required.
+
+       /**
+        * Remove listeners provided from the load balancer. Useful when 
attached
+        * listeners are no longer required.
+        * 
+        * @param loadBalancerName
+        * @param listeners
         */
-       public void removeListenersFromLoadBalancer(String loadBalancerName, 
List<Listener> listeners)
-       {
-               if(listeners.size() == 0)
+       public void removeListenersFromLoadBalancer(String loadBalancerName,
+                       List<Listener> listeners) {
+               if (listeners.size() == 0)
                        return;
-               
-               try
-               {
+
+               try {
                        DeleteLoadBalancerListenersRequest 
deleteLoadBalancerListenersRequest = new DeleteLoadBalancerListenersRequest();
-                       
deleteLoadBalancerListenersRequest.setLoadBalancerName(loadBalancerName);
-                       
+                       deleteLoadBalancerListenersRequest
+                                       .setLoadBalancerName(loadBalancerName);
+
                        List<Integer> loadBalancerPorts = new 
ArrayList<Integer>();
-                       
-                       for(Listener listener : listeners)
-                       {
+
+                       for (Listener listener : listeners) {
                                
loadBalancerPorts.add(listener.getLoadBalancerPort());
                        }
-                       
-                       
deleteLoadBalancerListenersRequest.setLoadBalancerPorts(loadBalancerPorts);
 
-                       AmazonElasticLoadBalancingClient lbClient = new 
AmazonElasticLoadBalancingClient(awsCredentials, clientConfiguration);
-                       
+                       deleteLoadBalancerListenersRequest
+                                       
.setLoadBalancerPorts(loadBalancerPorts);
+
+                       AmazonElasticLoadBalancingClient lbClient = new 
AmazonElasticLoadBalancingClient(
+                                       awsCredentials, clientConfiguration);
+
                        
lbClient.deleteLoadBalancerListeners(deleteLoadBalancerListenersRequest);
-                       
-               }
-               catch(Exception e)
-               {
-                       log.error("Could not remove listeners from load 
balancer " + loadBalancerName);
+
+               } catch (Exception e) {
+                       log.error("Could not remove listeners from load 
balancer "
+                                       + loadBalancerName);
                }
        }
-       
-       /*
-        * Returns all the listeners attached to the load balancer.
-        * Useful while deciding if all the listeners are necessary or some 
should be removed.
+
+       /**
+        * Returns all the listeners attached to the load balancer. Useful while
+        * deciding if all the listeners are necessary or some should be 
removed.
+        * 
+        * @param loadBalancerName
+        * @return list of instances attached to load balancer
         */
-       public List<Listener> getAttachedListeners(String loadBalancerName)
-       {
-               try
-               {
+       public List<Listener> getAttachedListeners(String loadBalancerName) {
+               try {
                        LoadBalancerDescription lbDescription = 
getLoadBalancerDescription(loadBalancerName);
-                       
-                       if(lbDescription == null)
-                       {                       
-                               log.warn("Could not find description of load 
balancer " + loadBalancerName);
+
+                       if (lbDescription == null) {
+                               log.warn("Could not find description of load 
balancer "
+                                               + loadBalancerName);
                                return null;
                        }
-                       
+
                        List<Listener> listeners = new ArrayList<Listener>();
-                       
-                       List<ListenerDescription> listenerDescriptions = 
lbDescription.getListenerDescriptions();
-                       
-                       for(ListenerDescription listenerDescription : 
listenerDescriptions)
-                       {
+
+                       List<ListenerDescription> listenerDescriptions = 
lbDescription
+                                       .getListenerDescriptions();
+
+                       for (ListenerDescription listenerDescription : 
listenerDescriptions) {
                                
listeners.add(listenerDescription.getListener());
                        }
-                       
+
                        return listeners;
 
-               }
-               catch(Exception e)
-               {
-                       log.error("Could not find description of load balancer 
" + loadBalancerName);
+               } catch (Exception e) {
+                       log.error("Could not find description of load balancer "
+                                       + loadBalancerName);
                        return null;
                }
 
-               
        }
-       
-       /*
-        * Returns the Listeners required for the service.
-        * Listeners are derived from the proxy port, port and protocol values 
of the service.
+
+       /**
+        * Returns the Listeners required for the service. Listeners are derived
+        * from the proxy port, port and protocol values of the service.
+        * 
+        * @param service
+        * @return list of listeners required for the service
         */
-       public List<Listener> getRequiredListeners(Service service)
-       {
+       public List<Listener> getRequiredListeners(Service service) {
                List<Listener> listeners = new ArrayList<Listener>();
-               
-               for(Port port : service.getPorts())
-               {
+
+               for (Port port : service.getPorts()) {
                        int instancePort = port.getValue();
                        int proxyPort = port.getProxy();
                        String protocol = port.getProtocol();
-                       
+
                        Listener listener = new Listener(protocol, proxyPort, 
instancePort);
-                       
+
                        listeners.add(listener);
                }
-               
+
                return listeners;
        }
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/94cf7564/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
----------------------------------------------------------------------
diff --git 
a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
 
b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
index 172c8fc..b3656b4 100644
--- 
a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
+++ 
b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
@@ -23,9 +23,6 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
-import java.util.Set;
-
-import javax.management.InstanceAlreadyExistsException;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -33,175 +30,183 @@ import org.apache.stratos.load.balancer.common.domain.*;
 import 
org.apache.stratos.load.balancer.extension.api.exception.LoadBalancerExtensionException;
 import org.apache.stratos.load.balancer.extension.api.LoadBalancer;
 
-import com.amazonaws.auth.AWSCredentials;
-import com.amazonaws.auth.BasicAWSCredentials;
-import com.amazonaws.ClientConfiguration;
 import com.amazonaws.services.elasticloadbalancing.model.Instance;
 import com.amazonaws.services.elasticloadbalancing.model.Listener;
 
 public class AWSLoadBalancer implements LoadBalancer {
-       
+
        private static final Log log = LogFactory.getLog(AWSLoadBalancer.class);
-       
+
        // A map <clusterId, load balancer id>
        private HashMap<String, String> clusterIdToLoadBalancerMap;
 
        private AWSHelper awsHelper;
-       
-       public AWSLoadBalancer()
-       {
+
+       public AWSLoadBalancer() {
                clusterIdToLoadBalancerMap = new HashMap<String, String>();
                awsHelper = new AWSHelper();
        }
-       
-       public boolean configure(Topology topology) throws 
LoadBalancerExtensionException {
-               
+
+       public boolean configure(Topology topology)
+                       throws LoadBalancerExtensionException {
+
                for (Service service : topology.getServices()) {
-                       
-                       List<Listener> listenersForThisService = 
awsHelper.getRequiredListeners(service);
-                       
-            for (Cluster cluster : service.getClusters()) {
-                
-               // Check if a load balancer is created for this cluster
-               
-               
if(clusterIdToLoadBalancerMap.containsKey(cluster.getClusterId()))
-                       {
-                       // A load balancer is already present for this cluster
-                       // Get the load balancer and update it.
-                       
-                       String loadBalancerName = 
clusterIdToLoadBalancerMap.get(cluster.getClusterId());
-                       
-                       // 1. Get all the instances attached
-                       //    Add/remove instances as necessary
-                       
-                       List<Instance> attachedInstances = 
awsHelper.getAttachedInstances(loadBalancerName);
-                       
-                       Collection<Member> clusterInstances = 
cluster.getMembers();
-                       
-                       List<Instance> instancesToAddToLoadBalancer = new 
ArrayList<Instance>();
-                       
-                       for(Member member : clusterInstances)
-                       {
-                               // if instance id of member is not in 
attachedInstances
-                               //              add this to 
instancesToAddToLoadBalancer
-                               
-                               
-                       }
-                       
-                       List<Instance> instancesToRemoveFromLoadBalancer = new 
ArrayList<Instance>();
-                       
-                       for(Instance instance : attachedInstances)
-                       {
-                               
if(!clusterInstances.contains(instance.getInstanceId()))
-                               {
-                                       
instancesToRemoveFromLoadBalancer.add(instance);
-                               }
-                       }
-                       
-                       if(instancesToRemoveFromLoadBalancer.size() > 0)
-                               
awsHelper.deregisterInstancesFromLoadBalancer(loadBalancerName, 
instancesToRemoveFromLoadBalancer);
-                       
-                       if(instancesToAddToLoadBalancer.size() > 0)
-                               
awsHelper.registerInstancesToLoadBalancer(loadBalancerName, 
instancesToAddToLoadBalancer);
-
-                       
-                       // 2. Get all the listeners
-                       //    Add/Remove listeners as necessary
-                       
-                       // Is it really necessary to add/remove listeners from 
a lb to a cluster
-                       // Need to add only if a cluster can be used for more 
than one service (because a service my get added later)
-                       //                     or service port mappings may 
change
-     
-                       // Need to remove only if ... same for above reason
-                       
-                       List<Listener> attachedListeners = 
awsHelper.getAttachedListeners(loadBalancerName);
-                       
-                       List<Listener> listenersToAddToLoadBalancer = new 
ArrayList<Listener>();
-                       
-                       for(Listener listener : listenersForThisService)
-                       {
-                               // Need to check if Listener class supports 
equals method or not
-                               
-                               // if listener required for this service is not 
in attachedListeners
-                               //              add this to 
listenersToAddToLoadBalancer
-                       }
-                       
-                       List<Listener> listenersToRemoveFromLoadBalancer = new 
ArrayList<Listener>();
-                       
-                       for(Listener listener : attachedListeners)
-                       {
-                               // Need to check if Listener class supports 
equals method or not
-                               
-                               if(!listenersForThisService.contains(listener))
-                               {
-                                       
listenersToRemoveFromLoadBalancer.add(listener);
-                               }
-                       }
-                       
-                       if(listenersToRemoveFromLoadBalancer.size() > 0)
-                               
awsHelper.removeListenersFromLoadBalancer(loadBalancerName, 
listenersToRemoveFromLoadBalancer);
-                       
-                       if(listenersToAddToLoadBalancer.size() > 0)
-                               
awsHelper.addListenersToLoadBalancer(loadBalancerName, 
listenersToAddToLoadBalancer);
-
-                       }
-               else
-               {
-                       // Create a new load balancer for this cluster
-                       
-                       String loadBalancerName = service.getServiceName() + 
"-" + cluster.getClusterId();
-                       
-                       String loadBalancerDNSName = 
awsHelper.createLoadBalancer(loadBalancerName, listenersForThisService);
-                       
-                       // register instances to LB
-                       
-                       List<Instance> instances = new ArrayList<Instance>();
-                       
-                       for(Member m : cluster.getMembers())
-                       {
-                               String instanceId = ""; // of member // after 
making changes suggested in mail
-                               
-                               Instance instance = new Instance();
-                               instance.setInstanceId(instanceId);
-                               
-                               instances.add(instance);
-                       }
-                       
-                       
awsHelper.registerInstancesToLoadBalancer(loadBalancerName, instances);
-                       
-                       // Create domain mappings
-               }
-               
-            }
-        }
-               
+
+                       List<Listener> listenersForThisService = awsHelper
+                                       .getRequiredListeners(service);
+
+                       for (Cluster cluster : service.getClusters()) {
+
+                               // cluster.getHostNames()
+
+                               // Check if a load balancer is created for this 
cluster
+
+                               if 
(clusterIdToLoadBalancerMap.containsKey(cluster
+                                               .getClusterId())) {
+                                       // A load balancer is already present 
for this cluster
+                                       // Get the load balancer and update it.
+
+                                       String loadBalancerName = 
clusterIdToLoadBalancerMap
+                                                       
.get(cluster.getClusterId());
+
+                                       // 1. Get all the instances attached
+                                       // Add/remove instances as necessary
+
+                                       List<Instance> attachedInstances = 
awsHelper
+                                                       
.getAttachedInstances(loadBalancerName);
+
+                                       Collection<Member> clusterInstances = 
cluster.getMembers();
+
+                                       List<Instance> 
instancesToAddToLoadBalancer = new ArrayList<Instance>();
+
+                                       for (Member member : clusterInstances) {
+                                               // if instance id of member is 
not in attachedInstances
+                                               // add this to 
instancesToAddToLoadBalancer
+
+                                       }
+
+                                       List<Instance> 
instancesToRemoveFromLoadBalancer = new ArrayList<Instance>();
+
+                                       for (Instance instance : 
attachedInstances) {
+                                               if (!clusterInstances
+                                                               
.contains(instance.getInstanceId())) {
+                                                       
instancesToRemoveFromLoadBalancer.add(instance);
+                                               }
+                                       }
+
+                                       if 
(instancesToRemoveFromLoadBalancer.size() > 0)
+                                               
awsHelper.deregisterInstancesFromLoadBalancer(
+                                                               
loadBalancerName,
+                                                               
instancesToRemoveFromLoadBalancer);
+
+                                       if (instancesToAddToLoadBalancer.size() 
> 0)
+                                               
awsHelper.registerInstancesToLoadBalancer(
+                                                               
loadBalancerName, instancesToAddToLoadBalancer);
+
+                                       // 2. Get all the listeners
+                                       // Add/Remove listeners as necessary
+
+                                       // Is it really necessary to add/remove 
listeners from a lb
+                                       // to a cluster
+                                       // Need to add only if a cluster can be 
used for more than
+                                       // one service (because a service my 
get added later)
+                                       // or service port mappings may change
+
+                                       // Need to remove only if ... same for 
above reason
+
+                                       List<Listener> attachedListeners = 
awsHelper
+                                                       
.getAttachedListeners(loadBalancerName);
+
+                                       List<Listener> 
listenersToAddToLoadBalancer = new ArrayList<Listener>();
+
+                                       for (Listener listener : 
listenersForThisService) {
+                                               // Need to check if Listener 
class supports equals
+                                               // method or not
+
+                                               // if listener required for 
this service is not in
+                                               // attachedListeners
+                                               // add this to 
listenersToAddToLoadBalancer
+                                       }
+
+                                       List<Listener> 
listenersToRemoveFromLoadBalancer = new ArrayList<Listener>();
+
+                                       for (Listener listener : 
attachedListeners) {
+                                               // Need to check if Listener 
class supports equals
+                                               // method or not
+
+                                               if 
(!listenersForThisService.contains(listener)) {
+                                                       
listenersToRemoveFromLoadBalancer.add(listener);
+                                               }
+                                       }
+
+                                       if 
(listenersToRemoveFromLoadBalancer.size() > 0)
+                                               
awsHelper.removeListenersFromLoadBalancer(
+                                                               
loadBalancerName,
+                                                               
listenersToRemoveFromLoadBalancer);
+
+                                       if (listenersToAddToLoadBalancer.size() 
> 0)
+                                               
awsHelper.addListenersToLoadBalancer(loadBalancerName,
+                                                               
listenersToAddToLoadBalancer);
+
+                                       // Update domain mappings
+
+                               } else {
+                                       // Create a new load balancer for this 
cluster
+
+                                       String loadBalancerName = 
service.getServiceName() + "-"
+                                                       + 
cluster.getClusterId();
+
+                                       String loadBalancerDNSName = 
awsHelper.createLoadBalancer(
+                                                       loadBalancerName, 
listenersForThisService);
+
+                                       // register instances to LB
+
+                                       List<Instance> instances = new 
ArrayList<Instance>();
+
+                                       for (Member m : cluster.getMembers()) {
+                                               String instanceId = ""; // of 
member // after making
+                                                                               
                // changes suggested in mail
+
+                                               Instance instance = new 
Instance();
+                                               
instance.setInstanceId(instanceId);
+
+                                               instances.add(instance);
+                                       }
+
+                                       
awsHelper.registerInstancesToLoadBalancer(loadBalancerName,
+                                                       instances);
+
+                                       // Create domain mappings
+                               }
+
+                       }
+               }
+
                // Find out clusters which were present earlier but are not now.
                // Delete load balancers associated with those clusters.
-               
+
                return true;
        }
-       
+
        public void start() throws LoadBalancerExtensionException {
-               
+
                log.info("Started AWS load balancer extension.");
        }
-       
+
        public void reload() throws LoadBalancerExtensionException {
                // Check what is appropriate to do here.
        }
-            
-       public void stop() throws LoadBalancerExtensionException 
-       {
+
+       public void stop() throws LoadBalancerExtensionException {
                // Remove all load balancers
-               
-               for(String loadBalancerName : 
clusterIdToLoadBalancerMap.values())
-               {
+
+               for (String loadBalancerName : 
clusterIdToLoadBalancerMap.values()) {
                        // remove load balancer
                        awsHelper.deleteLoadBalancer(loadBalancerName);
-                       
+
                        // Check what all needs to be done
                }
-               
+
                // Remove domain mappings
-       }       
+       }
 }

Reply via email to