You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by sa...@apache.org on 2013/07/02 11:37:58 UTC

[16/23] Refactoring org.wso2.carbon to org.apache.stratos

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ee2ab783/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/api/DefaultTenantBilling.java
----------------------------------------------------------------------
diff --git a/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/api/DefaultTenantBilling.java b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/api/DefaultTenantBilling.java
new file mode 100644
index 0000000..cd9b4bd
--- /dev/null
+++ b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/api/DefaultTenantBilling.java
@@ -0,0 +1,201 @@
+package org.wso2.carbon.billing.mgt.api;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.wso2.carbon.billing.core.dataobjects.Customer;
+import org.wso2.carbon.billing.core.dataobjects.Item;
+import org.wso2.carbon.billing.core.dataobjects.Subscription;
+import org.wso2.carbon.billing.mgt.services.BillingDataAccessService;
+import org.wso2.carbon.stratos.common.TenantBillingService;
+import org.wso2.carbon.stratos.common.exception.StratosException;
+import org.wso2.carbon.stratos.common.internal.CloudCommonServiceComponent;
+import org.wso2.carbon.user.api.Tenant;
+import org.wso2.carbon.user.api.TenantManager;
+
+import java.util.Calendar;
+
+public class DefaultTenantBilling implements TenantBillingService {
+    
+    private static final Log log = LogFactory.getLog(DefaultTenantBilling.class);
+    
+    public void addUsagePlan(Tenant tenant, String usagePlan) throws StratosException {
+
+        Customer customer = new Customer();
+        customer.setName(tenant.getDomain());
+        customer.setEmail(tenant.getEmail());
+        customer.setStartedDate(tenant.getCreatedDate());
+        customer.setFullName(tenant.getAdminFirstName() + " " + tenant.getAdminLastName());
+
+        customer.setId(tenant.getId());
+        Subscription subscription = new Subscription();
+        subscription.setCustomer(customer);
+        subscription.setActive(false);
+        subscription.setActiveSince(Calendar.getInstance().getTime());
+        Item item = new Item();
+        subscription.setItem(item);
+        subscription.setSubscriptionPlan(usagePlan);
+        try {
+            BillingDataAccessService dataAccessService = new BillingDataAccessService();
+            dataAccessService.addSubscription(subscription);
+        } catch (Exception e) {
+            log.error("Could not add new subscription for tenant: " +
+                      tenant.getDomain() + " " + e.getMessage(), e);
+        }
+    }
+
+    public String getActiveUsagePlan(String tenantDomain) throws StratosException {
+        Subscription subscription;
+        try {
+            TenantManager tenantMan = CloudCommonServiceComponent.getRealmService().getTenantManager();
+            int tenantId = tenantMan.getTenantId(tenantDomain);
+            BillingDataAccessService billingDataAccessService = new BillingDataAccessService();
+            subscription = billingDataAccessService.getActiveSubscriptionOfCustomerBySuperTenant(tenantId);
+        } catch (Exception e) {
+            String msg = "Error occurred while getting the usage plan for tenant: " + 
+                         tenantDomain + " " + e.getMessage();
+            log.error(msg);
+            throw new StratosException(msg, e);
+        }
+
+        if(subscription!=null){
+            return subscription.getSubscriptionPlan();
+        }else{
+            return null;
+        }
+    }
+
+    public void updateUsagePlan(String tenantDomain, String usagePlan) throws StratosException {
+        try {
+            TenantManager tenantManager = CloudCommonServiceComponent.getRealmService().getTenantManager();
+            int tenantId = tenantManager.getTenantId(tenantDomain);
+
+            BillingDataAccessService billingDataAccessService = new BillingDataAccessService();
+            Subscription currentSubscription = billingDataAccessService.
+                    getActiveSubscriptionOfCustomerBySuperTenant(tenantId);
+
+            if (currentSubscription != null && currentSubscription.getSubscriptionPlan() != null) {
+                if (!currentSubscription.getSubscriptionPlan().equals(usagePlan)) {
+                    boolean updated = billingDataAccessService.changeSubscriptionBySuperTenant(tenantId, usagePlan);
+                    if (updated) {
+                        log.debug("Usage plan was changed successfully from " + currentSubscription.getSubscriptionPlan() +
+                                  " to " + usagePlan);
+                    } else {
+                        log.debug("Usage plan was not changed");
+                    }
+                }
+            }else{
+                //tenant does not have an active subscription. First we have to check whether the tenant
+                //is active. If he is active only we will add a new usage plan. Otherwise it is useless
+                //to add a usage plan to an inactive tenant
+                Tenant tenant = tenantManager.getTenant(tenantId);
+                if(tenant.isActive()){
+                    //we add a new subscription
+                    Subscription subscription = new Subscription();
+                    subscription.setActive(true);
+                    subscription.setSubscriptionPlan(usagePlan);
+                    subscription.setActiveSince(null);
+                    subscription.setActiveUntil(null);
+                    Customer customer = new Customer();
+                    customer.setName(tenantDomain);
+                    customer.setId(tenantId);
+                    subscription.setCustomer(customer);
+
+                    int subsId = billingDataAccessService.addSubscription(subscription);
+                    if(subsId>0){
+                        log.info("Added a new " + subscription.getSubscriptionPlan() + " plan for the tenant " +
+                                 tenantDomain);
+                    }
+                }
+            }
+        } catch (Exception e) {
+            String msg = "Error occurred while changing the subscription plan for tenant: " + tenantDomain;
+            log.error(msg, e);
+            throw new StratosException(msg, e);
+        }
+    }
+
+    public void activateUsagePlan(String tenantDomain) throws StratosException {
+        try {
+            TenantManager tenantManager = CloudCommonServiceComponent.getRealmService().getTenantManager();
+            int tenantId = tenantManager.getTenantId(tenantDomain);
+            BillingDataAccessService dataAccessService = new BillingDataAccessService();
+            Subscription subscription = dataAccessService.getActiveSubscriptionOfCustomerBySuperTenant(tenantId);
+            if (subscription != null) {
+                String msg = "Unable to activate the subscription for tenant: " + tenantId +
+                             ". An active subscription already exists";
+                log.info(msg);
+            } else {
+                Subscription[] inactiveSubscriptions = dataAccessService.getInactiveSubscriptionsOfCustomer(tenantId);
+                if (inactiveSubscriptions.length == 1) {
+                    //This is the scenario where the tenant has registered, but not activated yet
+                    subscription = inactiveSubscriptions[0];
+                    boolean activated = dataAccessService.activateSubscription(subscription.getId());
+                    if (activated) {
+                        log.info("Subscription was activated for tenant: " + tenantId);
+                    }
+                }else if(inactiveSubscriptions.length > 1){
+                    //this is the scenario where the tenant has been deactivated by admin and
+                    //again activated. Here, I am adding a new active subscription which is similar to the
+                    //last existed one
+                    Subscription subscriptionToAdd = inactiveSubscriptions[0];
+                    subscriptionToAdd.setActive(true);
+                    subscriptionToAdd.setActiveSince(null);
+                    subscriptionToAdd.setActiveUntil(null);
+
+                    int subsId = dataAccessService.addSubscription(subscriptionToAdd);
+                    if(subsId>0){
+                        log.info("New subscription: " + subscriptionToAdd.getSubscriptionPlan() +
+                                " added and it was activated for tenant: " + tenantId);
+                    }
+                }else{
+                    //this means there are no subscriptions. Lets handle this later
+                }
+            }
+        } catch (Exception e) {
+            String msg = "Error occurred while activating the subscription for tenant: " +
+                    tenantDomain;
+            log.error(msg, e);
+            throw new StratosException(msg, e);
+        }
+    }
+
+    public void deactivateActiveUsagePlan(String tenantDomain) throws StratosException {
+        try {
+            TenantManager tenantMan = 
+                    CloudCommonServiceComponent.getRealmService().getTenantManager();
+            int tenantId = tenantMan.getTenantId(tenantDomain);
+            BillingDataAccessService dataAccessService = new BillingDataAccessService();
+
+            Subscription subscription = 
+                    dataAccessService.getActiveSubscriptionOfCustomerBySuperTenant(tenantId);
+
+            if (subscription == null) {
+                String msg = "Unable to deactivate the subscription for tenant: " + tenantId +
+                        ". An active subscription doesn't exist";
+                log.info(msg);
+            } else {
+                boolean deactivated = 
+                        dataAccessService.deactivateActiveSubscriptionBySuperTenant(tenantId);
+                if (deactivated) {
+                    log.info("Active subscription of tenant " + tenantId + " was deactivated");
+                }
+            }
+        } catch (Exception e) {
+            String msg = "Error occurred while deactivating the active subscription of tenant: " +
+                    tenantDomain;
+            log.error(msg, e);
+            throw new StratosException(msg, e);
+        }
+    }
+
+    public void deleteBillingData(int tenantId) throws StratosException{
+        try {
+            BillingDataAccessService dataAccessService = new BillingDataAccessService();
+            dataAccessService.deleteBillingData(tenantId);
+        } catch (Exception e) {
+            String msg = "Error deleting subscription  for tenant: " + tenantId;
+            log.error(msg, e);
+            throw new StratosException(msg, e);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ee2ab783/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/api/MultitenancyBillingInfo.java
----------------------------------------------------------------------
diff --git a/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/api/MultitenancyBillingInfo.java b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/api/MultitenancyBillingInfo.java
new file mode 100644
index 0000000..eacb0d4
--- /dev/null
+++ b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/api/MultitenancyBillingInfo.java
@@ -0,0 +1,178 @@
+/*
+ * Copyright (c) 2008, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed 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.wso2.carbon.billing.mgt.api;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.xpath.AXIOMXPath;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.jaxen.JaxenException;
+import org.wso2.carbon.billing.core.BillingException;
+import org.wso2.carbon.billing.core.dataobjects.Cash;
+import org.wso2.carbon.billing.mgt.dataobjects.MultitenancyPackage;
+import org.wso2.carbon.stratos.common.constants.StratosConstants;
+import org.wso2.carbon.stratos.common.util.CommonUtil;
+import org.wso2.carbon.utils.CarbonUtils;
+
+import javax.xml.namespace.QName;
+import java.io.File;
+import java.io.FileInputStream;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Reads the multitenancy-packages.xml and populates the
+ * multitenancy packages list
+ */
+public class MultitenancyBillingInfo {
+    private static Log log = LogFactory.getLog(MultitenancyBillingInfo.class);
+    private static final String PACKAGE_DESCRIPTION_CONFIG_FILENAME = "multitenancy-packages.xml";
+    private static final String PACKAGE_DESCRIPTION_CONFIG_NS =
+            "http://wso2.com/carbon/multitenancy/billing/pacakges";
+    
+    List<MultitenancyPackage> multitenancyPackages = new ArrayList<MultitenancyPackage>();
+
+    public MultitenancyBillingInfo() throws BillingException {
+        // this should be only available to the super tenants..
+        multitenancyPackages = deserializePackageDescriptionConfig();
+    }
+
+    public List<MultitenancyPackage> getMultitenancyPackages() {
+        return multitenancyPackages;
+    }
+
+    /*
+     * Deserialize following XML
+    <packages xmlns="http://wso2.com/carbon/multitenancy/billing/pacakges">
+        <package name="multitenancy-free">
+            <!--<subscriptionCharge>0</subscriptionCharge>--> <!-- $ per month -->
+            <users>
+                <limit>5</limit>
+                <charge>0</charge> <!-- charge per month -->
+            </users>
+            <resourceVolume>
+                <limit>10</limit> <!--mb per user -->
+            </resourceVolume>
+            <bandwidth>
+                <limit>1000</limit> <!-- mb per user -->
+                <overuseCharge>0</overuseCharge> <!-- $ per user per month -->
+            </bandwidth>
+        </package>
+        <package name="multitenancy-small">
+            ...
+        </package>
+    </packages>
+     */
+    private List<MultitenancyPackage> deserializePackageDescriptionConfig() throws BillingException {
+        String configFilePath = CarbonUtils.getCarbonConfigDirPath() + File.separator +
+                StratosConstants.MULTITENANCY_CONFIG_FOLDER + File.separator +
+                PACKAGE_DESCRIPTION_CONFIG_FILENAME;
+        
+        OMElement packageConfigs;
+        try {
+            packageConfigs = CommonUtil.buildOMElement(new FileInputStream(configFilePath));
+        } catch (Exception e) {
+            String msg = "Error in deserializing the packageConfigs file: " + configFilePath + ".";
+            log.error(msg, e);
+            throw new BillingException(msg, e);
+        }
+
+        Iterator packageConfigsChildsIt = packageConfigs.getChildElements();
+        while (packageConfigsChildsIt.hasNext()) {
+            OMElement packageConfigEle = (OMElement) packageConfigsChildsIt.next();
+            if (!new QName(PACKAGE_DESCRIPTION_CONFIG_NS, "package").equals(
+                    packageConfigEle.getQName())) {
+                continue;
+            }
+            
+            MultitenancyPackage multitenancyPackage = new MultitenancyPackage();
+            String packageName = packageConfigEle.getAttributeValue(new QName("name"));
+            String subscriptionCharge = getPackageConfigValue("subscriptionCharge", packageConfigEle);
+            String usersLimit = getPackageConfigValue("users.limit", packageConfigEle);
+            String usersCharge = getPackageConfigValue("users.charge", packageConfigEle);
+
+            String resourceVolumeLimit =
+                    getPackageConfigValue("resourceVolume.limit", packageConfigEle);
+            String resourceVolumeOveruseCharge =
+                    getPackageConfigValue("resourceVolume.overuseCharge", packageConfigEle);
+            String bandwidthLimit = getPackageConfigValue("bandwidth.limit", packageConfigEle);
+            String bandwidthOveruseCharge =
+                    getPackageConfigValue("bandwidth.overuseCharge", packageConfigEle);
+            String cartridgeCPUHourLimit =
+                    getPackageConfigValue("cartridge.hourLimit", packageConfigEle);
+            String cartridgeCPUHourOverUsageCharge =
+                    getPackageConfigValue("cartridge.overUsageCharge", packageConfigEle);
+            int usersLimitInt = -1;
+            if (!usersLimit.equals("unlimited")) {
+                usersLimitInt = Integer.parseInt(usersLimit);
+            }
+            int resourceVolumeLimitInt = -1;
+            if (!resourceVolumeLimit.equals("unlimited")) {
+                resourceVolumeLimitInt = Integer.parseInt(resourceVolumeLimit);
+            }
+            int bandwidthLimitInt = -1;
+            if (!bandwidthLimit.equals("unlimited")) {
+                bandwidthLimitInt = Integer.parseInt(bandwidthLimit);
+            }
+            
+            int cartridgeCPUHourLimitInt = -1;
+            if(cartridgeCPUHourLimit!=null && !cartridgeCPUHourLimit.equals("unlimited")){
+                cartridgeCPUHourLimitInt = Integer.parseInt(cartridgeCPUHourLimit);
+            }
+
+
+            multitenancyPackage.setName(packageName);
+            multitenancyPackage.setSubscriptionCharge(new Cash(subscriptionCharge));
+            multitenancyPackage.setUsersLimit(usersLimitInt);
+            multitenancyPackage.setChargePerUser(new Cash(usersCharge));
+            multitenancyPackage.setResourceVolumeLimit(resourceVolumeLimitInt);
+            multitenancyPackage.setResourceVolumeOveruseCharge(new Cash(resourceVolumeOveruseCharge));
+            multitenancyPackage.setBandwidthLimit(bandwidthLimitInt);
+            multitenancyPackage.setBandwidthOveruseCharge(new Cash(bandwidthOveruseCharge));
+            multitenancyPackage.setCartridgeCPUHourLimit(cartridgeCPUHourLimitInt);
+            multitenancyPackage.setCartridgeCPUOveruseCharge(new Cash(cartridgeCPUHourOverUsageCharge));
+
+            
+            multitenancyPackages.add(multitenancyPackage);
+        }
+        return multitenancyPackages;
+    }
+
+    private String getPackageConfigValue(String key, OMElement packageNode) throws BillingException {
+        String qualifiedKey = "ns:" + key.replaceAll("\\.", "/ns:");
+        AXIOMXPath xpathExpression;
+        try {
+            xpathExpression = new AXIOMXPath(qualifiedKey);
+            xpathExpression.addNamespace("ns", PACKAGE_DESCRIPTION_CONFIG_NS);
+            List valueNodes = xpathExpression.selectNodes(packageNode);
+            if (valueNodes.isEmpty()) {
+                if (log.isDebugEnabled()) {
+                    String msg = "No results found parsing package configuration for key: " + 
+                            qualifiedKey + ".";
+                    log.debug(msg);
+                }
+                return null;
+            }
+            OMElement valueNode = (OMElement) valueNodes.get(0);
+            return valueNode.getText();
+        } catch (JaxenException e) {
+            String msg = "Error in retrieving the key: " + qualifiedKey + ".";
+            log.error(msg, e);
+            throw new BillingException(msg, e);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ee2ab783/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/beans/BilledEntry.java
----------------------------------------------------------------------
diff --git a/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/beans/BilledEntry.java b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/beans/BilledEntry.java
new file mode 100644
index 0000000..8b88b99
--- /dev/null
+++ b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/beans/BilledEntry.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2008, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed 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.wso2.carbon.billing.mgt.beans;
+
+/**
+ * Under a subscription there are billed items. For example
+ * subscription fee, bandwidth overuse charge, storage overuse charge
+ */
+public class BilledEntry {
+    String name;
+    String cost;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getCost() {
+        return cost;
+    }
+
+    public void setCost(String cost) {
+        this.cost = cost;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ee2ab783/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/beans/BillingPeriod.java
----------------------------------------------------------------------
diff --git a/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/beans/BillingPeriod.java b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/beans/BillingPeriod.java
new file mode 100644
index 0000000..76ff4e6
--- /dev/null
+++ b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/beans/BillingPeriod.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2008, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed 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.wso2.carbon.billing.mgt.beans;
+
+import java.util.Date;
+
+/**
+ * This class is used when providing available invoices to the user
+ */
+public class BillingPeriod {
+    private int invoiceId;
+    private Date startDate;
+    private Date endDate;
+    private Date invoiceDate;
+
+    public int getInvoiceId() {
+        return invoiceId;
+    }
+
+    public void setInvoiceId(int invoiceId) {
+        this.invoiceId = invoiceId;
+    }
+
+    public Date getStartDate() {
+        return new Date(startDate.getTime());
+    }
+
+    public void setStartDate(Date startDate) {
+        this.startDate = new Date(startDate.getTime());
+    }
+
+    public Date getEndDate() {
+        return new Date(endDate.getTime());
+    }
+
+    public void setEndDate(Date endDate) {
+        this.endDate = new Date(endDate.getTime());
+    }
+
+    public Date getInvoiceDate() {
+        return new Date(invoiceDate.getTime());
+    }
+
+    public void setInvoiceDate(Date invoiceDate) {
+        this.invoiceDate = new Date(invoiceDate.getTime());
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ee2ab783/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/beans/MultitenancyInvoice.java
----------------------------------------------------------------------
diff --git a/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/beans/MultitenancyInvoice.java b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/beans/MultitenancyInvoice.java
new file mode 100644
index 0000000..994a161
--- /dev/null
+++ b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/beans/MultitenancyInvoice.java
@@ -0,0 +1,121 @@
+/*
+ * Copyright (c) 2008, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed 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.wso2.carbon.billing.mgt.beans;
+
+import java.util.Arrays;
+import java.util.Date;
+
+public class MultitenancyInvoice {
+    private int invoiceId;
+    private Date billingDate;
+    private Date startDate;
+    private Date endDate;
+    private String boughtForward;
+    private String carriedForward;
+    private String totalPayments;
+    private String totalCost;
+    private boolean lastInvoice;
+    private MultitenancySubscription[] subscriptions;
+    private MultitenancyPurchaseOrder[] purchaseOrders;
+
+    public int getInvoiceId() {
+        return invoiceId;
+    }
+
+    public void setInvoiceId(int invoiceId) {
+        this.invoiceId = invoiceId;
+    }
+
+    public Date getBillingDate() {
+        return new Date(billingDate.getTime());
+    }
+
+    public void setBillingDate(Date billingDate) {
+        this.billingDate = new Date(billingDate.getTime());
+    }
+
+    public Date getStartDate() {
+        return new Date(startDate.getTime());
+    }
+
+    public void setStartDate(Date startDate) {
+        this.startDate = new Date(startDate.getTime());
+    }
+
+    public Date getEndDate() {
+        return new Date(endDate.getTime());
+    }
+
+    public void setEndDate(Date endDate) {
+        this.endDate = new Date(endDate.getTime());
+    }
+
+    public String getBoughtForward() {
+        return boughtForward;
+    }
+
+    public void setBoughtForward(String boughtForward) {
+        this.boughtForward = boughtForward;
+    }
+
+    public String getCarriedForward() {
+        return carriedForward;
+    }
+
+    public void setCarriedForward(String carriedForward) {
+        this.carriedForward = carriedForward;
+    }
+
+    public String getTotalPayments() {
+        return totalPayments;
+    }
+
+    public void setTotalPayments(String totalPayments) {
+        this.totalPayments = totalPayments;
+    }
+
+    public String getTotalCost() {
+        return totalCost;
+    }
+
+    public void setTotalCost(String totalCost) {
+        this.totalCost = totalCost;
+    }
+
+    public boolean isLastInvoice() {
+        return lastInvoice;
+    }
+
+    public void setLastInvoice(boolean lastInvoice) {
+        this.lastInvoice = lastInvoice;
+    }
+
+    public MultitenancySubscription[] getSubscriptions() {
+        return Arrays.copyOf(subscriptions, subscriptions.length);
+    }
+
+    public void setSubscriptions(MultitenancySubscription[] subscriptions) {
+        this.subscriptions = Arrays.copyOf(subscriptions, subscriptions.length);
+    }
+
+    public MultitenancyPurchaseOrder[] getPurchaseOrders() {
+        return Arrays.copyOf(purchaseOrders, purchaseOrders.length);
+    }
+
+    public void setPurchaseOrders(MultitenancyPurchaseOrder[] purchaseOrders) {
+        this.purchaseOrders = Arrays.copyOf(purchaseOrders, purchaseOrders.length);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ee2ab783/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/beans/MultitenancyPurchaseOrder.java
----------------------------------------------------------------------
diff --git a/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/beans/MultitenancyPurchaseOrder.java b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/beans/MultitenancyPurchaseOrder.java
new file mode 100644
index 0000000..ffddda0
--- /dev/null
+++ b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/beans/MultitenancyPurchaseOrder.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2008, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed 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.wso2.carbon.billing.mgt.beans;
+
+import java.util.Date;
+
+/**
+ * Payment record details
+ */
+public class MultitenancyPurchaseOrder {
+    private Date paymentDate;
+    private String payment;
+    private int id;
+    private String transactionId;
+
+    public Date getPaymentDate() {
+        return new Date(paymentDate.getTime());
+    }
+
+    public void setPaymentDate(Date paymentDate) {
+        this.paymentDate = new Date(paymentDate.getTime());
+    }
+
+    public String getPayment() {
+        return payment;
+    }
+
+    public void setPayment(String payment) {
+        this.payment = payment;
+    }
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public String getTransactionId() {
+        return transactionId;
+    }
+
+    public void setTransactionId(String transactionId) {
+        this.transactionId = transactionId;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ee2ab783/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/beans/MultitenancySubscription.java
----------------------------------------------------------------------
diff --git a/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/beans/MultitenancySubscription.java b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/beans/MultitenancySubscription.java
new file mode 100644
index 0000000..5490e9f
--- /dev/null
+++ b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/beans/MultitenancySubscription.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2008, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed 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.wso2.carbon.billing.mgt.beans;
+
+import java.util.Arrays;
+import java.util.Date;
+
+public class MultitenancySubscription {
+    String subscribedPackage;
+    BilledEntry[] billedEntries;
+    Date activeSince;
+    Date activeUntil;
+    boolean isActive;
+
+    public String getSubscribedPackage() {
+        return subscribedPackage;
+    }
+
+    public void setSubscribedPackage(String subscribedPackage) {
+        this.subscribedPackage = subscribedPackage;
+    }
+
+    public BilledEntry[] getBilledEntries() {
+        return Arrays.copyOf(billedEntries, billedEntries.length);
+    }
+
+    public void setBilledEntries(BilledEntry[] billedEntries) {
+        this.billedEntries = Arrays.copyOf(billedEntries, billedEntries.length);
+    }
+
+    public Date getActiveSince() {
+        return new Date(activeSince.getTime());
+    }
+
+    public void setActiveSince(Date activeSince) {
+        this.activeSince = new Date(activeSince.getTime());
+    }
+
+    public Date getActiveUntil() {
+        return new Date(activeUntil.getTime());
+    }
+
+    public void setActiveUntil(Date activeUntil) {
+        this.activeUntil = new Date( activeUntil.getTime());
+    }
+
+    public boolean isActive() {
+        return isActive;
+    }
+
+    public void setActive(boolean active) {
+        isActive = active;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ee2ab783/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/dataobjects/MultitenancyCustomer.java
----------------------------------------------------------------------
diff --git a/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/dataobjects/MultitenancyCustomer.java b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/dataobjects/MultitenancyCustomer.java
new file mode 100644
index 0000000..5d8aea6
--- /dev/null
+++ b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/dataobjects/MultitenancyCustomer.java
@@ -0,0 +1,135 @@
+/*
+ * Copyright (c) 2008, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed 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.wso2.carbon.billing.mgt.dataobjects;
+
+import org.wso2.carbon.billing.core.dataobjects.Customer;
+import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
+
+/**
+ * Multitenancy customer class
+ */
+public class MultitenancyCustomer extends Customer {
+
+    private int numberOfUsers;
+    private long incomingBandwidth;
+    private long outgoingBandwidth;
+    private long totalBandwidth;
+    private long currentStorage;
+    private long historyStorage;
+    private long totalStorage;
+    private long cartridgeCPUHours;
+    private int tenantId = MultitenantConstants.INVALID_TENANT_ID;
+
+    public int getTenantId() {
+        return tenantId;
+    }
+
+    public void setTenantId(int tenantId) {
+        this.tenantId = tenantId;
+    }
+
+    public int getNumberOfUsers() {
+        return numberOfUsers;
+    }
+
+    public void setNumberOfUsers(int numberOfUsers) {
+        this.numberOfUsers = numberOfUsers;
+    }
+
+    public long getIncomingBandwidth() {
+        return incomingBandwidth;
+    }
+
+    public void setIncomingBandwidth(long incomingBandwidth) {
+        this.incomingBandwidth = incomingBandwidth;
+    }
+
+    public long getOutgoingBandwidth() {
+        return outgoingBandwidth;
+    }
+
+    public void setOutgoingBandwidth(long outgoingBandwidth) {
+        this.outgoingBandwidth = outgoingBandwidth;
+    }
+
+    public long getTotalBandwidth() {
+        return totalBandwidth;
+    }
+
+    public void setTotalBandwidth(long totalBandwidth) {
+        this.totalBandwidth = totalBandwidth;
+    }
+
+    public long getCurrentStorage() {
+        return currentStorage;
+    }
+
+    public void setCurrentStorage(long currentStorage) {
+        this.currentStorage = currentStorage;
+    }
+
+    public long getHistoryStorage() {
+        return historyStorage;
+    }
+
+    public void setHistoryStorage(long historyStorage) {
+        this.historyStorage = historyStorage;
+    }
+
+    public long getTotalStorage() {
+        return totalStorage;
+    }
+
+    public void setTotalStorage(long totalStorage) {
+        this.totalStorage = totalStorage;
+    }
+
+    public long getCartridgeCPUHours() {
+        return cartridgeCPUHours;
+    }
+
+    public void setCartridgeCPUHours(long cartridgeCPUHours) {
+        this.cartridgeCPUHours = cartridgeCPUHours;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        if (!super.equals(o)) {
+            return false;
+        }
+
+        MultitenancyCustomer that = (MultitenancyCustomer) o;
+
+        if (tenantId != that.tenantId) {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = super.hashCode();
+        result = 31 * result + tenantId;
+        return result;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ee2ab783/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/dataobjects/MultitenancyPackage.java
----------------------------------------------------------------------
diff --git a/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/dataobjects/MultitenancyPackage.java b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/dataobjects/MultitenancyPackage.java
new file mode 100644
index 0000000..346b3ca
--- /dev/null
+++ b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/dataobjects/MultitenancyPackage.java
@@ -0,0 +1,152 @@
+/*
+ * Copyright (c) 2008, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed 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.wso2.carbon.billing.mgt.dataobjects;
+
+import org.wso2.carbon.billing.core.dataobjects.Cash;
+import org.wso2.carbon.billing.core.dataobjects.Item;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class MultitenancyPackage extends Item {
+    public static final String SUBSCRIPTION_SUB_ITEM_NAME = "subscription";
+    public static final String BW_OVERUSE_SUB_ITEM_NAME = "bwOveruse";
+    public static final String STORAGE_OVERUSE_SUB_ITEM_NAME = "storageOveruse";
+    public static final String CARTRIDGE_OVERUSE_SUB_ITEM_NAME = "cartridgeOveruse";
+
+    private int usersLimit;
+    private Cash subscriptionCharge;
+    private Cash chargePerUser;
+    //private int resourceVolumeLimit;
+    //private int bandwidthLimit;
+    //private Cash bandwidthOveruseCharge;
+    private List<MultitenancyPackageSubItem> subItems;
+
+    public MultitenancyPackage() {
+        subItems = new ArrayList<MultitenancyPackageSubItem>();
+        // adding subscription sub item
+        MultitenancyPackageSubItem subscriptionSubItem = new MultitenancyPackageSubItem();
+        subscriptionSubItem.setName(SUBSCRIPTION_SUB_ITEM_NAME);
+        subscriptionSubItem.setDescription("Subscription for package");
+        subscriptionSubItem.setParent(this);
+        subItems.add(subscriptionSubItem);
+
+        // adding bandwidth overuse sub item
+        MultitenancyPackageSubItem bandwdithOverUseSubItem = new MultitenancyPackageSubItem();
+        bandwdithOverUseSubItem.setName(BW_OVERUSE_SUB_ITEM_NAME);
+        bandwdithOverUseSubItem.setDescription("Bandwidth overuse");
+        bandwdithOverUseSubItem.setParent(this);
+        subItems.add(bandwdithOverUseSubItem);
+
+        // adding storage overuse sub item
+        MultitenancyPackageSubItem storageOverUseSubItem = new MultitenancyPackageSubItem();
+        storageOverUseSubItem.setName(STORAGE_OVERUSE_SUB_ITEM_NAME);
+        storageOverUseSubItem.setDescription("Storage overuse");
+        storageOverUseSubItem.setParent(this);
+        subItems.add(storageOverUseSubItem);
+
+        // adding cartridge overuse sub item
+        MultitenancyPackageSubItem cartridgeOverUseSubItem = new MultitenancyPackageSubItem();
+        cartridgeOverUseSubItem.setName(CARTRIDGE_OVERUSE_SUB_ITEM_NAME);
+        cartridgeOverUseSubItem.setDescription("Cartridge overuse");
+        cartridgeOverUseSubItem.setParent(this);
+        subItems.add(cartridgeOverUseSubItem);
+
+    }
+
+    public MultitenancyPackage(MultitenancyPackage staticMtPackage, boolean isSubscriptionActive) {
+        usersLimit = staticMtPackage.getUsersLimit();
+        chargePerUser = staticMtPackage.getChargePerUser();
+        subscriptionCharge = staticMtPackage.getSubscriptionCharge();
+        super.setResourceVolumeLimit(staticMtPackage.getResourceVolumeLimit());
+        super.setResourceVolumeOveruseCharge(staticMtPackage.getResourceVolumeOveruseCharge());
+        super.setBandwidthLimit(staticMtPackage.getBandwidthLimit());
+        super.setBandwidthOveruseCharge(staticMtPackage.getBandwidthOveruseCharge());
+        super.setCartridgeCPUHourLimit(staticMtPackage.getCartridgeCPUHourLimit());
+        super.setCartridgeCPUOveruseCharge(staticMtPackage.getCartridgeCPUOveruseCharge());
+        setId(staticMtPackage.getId());
+        setName(staticMtPackage.getName());
+        setCost(staticMtPackage.getCost());
+        setDescription(staticMtPackage.getDescription());
+
+        subItems = new ArrayList<MultitenancyPackageSubItem>();
+        for (Item subItem : staticMtPackage.getChildren()) {
+            //Storage overuse and Bandwidth overuse sub items will be added only to active subscription
+            if(isSubscriptionActive || (!isSubscriptionActive && SUBSCRIPTION_SUB_ITEM_NAME.equals(subItem.getName()))){
+
+                MultitenancyPackageSubItem subscriptionSubItem = new MultitenancyPackageSubItem();
+                subscriptionSubItem.setId(subItem.getId());
+                subscriptionSubItem.setName(subItem.getName());
+                subscriptionSubItem.setDescription(subItem.getDescription());
+                subscriptionSubItem.setParent(this);
+                subItems.add(subscriptionSubItem);
+            }
+        }
+    }
+
+    public int getUsersLimit() {
+        return usersLimit;
+    }
+
+    public void setUsersLimit(int usersLimit) {
+        this.usersLimit = usersLimit;
+    }
+
+    public Cash getChargePerUser() {
+        return chargePerUser;
+    }
+
+    public void setChargePerUser(Cash chargePerUser) {
+        this.chargePerUser = chargePerUser;
+    }
+
+    public Cash getSubscriptionCharge() {
+        return subscriptionCharge;
+    }
+
+    public void setSubscriptionCharge(Cash subscriptionCharge) {
+        this.subscriptionCharge = subscriptionCharge;
+    }
+
+    /*public int getResourceVolumeLimit() {
+        return resourceVolumeLimit;
+    }
+
+    public void setResourceVolumeLimit(int resourceVolumeLimit) {
+        this.resourceVolumeLimit = resourceVolumeLimit;
+    }
+    */
+    /*public int getBandwidthLimit() {
+        return bandwidthLimit;
+    }
+
+    public void setBandwidthLimit(int bandwidthLimit) {
+        this.bandwidthLimit = bandwidthLimit;
+    }
+
+    public Cash getBandwidthOveruseCharge() {
+        return bandwidthOveruseCharge;
+    }
+
+    public void setBandwidthOveruseCharge(Cash bandwidthOveruseCharge) {
+        this.bandwidthOveruseCharge = bandwidthOveruseCharge;
+    }*/
+
+    @Override
+    public List<? extends Item> getChildren() {
+        return subItems;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ee2ab783/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/dataobjects/MultitenancyPackageSubItem.java
----------------------------------------------------------------------
diff --git a/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/dataobjects/MultitenancyPackageSubItem.java b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/dataobjects/MultitenancyPackageSubItem.java
new file mode 100644
index 0000000..7245a2a
--- /dev/null
+++ b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/dataobjects/MultitenancyPackageSubItem.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2008, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed 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.wso2.carbon.billing.mgt.dataobjects;
+
+import org.wso2.carbon.billing.core.dataobjects.Item;
+
+public class MultitenancyPackageSubItem extends Item {
+    
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ee2ab783/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/handlers/MultitenancySubscriptionFeedingHandler.java
----------------------------------------------------------------------
diff --git a/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/handlers/MultitenancySubscriptionFeedingHandler.java b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/handlers/MultitenancySubscriptionFeedingHandler.java
new file mode 100644
index 0000000..187c261
--- /dev/null
+++ b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/handlers/MultitenancySubscriptionFeedingHandler.java
@@ -0,0 +1,234 @@
+/*
+ * Copyright (c) 2008, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed 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.wso2.carbon.billing.mgt.handlers;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.wso2.carbon.billing.core.BillingEngineContext;
+import org.wso2.carbon.billing.core.BillingException;
+import org.wso2.carbon.billing.core.BillingHandler;
+import org.wso2.carbon.billing.core.dataobjects.Customer;
+import org.wso2.carbon.billing.core.dataobjects.Item;
+import org.wso2.carbon.billing.core.dataobjects.Subscription;
+import org.wso2.carbon.billing.core.jdbc.DataAccessObject;
+import org.wso2.carbon.billing.core.utilities.CustomerUtils;
+import org.wso2.carbon.billing.mgt.api.MultitenancyBillingInfo;
+import org.wso2.carbon.billing.mgt.dataobjects.MultitenancyCustomer;
+import org.wso2.carbon.billing.mgt.dataobjects.MultitenancyPackage;
+import org.wso2.carbon.billing.mgt.util.Util;
+import org.wso2.carbon.stratos.common.util.CommonUtil;
+import org.wso2.carbon.usage.beans.TenantUsage;
+
+import java.util.*;
+
+/**
+ * Gets subscriptions and feeds them to the Billing Engine context
+ */
+public class MultitenancySubscriptionFeedingHandler implements BillingHandler {
+    private static final Log log = LogFactory.getLog(MultitenancySubscriptionFeedingHandler.class);
+
+    private Map<Integer, MultitenancyPackage> multitenancyPackagesMap = new HashMap<Integer, MultitenancyPackage>();
+
+    public void init(Map<String, String> handlerConfig) throws BillingException {
+        DataAccessObject dataAccessObject = Util.getBillingManager().getDataAccessObject();
+        MultitenancyBillingInfo billingInfo = Util.getMultitenancyBillingInfo();
+
+        // here we are initializing the packages
+        List<MultitenancyPackage> multitenancyPackages = billingInfo.getMultitenancyPackages();
+        try {
+            dataAccessObject.beginTransaction();
+            for (MultitenancyPackage multitenancyPackage : multitenancyPackages) {
+                // check the package existence in the database; If not available, insert it
+                int itemId = dataAccessObject.getItemIdWithName(multitenancyPackage.getName());
+                if (itemId == DataAccessObject.INVALID) {
+                    itemId = dataAccessObject.addItem(multitenancyPackage);
+                }
+                multitenancyPackage.setId(itemId);
+                multitenancyPackagesMap.put(itemId, multitenancyPackage);
+                // and add all the sub items too
+                for (Item subItem : multitenancyPackage.getChildren()) {
+                    int subItemId = dataAccessObject.getItemId(subItem.getName(), itemId);
+                    if (subItemId == DataAccessObject.INVALID) {
+                        subItemId = dataAccessObject.addItem(subItem);
+                    }
+                    subItem.setId(subItemId);
+                }
+            }
+            dataAccessObject.commitTransaction();
+        }catch (Exception e){
+            dataAccessObject.rollbackTransaction();
+            log.error(e.getMessage());
+            throw new BillingException(e.getMessage(), e);
+        }
+    }
+
+    public void execute(BillingEngineContext handlerContext) throws BillingException {
+        feedSubscriptions(handlerContext);
+    }
+
+    private void feedSubscriptions(BillingEngineContext handlerContext) throws BillingException {
+        // get the subscriptions of the customer.
+        Customer customer = handlerContext.getCustomer();
+        List<Subscription> subscriptions = getSubscriptions(null, customer); //if the customer is null
+                                                                               // this will get subscriptions
+                                                                              // of all customers
+
+        //Filtering out the subscription entries of customers who has not activated their accounts
+        //This will avoid an invoice being generated for such customers
+        Date endDate = new Date();
+
+        Iterator iterator = subscriptions.iterator();
+        while(iterator.hasNext()){
+            Subscription subscription = (Subscription) iterator.next();
+            if(!subscription.isActive() && subscription.getActiveUntil().after(endDate)){
+                iterator.remove();
+            }
+        }
+
+        // prepare the handler context
+        handlerContext.setSubscriptions(subscriptions);
+        String infoMsg = "Subscription feeding phase completed. ";
+        if (subscriptions!=null){
+            infoMsg += subscriptions.size() + " subscriptions fed. ";
+        }else{
+            infoMsg += "0 subscriptions fed. ";
+        }
+        log.info(infoMsg);
+        // resetting the single customer back from the fed data
+        // this is applicable in the interim invoice scenario
+        //If this is not done, the customer object in the BillingEngine context will have
+        //a null invoice
+        if (customer != null && subscriptions != null && subscriptions.size() > 0) {
+            Subscription subscription = subscriptions.get(0);
+            handlerContext.setCustomer(subscription.getCustomer());
+        }
+    }
+
+    /**
+     *
+     * @param itemId
+     * @param isSubscriptionActive subitems for bw_overuse and storage_overuse
+     * will be added only for the active subscription
+     * @return
+     */
+    private Item getItem(int itemId, boolean isSubscriptionActive) {
+        return new MultitenancyPackage(multitenancyPackagesMap.get(itemId), isSubscriptionActive);
+    }
+
+    /**
+     * Retrieving tenant info and filling the customer object
+     * @param customerId
+     * @return
+     * @throws BillingException
+     */
+    private Customer getCustomer(int customerId) throws BillingException {
+        MultitenancyCustomer customer = new MultitenancyCustomer();
+        CustomerUtils.fillCustomerData(customerId, customer);
+        customer.setTenantId(customerId);
+
+        fillTenantUsage(customer);
+        return customer;
+    }
+
+    /**
+     * Fill usage data of the customer
+     * @param customer
+     * @throws BillingException
+     */
+    private void fillTenantUsage(MultitenancyCustomer customer) throws BillingException {
+        // first get the current month string
+        Calendar calendar = Calendar.getInstance();
+        String monthString = CommonUtil.getMonthString(calendar);
+        try {
+            int tenantId = customer.getTenantId();
+            TenantUsage usage =
+                    Util.getTenantUsageRetriever().getTenantUsage(tenantId, monthString);
+
+            long currentDataCapacity = usage.getRegistryContentCapacity();
+            long historyDataCapacity = usage.getRegistryContentHistoryCapacity();
+            customer.setCurrentStorage(currentDataCapacity);
+            customer.setHistoryStorage(historyDataCapacity);
+            customer.setTotalStorage(currentDataCapacity + historyDataCapacity);
+
+            long incomingBW = usage.getTotalIncomingBandwidth();
+            long outgoingBW = usage.getTotalOutgoingBandwidth();
+            customer.setIncomingBandwidth(incomingBW);
+            customer.setOutgoingBandwidth(outgoingBW);
+            customer.setTotalBandwidth(incomingBW + outgoingBW);
+
+            //Getting the cartridge hours and setting it to the customer
+            customer.setTotalCartridgeCPUHours(usage.getTotalCartridgeHours().getCartridgeHours());
+
+            log.debug("Customer: " + customer.getTenantId() + " - Data Capacity: " + customer.getTotalStorage());
+
+            customer.setNumberOfUsers(usage.getNumberOfUsers());
+        } catch (Exception e) {
+            String msg = "Error in getting the tenant usage for customer name: " 
+                    + customer.getName() + ".";
+            log.error(msg);
+            throw new BillingException(msg, e);
+        }
+    }
+
+    /**
+     * Gets subscriptions of customer(s)
+     * @param filter currently there is a filter "multitenancy" defined in the billing-config.xml
+     * This will be removed in the future trunk
+     * @param customer if the customer is null it gets subscriptions of all customers
+     * @return
+     * @throws BillingException
+     */
+    private List<Subscription> getSubscriptions(String filter,
+                                                Customer customer)throws BillingException {
+        DataAccessObject dataAccessObject = Util.getBillingManager().getDataAccessObject();
+        List<Subscription> subscriptions = null;
+        Map<Integer, Customer> customersCache = new HashMap<Integer, Customer>();
+        try {
+            dataAccessObject.beginTransaction();
+            if (customer == null) {
+                subscriptions = dataAccessObject.getFilteredActiveSubscriptions(filter);
+            } else {
+                subscriptions = dataAccessObject.getFilteredActiveSubscriptionsForCustomer(filter, customer);
+            }
+            if(subscriptions!=null && subscriptions.size()>0){
+                for (Subscription subscription : subscriptions) {
+                    Customer dummyCustomer = subscription.getCustomer();
+                    int customerId = dummyCustomer.getId();
+                    Customer correctCustomer = customersCache.get(customerId);
+                    if (correctCustomer == null) {
+                        correctCustomer = getCustomer(customerId);
+                        customersCache.put(customerId, correctCustomer);
+                    }
+                    subscription.setCustomer(correctCustomer);
+
+                    Item dummyItem = subscription.getItem();
+                    Item correctItem = getItem(dummyItem.getId(), subscription.isActive());
+                    subscription.setItem(correctItem);
+                }
+            }
+            dataAccessObject.commitTransaction();
+        }catch (Exception e){
+            dataAccessObject.rollbackTransaction();
+            String msg = "Error occurred while getting subscriptions";
+            if(customer != null) {
+                msg = msg + " for customer: " + customer.getName();
+            }
+            log.error(msg);
+            throw new BillingException(msg, e);
+        }
+        return subscriptions;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ee2ab783/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/internal/MultitenancyBillingServiceComponent.java
----------------------------------------------------------------------
diff --git a/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/internal/MultitenancyBillingServiceComponent.java b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/internal/MultitenancyBillingServiceComponent.java
new file mode 100644
index 0000000..d30e3f9
--- /dev/null
+++ b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/internal/MultitenancyBillingServiceComponent.java
@@ -0,0 +1,109 @@
+/*
+*  Copyright (c) 2005-2011, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+*
+*  WSO2 Inc. 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.wso2.carbon.billing.mgt.internal;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.osgi.service.component.ComponentContext;
+import org.wso2.carbon.billing.core.BillingManager;
+import org.wso2.carbon.billing.mgt.util.Util;
+import org.wso2.carbon.registry.core.service.RegistryService;
+import org.wso2.carbon.usage.api.TenantUsageRetriever;
+import org.wso2.carbon.user.core.service.RealmService;
+import org.wso2.carbon.utils.ConfigurationContextService;
+
+/**
+ * @scr.component name="org.wso2.carbon.billing.mgt"
+ * immediate="true"
+ * @scr.reference name="registry.service"
+ * interface="org.wso2.carbon.registry.core.service.RegistryService" cardinality="1..1"
+ * policy="dynamic" bind="setRegistryService" unbind="unsetRegistryService"
+ * @scr.reference name="user.realmservice.default"
+ * interface="org.wso2.carbon.user.core.service.RealmService" cardinality="1..1"
+ * policy="dynamic" bind="setRealmService" unbind="unsetRealmService"
+ * @scr.reference name="billingManager.service"
+ * interface="org.wso2.carbon.billing.core.BillingManager" cardinality="1..1"
+ * policy="dynamic" bind="setBillingManager" unbind="unsetBillingManager"
+ * @scr.reference name="tenant.usage.retriever.service"
+ * interface="org.wso2.carbon.usage.api.TenantUsageRetriever" cardinality="1..1"
+ * policy="dynamic" bind="setTenantUsageRetriever" unbind="unsetTenantUsageRetriever"
+ */
+public class MultitenancyBillingServiceComponent {
+    private static Log log = LogFactory.getLog(MultitenancyBillingServiceComponent.class);
+    private static ConfigurationContextService contextService;
+
+    protected void activate(ComponentContext context) {
+        try {
+            Util.registerSubscriptionFeedingHandlers(context.getBundleContext());
+            Util.scheduleBilling();
+            Util.registerBillingInfo(context.getBundleContext());
+            Util.initDataAccessManager();
+            Util.initializeThrottling(context.getBundleContext());
+            Util.registerTenantBillingService(context.getBundleContext());
+            log.debug("******* Multitenancy Billing bundle is activated ******* ");
+        } catch (Throwable e) {
+            log.error("******* Multitenancy Billing bundle failed activating ****", e);
+        }
+    }
+
+    @SuppressWarnings("unused")
+    protected void deactivate(ComponentContext context) {
+        log.debug("******* Multitenancy Billing is deactivated ******* ");
+    }
+
+    protected void setRegistryService(RegistryService registryService) {
+        Util.setRegistryService(registryService);
+    }
+
+    @SuppressWarnings("unused")
+    protected void unsetRegistryService(RegistryService registryService) {
+        Util.setRegistryService(null);
+    }
+
+    protected void setRealmService(RealmService realmService) {
+        Util.setRealmService(realmService);
+    }
+
+    @SuppressWarnings("unused")
+    protected void unsetRealmService(RealmService realmService) {
+        Util.setRealmService(null);
+    }
+
+    public void setBillingManager(BillingManager billingManager) {
+        log.debug("Receiving billingManager service");
+        Util.setBillingManager(billingManager);
+    }
+
+    @SuppressWarnings("unused")
+    public void unsetBillingManager(BillingManager billingManager) {
+        log.debug("Unsetting billingManager service");
+        Util.setBillingManager(null);
+    }
+
+    public void setTenantUsageRetriever(TenantUsageRetriever tenantUsageRetriever) {
+        log.debug("Setting Tenant Usage Retriever service");
+        Util.setTenantUsageRetriever(tenantUsageRetriever);
+    }
+
+    @SuppressWarnings("unused")
+    public void unsetTenantUsageRetriever(TenantUsageRetriever tenantUsageRetriever) {
+        log.debug("Unsetting Tenant Usage Retriever service");
+        Util.setBillingManager(null);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ee2ab783/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/services/BillingDataAccessService.java
----------------------------------------------------------------------
diff --git a/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/services/BillingDataAccessService.java b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/services/BillingDataAccessService.java
new file mode 100644
index 0000000..0298c4d
--- /dev/null
+++ b/components/stratos/billing/org.apache.stratos.billing.mgt/2.1.3/src/main/java/org/wso2/carbon/billing/mgt/services/BillingDataAccessService.java
@@ -0,0 +1,281 @@
+/*
+ * Copyright (c) 2008, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed 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.wso2.carbon.billing.mgt.services;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.wso2.carbon.billing.core.DataAccessManager;
+import org.wso2.carbon.billing.core.dataobjects.Customer;
+import org.wso2.carbon.billing.core.dataobjects.Subscription;
+import org.wso2.carbon.billing.mgt.util.Util;
+import org.wso2.carbon.core.AbstractAdmin;
+import org.wso2.carbon.registry.core.session.UserRegistry;
+import org.wso2.carbon.user.core.tenant.TenantManager;
+
+import java.util.List;
+
+/**
+ * This service allows other components to access billing related data
+ * without going through a billing manager
+ */
+public class BillingDataAccessService extends AbstractAdmin {
+    private static Log log = LogFactory.getLog(BillingDataAccessService.class);
+
+    /**
+     * Add a new subscription to the BC_SUBSCRIPTION table
+     * @param subscription  object with subscription info
+     * @return the subscription id of the added subscription
+     * @throws Exception Exception
+     */
+    public int addSubscription(Subscription subscription) throws Exception {
+        DataAccessManager dataAccessManager = Util.getDataAccessManager();
+        return dataAccessManager.addSubscription(subscription);
+    }
+
+    /**
+     * Delete a particular tenants subscription and all related data
+     * @param tenantId id of the tenant whose billing details should be deleted
+     * @throws Exception thorwn if an error is occured while deleting data
+     */
+    public void deleteBillingData(int tenantId) throws Exception {
+        DataAccessManager dataAccessManager = Util.getDataAccessManager();
+        dataAccessManager.deleteBillingData(tenantId);
+    }
+
+    /**
+     * Finds the customer with a given tenant domain
+     * @param customerName  is the tenant domain
+     * @return  a customer object
+     * @throws Exception Exception
+     */
+    public Customer getCustomerWithName(String customerName) throws Exception {
+
+        //This is invoked by tenants only. Therefore securing this by checking 
+        //whether the string passed is actually the domain of current tenant
+        UserRegistry userRegistry = (UserRegistry) getGovernanceUserRegistry();
+        int currentTenantId = userRegistry.getTenantId();
+
+        TenantManager tenantManager = Util.getRealmService().getTenantManager();
+        String currentTenantDomain = tenantManager.getDomain(currentTenantId);
+
+        if(!customerName.equals(currentTenantDomain)){
+            String msg = "Tenant: " + currentTenantDomain + " is trying to get customer object of tenant: " +
+                    customerName + ".";
+            log.error(msg);
+            throw new Exception(msg);
+        }
+
+        DataAccessManager dataAccessManager = Util.getDataAccessManager();
+        Customer customer = null;
+        List<Customer> customers = dataAccessManager.getCustomersWithName(customerName);
+        if (customers.size() > 0) {
+            customer = customers.get(0);
+        }
+        return customer;
+    }
+
+    /**
+     * Get a subscription with a given id
+     * @param subscriptionId subscription id
+     * @return a subscription object
+     * @throws Exception Exception
+     */
+    public Subscription getSubscription(int subscriptionId) throws Exception {
+        DataAccessManager dataAccessManager = Util.getDataAccessManager();
+        return dataAccessManager.getSubscription(subscriptionId);
+    }
+
+
+    /**
+     * Method called by super-tenant to get the active subscription of a tenant
+     * @param tenantId Tenant Id
+     * @return subscription details
+     * @throws Exception Exception
+     */
+    public Subscription getActiveSubscriptionOfCustomerBySuperTenant(int tenantId) throws Exception{
+        return getActiveSubscriptionOfCustomer(tenantId);
+    }
+
+    /**
+     * Method called by tenant to get the active subscription
+     * @return subscription details
+     * @throws Exception Exception
+     */
+    public Subscription getActiveSubscriptionOfCustomerByTenant() throws Exception{
+        UserRegistry userRegistry = (UserRegistry) getGovernanceUserRegistry();
+        int tenantId = userRegistry.getTenantId();
+
+        return getActiveSubscriptionOfCustomer(tenantId);
+    }
+
+
+    /**
+     * Gets the item id for a given item name and a parent id
+     * For example "subscription" item id of Demo subscription
+     * @param name  e.g. "subscription", "bwOveruse", "storageOveruse"
+     * @param parentId there is a parent item in BC_ITEM
+     * @return  the item id from the BC_ITEM table
+     * @throws Exception Exception
+     */
+    public int getItemIdWithName(String name, int parentId) throws Exception {
+        DataAccessManager dataAccessManager = Util.getDataAccessManager();
+        return dataAccessManager.getItemIdWithName(name, parentId);
+    }
+
+    /**
+     * This is used by the tenants 
+     * @param subscriptionPlan new Usage plan name that user expect to go
+     * @return whether the operation was successful or not
+     * @throws Exception Exception
+     */
+    public boolean changeSubscriptionByTenant(String subscriptionPlan) throws Exception {
+
+        UserRegistry userRegistry = (UserRegistry) getGovernanceUserRegistry();
+        int tenantId = userRegistry.getTenantId();
+
+        return changeSubscription(tenantId, subscriptionPlan);
+
+    }
+
+    /**
+     * This is used by the super tenant
+     * @param customerId    this is the tenant id
+     * @param subscriptionPlan  new usage plan name
+     * @return  whether the operation was successful or not
+     * @throws Exception Exception
+     */
+    public boolean changeSubscriptionBySuperTenant(int customerId, String subscriptionPlan) throws Exception {
+
+        return changeSubscription(customerId, subscriptionPlan);
+    }
+
+    /**
+     * Gets the inactive subscriptions of a customer ordered by ACTIVE_SINCE time
+     * in the descending order (i.e. latest ones first)
+     * @param customerId this is the tenant id
+     * @return  an array of subscriptions
+     * @throws Exception Exception
+     */
+    public Subscription[] getInactiveSubscriptionsOfCustomer(int customerId) throws Exception {
+        DataAccessManager dataAccessManager = Util.getDataAccessManager();
+        List<Subscription> subscriptions = dataAccessManager.getInactiveSubscriptionsOfCustomer(customerId);
+        Subscription[] subscriptionArray;
+        if (subscriptions != null && subscriptions.size() > 0) {
+            subscriptionArray = subscriptions.toArray(new Subscription[subscriptions.size()]);
+        } else {
+            subscriptionArray = new Subscription[0];
+        }
+        return subscriptionArray;
+    }
+
+    /**
+     * Activate a subscription with a given id
+     * @param subscriptionId is the id of subscription which needs to be activated
+     * @return  true or false based on whether the operation was successful or not
+     * @throws Exception Exception
+     */
+    public boolean activateSubscription(int subscriptionId) throws Exception {
+        DataAccessManager dataAccessManager = Util.getDataAccessManager();
+        return dataAccessManager.activateSubscription(subscriptionId);
+    }
+
+    /**
+     * Method called by tenants when deactivating the active subscriptions
+     * @return true | false based on whether the operation was successful or not
+     * @throws Exception Exception
+     */
+    public boolean deactivateActiveSubscriptionByTenant() throws Exception{
+        UserRegistry registry = (UserRegistry) getGovernanceUserRegistry();
+        int currentTenantId = registry.getTenantId();
+
+        return deactivateActiveSubscription(currentTenantId);
+    }
+
+
+    /**
+     * Method called by super-tenant
+     * @param tenantId Tenant Id
+     * @return true | false based on whether the operation was successful or not
+     * @throws Exception Exception
+     */
+    public boolean deactivateActiveSubscriptionBySuperTenant(int tenantId) throws Exception{
+        return deactivateActiveSubscription(tenantId);
+    }
+    
+
+
+    /**
+     * This is the private method called by tenant or super tenant operations when
+     * changing(updating) the subscription plan
+     * @param tenantId Tenant ID
+     * @param subscriptionPlan new usage plan (subscription plan) name
+     * @return true or false based on whether the operation was successful
+     * @throws Exception Exception
+     */
+    private boolean changeSubscription(int tenantId, String subscriptionPlan) throws Exception {
+
+        DataAccessManager dataAccessManager = Util.getDataAccessManager();
+        if (dataAccessManager.changeSubscription(tenantId, subscriptionPlan)) {
+            try {
+                Util.executeThrottlingRules(tenantId);
+            }
+            catch (Exception e) {
+                log.error("Error occurred executing throttling rules after updating the subscription " +
+                        "to " + subscriptionPlan + ". " + e.toString());
+            }
+            //send mail
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    /**
+     * Gets the active subscription of a customer. There can be only one active
+     * subscription for a customer at a given time
+     * @param tenantId Tenant Id
+     * @return  a subscription object
+     * @throws Exception Exception
+     */
+    private Subscription getActiveSubscriptionOfCustomer(int tenantId) throws Exception {
+        DataAccessManager dataAccessManager = Util.getDataAccessManager();
+        return dataAccessManager.getActiveSubscriptionOfCustomer(tenantId);
+    }
+
+    /**
+     * Deactivates the active subscription of a customer
+     * @param tenantId is the customer id (both have the same meaning)
+     * @return true or false based on whether the operation was successful or not
+     * @throws Exception Exception
+     */
+    private boolean deactivateActiveSubscription(int tenantId) throws Exception {
+        DataAccessManager dataAccessManager = Util.getDataAccessManager();
+        return dataAccessManager.deactivateActiveSubscription(tenantId);
+    }
+
+    /**
+     * This method is used to update the usage plan when updating the from Demo to a paying plan
+     * @param subscriptionPlan new Usage plan name that user expect to go
+     * @param tenantDomain the domain of the tenant that should be updated 
+     * @return whether the operation was successful or not
+     * @throws Exception Exception
+     */
+    public boolean changeSubscriptionForTenant(String subscriptionPlan, String tenantDomain) throws Exception {
+        int tenantId = Util.getRealmService().getTenantManager().getTenantId(tenantDomain);
+        return changeSubscription(tenantId, subscriptionPlan);
+    }
+
+}