2121
2222import org .apache .commons .logging .Log ;
2323import org .apache .commons .logging .LogFactory ;
24+ import org .apache .stratos .gce .extension .config .Constants ;
2425import org .apache .stratos .gce .extension .config .GCEClusterConfigurationHolder ;
2526import org .apache .stratos .gce .extension .config .GCEContext ;
2627import org .apache .stratos .gce .extension .util .GCEOperations ;
3233import java .security .GeneralSecurityException ;
3334import java .util .*;
3435
36+ /**
37+ * All the methods in Stratos load balancer API have been implemented in this class
38+ */
3539public class GCELoadBalancer implements LoadBalancer {
3640
3741 private static final Log log = LogFactory .getLog (GCELoadBalancer .class );
38- //PROTOCOL should be TCP or UDP
39- private static final String PROTOCOL = "TCP" ;
4042 private GCEOperations gceOperations ;
4143 /**
4244 * One configuration object per cluster will be created
@@ -48,7 +50,6 @@ public class GCELoadBalancer implements LoadBalancer {
4850 public GCELoadBalancer () throws IOException , GeneralSecurityException {
4951 gceOperations = new GCEOperations ();
5052 clusterToLoadBalancerConfigurationMap = new HashMap <String , GCEClusterConfigurationHolder >();
51-
5253 }
5354
5455 /**
@@ -60,7 +61,7 @@ public GCELoadBalancer() throws IOException, GeneralSecurityException {
6061 */
6162 @ Override
6263 public boolean configure (Topology topology ) throws LoadBalancerExtensionException {
63- log .info ("Complete topology received. Configuring Load balancer " );
64+ log .info ("Complete topology received. Configuring Load balancer... " );
6465
6566 //this list is used to hold the current clusters available in topology and which has at least one member.
6667 List <String > activeClusterIdList = new ArrayList <String >();
@@ -72,7 +73,7 @@ public boolean configure(Topology topology) throws LoadBalancerExtensionExceptio
7273 if (clusterToLoadBalancerConfigurationMap .containsKey (cluster .getClusterId ())) {
7374
7475 if (log .isDebugEnabled ()) {
75- log .debug ("Reconfiguring the existing cluster: " + cluster .getClusterId ());
76+ log .debug ("Reconfiguring the existing cluster: " + cluster .getClusterId () + "..." );
7677 }
7778
7879 //It already has a entry in clusterToLoadBalancerConfigurationMap.
@@ -82,15 +83,15 @@ public boolean configure(Topology topology) throws LoadBalancerExtensionExceptio
8283
8384 //if the cluster contains at least one member
8485 if (!cluster .getMembers ().isEmpty ()) {
85- //that cluster contains at least one member
86-
8786 if (log .isDebugEnabled ()) {
8887 log .debug ("Cluster " + cluster .getClusterId () + " has one or more members" );
8988 }
9089 activeClusterIdList .add (cluster .getClusterId ());
90+ if (log .isDebugEnabled ()){
91+ log .debug ("Cluster " + cluster .getClusterId () + " was added to active cluster id list" );
92+ }
9193
92- //***************detect member changes and update**************//
93-
94+ //detect member changes and update
9495 //check for newly created members
9596 List <String > membersToBeAddedToTargetPool = new ArrayList <String >();
9697 for (Member member : cluster .getMembers ()) {
@@ -106,7 +107,7 @@ public boolean configure(Topology topology) throws LoadBalancerExtensionExceptio
106107
107108 if (!membersToBeAddedToTargetPool .isEmpty ()) { //we have new members
108109 log .info ("New members in cluster" + cluster .getClusterId () + " found. Adding new members " +
109- "to cluster" );
110+ "to cluster... " );
110111
111112 //add them to configuration holder
112113 for (String memberId : membersToBeAddedToTargetPool ) {
@@ -139,7 +140,7 @@ public boolean configure(Topology topology) throws LoadBalancerExtensionExceptio
139140 }
140141
141142 if (!membersToBeRemovedFromTargetPool .isEmpty ()) { //found terminated members
142- log .info ("Terminated members found in cluster " + cluster .getClusterId () + ". Removing them" );
143+ log .info ("Terminated members found in cluster " + cluster .getClusterId () + ". Removing them... " );
143144
144145 //remove them from configuration holder
145146 for (String memberId : membersToBeRemovedFromTargetPool ) {
@@ -151,12 +152,9 @@ public boolean configure(Topology topology) throws LoadBalancerExtensionExceptio
151152 gceOperations .removeInstancesFromTargetPool (membersToBeRemovedFromTargetPool ,
152153 gceClusterConfigurationHolder .getTargetPoolName ());
153154 }
154-
155155 }
156-
157156 } else {
158157 //doesn't have a GCEClusterConfigurationHolder object. So crate a new one and add to hash map
159-
160158 log .info ("Found a new cluster: " + cluster .getClusterId ());
161159
162160 if (cluster .getMembers ().isEmpty ()) {
@@ -211,18 +209,16 @@ public boolean configure(Topology topology) throws LoadBalancerExtensionExceptio
211209
212210 clusterToLoadBalancerConfigurationMap .put (cluster .getClusterId (), gceClusterConfigurationHolder );
213211 createConfigurationForCluster (cluster .getClusterId ());
214-
215212 }
216213 }
217-
218214 }
219215 }
220216
221217 //if any cluster is removed from the topology or if any cluster does not have at least one member,
222218 //remove those clusters from map and remove the configuration from GCE too
223219 for (String clusterId : clusterToLoadBalancerConfigurationMap .keySet ()) {
224220 if (!activeClusterIdList .contains (clusterId )) {
225- log .info ("Removing the configuration for cluster " + clusterId );
221+ log .info ("Removing the configuration for cluster " + clusterId + "..." );
226222 deleteConfigurationForCluster (clusterId );
227223 clusterToLoadBalancerConfigurationMap .remove (clusterId );
228224 }
@@ -238,7 +234,7 @@ public boolean configure(Topology topology) throws LoadBalancerExtensionExceptio
238234 */
239235 private void deleteConfigurationForCluster (String clusterId ) throws LoadBalancerExtensionException {
240236
241- log .info ("Deleting configuration for cluster " + clusterId );
237+ log .info ("Deleting configuration for cluster " + clusterId + "..." );
242238 GCEClusterConfigurationHolder gceClusterConfigurationHolder = clusterToLoadBalancerConfigurationMap .get (clusterId );
243239 //delete forwarding rule
244240 gceOperations .deleteForwardingRule (gceClusterConfigurationHolder .getForwardingRuleName ());
@@ -256,7 +252,7 @@ private void deleteConfigurationForCluster(String clusterId) throws LoadBalancer
256252 */
257253 private void createConfigurationForCluster (String clusterId ) throws LoadBalancerExtensionException {
258254
259- log .info ("Creating configuration for cluster" );
255+ log .info ("Creating configuration for cluster " + clusterId + "... " );
260256
261257 GCEClusterConfigurationHolder gceClusterConfigurationHolder = clusterToLoadBalancerConfigurationMap .get (clusterId );
262258
@@ -278,9 +274,8 @@ private void createConfigurationForCluster(String clusterId) throws LoadBalancer
278274 //if the ip list is empty
279275 if (ipList .isEmpty ()) {
280276 log .warn ("Ip list is null" );
281- //set all ports to be opened
282- portRange = "1-65535" ;
283-
277+ //set all default port range
278+ portRange = Constants .DEFAULT_PORT_RANGE ;
284279 }
285280 //else if ip list has only one value
286281 else if (ipList .size () == 1 ) {
@@ -300,8 +295,8 @@ else if (ipList.size() == 1) {
300295
301296 //create the forwarding rule
302297 gceOperations .createForwardingRule (gceClusterConfigurationHolder .getForwardingRuleName (),
303- gceClusterConfigurationHolder .getTargetPoolName (), PROTOCOL , portRange );
304- log .info ("Created configuration for cluster" );
298+ gceClusterConfigurationHolder .getTargetPoolName (), Constants . PROTOCOL , portRange );
299+ log .info ("Created configuration for cluster" + clusterId );
305300 }
306301
307302 @ Override
@@ -314,7 +309,7 @@ public void start() throws LoadBalancerExtensionException {
314309 @ Override
315310 public void stop () throws LoadBalancerExtensionException {
316311
317- log .info ("GCE Load Balancer is stopping" );
312+ log .info ("GCE Load Balancer is stopping... " );
318313
319314 //iterate through hash map and remove all
320315 Iterator iterator = clusterToLoadBalancerConfigurationMap .entrySet ().iterator ();
@@ -331,10 +326,7 @@ public void stop() throws LoadBalancerExtensionException {
331326 */
332327 @ Override
333328 public void reload () throws LoadBalancerExtensionException {
334-
335329 //nothing to do here
336- log .info ("Configuration reloaded" );
337-
338330 }
339331
340332 /**
@@ -350,41 +342,42 @@ private String targetPoolNameCreator(String clusterId) {
350342 String targetPoolName = GCEContext .getInstance ().getNamePrefix ().toLowerCase () + "-" +
351343 clusterId .trim ().toLowerCase ().replace ("." , "-" );
352344 //length should be les than 62 characters
353- if (targetPoolName .length () >= 62 ) {
354- targetPoolName = targetPoolName .substring (0 , 62 );
345+ if (targetPoolName .length () >= Constants . MAX_NAME_LENGTH ) {
346+ targetPoolName = targetPoolName .substring (0 , Constants . MAX_NAME_LENGTH );
355347 }
356348 return targetPoolName ;
357349 }
358350
359351 /**
360352 * Create a valid forwarding rule name
361353 *
362- * @param clusterID - Id of the cluster
354+ * @param clusterId - Id of the cluster
363355 * @return - a proper name for forwarding rule
364356 */
365- private String forwardingRuleNameCreator (String clusterID ) {
366- String forwardingRuleName = GCEContext .getInstance ().getNamePrefix ()
367- + "-fr-" + clusterID .trim ().toLowerCase ().replace ("." , "-" );
357+ private String forwardingRuleNameCreator (String clusterId ) {
358+ String forwardingRuleName = GCEContext .getInstance ().getNamePrefix ().toLowerCase () + "-" +
359+ clusterId .trim ().toLowerCase ().replace ("." , "-" );
360+
368361 //length should be les than 62 characters
369- if (forwardingRuleName .length () >= 62 ) {
370- forwardingRuleName = forwardingRuleName .substring (0 , 62 );
362+ if (forwardingRuleName .length () >= Constants . MAX_NAME_LENGTH ) {
363+ forwardingRuleName = forwardingRuleName .substring (0 , Constants . MAX_NAME_LENGTH );
371364 }
372365 return forwardingRuleName ;
373366 }
374367
375368 /**
376369 * create a valid health check name
377370 *
378- * @param clusterID - id of the cluster
371+ * @param clusterId - id of the cluster
379372 * @return - a proper name for health check
380373 */
381- private String healthCheckNameCreator (String clusterID ) {
382- String healthCheckName = GCEContext .getInstance ().getNamePrefix ().toLowerCase () + "-hc- " +
383- clusterID .trim ().toLowerCase ().replace ("." , "-" );
374+ private String healthCheckNameCreator (String clusterId ) {
375+ String healthCheckName = GCEContext .getInstance ().getNamePrefix ().toLowerCase () + "-" +
376+ clusterId .trim ().toLowerCase ().replace ("." , "-" );
384377
385- //length should be les than 62 characters
386- if (healthCheckName .length () >= 62 ) {
387- healthCheckName = healthCheckName .substring (0 , 62 );
378+ //length should be less than 62 characters
379+ if (healthCheckName .length () >= Constants . MAX_NAME_LENGTH ) {
380+ healthCheckName = healthCheckName .substring (0 , Constants . MAX_NAME_LENGTH );
388381 }
389382 return healthCheckName ;
390383 }
0 commit comments