You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by im...@apache.org on 2014/12/11 18:25:28 UTC

stratos git commit: Adding configuration to enable/disable mock iaas service

Repository: stratos
Updated Branches:
  refs/heads/4.1.0-test 8540e700b -> 71605eb03


Adding configuration to enable/disable mock iaas service


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

Branch: refs/heads/4.1.0-test
Commit: 71605eb0335ac29f29941cdbe32cfe89b1fb4624
Parents: 8540e70
Author: Imesh Gunaratne <im...@apache.org>
Authored: Thu Dec 11 22:55:17 2014 +0530
Committer: Imesh Gunaratne <im...@apache.org>
Committed: Thu Dec 11 22:55:17 2014 +0530

----------------------------------------------------------------------
 .../controller/iaases/mock/MockIaasService.java |   4 +
 .../MockHealthStatisticsConfigParser.java       | 136 -----------------
 .../iaases/mock/config/MockIaasConfig.java      |  22 ++-
 .../mock/config/MockIaasConfigParser.java       | 147 +++++++++++++++++++
 .../CloudControllerServiceComponent.java        |   7 +-
 5 files changed, 172 insertions(+), 144 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/71605eb0/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 a026cfc..a68bab5 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
@@ -25,6 +25,7 @@ import org.apache.stratos.cloud.controller.domain.ClusterContext;
 import org.apache.stratos.cloud.controller.domain.MemberContext;
 import org.apache.stratos.cloud.controller.domain.Partition;
 import org.apache.stratos.cloud.controller.exception.*;
+import org.apache.stratos.cloud.controller.iaases.mock.config.MockIaasConfig;
 import org.apache.stratos.cloud.controller.iaases.mock.statistics.generator.MockHealthStatisticsGenerator;
 import org.apache.stratos.cloud.controller.iaases.validators.PartitionValidator;
 import org.apache.stratos.cloud.controller.registry.RegistryManager;
@@ -73,6 +74,9 @@ public class MockIaasService {
         if (instance == null) {
             synchronized (MockIaasService.class) {
                 if (instance == null) {
+                    if(!MockIaasConfig.getInstance().isEnabled()) {
+                        throw new RuntimeException("Mock IaaS is not enabled");
+                    }
                     instance = new MockIaasService();
                 }
             }

http://git-wip-us.apache.org/repos/asf/stratos/blob/71605eb0/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/config/MockHealthStatisticsConfigParser.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/config/MockHealthStatisticsConfigParser.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/config/MockHealthStatisticsConfigParser.java
deleted file mode 100644
index fe61078..0000000
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/config/MockHealthStatisticsConfigParser.java
+++ /dev/null
@@ -1,136 +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.iaases.mock.config;
-
-import org.apache.axiom.om.OMAttribute;
-import org.apache.axiom.om.OMElement;
-import org.apache.stratos.cloud.controller.iaases.mock.MockAutoscalingFactor;
-import org.apache.stratos.cloud.controller.iaases.mock.statistics.generator.MockHealthStatisticsPattern;
-import org.apache.stratos.cloud.controller.util.AxiomXpathParserUtil;
-
-import javax.xml.namespace.QName;
-import java.io.File;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-/**
- * Mock health statistics configuration parser.
- */
-public class MockHealthStatisticsConfigParser {
-    private static final QName TYPE_ATTRIBUTE = new QName("type");
-    private static final QName FACTOR_ATTRIBUTE = new QName("factor");
-    private static final String HEALTH_STATISTICS_ELEMENT = "health-statistics";
-    private static final String SAMPLE_VALUES_ELEMENT = "sampleValues";
-    private static final String SAMPLE_DURATION_ELEMENT = "sampleDuration";
-
-    /**
-     * Parse mock iaas health statistics configuration and return configuration object.
-     * @param filePath
-     * @return
-     */
-    public static MockHealthStatisticsConfig parse(String filePath) {
-        try {
-            MockHealthStatisticsConfig mockHealthStatisticsConfig = new MockHealthStatisticsConfig();
-            OMElement document = AxiomXpathParserUtil.parse(new File(filePath));
-            Iterator statisticsIterator = document.getChildElements();
-
-            while (statisticsIterator.hasNext()) {
-                OMElement statisticsElement = (OMElement) statisticsIterator.next();
-
-                if (HEALTH_STATISTICS_ELEMENT.equals(statisticsElement.getQName().getLocalPart())) {
-                    Iterator cartridgeIterator = statisticsElement.getChildElements();
-
-                    while (cartridgeIterator.hasNext()) {
-                        OMElement cartridgeElement = (OMElement) cartridgeIterator.next();
-                        OMAttribute typeAttribute = cartridgeElement.getAttribute(TYPE_ATTRIBUTE);
-                        if (typeAttribute == null) {
-                            throw new RuntimeException("Type attribute not found in cartridge element");
-                        }
-                        String cartridgeType = typeAttribute.getAttributeValue();
-                        Iterator patternIterator = cartridgeElement.getChildElements();
-
-                        while (patternIterator.hasNext()) {
-                            OMElement patternElement = (OMElement) patternIterator.next();
-                            OMAttribute factorAttribute = patternElement.getAttribute(FACTOR_ATTRIBUTE);
-
-                            if (factorAttribute == null) {
-                                throw new RuntimeException("Factor attribute not found in pattern element: " +
-                                        "[cartridge-type] " + cartridgeType);
-                            }
-                            String factorStr = factorAttribute.getAttributeValue();
-                            MockAutoscalingFactor autoscalingFactor = convertAutoscalingFactor(factorStr);
-                            String sampleValuesStr = null;
-                            String sampleDurationStr = null;
-                            Iterator patternChildIterator = patternElement.getChildElements();
-
-                            while (patternChildIterator.hasNext()) {
-                                OMElement patternChild = (OMElement) patternChildIterator.next();
-                                if (SAMPLE_VALUES_ELEMENT.equals(patternChild.getQName().getLocalPart())) {
-                                    sampleValuesStr = patternChild.getText();
-                                } else if (SAMPLE_DURATION_ELEMENT.equals(patternChild.getQName().getLocalPart())) {
-                                    sampleDurationStr = patternChild.getText();
-                                }
-                            }
-
-                            if (sampleValuesStr == null) {
-                                throw new RuntimeException("Sample values not found in pattern [factor] " + factorStr);
-                            }
-                            if (sampleDurationStr == null) {
-                                throw new RuntimeException("Sample duration not found in pattern [factor] " + factorStr);
-                            }
-
-                            String[] sampleValuesArray = sampleValuesStr.split(",");
-                            List<Integer> sampleValues = convertStringArrayToIntegerList(sampleValuesArray);
-                            MockHealthStatisticsPattern mockHealthStatisticsPattern = new MockHealthStatisticsPattern
-                                    (cartridgeType, autoscalingFactor, sampleValues, Integer.parseInt(sampleDurationStr));
-
-                            mockHealthStatisticsConfig.addStatisticsPattern(mockHealthStatisticsPattern);
-                        }
-                    }
-                }
-            }
-            return mockHealthStatisticsConfig;
-        } catch (Exception e) {
-            throw new RuntimeException("Could not parse mock health statistics configuration", e);
-        }
-    }
-
-    private static MockAutoscalingFactor convertAutoscalingFactor(String factorStr) {
-        if("memory-consumption".equals(factorStr)) {
-            return MockAutoscalingFactor.MemoryConsumption;
-        }
-        else if("load-average".equals(factorStr)) {
-            return MockAutoscalingFactor.LoadAverage;
-        }
-        else if("request-in-flight".equals(factorStr)) {
-            return MockAutoscalingFactor.RequestInFlight;
-        }
-        throw new RuntimeException("An unknown autoscaling factor found: " + factorStr);
-    }
-
-    private static List<Integer> convertStringArrayToIntegerList(String[] stringArray) {
-        List<Integer> integerList = new ArrayList<Integer>();
-        for (String value : stringArray) {
-            integerList.add(Integer.parseInt(value));
-        }
-        return integerList;
-    }
-}

http://git-wip-us.apache.org/repos/asf/stratos/blob/71605eb0/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/config/MockIaasConfig.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/config/MockIaasConfig.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/config/MockIaasConfig.java
index 8452f56..8e47f18 100644
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/config/MockIaasConfig.java
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/config/MockIaasConfig.java
@@ -19,8 +19,6 @@
 
 package org.apache.stratos.cloud.controller.iaases.mock.config;
 
-import org.wso2.carbon.context.CarbonContext;
-
 /**
  * Mock iaas configuration.
  */
@@ -31,25 +29,37 @@ public class MockIaasConfig {
 
     private static volatile MockIaasConfig instance;
 
+    private boolean enabled;
     private MockHealthStatisticsConfig mockHealthStatisticsConfig;
     
     public static MockIaasConfig getInstance() {
         if (instance == null) {
             synchronized (MockIaasConfig.class) {
                 if (instance == null) {
-                    instance = new MockIaasConfig();
+                    String confPath = System.getProperty(CARBON_HOME) + REPOSITORY_CONF;
+                    instance = MockIaasConfigParser.parse(confPath + MOCK_IAAS_CONFIG_FILE_NAME);
                 }
             }
         }
         return instance;
     }
 
-    private MockIaasConfig() {
-        String confPath = System.getProperty(CARBON_HOME) + REPOSITORY_CONF;
-        mockHealthStatisticsConfig = MockHealthStatisticsConfigParser.parse(confPath + MOCK_IAAS_CONFIG_FILE_NAME);
+    MockIaasConfig() {
+    }
+
+    void setEnabled(boolean enabled) {
+        this.enabled = enabled;
+    }
+
+    void setMockHealthStatisticsConfig(MockHealthStatisticsConfig mockHealthStatisticsConfig) {
+        this.mockHealthStatisticsConfig = mockHealthStatisticsConfig;
     }
 
     public MockHealthStatisticsConfig getMockHealthStatisticsConfig() {
         return mockHealthStatisticsConfig;
     }
+
+    public boolean isEnabled() {
+        return enabled;
+    }
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/71605eb0/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/config/MockIaasConfigParser.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/config/MockIaasConfigParser.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/config/MockIaasConfigParser.java
new file mode 100644
index 0000000..796afeb
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/config/MockIaasConfigParser.java
@@ -0,0 +1,147 @@
+/*
+ * 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.iaases.mock.config;
+
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.OMElement;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.stratos.cloud.controller.iaases.mock.MockAutoscalingFactor;
+import org.apache.stratos.cloud.controller.iaases.mock.statistics.generator.MockHealthStatisticsPattern;
+import org.apache.stratos.cloud.controller.util.AxiomXpathParserUtil;
+
+import javax.xml.namespace.QName;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Mock health statistics configuration parser.
+ */
+public class MockIaasConfigParser {
+    private static final QName ENABLED_ATTRIBUTE = new QName("enabled");
+    private static final QName TYPE_ATTRIBUTE = new QName("type");
+    private static final QName FACTOR_ATTRIBUTE = new QName("factor");
+    private static final String HEALTH_STATISTICS_ELEMENT = "health-statistics";
+    private static final String SAMPLE_VALUES_ELEMENT = "sampleValues";
+    private static final String SAMPLE_DURATION_ELEMENT = "sampleDuration";
+
+    /**
+     * Parse mock iaas configuration and return configuration object.
+     * @param filePath
+     * @return
+     */
+    public static MockIaasConfig parse(String filePath) {
+        try {
+            MockIaasConfig mockIaasConfig = new MockIaasConfig();
+            MockHealthStatisticsConfig mockHealthStatisticsConfig = new MockHealthStatisticsConfig();
+            mockIaasConfig.setMockHealthStatisticsConfig(mockHealthStatisticsConfig);
+
+            OMElement document = AxiomXpathParserUtil.parse(new File(filePath));
+            String enabledStr = document.getAttributeValue(ENABLED_ATTRIBUTE);
+            if(StringUtils.isEmpty(enabledStr)) {
+                throw new RuntimeException("Enabled attribute not found in mock-iaas element");
+            }
+            mockIaasConfig.setEnabled(Boolean.parseBoolean(enabledStr));
+
+            Iterator statisticsIterator = document.getChildElements();
+
+            while (statisticsIterator.hasNext()) {
+                OMElement statisticsElement = (OMElement) statisticsIterator.next();
+
+                if (HEALTH_STATISTICS_ELEMENT.equals(statisticsElement.getQName().getLocalPart())) {
+                    Iterator cartridgeIterator = statisticsElement.getChildElements();
+
+                    while (cartridgeIterator.hasNext()) {
+                        OMElement cartridgeElement = (OMElement) cartridgeIterator.next();
+                        OMAttribute typeAttribute = cartridgeElement.getAttribute(TYPE_ATTRIBUTE);
+                        if (typeAttribute == null) {
+                            throw new RuntimeException("Type attribute not found in cartridge element");
+                        }
+                        String cartridgeType = typeAttribute.getAttributeValue();
+                        Iterator patternIterator = cartridgeElement.getChildElements();
+
+                        while (patternIterator.hasNext()) {
+                            OMElement patternElement = (OMElement) patternIterator.next();
+                            OMAttribute factorAttribute = patternElement.getAttribute(FACTOR_ATTRIBUTE);
+
+                            if (factorAttribute == null) {
+                                throw new RuntimeException("Factor attribute not found in pattern element: " +
+                                        "[cartridge-type] " + cartridgeType);
+                            }
+                            String factorStr = factorAttribute.getAttributeValue();
+                            MockAutoscalingFactor autoscalingFactor = convertAutoscalingFactor(factorStr);
+                            String sampleValuesStr = null;
+                            String sampleDurationStr = null;
+                            Iterator patternChildIterator = patternElement.getChildElements();
+
+                            while (patternChildIterator.hasNext()) {
+                                OMElement patternChild = (OMElement) patternChildIterator.next();
+                                if (SAMPLE_VALUES_ELEMENT.equals(patternChild.getQName().getLocalPart())) {
+                                    sampleValuesStr = patternChild.getText();
+                                } else if (SAMPLE_DURATION_ELEMENT.equals(patternChild.getQName().getLocalPart())) {
+                                    sampleDurationStr = patternChild.getText();
+                                }
+                            }
+
+                            if (sampleValuesStr == null) {
+                                throw new RuntimeException("Sample values not found in pattern [factor] " + factorStr);
+                            }
+                            if (sampleDurationStr == null) {
+                                throw new RuntimeException("Sample duration not found in pattern [factor] " + factorStr);
+                            }
+
+                            String[] sampleValuesArray = sampleValuesStr.split(",");
+                            List<Integer> sampleValues = convertStringArrayToIntegerList(sampleValuesArray);
+                            MockHealthStatisticsPattern mockHealthStatisticsPattern = new MockHealthStatisticsPattern
+                                    (cartridgeType, autoscalingFactor, sampleValues, Integer.parseInt(sampleDurationStr));
+
+                            mockHealthStatisticsConfig.addStatisticsPattern(mockHealthStatisticsPattern);
+                        }
+                    }
+                }
+            }
+            return mockIaasConfig;
+        } catch (Exception e) {
+            throw new RuntimeException("Could not parse mock health statistics configuration", e);
+        }
+    }
+
+    private static MockAutoscalingFactor convertAutoscalingFactor(String factorStr) {
+        if("memory-consumption".equals(factorStr)) {
+            return MockAutoscalingFactor.MemoryConsumption;
+        }
+        else if("load-average".equals(factorStr)) {
+            return MockAutoscalingFactor.LoadAverage;
+        }
+        else if("request-in-flight".equals(factorStr)) {
+            return MockAutoscalingFactor.RequestInFlight;
+        }
+        throw new RuntimeException("An unknown autoscaling factor found: " + factorStr);
+    }
+
+    private static List<Integer> convertStringArrayToIntegerList(String[] stringArray) {
+        List<Integer> integerList = new ArrayList<Integer>();
+        for (String value : stringArray) {
+            integerList.add(Integer.parseInt(value));
+        }
+        return integerList;
+    }
+}

http://git-wip-us.apache.org/repos/asf/stratos/blob/71605eb0/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/internal/CloudControllerServiceComponent.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/internal/CloudControllerServiceComponent.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/internal/CloudControllerServiceComponent.java
index b60021b..5f7c9ff 100644
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/internal/CloudControllerServiceComponent.java
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/internal/CloudControllerServiceComponent.java
@@ -26,6 +26,7 @@ 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.iaases.mock.MockIaasService;
+import org.apache.stratos.cloud.controller.iaases.mock.config.MockIaasConfig;
 import org.apache.stratos.cloud.controller.messaging.receiver.application.ApplicationTopicReceiver;
 import org.apache.stratos.cloud.controller.messaging.receiver.cluster.status.ClusterStatusTopicReceiver;
 import org.apache.stratos.cloud.controller.exception.CloudControllerException;
@@ -107,8 +108,10 @@ public class CloudControllerServiceComponent {
                 executeCoordinatorTasks();
             }
 
-            // Start mock members if present in registry
-            MockIaasService.startMockMembersIfPresentInRegistry();
+            if(MockIaasConfig.getInstance().isEnabled()) {
+                // Start mock members if present in registry
+                MockIaasService.startMockMembersIfPresentInRegistry();
+            }
 		} catch (Throwable e) {
 			log.error("******* Cloud Controller Service bundle is failed to activate ****", e);
         }