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/24 18:36:26 UTC
[03/12] stratos git commit: Renaming iaas classes and moving them to
new packages
http://git-wip-us.apache.org/repos/asf/stratos/blob/e195f2f1/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
deleted file mode 100644
index ec39265..0000000
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/config/MockIaasConfigParser.java
+++ /dev/null
@@ -1,172 +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.commons.lang3.StringUtils;
-import org.apache.stratos.cloud.controller.iaases.mock.MockAutoscalingFactor;
-import org.apache.stratos.cloud.controller.iaases.mock.statistics.StatisticsPatternMode;
-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 QName MODE_ATTRIBUTE = new QName("mode");
- 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);
-
- OMAttribute modeAttribute = patternElement.getAttribute(MODE_ATTRIBUTE);
- if(modeAttribute == null) {
- throw new RuntimeException("Mode attribute not found in pattern element: " +
- "[cartridge-type] " + cartridgeType);
- }
- String modeStr = modeAttribute.getAttributeValue();
- StatisticsPatternMode mode = convertMode(modeStr);
-
- 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);
- int sampleDuration = Integer.parseInt(sampleDurationStr);
-
- MockHealthStatisticsPattern mockHealthStatisticsPattern = new MockHealthStatisticsPattern
- (cartridgeType, autoscalingFactor, mode, sampleValues, sampleDuration);
- mockHealthStatisticsConfig.addStatisticsPattern(mockHealthStatisticsPattern);
- }
- }
- }
- }
- return mockIaasConfig;
- } catch (Exception e) {
- throw new RuntimeException("Could not parse mock health statistics configuration", e);
- }
- }
-
- private static StatisticsPatternMode convertMode(String modeStr) {
- if("loop".equals(modeStr)) {
- return StatisticsPatternMode.Loop;
- }
- else if("continue".equals(modeStr)) {
- return StatisticsPatternMode.Continue;
- }
- else if("stop".equals(modeStr)) {
- return StatisticsPatternMode.Stop;
- }
- throw new RuntimeException("An unknown statistics pattern mode found: " + modeStr);
- }
-
- 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/e195f2f1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/exceptions/ContinueLastSampleValueException.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/exceptions/ContinueLastSampleValueException.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/exceptions/ContinueLastSampleValueException.java
deleted file mode 100644
index a4922ee..0000000
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/exceptions/ContinueLastSampleValueException.java
+++ /dev/null
@@ -1,36 +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.exceptions;
-
-/**
- * Thrown when statistics pattern mode is set to continue and pattern reaches
- * the last sample value.
- */
-public class ContinueLastSampleValueException extends Exception {
- private int lastSampleValue;
-
- public ContinueLastSampleValueException(int lastSampleValue) {
- this.lastSampleValue = lastSampleValue;
- }
-
- public int getLastSampleValue() {
- return lastSampleValue;
- }
-}
http://git-wip-us.apache.org/repos/asf/stratos/blob/e195f2f1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/exceptions/NoSampleValuesFoundException.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/exceptions/NoSampleValuesFoundException.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/exceptions/NoSampleValuesFoundException.java
deleted file mode 100644
index 31ffa9e..0000000
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/exceptions/NoSampleValuesFoundException.java
+++ /dev/null
@@ -1,26 +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.exceptions;
-
-/**
- * No sample values found exception.
- */
-public class NoSampleValuesFoundException extends Exception {
-}
http://git-wip-us.apache.org/repos/asf/stratos/blob/e195f2f1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/exceptions/NoStatisticsFoundException.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/exceptions/NoStatisticsFoundException.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/exceptions/NoStatisticsFoundException.java
deleted file mode 100644
index 3952d1d..0000000
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/exceptions/NoStatisticsFoundException.java
+++ /dev/null
@@ -1,26 +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.exceptions;
-
-/**
- * No statistics found exception.
- */
-public class NoStatisticsFoundException extends Exception {
-}
http://git-wip-us.apache.org/repos/asf/stratos/blob/e195f2f1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/exceptions/StopStatisticsPublishingException.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/exceptions/StopStatisticsPublishingException.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/exceptions/StopStatisticsPublishingException.java
deleted file mode 100644
index 84dbf44..0000000
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/exceptions/StopStatisticsPublishingException.java
+++ /dev/null
@@ -1,26 +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.exceptions;
-
-/**
- * Stop statistics publishing exception.
- */
-public class StopStatisticsPublishingException extends Exception {
-}
http://git-wip-us.apache.org/repos/asf/stratos/blob/e195f2f1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockAutoscalingFactor.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockAutoscalingFactor.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockAutoscalingFactor.java
new file mode 100644
index 0000000..684ce45
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockAutoscalingFactor.java
@@ -0,0 +1,27 @@
+/*
+ * 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.service;
+
+/**
+ * Mock autoscaling factor enumeration
+ */
+public enum MockAutoscalingFactor {
+ MemoryConsumption, LoadAverage, RequestInFlight
+}
http://git-wip-us.apache.org/repos/asf/stratos/blob/e195f2f1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockConstants.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockConstants.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockConstants.java
new file mode 100644
index 0000000..360da68
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockConstants.java
@@ -0,0 +1,27 @@
+/*
+ * 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.service;
+
+/**
+ * Mock constant definitions.
+ */
+public class MockConstants {
+ public static final int MAX_MOCK_MEMBER_COUNT = 100;
+}
http://git-wip-us.apache.org/repos/asf/stratos/blob/e195f2f1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockIPAddressPool.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockIPAddressPool.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockIPAddressPool.java
new file mode 100644
index 0000000..936a701
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockIPAddressPool.java
@@ -0,0 +1,100 @@
+/*
+ * 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.service;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.cloud.controller.registry.RegistryManager;
+import org.wso2.carbon.registry.core.exceptions.RegistryException;
+
+import java.io.Serializable;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Mock IP address pool is a singleton class for managing mocked private and public IP addresses.
+ */
+public class MockIPAddressPool {
+ private static final Log log = LogFactory.getLog(MockIPAddressPool.class);
+
+ private static final String MOCK_IAAS_PRIVATE_IP_SEQUENCE = "/mock/iaas/private-ip-sequence";
+ private static final String MOCK_IAAS_PUBLIC_IP_SEQUENCE = "/mock/iaas/public-ip-sequence";
+ private static final String PRIVATE_IP_PREFIX = "10.0.0.";
+ private static final String PUBLIC_IP_PREFIX = "20.0.0.";
+
+ private static volatile MockIPAddressPool instance;
+
+ private AtomicInteger privateIpSequence;
+ private AtomicInteger publicIpSequence;
+
+ private MockIPAddressPool() {
+ privateIpSequence = readFromRegistry(MOCK_IAAS_PRIVATE_IP_SEQUENCE);
+ if (privateIpSequence == null) {
+ privateIpSequence = new AtomicInteger();
+ }
+
+ publicIpSequence = readFromRegistry(MOCK_IAAS_PUBLIC_IP_SEQUENCE);
+ if (publicIpSequence == null) {
+ publicIpSequence = new AtomicInteger();
+ }
+ }
+
+ public static MockIPAddressPool getInstance() {
+ if (instance == null) {
+ synchronized (MockIPAddressPool.class) {
+ if (instance == null) {
+ instance = new MockIPAddressPool();
+ }
+ }
+ }
+ return instance;
+ }
+
+ public String getNextPrivateIpAddress() {
+ int nextSequence = privateIpSequence.incrementAndGet();
+ String ipAddress = PRIVATE_IP_PREFIX + nextSequence;
+ persistInRegistry(MOCK_IAAS_PRIVATE_IP_SEQUENCE, privateIpSequence);
+ if (log.isInfoEnabled()) {
+ log.info("Mock private IP address allocated: " + ipAddress);
+ }
+ return ipAddress;
+ }
+
+ public String getNextPublicIpAddress() {
+ int nextSequence = publicIpSequence.incrementAndGet();
+ String ipAddress = PUBLIC_IP_PREFIX + nextSequence;
+ persistInRegistry(MOCK_IAAS_PRIVATE_IP_SEQUENCE, publicIpSequence);
+ if (log.isInfoEnabled()) {
+ log.info("Mock public IP address allocated: " + ipAddress);
+ }
+ return ipAddress;
+ }
+
+ private void persistInRegistry(String resourcePath, Serializable serializable) {
+ try {
+ RegistryManager.getInstance().persist(resourcePath, serializable);
+ } catch (RegistryException e) {
+ log.error(String.format("Could not persist mock iaas ip sequence [%s] in registry", resourcePath), e);
+ }
+ }
+
+ private AtomicInteger readFromRegistry(String resourcePath) {
+ return (AtomicInteger) RegistryManager.getInstance().read(resourcePath);
+ }
+}
http://git-wip-us.apache.org/repos/asf/stratos/blob/e195f2f1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockIaasService.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockIaasService.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockIaasService.java
new file mode 100644
index 0000000..67f82f7
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockIaasService.java
@@ -0,0 +1,224 @@
+/*
+ * 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.service;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.cloud.controller.domain.*;
+import org.apache.stratos.cloud.controller.exception.*;
+import org.apache.stratos.cloud.controller.iaases.mock.MockPartitionValidator;
+import org.apache.stratos.cloud.controller.iaases.mock.service.config.MockIaasConfig;
+import org.apache.stratos.cloud.controller.iaases.mock.service.statistics.generator.MockHealthStatisticsGenerator;
+import org.apache.stratos.cloud.controller.iaases.PartitionValidator;
+import org.apache.stratos.cloud.controller.registry.RegistryManager;
+import org.apache.stratos.common.threading.StratosThreadPool;
+import org.wso2.carbon.registry.core.exceptions.RegistryException;
+
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+
+/**
+ * Mock IaaS service implementation. This is a singleton class that simulates a standard Infrastructure as a Service
+ * platform by creating mock members and managing their lifecycle states.
+ *
+ * How does this work:
+ * - Mock IaaS starts a Mock Member thread or each instance created
+ * - A sample private IP and a public IP will be assigned to the instance
+ * - Mock Member will publish Instance Started and Instance Activated events once the thread is started
+ * - Afterwards it will start publishing sample health statistics values to CEP
+ * - If the Mock IaaS was asked to terminate an instance it will stop the relevant thread
+ */
+public class MockIaasService {
+
+ private static final Log log = LogFactory.getLog(MockIaasService.class);
+
+ private static final ExecutorService mockMemberExecutorService =
+ StratosThreadPool.getExecutorService("MOCK_MEMBER_EXECUTOR_SERVICE", MockConstants.MAX_MOCK_MEMBER_COUNT);
+ private static final String MOCK_IAAS_MEMBERS = "/mock/iaas/members";
+ private static volatile MockIaasService instance;
+
+ private MockPartitionValidator partitionValidator;
+ // Map<ServiceName, Map<MemberId,MockMember>>
+ private Map<String, Map<String, MockMember>> serviceNameToMockMemberMap;
+
+ private MockIaasService() {
+ super();
+ partitionValidator = new MockPartitionValidator();
+ serviceNameToMockMemberMap = readFromRegistry();
+ if(serviceNameToMockMemberMap == null) {
+ // No members found in registry, create a new map
+ serviceNameToMockMemberMap = new ConcurrentHashMap<String, Map<String, MockMember>>();
+ }
+ }
+
+ public static MockIaasService getInstance() {
+ if (instance == null) {
+ synchronized (MockIaasService.class) {
+ if (instance == null) {
+ if(!MockIaasConfig.getInstance().isEnabled()) {
+ throw new RuntimeException("Mock IaaS is not enabled");
+ }
+ instance = new MockIaasService();
+ }
+ }
+ }
+ return instance;
+ }
+
+ /**
+ * Start mock members
+ */
+ public void startMockMembers() {
+ if(serviceNameToMockMemberMap != null) {
+ for(Map.Entry<String, Map<String, MockMember>> serviceNameEntry : serviceNameToMockMemberMap.entrySet()) {
+ // Start mock members
+ for(Map.Entry<String, MockMember> memberEntry : serviceNameEntry.getValue().entrySet()) {
+ mockMemberExecutorService.submit(memberEntry.getValue());
+ }
+
+ // Schedule statistics updater tasks for service
+ if(serviceNameEntry.getValue().entrySet().size() > 0) {
+ MockHealthStatisticsGenerator.getInstance().scheduleStatisticsUpdaterTasks(serviceNameEntry.getKey());
+ }
+ }
+ }
+ }
+
+ public MemberContext createInstance(MemberContext memberContext) {
+ synchronized (MockIaasService.class) {
+ // Create mock member instance
+ MockMemberContext mockMemberContext = new MockMemberContext(memberContext.getCartridgeType(),
+ memberContext.getClusterId(), memberContext.getMemberId(), memberContext.getInstanceId(),
+ memberContext.getClusterInstanceId(), memberContext.getNetworkPartitionId(),
+ memberContext.getPartition().getId());
+
+ MockMember mockMember = new MockMember(mockMemberContext);
+ addMemberToMap(mockMember);
+ mockMemberExecutorService.submit(mockMember);
+
+ // Generate instance id
+ memberContext.setInstanceId(UUID.randomUUID().toString());
+
+ // Persist changes
+ persistInRegistry();
+
+ String serviceName = mockMemberContext.getServiceName();
+ MockHealthStatisticsGenerator.getInstance().scheduleStatisticsUpdaterTasks(serviceName);
+
+ return memberContext;
+ }
+ }
+
+ private void addMemberToMap(MockMember mockMember) {
+ String serviceName = mockMember.getMockMemberContext().getServiceName();
+ Map<String, MockMember> memberMap = serviceNameToMockMemberMap.get(serviceName);
+ if(memberMap == null) {
+ memberMap = new ConcurrentHashMap<String, MockMember>();
+ serviceNameToMockMemberMap.put(serviceName, memberMap);
+ }
+ memberMap.put(mockMember.getMockMemberContext().getMemberId(), mockMember);
+ }
+
+ private void persistInRegistry() {
+ try {
+ RegistryManager.getInstance().persist(MOCK_IAAS_MEMBERS,
+ (ConcurrentHashMap<String, Map<String, MockMember>>)serviceNameToMockMemberMap);
+ } catch (RegistryException e) {
+ log.error("Could not persist mock iaas members in registry", e);
+ }
+ }
+
+ private ConcurrentHashMap<String, Map<String, MockMember>> readFromRegistry() {
+ return (ConcurrentHashMap<String, Map<String, MockMember>>)
+ RegistryManager.getInstance().read(MOCK_IAAS_MEMBERS);
+ }
+
+ public void allocateIpAddress(String clusterId, MemberContext memberContext, Partition partition) {
+ // Allocate mock ip addresses
+ memberContext.setDefaultPrivateIP(MockIPAddressPool.getInstance().getNextPrivateIpAddress());
+ memberContext.setDefaultPublicIP(MockIPAddressPool.getInstance().getNextPublicIpAddress());
+ }
+
+ public void releaseAddress(String ip) {
+
+ }
+
+ public boolean isValidRegion(String region) throws InvalidRegionException {
+ return true;
+ }
+
+ public boolean isValidZone(String region, String zone) throws InvalidZoneException, InvalidRegionException {
+ return true;
+ }
+
+ public boolean isValidHost(String zone, String host) throws InvalidHostException {
+ return true;
+ }
+
+ public PartitionValidator getPartitionValidator() {
+ return partitionValidator;
+ }
+
+ public String createVolume(int sizeGB, String snapshotId) {
+ return null;
+ }
+
+ public String attachVolume(String instanceId, String volumeId, String deviceName) {
+ return null;
+ }
+
+ public void detachVolume(String instanceId, String volumeId) {
+
+ }
+
+ public void deleteVolume(String volumeId) {
+
+ }
+
+ public String getIaasDevice(String device) {
+ return null;
+ }
+
+ public void setDynamicPayload(byte[] payload) {
+
+ }
+
+ public void terminateInstance(MemberContext memberContext) throws InvalidCartridgeTypeException, InvalidMemberException {
+ synchronized (MockIaasService.class) {
+ String serviceName = memberContext.getCartridgeType();
+ Map<String, MockMember> memberMap = serviceNameToMockMemberMap.get(serviceName);
+ if(memberMap != null) {
+ MockMember mockMember = memberMap.get(memberContext.getMemberId());
+ if(mockMember != null) {
+ if (mockMember != null) {
+ mockMember.terminate();
+ memberMap.remove(memberContext.getMemberId());
+ }
+
+ if (memberMap.size() == 0) {
+ MockHealthStatisticsGenerator.getInstance().stopStatisticsUpdaterTasks(serviceName);
+ }
+ }
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/stratos/blob/e195f2f1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockMember.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockMember.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockMember.java
new file mode 100644
index 0000000..529c590
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockMember.java
@@ -0,0 +1,155 @@
+/*
+ * 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.service;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.cloud.controller.iaases.mock.service.statistics.publisher.MockHealthStatisticsNotifier;
+import org.apache.stratos.common.threading.StratosThreadPool;
+import org.apache.stratos.messaging.event.Event;
+import org.apache.stratos.messaging.event.instance.notifier.InstanceCleanupClusterEvent;
+import org.apache.stratos.messaging.event.instance.notifier.InstanceCleanupMemberEvent;
+import org.apache.stratos.messaging.listener.instance.notifier.InstanceCleanupClusterEventListener;
+import org.apache.stratos.messaging.listener.instance.notifier.InstanceCleanupMemberEventListener;
+import org.apache.stratos.messaging.message.receiver.instance.notifier.InstanceNotifierEventReceiver;
+
+import java.io.Serializable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Mock member definition.
+ */
+public class MockMember implements Runnable, Serializable {
+
+ private static final Log log = LogFactory.getLog(MockMember.class);
+ private static final ExecutorService instanceNotifierExecutorService =
+ StratosThreadPool.getExecutorService("MOCK_MEMBER_INSTANCE_NOTIFIER_EXECUTOR_SERVICE", 20);
+ private static final ScheduledExecutorService healthStatNotifierExecutorService =
+ StratosThreadPool.getScheduledExecutorService("MOCK_MEMBER_HEALTH_STAT_NOTIFIER_EXECUTOR_SERVICE", 20);
+ private static final int HEALTH_STAT_INTERVAL = 15; // 15 seconds
+
+ private final MockMemberContext mockMemberContext;
+ private boolean terminated;
+
+ public MockMember(MockMemberContext mockMemberContext) {
+ this.mockMemberContext = mockMemberContext;
+ }
+
+ @Override
+ public void run() {
+ if (log.isInfoEnabled()) {
+ log.info(String.format("Mock member started: [member-id] %s", mockMemberContext.getMemberId()));
+ }
+
+ sleep(5000);
+ MockMemberEventPublisher.publishInstanceStartedEvent(mockMemberContext);
+
+ sleep(5000);
+ MockMemberEventPublisher.publishInstanceActivatedEvent(mockMemberContext);
+
+ startInstanceNotifierReceiver();
+ startHealthStatisticsPublisher();
+
+ while (!terminated) {
+ sleep(1000);
+ }
+
+ if (log.isInfoEnabled()) {
+ log.info(String.format("Mock member terminated: [member-id] %s", mockMemberContext.getMemberId()));
+ }
+ }
+
+ private void startInstanceNotifierReceiver() {
+ if (log.isDebugEnabled()) {
+ log.debug("Starting instance notifier event message receiver");
+ }
+
+ final InstanceNotifierEventReceiver instanceNotifierEventReceiver = new InstanceNotifierEventReceiver();
+ instanceNotifierEventReceiver.addEventListener(new InstanceCleanupClusterEventListener() {
+ @Override
+ protected void onEvent(Event event) {
+ InstanceCleanupClusterEvent instanceCleanupClusterEvent = (InstanceCleanupClusterEvent) event;
+ if (mockMemberContext.getClusterId().equals(instanceCleanupClusterEvent.getClusterId()) &&
+ mockMemberContext.getClusterInstanceId().equals(
+ instanceCleanupClusterEvent.getClusterInstanceId())) {
+ handleMemberTermination();
+ }
+ }
+ });
+
+ instanceNotifierEventReceiver.addEventListener(new InstanceCleanupMemberEventListener() {
+ @Override
+ protected void onEvent(Event event) {
+ InstanceCleanupMemberEvent instanceCleanupClusterEvent = (InstanceCleanupMemberEvent) event;
+ if (mockMemberContext.getMemberId().equals(instanceCleanupClusterEvent.getMemberId())) {
+ handleMemberTermination();
+ }
+ }
+ });
+
+ instanceNotifierExecutorService.submit(new Runnable() {
+ @Override
+ public void run() {
+ instanceNotifierEventReceiver.execute();
+ }
+ });
+
+ if (log.isDebugEnabled()) {
+ log.debug("Instance notifier event message receiver started");
+ }
+ }
+
+ private void handleMemberTermination() {
+ MockMemberEventPublisher.publishMaintenanceModeEvent(mockMemberContext);
+ sleep(2000);
+ MockMemberEventPublisher.publishInstanceReadyToShutdownEvent(mockMemberContext);
+ }
+
+ private void startHealthStatisticsPublisher() {
+ if (log.isDebugEnabled()) {
+ log.debug(String.format("Starting health statistics notifier: [member-id] %s", mockMemberContext.getMemberId()));
+ }
+
+ healthStatNotifierExecutorService.scheduleAtFixedRate(new MockHealthStatisticsNotifier(mockMemberContext),
+ 0, HEALTH_STAT_INTERVAL, TimeUnit.SECONDS);
+
+ if (log.isDebugEnabled()) {
+ log.debug(String.format("Health statistics notifier started: [member-id] %s", mockMemberContext.getMemberId()));
+ }
+ }
+
+ private void sleep(long time) {
+ try {
+ Thread.sleep(time);
+ } catch (InterruptedException ignore) {
+ terminate();
+ }
+ }
+
+ public MockMemberContext getMockMemberContext() {
+ return mockMemberContext;
+ }
+
+ public void terminate() {
+ terminated = true;
+ }
+}
http://git-wip-us.apache.org/repos/asf/stratos/blob/e195f2f1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockMemberContext.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockMemberContext.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockMemberContext.java
new file mode 100644
index 0000000..48bb3b2
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockMemberContext.java
@@ -0,0 +1,74 @@
+/*
+ * 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.service;
+
+import java.io.Serializable;
+
+/**
+ * Mock member context.
+ */
+public class MockMemberContext implements Serializable {
+ private final String serviceName;
+ private final String clusterId;
+ private final String memberId;
+ private final String instanceId;
+ private final String clusterInstanceId;
+ private final String networkPartitionId;
+ private final String partitionId;
+
+ public MockMemberContext(String serviceName, String clusterId, String memberId, String instanceId,
+ String clusterInstanceId, String networkPartitionId, String partitionId) {
+ this.serviceName = serviceName;
+ this.clusterId = clusterId;
+ this.memberId = memberId;
+ this.instanceId = instanceId;
+ this.clusterInstanceId = clusterInstanceId;
+ this.networkPartitionId = networkPartitionId;
+ this.partitionId = partitionId;
+ }
+
+ public String getServiceName() {
+ return serviceName;
+ }
+
+ public String getClusterId() {
+ return clusterId;
+ }
+
+ public String getMemberId() {
+ return memberId;
+ }
+
+ public String getInstanceId() {
+ return instanceId;
+ }
+
+ public String getClusterInstanceId() {
+ return clusterInstanceId;
+ }
+
+ public String getNetworkPartitionId() {
+ return networkPartitionId;
+ }
+
+ public String getPartitionId() {
+ return partitionId;
+ }
+}
http://git-wip-us.apache.org/repos/asf/stratos/blob/e195f2f1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockMemberEventPublisher.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockMemberEventPublisher.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockMemberEventPublisher.java
new file mode 100644
index 0000000..cb0a6f5
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/MockMemberEventPublisher.java
@@ -0,0 +1,124 @@
+/*
+ * 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.service;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.messaging.broker.publish.EventPublisher;
+import org.apache.stratos.messaging.broker.publish.EventPublisherPool;
+import org.apache.stratos.messaging.event.instance.status.InstanceActivatedEvent;
+import org.apache.stratos.messaging.event.instance.status.InstanceMaintenanceModeEvent;
+import org.apache.stratos.messaging.event.instance.status.InstanceReadyToShutdownEvent;
+import org.apache.stratos.messaging.event.instance.status.InstanceStartedEvent;
+import org.apache.stratos.messaging.util.Util;
+
+/**
+ * Mock member event publisher.
+ */
+public class MockMemberEventPublisher {
+
+ private static final Log log = LogFactory.getLog(MockMemberEventPublisher.class);
+
+ public static void publishInstanceStartedEvent(MockMemberContext mockMemberContext) {
+ if (log.isInfoEnabled()) {
+ log.info("Publishing instance started event");
+ }
+ InstanceStartedEvent event = new InstanceStartedEvent(
+ mockMemberContext.getServiceName(),
+ mockMemberContext.getClusterId(),
+ mockMemberContext.getMemberId(),
+ mockMemberContext.getClusterInstanceId(),
+ mockMemberContext.getNetworkPartitionId(),
+ mockMemberContext.getPartitionId());
+ String topic = Util.getMessageTopicName(event);
+ EventPublisher eventPublisher = EventPublisherPool
+ .getPublisher(topic);
+ eventPublisher.publish(event);
+ if (log.isInfoEnabled()) {
+ log.info("Instance started event published");
+ }
+ }
+
+ public static void publishInstanceActivatedEvent(MockMemberContext mockMemberContext) {
+ if (log.isInfoEnabled()) {
+ log.info("Publishing instance activated event");
+ }
+ InstanceActivatedEvent event = new InstanceActivatedEvent(
+ mockMemberContext.getServiceName(),
+ mockMemberContext.getClusterId(),
+ mockMemberContext.getMemberId(),
+ mockMemberContext.getInstanceId(),
+ mockMemberContext.getClusterInstanceId(),
+ mockMemberContext.getNetworkPartitionId(),
+ mockMemberContext.getPartitionId());
+
+ // Event publisher connection will
+ String topic = Util.getMessageTopicName(event);
+ EventPublisher eventPublisher = EventPublisherPool.getPublisher(topic);
+ eventPublisher.publish(event);
+ if (log.isInfoEnabled()) {
+ log.info("Instance activated event published");
+ }
+ }
+
+ public static void publishInstanceReadyToShutdownEvent(MockMemberContext mockMemberContext) {
+ if (log.isInfoEnabled()) {
+ log.info(String.format("Publishing instance ready to shutdown event: [member-id] %s",
+ mockMemberContext.getMemberId()));
+ }
+ InstanceReadyToShutdownEvent event = new InstanceReadyToShutdownEvent(
+ mockMemberContext.getServiceName(),
+ mockMemberContext.getClusterId(),
+ mockMemberContext.getMemberId(),
+ mockMemberContext.getClusterInstanceId(),
+ mockMemberContext.getNetworkPartitionId(),
+ mockMemberContext.getPartitionId());
+ String topic = Util.getMessageTopicName(event);
+ EventPublisher eventPublisher = EventPublisherPool
+ .getPublisher(topic);
+ eventPublisher.publish(event);
+ if (log.isInfoEnabled()) {
+ log.info(String.format("Instance ready to shutDown event published: [member-id] %s",
+ mockMemberContext.getMemberId()));
+ }
+ }
+
+ public static void publishMaintenanceModeEvent(MockMemberContext mockMemberContext) {
+ if (log.isInfoEnabled()) {
+ log.info(String.format("Publishing instance maintenance mode event: [member-id] %s",
+ mockMemberContext.getMemberId()));
+ }
+ InstanceMaintenanceModeEvent event = new InstanceMaintenanceModeEvent(
+ mockMemberContext.getServiceName(),
+ mockMemberContext.getClusterId(),
+ mockMemberContext.getMemberId(),
+ mockMemberContext.getClusterInstanceId(),
+ mockMemberContext.getNetworkPartitionId(),
+ mockMemberContext.getPartitionId());
+ String topic = Util.getMessageTopicName(event);
+ EventPublisher eventPublisher = EventPublisherPool.getPublisher(topic);
+ eventPublisher.publish(event);
+
+ if (log.isInfoEnabled()) {
+ log.info(String.format("Instance Maintenance mode event published: [member-id] %s",
+ mockMemberContext.getMemberId()));
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/stratos/blob/e195f2f1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/config/MockHealthStatisticsConfig.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/config/MockHealthStatisticsConfig.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/config/MockHealthStatisticsConfig.java
new file mode 100644
index 0000000..7abb118
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/config/MockHealthStatisticsConfig.java
@@ -0,0 +1,44 @@
+/*
+ * 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.service.config;
+
+import org.apache.stratos.cloud.controller.iaases.mock.service.statistics.generator.MockHealthStatisticsPattern;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Mock health statistics configuration.
+ */
+public class MockHealthStatisticsConfig {
+ List<MockHealthStatisticsPattern> statisticsPatternList;
+
+ public MockHealthStatisticsConfig() {
+ statisticsPatternList = new ArrayList<MockHealthStatisticsPattern>();
+ }
+
+ public void addStatisticsPattern(MockHealthStatisticsPattern statisticsPattern) {
+ statisticsPatternList.add(statisticsPattern);
+ }
+
+ public List<MockHealthStatisticsPattern> getStatisticsPatterns() {
+ return statisticsPatternList;
+ }
+}
http://git-wip-us.apache.org/repos/asf/stratos/blob/e195f2f1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/config/MockIaasConfig.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/config/MockIaasConfig.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/config/MockIaasConfig.java
new file mode 100644
index 0000000..ba97c49
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/config/MockIaasConfig.java
@@ -0,0 +1,65 @@
+/*
+ * 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.service.config;
+
+/**
+ * Mock iaas configuration.
+ */
+public class MockIaasConfig {
+ private static final String MOCK_IAAS_CONFIG_FILE_NAME = "mock-iaas.xml";
+ private static final String CARBON_HOME = "carbon.home";
+ private static final String REPOSITORY_CONF = "/repository/conf/";
+
+ private static volatile MockIaasConfig instance;
+
+ private boolean enabled;
+ private MockHealthStatisticsConfig mockHealthStatisticsConfig;
+
+ public static MockIaasConfig getInstance() {
+ if (instance == null) {
+ synchronized (MockIaasConfig.class) {
+ if (instance == null) {
+ String confPath = System.getProperty(CARBON_HOME) + REPOSITORY_CONF;
+ instance = MockIaasConfigParser.parse(confPath + MOCK_IAAS_CONFIG_FILE_NAME);
+ }
+ }
+ }
+ return instance;
+ }
+
+ 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/e195f2f1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/config/MockIaasConfigParser.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/config/MockIaasConfigParser.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/config/MockIaasConfigParser.java
new file mode 100644
index 0000000..5faf92a
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/config/MockIaasConfigParser.java
@@ -0,0 +1,172 @@
+/*
+ * 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.service.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.service.MockAutoscalingFactor;
+import org.apache.stratos.cloud.controller.iaases.mock.service.statistics.StatisticsPatternMode;
+import org.apache.stratos.cloud.controller.iaases.mock.service.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 QName MODE_ATTRIBUTE = new QName("mode");
+ 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);
+
+ OMAttribute modeAttribute = patternElement.getAttribute(MODE_ATTRIBUTE);
+ if(modeAttribute == null) {
+ throw new RuntimeException("Mode attribute not found in pattern element: " +
+ "[cartridge-type] " + cartridgeType);
+ }
+ String modeStr = modeAttribute.getAttributeValue();
+ StatisticsPatternMode mode = convertMode(modeStr);
+
+ 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);
+ int sampleDuration = Integer.parseInt(sampleDurationStr);
+
+ MockHealthStatisticsPattern mockHealthStatisticsPattern = new MockHealthStatisticsPattern
+ (cartridgeType, autoscalingFactor, mode, sampleValues, sampleDuration);
+ mockHealthStatisticsConfig.addStatisticsPattern(mockHealthStatisticsPattern);
+ }
+ }
+ }
+ }
+ return mockIaasConfig;
+ } catch (Exception e) {
+ throw new RuntimeException("Could not parse mock health statistics configuration", e);
+ }
+ }
+
+ private static StatisticsPatternMode convertMode(String modeStr) {
+ if("loop".equals(modeStr)) {
+ return StatisticsPatternMode.Loop;
+ }
+ else if("continue".equals(modeStr)) {
+ return StatisticsPatternMode.Continue;
+ }
+ else if("stop".equals(modeStr)) {
+ return StatisticsPatternMode.Stop;
+ }
+ throw new RuntimeException("An unknown statistics pattern mode found: " + modeStr);
+ }
+
+ 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/e195f2f1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/exceptions/ContinueLastSampleValueException.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/exceptions/ContinueLastSampleValueException.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/exceptions/ContinueLastSampleValueException.java
new file mode 100644
index 0000000..ccb8c03
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/exceptions/ContinueLastSampleValueException.java
@@ -0,0 +1,36 @@
+/*
+ * 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.service.exceptions;
+
+/**
+ * Thrown when statistics pattern mode is set to continue and pattern reaches
+ * the last sample value.
+ */
+public class ContinueLastSampleValueException extends Exception {
+ private int lastSampleValue;
+
+ public ContinueLastSampleValueException(int lastSampleValue) {
+ this.lastSampleValue = lastSampleValue;
+ }
+
+ public int getLastSampleValue() {
+ return lastSampleValue;
+ }
+}
http://git-wip-us.apache.org/repos/asf/stratos/blob/e195f2f1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/exceptions/NoSampleValuesFoundException.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/exceptions/NoSampleValuesFoundException.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/exceptions/NoSampleValuesFoundException.java
new file mode 100644
index 0000000..d94d914
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/exceptions/NoSampleValuesFoundException.java
@@ -0,0 +1,26 @@
+/*
+ * 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.service.exceptions;
+
+/**
+ * No sample values found exception.
+ */
+public class NoSampleValuesFoundException extends Exception {
+}
http://git-wip-us.apache.org/repos/asf/stratos/blob/e195f2f1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/exceptions/NoStatisticsFoundException.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/exceptions/NoStatisticsFoundException.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/exceptions/NoStatisticsFoundException.java
new file mode 100644
index 0000000..940d24f
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/exceptions/NoStatisticsFoundException.java
@@ -0,0 +1,26 @@
+/*
+ * 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.service.exceptions;
+
+/**
+ * No statistics found exception.
+ */
+public class NoStatisticsFoundException extends Exception {
+}
http://git-wip-us.apache.org/repos/asf/stratos/blob/e195f2f1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/exceptions/StopStatisticsPublishingException.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/exceptions/StopStatisticsPublishingException.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/exceptions/StopStatisticsPublishingException.java
new file mode 100644
index 0000000..d31177b
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/exceptions/StopStatisticsPublishingException.java
@@ -0,0 +1,26 @@
+/*
+ * 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.service.exceptions;
+
+/**
+ * Stop statistics publishing exception.
+ */
+public class StopStatisticsPublishingException extends Exception {
+}
http://git-wip-us.apache.org/repos/asf/stratos/blob/e195f2f1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/statistics/MockHealthStatistics.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/statistics/MockHealthStatistics.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/statistics/MockHealthStatistics.java
new file mode 100644
index 0000000..d782166
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/statistics/MockHealthStatistics.java
@@ -0,0 +1,122 @@
+/*
+ * 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.service.statistics;
+
+import org.apache.stratos.cloud.controller.iaases.mock.service.MockAutoscalingFactor;
+import org.apache.stratos.cloud.controller.iaases.mock.service.exceptions.NoStatisticsFoundException;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Mock health statistics singleton class.
+ */
+public class MockHealthStatistics {
+ private final static int DEFAULT_MEMORY_CONSUMPTION = 20;
+ private final static int DEFAULT_LOAD_AVERAGE = 20;
+ private final static int DEFAULT_REQUESTS_IN_FLIGHT = 1;
+
+ private static volatile MockHealthStatistics instance;
+
+ private Map<String, Map<String, Integer>> statisticsMap;
+
+ private MockHealthStatistics() {
+ statisticsMap = new ConcurrentHashMap<String, Map<String, Integer>>();
+ }
+
+ public static MockHealthStatistics getInstance() {
+ if (instance == null) {
+ synchronized (MockHealthStatistics.class) {
+ if (instance == null) {
+ instance = new MockHealthStatistics();
+ }
+ }
+ }
+ return instance;
+ }
+
+ /**
+ * Add statistics value for a cartridge type, autoscaling factor
+ * @param cartridgeType
+ * @param autoscalingFactor
+ * @param value
+ */
+ public void addStatistics(String cartridgeType, MockAutoscalingFactor autoscalingFactor, Integer value) {
+ Map<String, Integer> factorValueMap = statisticsMap.get(cartridgeType);
+ if(factorValueMap == null) {
+ synchronized (MockHealthStatistics.class) {
+ if(factorValueMap == null) {
+ factorValueMap = new ConcurrentHashMap<String, Integer>();
+ statisticsMap.put(cartridgeType, factorValueMap);
+ }
+ }
+ }
+ factorValueMap.put(autoscalingFactor.toString(), value);
+ }
+
+ /**
+ * Returns current statistics of the given cartridge type, autoscaling factor
+ * @param cartridgeType
+ * @param autoscalingFactor
+ * @return
+ */
+ public int getStatistics(String cartridgeType, MockAutoscalingFactor autoscalingFactor) throws NoStatisticsFoundException {
+ Map<String, Integer> factorValueMap = statisticsMap.get(cartridgeType);
+ if(factorValueMap != null) {
+ if(factorValueMap.containsKey(autoscalingFactor.toString())) {
+ return factorValueMap.get(autoscalingFactor.toString());
+ } else {
+ throw new NoStatisticsFoundException();
+ }
+ }
+ // No statistics patterns found, return default
+ return findDefault(autoscalingFactor);
+ }
+
+ /**
+ * Remove statistics found for the cartridge type, autoscaling factor
+ * @param cartridgeType
+ * @param autoscalingFactor
+ */
+ public void removeStatistics(String cartridgeType, MockAutoscalingFactor autoscalingFactor) {
+ Map<String, Integer> factorValueMap = statisticsMap.get(cartridgeType);
+ if(factorValueMap != null) {
+ if(factorValueMap.containsKey(autoscalingFactor.toString())) {
+ factorValueMap.remove(autoscalingFactor.toString());
+ }
+ }
+ }
+
+ /**
+ * Find default statistics value of the given autoscaling factor
+ * @param autoscalingFactor
+ * @return
+ */
+ private int findDefault(MockAutoscalingFactor autoscalingFactor) {
+ if(autoscalingFactor == MockAutoscalingFactor.MemoryConsumption) {
+ return DEFAULT_MEMORY_CONSUMPTION;
+ } else if(autoscalingFactor == MockAutoscalingFactor.LoadAverage) {
+ return DEFAULT_LOAD_AVERAGE;
+ } else if(autoscalingFactor == MockAutoscalingFactor.RequestInFlight) {
+ return DEFAULT_REQUESTS_IN_FLIGHT;
+ }
+ throw new RuntimeException("An unknown autoscaling factor found: " + autoscalingFactor);
+ }
+}
http://git-wip-us.apache.org/repos/asf/stratos/blob/e195f2f1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/statistics/StatisticsPatternMode.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/statistics/StatisticsPatternMode.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/statistics/StatisticsPatternMode.java
new file mode 100644
index 0000000..f4a2b39
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/service/statistics/StatisticsPatternMode.java
@@ -0,0 +1,27 @@
+/*
+ * 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.service.statistics;
+
+/**
+ * Statistics pattern mode.
+ */
+public enum StatisticsPatternMode {
+ Loop, Continue, Stop
+}