Repository: incubator-stratos
Updated Branches:
  refs/heads/master 7af2113c7 -> 6932793f6


Adding the bam publishing funcitionality to stratos manager

Signed-off-by: Sajith Kariyawasam <[email protected]>


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

Branch: refs/heads/master
Commit: 8c6385882c88e46cb36d966ff7176303b7871048
Parents: 7af2113
Author: Vimuth Fernando <[email protected]>
Authored: Fri Mar 14 18:40:11 2014 +0530
Committer: Sajith Kariyawasam <[email protected]>
Committed: Sat Mar 22 02:22:55 2014 +0530

----------------------------------------------------------------------
 .../CartridgeSubscriptionDataPublisher.java     | 169 +++++++++++++++++++
 .../src/main/conf/cartridge-config.properties   |   4 +
 2 files changed, 173 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/8c638588/components/org.apache.stratos.manager/src/main/java/org/apache/stratos/manager/publisher/CartridgeSubscriptionDataPublisher.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.manager/src/main/java/org/apache/stratos/manager/publisher/CartridgeSubscriptionDataPublisher.java
 
b/components/org.apache.stratos.manager/src/main/java/org/apache/stratos/manager/publisher/CartridgeSubscriptionDataPublisher.java
new file mode 100644
index 0000000..83fff6a
--- /dev/null
+++ 
b/components/org.apache.stratos.manager/src/main/java/org/apache/stratos/manager/publisher/CartridgeSubscriptionDataPublisher.java
@@ -0,0 +1,169 @@
+/*
+ * 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.manager.publisher;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.manager.exception.ADCException;
+import org.apache.stratos.manager.utils.CartridgeConstants;
+import org.wso2.carbon.base.ServerConfiguration;
+import org.wso2.carbon.databridge.agent.thrift.AsyncDataPublisher;
+import org.wso2.carbon.databridge.agent.thrift.exception.AgentException;
+import org.wso2.carbon.databridge.commons.Attribute;
+import org.wso2.carbon.databridge.commons.AttributeType;
+import org.wso2.carbon.databridge.commons.Event;
+import org.wso2.carbon.databridge.commons.StreamDefinition;
+import org.wso2.carbon.utils.CarbonUtils;
+
+/**
+ * This will publish the subscription details of tenants to a data receiver.
+ * Data is published when a tenant subscribe to a cartridge and when it 
unsubscribe.
+ */
+public class CartridgeSubscriptionDataPublisher {
+
+       private static final Log log = LogFactory
+                       .getLog(CartridgeSubscriptionDataPublisher.class);
+       private static AsyncDataPublisher dataPublisher;
+       private static StreamDefinition streamDefinition;
+       private static final String stratosManagerEventStreamVersion = "1.0.0";
+
+       @SuppressWarnings("deprecation")
+       public static void publish(int tenantID, String adminUser,
+                       String cartridgeAlias, String cartridgeType, String 
repositoryUrl,
+                       boolean isMultiTenant, String autoScalingPolicy,
+                       String deploymentPolicy, String clusterID, String 
hostName,
+                       String mappedDomain, String action) throws ADCException 
{
+               
+               //check if bam is enabled in cartridge-config.properties
+               if(! 
Boolean.parseBoolean(System.getProperty(CartridgeConstants.BAM_PUBLISHER_ENABLED))){
+            return;
+        }
+               
+               log.debug(CartridgeConstants.DATA_PUB_TASK_NAME+" cycle 
started.");
+
+               if (dataPublisher == null) {
+                       createDataPublisher();
+
+                       // If we cannot create a data publisher we should give 
up
+                       // this means data will not be published
+                       if (dataPublisher == null) {
+                               log.error("Data Publisher cannot be created or 
found.");
+                               return;
+                       }
+               }
+
+               //Construct the data to be published
+               List<Object> payload = new ArrayList<Object>();
+               
+               // Payload values
+               payload.add(String.valueOf(tenantID));
+               payload.add(adminUser);
+               payload.add(cartridgeAlias);
+               payload.add(cartridgeType);
+               payload.add(repositoryUrl);
+               payload.add(String.valueOf(isMultiTenant));
+               payload.add(autoScalingPolicy);
+               payload.add(deploymentPolicy);
+               payload.add(String.valueOf(clusterID));
+               payload.add(hostName);
+               payload.add(mappedDomain);
+               payload.add(action);
+
+               Event event = new Event();
+               event.setPayloadData(payload.toArray());
+               event.setArbitraryDataMap(new HashMap<String, String>());
+
+               try {
+                       if (log.isDebugEnabled()) {
+                               log.debug(String.format(
+                                               "Publishing BAM event: [stream] 
%s [version] %s",
+                                               streamDefinition.getName(),
+                                               streamDefinition.getVersion()));
+                       }
+                       dataPublisher.publish(streamDefinition.getName(),
+                                       streamDefinition.getVersion(), event);
+               } catch (AgentException e) {
+                       if (log.isErrorEnabled()) {
+                               log.error(
+                                               String.format(
+                                                               "Could not 
publish BAM event: [stream] %s [version] %s",
+                                                               
streamDefinition.getName(),
+                                                               
streamDefinition.getVersion()), e);
+                       }
+               }
+       }
+
+       private static StreamDefinition initializeStream() throws Exception {
+               streamDefinition = new StreamDefinition( 
CartridgeConstants.STRATOS_MANAGER_EVENT_STREAM,
+                               stratosManagerEventStreamVersion);
+               streamDefinition.setNickName("stratos.manager");
+               streamDefinition.setDescription("Tenant Subscription Data");
+               // Payload definition
+               List<Attribute> payloadData = new ArrayList<Attribute>();
+               payloadData.add(new Attribute(CartridgeConstants.TENANT_ID_COL, 
AttributeType.STRING));
+               payloadData.add(new 
Attribute(CartridgeConstants.ADMIN_USER_COL, AttributeType.STRING));
+               payloadData.add(new 
Attribute(CartridgeConstants.CARTRIDGE_ALIAS_COL, AttributeType.STRING));
+               payloadData.add(new 
Attribute(CartridgeConstants.CARTRIDGE_TYPE_COL, AttributeType.STRING));
+               payloadData.add(new 
Attribute(CartridgeConstants.REPOSITORY_URL_COL, AttributeType.STRING));
+               payloadData.add(new 
Attribute(CartridgeConstants.MULTI_TENANT_BEHAVIOR_COL, AttributeType.STRING));
+               payloadData.add(new 
Attribute(CartridgeConstants.AUTO_SCALE_POLICY_COL, AttributeType.STRING));
+               payloadData
+                               .add(new 
Attribute(CartridgeConstants.DEPLOYMENT_POLICY_COL, AttributeType.STRING));
+               payloadData.add(new 
Attribute(CartridgeConstants.CLUSTER_ID_COL, AttributeType.STRING));
+               payloadData.add(new Attribute(CartridgeConstants.HOST_NAME_COL, 
AttributeType.STRING));
+               payloadData.add(new 
Attribute(CartridgeConstants.MAPPED_DOMAIN_COL, AttributeType.STRING));
+               payloadData.add(new Attribute(CartridgeConstants.ACTION_COL, 
AttributeType.STRING));
+               streamDefinition.setPayloadData(payloadData);
+               return streamDefinition;
+       }
+
+       private static void createDataPublisher() throws ADCException {
+               // creating the agent
+               ServerConfiguration serverConfig = 
CarbonUtils.getServerConfiguration();
+               String trustStorePath = 
serverConfig.getFirstProperty("Security.TrustStore.Location");
+               String trustStorePassword = 
serverConfig.getFirstProperty("Security.TrustStore.Password");
+               
+               //value is in the carbon.xml file and should be set to the 
thrift port of BAM
+               String bamServerUrl = 
serverConfig.getFirstProperty("BamServerURL");
+        
+        //getting the BAM related values from cartridge-config.properties
+               String adminUsername = 
System.getProperty(CartridgeConstants.BAM_ADMIN_USERNAME);
+               String adminPassword = 
System.getProperty(CartridgeConstants.BAM_ADMIN_PASSWORD); 
+
+               System.setProperty("javax.net.ssl.trustStore", trustStorePath);
+               System.setProperty("javax.net.ssl.trustStorePassword",
+                               trustStorePassword);
+
+               try {
+                       dataPublisher = new AsyncDataPublisher(
+                                       "tcp://" + bamServerUrl + "", 
adminUsername, adminPassword);
+                       initializeStream();
+                       dataPublisher.addStreamDefinition(streamDefinition);
+               } catch (Exception e) {
+                       String msg = "Unable to create a data publisher to "+ 
bamServerUrl;
+                       log.error(msg, e);
+                       throw new ADCException(msg, e);
+               }
+       }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/8c638588/products/stratos-manager/modules/distribution/src/main/conf/cartridge-config.properties
----------------------------------------------------------------------
diff --git 
a/products/stratos-manager/modules/distribution/src/main/conf/cartridge-config.properties
 
b/products/stratos-manager/modules/distribution/src/main/conf/cartridge-config.properties
index eaa7dcc..9404e09 100644
--- 
a/products/stratos-manager/modules/distribution/src/main/conf/cartridge-config.properties
+++ 
b/products/stratos-manager/modules/distribution/src/main/conf/cartridge-config.properties
@@ -25,3 +25,7 @@ puppet.ip=127.0.0.1
 puppet.hostname=puppet.apache.stratos.org
 puppet.environment=stratos
 
+#BAM Server Info - default values are 'admin' and 'admin'
+bam.publisher.enabled=false
+bam.admin.username=admin
+bam.admin.password=admin
\ No newline at end of file

Reply via email to