Adding basic logic for configure method of AWSLoadBalancer

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

Branch: refs/heads/gsoc-projects-2015
Commit: bcf2c8ef299874f132210494bb05a4eea0737174
Parents: 4babb61
Author: swapnilpatilRajaram <[email protected]>
Authored: Thu May 28 14:26:44 2015 +0000
Committer: swapnilpatilRajaram <[email protected]>
Committed: Thu May 28 14:26:44 2015 +0000

----------------------------------------------------------------------
 extensions/load-balancer/aws-extension/pom.xml  |  70 +++++++++++++
 .../apache/stratos/aws/extension/AWSHelper.java | 102 +++++++++++++++++++
 .../stratos/aws/extension/AWSLoadBalancer.java  |  92 +++++++++++++++++
 3 files changed, 264 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/bcf2c8ef/extensions/load-balancer/aws-extension/pom.xml
----------------------------------------------------------------------
diff --git a/extensions/load-balancer/aws-extension/pom.xml 
b/extensions/load-balancer/aws-extension/pom.xml
new file mode 100644
index 0000000..e47e378
--- /dev/null
+++ b/extensions/load-balancer/aws-extension/pom.xml
@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one
+  ~ or more contributor license agreements.  See the NOTICE file
+  ~ distributed with this work for additional information
+  ~ regarding copyright ownership.  The ASF licenses this file
+  ~ to you under the Apache License, Version 2.0 (the
+  ~ "License"); you may not use this file except in compliance
+  ~ with the License.  You may obtain a copy of the License at
+  ~
+  ~     http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing,
+  ~ software distributed under the License is distributed on an
+  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  ~ KIND, either express or implied.  See the License for the
+  ~ specific language governing permissions and limitations
+  ~ under the License.
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+  
+  <parent>
+    <groupId>org.apache.stratos</groupId>
+    <artifactId>stratos-load-balancer-extensions</artifactId>
+    <version>4.1.0-SNAPSHOT</version>
+  </parent>
+  
+  <artifactId>org.apache.stratos.aws.extension</artifactId>
+  <name>Apache Stratos - AWS Extension</name>
+  <description>Apache Stratos AWS Extension for Load Balancing</description>
+  <dependencies>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-log4j12</artifactId>
+            <version>1.7.5</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.stratos</groupId>
+            <artifactId>org.apache.stratos.common</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.stratos</groupId>
+            <artifactId>org.apache.stratos.messaging</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.stratos</groupId>
+            
<artifactId>org.apache.stratos.load.balancer.extension.api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.velocity</groupId>
+            <artifactId>velocity</artifactId>
+            <version>1.7</version>
+        </dependency>
+        <dependency>
+            <groupId>org.wso2.andes.wso2</groupId>
+            <artifactId>andes-client</artifactId>
+            <version>0.13.wso2v8</version>
+        </dependency>
+    </dependencies>
+</project>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/stratos/blob/bcf2c8ef/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
new file mode 100644
index 0000000..4bca6f8
--- /dev/null
+++ 
b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSHelper.java
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.stratos.aws.extension;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.load.balancer.common.domain.*;
+
+import com.amazonaws.ClientConfiguration;
+import com.amazonaws.auth.BasicAWSCredentials;
+import 
com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancingClient;
+import 
com.amazonaws.services.elasticloadbalancing.model.CreateLoadBalancerRequest;
+import 
com.amazonaws.services.elasticloadbalancing.model.CreateLoadBalancerResult;
+import com.amazonaws.services.elasticloadbalancing.model.Listener;
+
+
+public class AWSHelper 
+{
+       private String awsAccessKey;
+       private String awsSecretKey;
+       private String availabilityZone;
+
+       private BasicAWSCredentials awsCredentials;
+       private ClientConfiguration clientConfiguration;
+       
+       private static final Log log = LogFactory.getLog(AWSHelper.class);
+       
+       public AWSHelper()
+       {
+               // Read values for awsAccessKey and awsSecretKey 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.
+        */
+       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);
+                       
+                       return clbResult.getDNSName();
+               }
+               catch(Exception e)
+               {
+                       log.error("Could not create load balancer " + name + 
".", e);
+                       return null;
+               }
+       }
+       
+       public List<Listener> getRequiredListeners(Service service)
+       {
+               List<Listener> listeners = new ArrayList<Listener>();
+               
+               for(Port port : service.getPorts())
+               {
+                       int instancePort = port.getValue();
+                       // Read other values as well and create a listener 
object
+                       
+                       
+               }
+               
+               return listeners;
+       }
+}

http://git-wip-us.apache.org/repos/asf/stratos/blob/bcf2c8ef/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
new file mode 100644
index 0000000..5e1f7b2
--- /dev/null
+++ 
b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.stratos.aws.extension;
+
+import java.util.HashMap;
+import java.util.List;
+
+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.Listener;
+
+
+
+public class AWSLoadBalancer implements LoadBalancer {
+       
+       // A map <clusterId, load balancer id>
+       private HashMap<String, String> clusterToLoadBalancer;
+
+       private AWSHelper awsHelper;
+       
+       public AWSLoadBalancer()
+       {
+               clusterToLoadBalancer = new HashMap<String, String>();
+               awsHelper = new AWSHelper();
+       }
+       
+       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(clusterToLoadBalancer.containsKey(cluster.getClusterId()))
+                       {
+                       // Get the load balancer
+                       // Update it 
+                       }
+               else
+               {
+                       String loadBalancerName 
=service.getServiceName()+"-"+cluster.getClusterId();
+                       
+                       String loadBalancerDNSName = 
awsHelper.createLoadBalancer(loadBalancerName, listenersForThisService);
+                       
+                       // register instances to LB
+                       
+                       // Create domain mappings
+               }
+               
+            }
+        }
+               
+               return true;
+       }
+       
+       public void start() throws LoadBalancerExtensionException {
+               
+       }
+       
+       public void reload() throws LoadBalancerExtensionException {
+               
+       }
+            
+       public void stop() throws LoadBalancerExtensionException {
+               
+       }       
+}

Reply via email to