stratos-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From im...@apache.org
Subject stratos git commit: Moving jclouds member termination methods to JcloudsIaas and renaming logTermination() method to executeMemberTerminationPostProcess()
Date Wed, 10 Dec 2014 09:19:46 GMT
Repository: stratos
Updated Branches:
  refs/heads/4.1.0-test 0e540557e -> aeb4458ce


Moving jclouds member termination methods to JcloudsIaas and renaming logTermination() method to executeMemberTerminationPostProcess()


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

Branch: refs/heads/4.1.0-test
Commit: aeb4458cef748fe44e96b4d52a37d89a5368d847
Parents: 0e54055
Author: Imesh Gunaratne <imesh@apache.org>
Authored: Wed Dec 10 14:49:38 2014 +0530
Committer: Imesh Gunaratne <imesh@apache.org>
Committed: Wed Dec 10 14:49:38 2014 +0530

----------------------------------------------------------------------
 .../cloud/controller/iaases/JcloudsIaas.java    |  79 +++++--
 .../controller/iaases/mock/MockIaasService.java |   5 -
 .../CartridgeInstanceDataPublisher.java         | 214 -------------------
 .../publisher/StatisticsDataPublisher.java      | 212 ++++++++++++++++++
 .../messaging/topology/TopologyBuilder.java     |  54 ++---
 .../impl/CloudControllerServiceImpl.java        |  95 ++++----
 .../impl/CloudControllerServiceUtil.java        |  99 +--------
 .../services/impl/InstanceCreator.java          |  12 +-
 .../services/impl/InstanceTerminator.java       |   4 +
 9 files changed, 359 insertions(+), 415 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/aeb4458c/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/JcloudsIaas.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/JcloudsIaas.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/JcloudsIaas.java
index 8a0656a..6c02770 100644
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/JcloudsIaas.java
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/JcloudsIaas.java
@@ -34,6 +34,7 @@ import org.jclouds.compute.ComputeService;
 import org.jclouds.compute.domain.NodeMetadata;
 import org.jclouds.compute.domain.NodeMetadataBuilder;
 import org.jclouds.compute.domain.Template;
+import org.jclouds.rest.ResourceNotFoundException;
 
 import java.util.Set;
 
@@ -173,8 +174,7 @@ public abstract class JcloudsIaas extends Iaas {
                                     " - terminating node:" + memberContext.toString();
                             log.error(msg);
                             // terminate instance
-                            CloudControllerServiceUtil.terminate(getIaasProvider(),
-                                    node.getId(), memberContext);
+                            destroyNode(node.getId(), memberContext);
                             throw new CloudControllerException(msg);
                         }
                     } else {
@@ -182,7 +182,7 @@ public abstract class JcloudsIaas extends Iaas {
                                 " - terminating node:" + memberContext.toString();
                         log.error(msg);
                         // terminate instance
-                        CloudControllerServiceUtil.terminate(getIaasProvider(), node.getId(), memberContext);
+                        destroyNode(node.getId(), memberContext);
                         throw new CloudControllerException(msg);
                     }
 
@@ -262,42 +262,81 @@ public abstract class JcloudsIaas extends Iaas {
     }
 
     public void terminateInstance(MemberContext memberContext) throws InvalidCartridgeTypeException, InvalidMemberException {
-
         String memberId = memberContext.getMemberId();
-        String clusterId = memberContext.getClusterId();
-        String partitionId = memberContext.getPartition().getId();
         String cartridgeType = memberContext.getCartridgeType();
         String nodeId = memberContext.getNodeId();
         Cartridge cartridge = CloudControllerContext.getInstance().getCartridge(cartridgeType);
 
-        log.info("Starting to terminate an instance with member id : " + memberId +
-                " in partition id: " + partitionId + " of cluster id: " + clusterId +
-                " and of cartridge type: " + cartridgeType);
+        if(log.isInfoEnabled()) {
+            log.info(String.format("Starting to terminate member: [cartridge-type] %s [member-id] %s",
+                    cartridgeType, memberId));
+        }
 
         if (cartridge == null) {
-            String msg = "Termination of Member Id: " + memberId + " failed. " +
-                    "Cannot find a matching Cartridge for type: " +
-                    cartridgeType;
+            String msg = String.format("Member termination failed, could not find cartridge in cloud controller " +
+                            "context: [cartridge-type] %s [member-id] %s",
+                    cartridgeType, memberId);
             log.error(msg);
             throw new InvalidCartridgeTypeException(msg);
         }
 
         // if no matching node id can be found.
         if (nodeId == null) {
-            String msg = "Termination failed. Cannot find a node id for Member Id: " + memberId;
+            String msg = String.format("Member termination failed, could not find node id in member context: " +
+                            "[cartridge-type] %s [member-id] %s",
+                    cartridgeType, memberId);
 
-            // log information
-            CloudControllerServiceUtil.logTermination(memberContext);
+            // Execute member termination post process
+            CloudControllerServiceUtil.executeMemberTerminationPostProcess(memberContext);
             log.error(msg);
             throw new InvalidMemberException(msg);
         }
 
-        IaasProvider iaasProvider = cartridge.getIaasProviderOfPartition(partitionId);
+        // Terminate the actual member instance
+        destroyNode(nodeId, memberContext);
+    }
 
-        // terminate it!
-        CloudControllerServiceUtil.terminate(iaasProvider, nodeId, memberContext);
+    /**
+     * Terminate member instance via jclouds API
+     *
+     * @param memberContext
+     * @param nodeId
+     * @return will return the IaaSProvider
+     */
+    private void destroyNode(String nodeId, MemberContext memberContext) {
+        // Detach volumes if any
+        detachVolume(memberContext);
 
-        // log information
-        CloudControllerServiceUtil.logTermination(memberContext);
+        // Destroy the node via jclouds
+        getIaasProvider().getComputeService().destroyNode(nodeId);
+
+        // release allocated IP address
+        if (memberContext.getAllocatedIpAddress() != null) {
+            releaseAddress(memberContext.getAllocatedIpAddress());
+        }
+
+        if (log.isInfoEnabled()) {
+            log.info("Member terminated: [member-id] " + memberContext.getMemberId());
+        }
+    }
+
+    private void detachVolume(MemberContext ctxt) {
+        String clusterId = ctxt.getClusterId();
+        ClusterContext clusterContext = CloudControllerContext.getInstance().getClusterContext(clusterId);
+        if (clusterContext.getVolumes() != null) {
+            for (Volume volume : clusterContext.getVolumes()) {
+                try {
+                    String volumeId = volume.getId();
+                    if (volumeId == null) {
+                        return;
+                    }
+                    detachVolume(ctxt.getInstanceId(), volumeId);
+                } catch (ResourceNotFoundException ignore) {
+                    if (log.isDebugEnabled()) {
+                        log.debug(ignore);
+                    }
+                }
+            }
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/aeb4458c/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/MockIaasService.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/MockIaasService.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/MockIaasService.java
index f9d348c..5c3d83f 100644
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/MockIaasService.java
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/MockIaasService.java
@@ -181,11 +181,6 @@ public class MockIaasService {
         synchronized (MockIaasService.class) {
             MockMember mockMember = membersMap.get(memberContext.getMemberId());
             if (mockMember != null) {
-                //updating the topology
-                TopologyBuilder.handleMemberTerminated(memberContext.getCartridgeType(),
-                        memberContext.getClusterId(), memberContext.getNetworkPartitionId(),
-                        memberContext.getPartition().getId(), memberContext.getMemberId());
-
                 mockMember.terminate();
                 membersMap.remove(memberContext.getMemberId());
             }

http://git-wip-us.apache.org/repos/asf/stratos/blob/aeb4458c/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/messaging/publisher/CartridgeInstanceDataPublisher.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/messaging/publisher/CartridgeInstanceDataPublisher.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/messaging/publisher/CartridgeInstanceDataPublisher.java
deleted file mode 100644
index 5f27a7a..0000000
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/messaging/publisher/CartridgeInstanceDataPublisher.java
+++ /dev/null
@@ -1,214 +0,0 @@
-/*
- * 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.cloud.controller.messaging.publisher;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.stratos.cloud.controller.config.CloudControllerConfig;
-import org.apache.stratos.cloud.controller.context.CloudControllerContext;
-import org.apache.stratos.cloud.controller.exception.CloudControllerException;
-import org.apache.stratos.cloud.controller.domain.Cartridge;
-import org.apache.stratos.cloud.controller.domain.MemberContext;
-import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
-import org.jclouds.compute.domain.NodeMetadata;
-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;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-
-/**
- *  This will publish the state changes of a node in the topology to a data receiver
- */
-public class CartridgeInstanceDataPublisher {
-    
-    private static final Log log = LogFactory.getLog(CartridgeInstanceDataPublisher.class);
-    private static AsyncDataPublisher dataPublisher;
-    private static StreamDefinition streamDefinition;
-    private static final String cloudControllerEventStreamVersion = "1.0.0";
-
-    public static void publish(String memberId,
-                               String partitionId,
-                               String networkId,
-                               String clusterId,
-                               String serviceName,
-                               String status,
-                               NodeMetadata metadata) {
-        if(!CloudControllerConfig.getInstance().isBAMDataPublisherEnabled()){
-            return;
-        }
-        log.debug(CloudControllerConstants.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.");
-                release();
-                return;
-            }
-        }
-
-
-        MemberContext memberContext = CloudControllerContext.getInstance().getMemberContextOfMemberId(memberId);
-        String cartridgeType = memberContext.getCartridgeType();
-        Cartridge cartridge = CloudControllerContext.getInstance().getCartridge(cartridgeType);
-        
-        //Construct the data to be published
-        List<Object> payload = new ArrayList<Object>();
-        // Payload values
-        payload.add(memberId);
-        payload.add(serviceName);
-        payload.add(clusterId);
-        payload.add(handleNull(memberContext.getLbClusterId()));
-        payload.add(handleNull(partitionId));
-        payload.add(handleNull(networkId));
-		if (cartridge != null) {
-			payload.add(handleNull(String.valueOf(cartridge.isMultiTenant())));
-		} else {
-			payload.add("");
-		}
-        payload.add(handleNull(memberContext.getPartition().getProvider()));
-        payload.add(handleNull(status));
-
-        if(metadata != null) {
-            payload.add(metadata.getHostname());
-            payload.add(metadata.getHardware().getHypervisor());
-            payload.add(String.valueOf(metadata.getHardware().getRam()));
-            payload.add(metadata.getImageId());
-            payload.add(metadata.getLoginPort());
-            payload.add(metadata.getOperatingSystem().getName());
-            payload.add(metadata.getOperatingSystem().getVersion());
-            payload.add(metadata.getOperatingSystem().getArch());
-            payload.add(String.valueOf(metadata.getOperatingSystem().is64Bit()));
-        } else {
-            payload.add("");
-            payload.add("");
-            payload.add("");
-            payload.add("");
-            payload.add(0);
-            payload.add("");
-            payload.add("");
-            payload.add("");
-            payload.add("");
-        }
-
-        payload.add(handleNull(memberContext.getPrivateIpAddress()));
-        payload.add(handleNull(memberContext.getPublicIpAddress()));
-        payload.add(handleNull(memberContext.getAllocatedIpAddress()));
-
-        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 void release(){
-        CloudControllerContext.getInstance().setPublisherRunning(false);
-    }
-    
-    private static StreamDefinition initializeStream() throws Exception {
-        streamDefinition = new StreamDefinition(
-                CloudControllerConstants.CLOUD_CONTROLLER_EVENT_STREAM,
-                cloudControllerEventStreamVersion);
-        streamDefinition.setNickName("cloud.controller");
-        streamDefinition.setDescription("Instances booted up by the Cloud Controller");
-        // Payload definition
-        List<Attribute> payloadData = new ArrayList<Attribute>();
-        payloadData.add(new Attribute(CloudControllerConstants.MEMBER_ID_COL, AttributeType.STRING));
-        payloadData.add(new Attribute(CloudControllerConstants.CARTRIDGE_TYPE_COL, AttributeType.STRING));
-        payloadData.add(new Attribute(CloudControllerConstants.CLUSTER_ID_COL, AttributeType.STRING));
-        payloadData.add(new Attribute(CloudControllerConstants.LB_CLUSTER_ID_COL, AttributeType.STRING));
-        payloadData.add(new Attribute(CloudControllerConstants.PARTITION_ID_COL, AttributeType.STRING));
-        payloadData.add(new Attribute(CloudControllerConstants.NETWORK_ID_COL, AttributeType.STRING));
-        payloadData.add(new Attribute(CloudControllerConstants.IS_MULTI_TENANT_COL, AttributeType.STRING));
-        payloadData.add(new Attribute(CloudControllerConstants.IAAS_COL, AttributeType.STRING));
-        payloadData.add(new Attribute(CloudControllerConstants.STATUS_COL, AttributeType.STRING));
-        payloadData.add(new Attribute(CloudControllerConstants.HOST_NAME_COL, AttributeType.STRING));
-        payloadData.add(new Attribute(CloudControllerConstants.HYPERVISOR_COL, AttributeType.STRING));
-        payloadData.add(new Attribute(CloudControllerConstants.RAM_COL, AttributeType.STRING));
-        payloadData.add(new Attribute(CloudControllerConstants.IMAGE_ID_COL, AttributeType.STRING));
-        payloadData.add(new Attribute(CloudControllerConstants.LOGIN_PORT_COL, AttributeType.INT));
-        payloadData.add(new Attribute(CloudControllerConstants.OS_NAME_COL, AttributeType.STRING));
-        payloadData.add(new Attribute(CloudControllerConstants.OS_VERSION_COL, AttributeType.STRING));
-        payloadData.add(new Attribute(CloudControllerConstants.OS_ARCH_COL, AttributeType.STRING));
-        payloadData.add(new Attribute(CloudControllerConstants.OS_BIT_COL, AttributeType.STRING));
-        payloadData.add(new Attribute(CloudControllerConstants.PRIV_IP_COL, AttributeType.STRING));
-        payloadData.add(new Attribute(CloudControllerConstants.PUB_IP_COL, AttributeType.STRING));
-        payloadData.add(new Attribute(CloudControllerConstants.ALLOCATE_IP_COL, AttributeType.STRING));
-        streamDefinition.setPayloadData(payloadData);
-        return streamDefinition;
-    }
-
-
-    private static void createDataPublisher(){
-        //creating the agent
-
-        ServerConfiguration serverConfig =  CarbonUtils.getServerConfiguration();
-        String trustStorePath = serverConfig.getFirstProperty("Security.TrustStore.Location");
-        String trustStorePassword = serverConfig.getFirstProperty("Security.TrustStore.Password");
-        String bamServerUrl = serverConfig.getFirstProperty("BamServerURL");
-        String adminUsername = CloudControllerConfig.getInstance().getDataPubConfig().getBamUsername();
-        String adminPassword = CloudControllerConfig.getInstance().getDataPubConfig().getBamPassword();
-
-        System.setProperty("javax.net.ssl.trustStore", trustStorePath);
-        System.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword);
-
-
-        try {
-            dataPublisher = new AsyncDataPublisher("tcp://" +  bamServerUrl + "", adminUsername, adminPassword);
-            CloudControllerContext.getInstance().setDataPublisher(dataPublisher);
-            initializeStream();
-            dataPublisher.addStreamDefinition(streamDefinition);
-        } catch (Exception e) {
-            String msg = "Unable to create a data publisher to " + bamServerUrl +
-                    ". Usage Agent will not function properly. ";
-            log.error(msg, e);
-            throw new CloudControllerException(msg, e);
-        }
-    }
-    
-    private static String handleNull(String val) {
-        if (val == null) {
-            return "";
-        }
-        return val;
-    }
-
-    
-}

http://git-wip-us.apache.org/repos/asf/stratos/blob/aeb4458c/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/messaging/publisher/StatisticsDataPublisher.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/messaging/publisher/StatisticsDataPublisher.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/messaging/publisher/StatisticsDataPublisher.java
new file mode 100644
index 0000000..15cf63b
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/messaging/publisher/StatisticsDataPublisher.java
@@ -0,0 +1,212 @@
+/*
+ * 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.cloud.controller.messaging.publisher;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.cloud.controller.config.CloudControllerConfig;
+import org.apache.stratos.cloud.controller.context.CloudControllerContext;
+import org.apache.stratos.cloud.controller.exception.CloudControllerException;
+import org.apache.stratos.cloud.controller.domain.Cartridge;
+import org.apache.stratos.cloud.controller.domain.MemberContext;
+import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
+import org.jclouds.compute.domain.NodeMetadata;
+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;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+/**
+ *  Statistics data publisher for publishing instance statistics to BAM.
+ */
+public class StatisticsDataPublisher {
+    
+    private static final Log log = LogFactory.getLog(StatisticsDataPublisher.class);
+    private static AsyncDataPublisher dataPublisher;
+    private static StreamDefinition streamDefinition;
+    private static final String cloudControllerEventStreamVersion = "1.0.0";
+
+    public static void publish(String memberId,
+                               String partitionId,
+                               String networkId,
+                               String clusterId,
+                               String serviceName,
+                               String status,
+                               NodeMetadata metadata) {
+        if(!CloudControllerConfig.getInstance().isBAMDataPublisherEnabled()){
+            return;
+        }
+        log.debug(CloudControllerConstants.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.");
+                release();
+                return;
+            }
+        }
+
+
+        MemberContext memberContext = CloudControllerContext.getInstance().getMemberContextOfMemberId(memberId);
+        String cartridgeType = memberContext.getCartridgeType();
+        Cartridge cartridge = CloudControllerContext.getInstance().getCartridge(cartridgeType);
+        
+        //Construct the data to be published
+        List<Object> payload = new ArrayList<Object>();
+        // Payload values
+        payload.add(memberId);
+        payload.add(serviceName);
+        payload.add(clusterId);
+        payload.add(handleNull(memberContext.getLbClusterId()));
+        payload.add(handleNull(partitionId));
+        payload.add(handleNull(networkId));
+		if (cartridge != null) {
+			payload.add(handleNull(String.valueOf(cartridge.isMultiTenant())));
+		} else {
+			payload.add("");
+		}
+        payload.add(handleNull(memberContext.getPartition().getProvider()));
+        payload.add(handleNull(status));
+
+        if(metadata != null) {
+            payload.add(metadata.getHostname());
+            payload.add(metadata.getHardware().getHypervisor());
+            payload.add(String.valueOf(metadata.getHardware().getRam()));
+            payload.add(metadata.getImageId());
+            payload.add(metadata.getLoginPort());
+            payload.add(metadata.getOperatingSystem().getName());
+            payload.add(metadata.getOperatingSystem().getVersion());
+            payload.add(metadata.getOperatingSystem().getArch());
+            payload.add(String.valueOf(metadata.getOperatingSystem().is64Bit()));
+        } else {
+            payload.add("");
+            payload.add("");
+            payload.add("");
+            payload.add("");
+            payload.add(0);
+            payload.add("");
+            payload.add("");
+            payload.add("");
+            payload.add("");
+        }
+
+        payload.add(handleNull(memberContext.getPrivateIpAddress()));
+        payload.add(handleNull(memberContext.getPublicIpAddress()));
+        payload.add(handleNull(memberContext.getAllocatedIpAddress()));
+
+        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 void release(){
+        CloudControllerContext.getInstance().setPublisherRunning(false);
+    }
+    
+    private static StreamDefinition initializeStream() throws Exception {
+        streamDefinition = new StreamDefinition(
+                CloudControllerConstants.CLOUD_CONTROLLER_EVENT_STREAM,
+                cloudControllerEventStreamVersion);
+        streamDefinition.setNickName("cloud.controller");
+        streamDefinition.setDescription("Instances booted up by the Cloud Controller");
+        // Payload definition
+        List<Attribute> payloadData = new ArrayList<Attribute>();
+        payloadData.add(new Attribute(CloudControllerConstants.MEMBER_ID_COL, AttributeType.STRING));
+        payloadData.add(new Attribute(CloudControllerConstants.CARTRIDGE_TYPE_COL, AttributeType.STRING));
+        payloadData.add(new Attribute(CloudControllerConstants.CLUSTER_ID_COL, AttributeType.STRING));
+        payloadData.add(new Attribute(CloudControllerConstants.LB_CLUSTER_ID_COL, AttributeType.STRING));
+        payloadData.add(new Attribute(CloudControllerConstants.PARTITION_ID_COL, AttributeType.STRING));
+        payloadData.add(new Attribute(CloudControllerConstants.NETWORK_ID_COL, AttributeType.STRING));
+        payloadData.add(new Attribute(CloudControllerConstants.IS_MULTI_TENANT_COL, AttributeType.STRING));
+        payloadData.add(new Attribute(CloudControllerConstants.IAAS_COL, AttributeType.STRING));
+        payloadData.add(new Attribute(CloudControllerConstants.STATUS_COL, AttributeType.STRING));
+        payloadData.add(new Attribute(CloudControllerConstants.HOST_NAME_COL, AttributeType.STRING));
+        payloadData.add(new Attribute(CloudControllerConstants.HYPERVISOR_COL, AttributeType.STRING));
+        payloadData.add(new Attribute(CloudControllerConstants.RAM_COL, AttributeType.STRING));
+        payloadData.add(new Attribute(CloudControllerConstants.IMAGE_ID_COL, AttributeType.STRING));
+        payloadData.add(new Attribute(CloudControllerConstants.LOGIN_PORT_COL, AttributeType.INT));
+        payloadData.add(new Attribute(CloudControllerConstants.OS_NAME_COL, AttributeType.STRING));
+        payloadData.add(new Attribute(CloudControllerConstants.OS_VERSION_COL, AttributeType.STRING));
+        payloadData.add(new Attribute(CloudControllerConstants.OS_ARCH_COL, AttributeType.STRING));
+        payloadData.add(new Attribute(CloudControllerConstants.OS_BIT_COL, AttributeType.STRING));
+        payloadData.add(new Attribute(CloudControllerConstants.PRIV_IP_COL, AttributeType.STRING));
+        payloadData.add(new Attribute(CloudControllerConstants.PUB_IP_COL, AttributeType.STRING));
+        payloadData.add(new Attribute(CloudControllerConstants.ALLOCATE_IP_COL, AttributeType.STRING));
+        streamDefinition.setPayloadData(payloadData);
+        return streamDefinition;
+    }
+
+
+    private static void createDataPublisher(){
+        //creating the agent
+
+        ServerConfiguration serverConfig =  CarbonUtils.getServerConfiguration();
+        String trustStorePath = serverConfig.getFirstProperty("Security.TrustStore.Location");
+        String trustStorePassword = serverConfig.getFirstProperty("Security.TrustStore.Password");
+        String bamServerUrl = serverConfig.getFirstProperty("BamServerURL");
+        String adminUsername = CloudControllerConfig.getInstance().getDataPubConfig().getBamUsername();
+        String adminPassword = CloudControllerConfig.getInstance().getDataPubConfig().getBamPassword();
+
+        System.setProperty("javax.net.ssl.trustStore", trustStorePath);
+        System.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword);
+
+
+        try {
+            dataPublisher = new AsyncDataPublisher("tcp://" +  bamServerUrl + "", adminUsername, adminPassword);
+            CloudControllerContext.getInstance().setDataPublisher(dataPublisher);
+            initializeStream();
+            dataPublisher.addStreamDefinition(streamDefinition);
+        } catch (Exception e) {
+            String msg = "Unable to create a data publisher to " + bamServerUrl +
+                    ". Usage Agent will not function properly. ";
+            log.error(msg, e);
+            throw new CloudControllerException(msg, e);
+        }
+    }
+    
+    private static String handleNull(String val) {
+        if (val == null) {
+            return "";
+        }
+        return val;
+    }
+}

http://git-wip-us.apache.org/repos/asf/stratos/blob/aeb4458c/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/messaging/topology/TopologyBuilder.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/messaging/topology/TopologyBuilder.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/messaging/topology/TopologyBuilder.java
index 1362f42..b97e98b 100644
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/messaging/topology/TopologyBuilder.java
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/messaging/topology/TopologyBuilder.java
@@ -24,10 +24,9 @@ import org.apache.commons.logging.LogFactory;
 import org.apache.stratos.cloud.controller.context.CloudControllerContext;
 import org.apache.stratos.cloud.controller.domain.*;
 import org.apache.stratos.cloud.controller.domain.Cartridge;
-import org.apache.stratos.cloud.controller.exception.CloudControllerException;
 import org.apache.stratos.cloud.controller.exception.InvalidCartridgeTypeException;
 import org.apache.stratos.cloud.controller.exception.InvalidMemberException;
-import org.apache.stratos.cloud.controller.messaging.publisher.CartridgeInstanceDataPublisher;
+import org.apache.stratos.cloud.controller.messaging.publisher.StatisticsDataPublisher;
 import org.apache.stratos.cloud.controller.util.CloudControllerUtil;
 import org.apache.stratos.common.constants.StratosConstants;
 import org.apache.stratos.messaging.domain.applications.ClusterDataHolder;
@@ -42,7 +41,6 @@ import org.apache.stratos.messaging.event.instance.status.InstanceStartedEvent;
 import org.apache.stratos.messaging.event.topology.*;
 import org.apache.stratos.metadata.client.defaults.DefaultMetaDataServiceClient;
 import org.apache.stratos.metadata.client.defaults.MetaDataServiceClient;
-import org.wso2.carbon.registry.core.exceptions.RegistryException;
 
 import java.util.*;
 
@@ -510,13 +508,13 @@ public class TopologyBuilder {
         //memberStartedEvent.
         TopologyEventPublisher.sendMemberStartedEvent(instanceStartedEvent);
         //publishing data
-        CartridgeInstanceDataPublisher.publish(instanceStartedEvent.getMemberId(),
-                                            instanceStartedEvent.getPartitionId(),
-                                            instanceStartedEvent.getNetworkPartitionId(),
-                                            instanceStartedEvent.getClusterId(),
-                                            instanceStartedEvent.getServiceName(),
-                                            MemberStatus.Starting.toString(),
-                                            null);
+        StatisticsDataPublisher.publish(instanceStartedEvent.getMemberId(),
+                instanceStartedEvent.getPartitionId(),
+                instanceStartedEvent.getNetworkPartitionId(),
+                instanceStartedEvent.getClusterId(),
+                instanceStartedEvent.getServiceName(),
+                MemberStatus.Starting.toString(),
+                null);
     }
 
     public static void handleMemberActivated(InstanceActivatedEvent instanceActivatedEvent) {
@@ -585,13 +583,13 @@ public class TopologyBuilder {
         }
         TopologyEventPublisher.sendMemberActivatedEvent(memberActivatedEvent);
         //publishing data
-        CartridgeInstanceDataPublisher.publish(memberActivatedEvent.getMemberId(),
-                                            memberActivatedEvent.getPartitionId(),
-                                            memberActivatedEvent.getNetworkPartitionId(),
-                                            memberActivatedEvent.getClusterId(),
-                                            memberActivatedEvent.getServiceName(),
-                                            MemberStatus.Activated.toString(),
-                                            null);
+        StatisticsDataPublisher.publish(memberActivatedEvent.getMemberId(),
+                memberActivatedEvent.getPartitionId(),
+                memberActivatedEvent.getNetworkPartitionId(),
+                memberActivatedEvent.getClusterId(),
+                memberActivatedEvent.getServiceName(),
+                MemberStatus.Activated.toString(),
+                null);
     }
 
     public static void handleMemberReadyToShutdown(InstanceReadyToShutdownEvent instanceReadyToShutdownEvent)
@@ -641,13 +639,13 @@ public class TopologyBuilder {
         }
         TopologyEventPublisher.sendMemberReadyToShutdownEvent(memberReadyToShutdownEvent);
         //publishing data
-        CartridgeInstanceDataPublisher.publish(instanceReadyToShutdownEvent.getMemberId(),
-                                            instanceReadyToShutdownEvent.getPartitionId(),
-                                            instanceReadyToShutdownEvent.getNetworkPartitionId(),
-                                            instanceReadyToShutdownEvent.getClusterId(),
-                                            instanceReadyToShutdownEvent.getServiceName(),
-                                            MemberStatus.ReadyToShutDown.toString(),
-                                            null);
+        StatisticsDataPublisher.publish(instanceReadyToShutdownEvent.getMemberId(),
+                instanceReadyToShutdownEvent.getPartitionId(),
+                instanceReadyToShutdownEvent.getNetworkPartitionId(),
+                instanceReadyToShutdownEvent.getClusterId(),
+                instanceReadyToShutdownEvent.getServiceName(),
+                MemberStatus.ReadyToShutDown.toString(),
+                null);
         //termination of particular instance will be handled by autoscaler
     }
 
@@ -702,6 +700,14 @@ public class TopologyBuilder {
 
     }
 
+    /***
+     * Remove member from topology and send member terminated event.
+     * @param serviceName
+     * @param clusterId
+     * @param networkPartitionId
+     * @param partitionId
+     * @param memberId
+     */
     public static void handleMemberTerminated(String serviceName, String clusterId,
                                               String networkPartitionId, String partitionId,
                                               String memberId) {

http://git-wip-us.apache.org/repos/asf/stratos/blob/aeb4458c/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/CloudControllerServiceImpl.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/CloudControllerServiceImpl.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/CloudControllerServiceImpl.java
index f2a2e7b..e723cbd 100644
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/CloudControllerServiceImpl.java
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/CloudControllerServiceImpl.java
@@ -35,8 +35,7 @@ import org.apache.stratos.cloud.controller.functions.ContainerClusterContextToKu
 import org.apache.stratos.cloud.controller.functions.ContainerClusterContextToReplicationController;
 import org.apache.stratos.cloud.controller.functions.PodToMemberContext;
 import org.apache.stratos.cloud.controller.iaases.Iaas;
-import org.apache.stratos.cloud.controller.iaases.validators.PartitionValidator;
-import org.apache.stratos.cloud.controller.messaging.publisher.CartridgeInstanceDataPublisher;
+import org.apache.stratos.cloud.controller.messaging.publisher.StatisticsDataPublisher;
 import org.apache.stratos.cloud.controller.messaging.topology.TopologyBuilder;
 import org.apache.stratos.cloud.controller.messaging.topology.TopologyEventPublisher;
 import org.apache.stratos.cloud.controller.messaging.topology.TopologyManager;
@@ -45,8 +44,6 @@ import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
 import org.apache.stratos.cloud.controller.util.CloudControllerUtil;
 import org.apache.stratos.cloud.controller.util.PodActivationWatcher;
 import org.apache.stratos.common.Property;
-import org.apache.stratos.cloud.controller.iaases.validators.IaasBasedPartitionValidator;
-import org.apache.stratos.cloud.controller.iaases.validators.KubernetesBasedPartitionValidator;
 import org.apache.stratos.common.constants.StratosConstants;
 import org.apache.stratos.common.kubernetes.KubernetesGroup;
 import org.apache.stratos.common.kubernetes.KubernetesHost;
@@ -525,27 +522,23 @@ public class CloudControllerServiceImpl implements CloudControllerService {
     @Override
     public void terminateInstance(String memberId) throws InvalidMemberException, InvalidCartridgeTypeException {
 
-        handleNullObject(memberId, "Termination failed. Null member id.");
+        handleNullObject(memberId, "Member termination failed, member id is null.");
 
         MemberContext memberContext = CloudControllerContext.getInstance().getMemberContextOfMemberId(memberId);
-
         if (memberContext == null) {
-            String msg = "Termination failed. Invalid Member Id: " + memberId;
+            String msg = "Member termination failed, member context not found: [member-id] " + memberId;
             log.error(msg);
             throw new InvalidMemberException(msg);
         }
 
-        if (memberContext.getNodeId() == null && memberContext.getInstanceId() == null) {
-            // sending member terminated since this instance isn't reachable.
-            if (log.isInfoEnabled()){
-                log.info(String.format(
-                        "Member cannot be terminated because it is not reachable. [member] %s [nodeId] %s [instanceId] %s. Removing member from topology.",
-                        memberContext.getMemberId(),
-                        memberContext.getNodeId(),
-                        memberContext.getInstanceId()));
+        if ((memberContext.getNodeId() == null) && (memberContext.getInstanceId() == null)) {
+            if (log.isErrorEnabled()) {
+                log.error(String.format(
+                        "Member termination failed, node id and instance id are null: [member-id] %s " +
+                                "Removing member from topology.",
+                        memberContext.getMemberId()));
             }
-
-            CloudControllerServiceUtil.logTermination(memberContext);
+            CloudControllerServiceUtil.executeMemberTerminationPostProcess(memberContext);
         }
 
         // check if status == active, if true, then this is a termination on member faulty
@@ -553,44 +546,40 @@ public class CloudControllerServiceImpl implements CloudControllerService {
         try {
             TopologyManager.acquireReadLock();
             topology = TopologyManager.getTopology();
-        } finally {
-            TopologyManager.releaseReadLock();
-        }
-
-        org.apache.stratos.messaging.domain.topology.Service service = topology.getService(memberContext.getCartridgeType());
-
-        if (service != null) {
-            Cluster cluster = service.getCluster(memberContext.getClusterId());
-
-            if (cluster != null) {
-                Member member = cluster.getMember(memberId);
+            org.apache.stratos.messaging.domain.topology.Service service = topology.getService(memberContext.getCartridgeType());
+
+            if (service != null) {
+                Cluster cluster = service.getCluster(memberContext.getClusterId());
+                if (cluster != null) {
+                    Member member = cluster.getMember(memberId);
+                    if (member != null) {
+                        // change member status if termination on a faulty member
+                        if (fixMemberStatus(member, topology)) {
+                            // set the time this member was added to ReadyToShutdown status
+                            memberContext.setObsoleteInitTime(System.currentTimeMillis());
+                        }
 
-                if (member != null) {
-                    // change member status if termination on a faulty member
-                    if(fixMemberStatus(member, topology)){
-                        // set the time this member was added to ReadyToShutdown status
-                        memberContext.setObsoleteInitTime(System.currentTimeMillis());
-                    }
+                        // check if ready to shutdown member is expired and send
+                        // member terminated if it is.
+                        if (isMemberExpired(member, memberContext.getObsoleteInitTime(), memberContext.getObsoleteExpiryTime())) {
+                            if (log.isInfoEnabled()) {
+                                log.info(String.format(
+                                        "Member pending termination in ReadyToShutdown state exceeded expiry time. This member has to be manually deleted: %s",
+                                        memberContext.getMemberId()));
+                            }
 
-                    // check if ready to shutdown member is expired and send
-                    // member terminated if it is.
-                    if (isMemberExpired(member, memberContext.getObsoleteInitTime(), memberContext.getObsoleteExpiryTime())) {
-                        if (log.isInfoEnabled()) {
-                            log.info(String.format(
-                                    "Member pending termination in ReadyToShutdown state exceeded expiry time. This member has to be manually deleted: %s",
-                                    memberContext.getMemberId()));
+                            CloudControllerServiceUtil.executeMemberTerminationPostProcess(memberContext);
+                            return;
                         }
-
-                        CloudControllerServiceUtil.logTermination(memberContext);
-                        return;
                     }
                 }
             }
-        }
-
-        ThreadExecutor exec = ThreadExecutor.getInstance();
-        exec.execute(new InstanceTerminator(memberContext));
 
+            ThreadExecutor exec = ThreadExecutor.getInstance();
+            exec.execute(new InstanceTerminator(memberContext));
+        } finally {
+            TopologyManager.releaseReadLock();
+        }
     }
 
     /**
@@ -649,7 +638,7 @@ public class CloudControllerServiceImpl implements CloudControllerService {
 
             TopologyEventPublisher.sendMemberReadyToShutdownEvent(memberReadyToShutdownEvent);
             //publishing data
-            CartridgeInstanceDataPublisher.publish(member.getMemberId(),
+            StatisticsDataPublisher.publish(member.getMemberId(),
                     member.getPartitionId(),
                     member.getNetworkPartitionId(),
                     member.getClusterId(),
@@ -1263,7 +1252,7 @@ public class CloudControllerServiceImpl implements CloudControllerService {
     private String validateProperty(String property, org.apache.stratos.common.Properties properties, String object) {
 
         String propVal = CloudControllerUtil.getProperty(properties, property);
-        handleNullObject(propVal, "Property validation failed. Cannot find property: '" + property+ " in "+object);
+        handleNullObject(propVal, "Property validation failed. Cannot find property: '" + property + " in " + object);
         return propVal;
 
     }
@@ -1373,7 +1362,7 @@ public class CloudControllerServiceImpl implements CloudControllerService {
             List<MemberContext> membersToBeRemoved = CloudControllerContext.getInstance().getMemberContextsOfClusterId(clusterId);
 
             for (MemberContext memberContext : membersToBeRemoved) {
-                CloudControllerServiceUtil.logTermination(memberContext);
+                CloudControllerServiceUtil.executeMemberTerminationPostProcess(memberContext);
             }
 
             // persist
@@ -1509,7 +1498,7 @@ public class CloudControllerServiceImpl implements CloudControllerService {
                     for (Pod pod : difference) {
                         if (pod != null) {
                             MemberContext context = CloudControllerContext.getInstance().getMemberContextOfMemberId(pod.getId());
-                            CloudControllerServiceUtil.logTermination(context);
+                            CloudControllerServiceUtil.executeMemberTerminationPostProcess(context);
                             memberContexts.add(context);
                         }
                     }
@@ -1568,7 +1557,7 @@ public class CloudControllerServiceImpl implements CloudControllerService {
                 // member id = pod id
                 kubApi.deletePod(memberId);
                 MemberContext memberToBeRemoved = CloudControllerContext.getInstance().getMemberContextOfMemberId(memberId);
-                CloudControllerServiceUtil.logTermination(memberToBeRemoved);
+                CloudControllerServiceUtil.executeMemberTerminationPostProcess(memberToBeRemoved);
 
                 return memberToBeRemoved;
 

http://git-wip-us.apache.org/repos/asf/stratos/blob/aeb4458c/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/CloudControllerServiceUtil.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/CloudControllerServiceUtil.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/CloudControllerServiceUtil.java
index 1b014ab..cdde8d0 100644
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/CloudControllerServiceUtil.java
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/CloudControllerServiceUtil.java
@@ -26,24 +26,19 @@ import com.google.common.net.InetAddresses;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.stratos.cloud.controller.context.CloudControllerContext;
-import org.apache.stratos.cloud.controller.domain.ClusterContext;
-import org.apache.stratos.cloud.controller.domain.ContainerClusterContext;
 import org.apache.stratos.cloud.controller.domain.IaasProvider;
 import org.apache.stratos.cloud.controller.domain.MemberContext;
 import org.apache.stratos.cloud.controller.domain.Partition;
-import org.apache.stratos.cloud.controller.domain.Volume;
-import org.apache.stratos.cloud.controller.exception.CloudControllerException;
 import org.apache.stratos.cloud.controller.exception.InvalidIaasProviderException;
 import org.apache.stratos.cloud.controller.exception.InvalidPartitionException;
 import org.apache.stratos.cloud.controller.iaases.Iaas;
 import org.apache.stratos.cloud.controller.iaases.validators.IaasBasedPartitionValidator;
 import org.apache.stratos.cloud.controller.iaases.validators.KubernetesBasedPartitionValidator;
-import org.apache.stratos.cloud.controller.messaging.publisher.CartridgeInstanceDataPublisher;
+import org.apache.stratos.cloud.controller.messaging.publisher.StatisticsDataPublisher;
 import org.apache.stratos.cloud.controller.messaging.topology.TopologyBuilder;
 import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
 import org.apache.stratos.cloud.controller.util.CloudControllerUtil;
 import org.apache.stratos.messaging.domain.topology.MemberStatus;
-import org.jclouds.rest.ResourceNotFoundException;
 
 /**
  * Cloud controller service utility methods.
@@ -59,85 +54,24 @@ public class CloudControllerServiceUtil {
     }
 
     /**
-     * A helper method to terminate an instance.
-     *
-     * @param iaasProvider
-     * @param ctxt
-     * @param nodeId
-     * @return will return the IaaSProvider
+     * Update the topology, publish statistics to BAM, remove member context
+     * and persist cloud controller context.
+     * @param memberContext
      */
-    public static IaasProvider terminate(IaasProvider iaasProvider,
-                                   String nodeId, MemberContext ctxt) {
-        Iaas iaas = iaasProvider.getIaas();
-        if (iaas == null) {
-
-            try {
-                iaas = buildIaas(iaasProvider);
-            } catch (InvalidIaasProviderException e) {
-                String msg =
-                        "Instance termination failed. " + ctxt.toString() +
-                                ". Cause: Unable to build Iaas of this " + iaasProvider.toString();
-                log.error(msg, e);
-                throw new CloudControllerException(msg, e);
-            }
-
-        }
-
-        //detach volumes if any
-        detachVolume(iaasProvider, ctxt);
-
-        // destroy the node
-        iaasProvider.getComputeService().destroyNode(nodeId);
-
-        // release allocated IP address
-        if (ctxt.getAllocatedIpAddress() != null) {
-            iaas.releaseAddress(ctxt.getAllocatedIpAddress());
-        }
-
-        if (log.isDebugEnabled()) {
-            log.debug("Member is terminated: " + ctxt.toString());
-        } else if (log.isInfoEnabled()) {
-            log.info("Member with id " + ctxt.getMemberId() + " is terminated");
-        }
-        return iaasProvider;
-    }
-
-    private static void detachVolume(IaasProvider iaasProvider, MemberContext ctxt) {
-        String clusterId = ctxt.getClusterId();
-        ClusterContext clusterCtxt = CloudControllerContext.getInstance().getClusterContext(clusterId);
-        if (clusterCtxt.getVolumes() != null) {
-            for (Volume volume : clusterCtxt.getVolumes()) {
-                try {
-                    String volumeId = volume.getId();
-                    if (volumeId == null) {
-                        return;
-                    }
-                    Iaas iaas = iaasProvider.getIaas();
-                    iaas.detachVolume(ctxt.getInstanceId(), volumeId);
-                } catch (ResourceNotFoundException ignore) {
-                    if (log.isDebugEnabled()) {
-                        log.debug(ignore);
-                    }
-                }
-            }
-        }
-    }
-
-    public static void logTermination(MemberContext memberContext) {
-
+    public static void executeMemberTerminationPostProcess(MemberContext memberContext) {
         if (memberContext == null) {
             return;
         }
 
         String partitionId = memberContext.getPartition() == null ? null : memberContext.getPartition().getId();
 
-        //updating the topology
+        // Update the topology
         TopologyBuilder.handleMemberTerminated(memberContext.getCartridgeType(),
                 memberContext.getClusterId(), memberContext.getNetworkPartitionId(),
                 partitionId, memberContext.getMemberId());
 
-        //publishing data
-        CartridgeInstanceDataPublisher.publish(memberContext.getMemberId(),
+        // Publish statistics to BAM
+        StatisticsDataPublisher.publish(memberContext.getMemberId(),
                 partitionId,
                 memberContext.getNetworkPartitionId(),
                 memberContext.getClusterId(),
@@ -145,10 +79,10 @@ public class CloudControllerServiceUtil {
                 MemberStatus.Terminated.toString(),
                 null);
 
-        // update data holders
+        // Remove member context
         CloudControllerContext.getInstance().removeMemberContext(memberContext.getMemberId(), memberContext.getClusterId());
 
-        // persist
+        // Persist cloud controller context
         CloudControllerContext.getInstance().persist();
     }
 
@@ -166,19 +100,6 @@ public class CloudControllerServiceUtil {
             // if this is a IaaS based partition
             Iaas iaas = iaasProvider.getIaas();
 
-//            if (iaas == null) {
-//                try {
-//                    iaas = CloudControllerUtil.getIaas(iaasProvider);
-//                } catch (InvalidIaasProviderException e) {
-//                    String msg =
-//                            "Invalid Partition - " + partition.toString()
-//                                    + ". Cause: Unable to build Iaas of this IaasProvider [Provider] : " + provider
-//                                    + ". " + e.getMessage();
-//                    log.error(msg, e);
-//                    throw new InvalidPartitionException(msg, e);
-//                }
-//            }
-
             IaasBasedPartitionValidator validator = (IaasBasedPartitionValidator) iaas.getPartitionValidator();
             validator.setIaasProvider(iaasProvider);
             iaasProvider = validator.validate(partitionId, partitionProperties);

http://git-wip-us.apache.org/repos/asf/stratos/blob/aeb4458c/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/InstanceCreator.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/InstanceCreator.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/InstanceCreator.java
index a1f38a9..b666c1c 100644
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/InstanceCreator.java
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/InstanceCreator.java
@@ -19,24 +19,16 @@
 
 package org.apache.stratos.cloud.controller.services.impl;
 
-import com.google.common.collect.ImmutableSet;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.stratos.cloud.controller.context.CloudControllerContext;
 import org.apache.stratos.cloud.controller.domain.*;
-import org.apache.stratos.cloud.controller.exception.CloudControllerException;
 import org.apache.stratos.cloud.controller.iaases.Iaas;
-import org.apache.stratos.cloud.controller.iaases.JcloudsIaas;
-import org.apache.stratos.cloud.controller.messaging.publisher.CartridgeInstanceDataPublisher;
+import org.apache.stratos.cloud.controller.messaging.publisher.StatisticsDataPublisher;
 import org.apache.stratos.cloud.controller.messaging.topology.TopologyBuilder;
-import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
 import org.apache.stratos.messaging.domain.topology.MemberStatus;
-import org.jclouds.compute.ComputeService;
 import org.jclouds.compute.domain.NodeMetadata;
-import org.jclouds.compute.domain.NodeMetadataBuilder;
-import org.jclouds.compute.domain.Template;
 
-import java.util.Set;
 import java.util.concurrent.locks.Lock;
 
 /**
@@ -84,7 +76,7 @@ public class InstanceCreator implements Runnable {
                     memberContext);
 
             // Publish instance creation statistics to BAM
-            CartridgeInstanceDataPublisher.publish(memberContext.getMemberId(),
+            StatisticsDataPublisher.publish(memberContext.getMemberId(),
                     memberContext.getPartition().getId(),
                     memberContext.getNetworkPartitionId(),
                     memberContext.getClusterId(),

http://git-wip-us.apache.org/repos/asf/stratos/blob/aeb4458c/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/InstanceTerminator.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/InstanceTerminator.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/InstanceTerminator.java
index 34c07e3..fdd6b29 100644
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/InstanceTerminator.java
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/InstanceTerminator.java
@@ -52,7 +52,11 @@ public class InstanceTerminator implements Runnable {
         Lock lock = null;
         try {
             lock = CloudControllerContext.getInstance().acquireMemberContextWriteLock();
+            // Terminate the instance
             iaas.terminateInstance(memberContext);
+
+            // Execute member termination post process
+            CloudControllerServiceUtil.executeMemberTerminationPostProcess(memberContext);
         } catch (Exception e) {
             String msg = "Instance termination failed! " + memberContext.toString();
             log.error(msg, e);


Mime
View raw message