Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.hz; |
Yuta HIGUCHI | b4139d8 | 2014-09-23 18:41:33 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | 151cad8 | 2015-02-04 23:26:50 -0800 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | import static com.google.common.base.Preconditions.checkState; |
| 20 | |
Yuta HIGUCHI | b4139d8 | 2014-09-23 18:41:33 -0700 | [diff] [blame] | 21 | import java.io.FileNotFoundException; |
| 22 | import java.util.UUID; |
| 23 | |
| 24 | import com.hazelcast.config.Config; |
| 25 | import com.hazelcast.config.FileSystemXmlConfig; |
| 26 | import com.hazelcast.core.HazelcastInstance; |
Yuta HIGUCHI | 151cad8 | 2015-02-04 23:26:50 -0800 | [diff] [blame] | 27 | import com.hazelcast.test.TestHazelcastInstanceFactory; |
Yuta HIGUCHI | b4139d8 | 2014-09-23 18:41:33 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Dummy StoreManager to use specified Hazelcast instance. |
| 31 | */ |
| 32 | public class TestStoreManager extends StoreManager { |
| 33 | |
Yuta HIGUCHI | 151cad8 | 2015-02-04 23:26:50 -0800 | [diff] [blame] | 34 | private TestHazelcastInstanceFactory factory; |
| 35 | |
Yuta HIGUCHI | b4139d8 | 2014-09-23 18:41:33 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Gets the Hazelcast Config for testing. |
| 38 | * |
Yuta HIGUCHI | 151cad8 | 2015-02-04 23:26:50 -0800 | [diff] [blame] | 39 | * @return Hazelcast Configuration for testing |
Yuta HIGUCHI | b4139d8 | 2014-09-23 18:41:33 -0700 | [diff] [blame] | 40 | */ |
| 41 | public static Config getTestConfig() { |
| 42 | Config config; |
| 43 | try { |
| 44 | config = new FileSystemXmlConfig(HAZELCAST_XML_FILE); |
| 45 | } catch (FileNotFoundException e) { |
| 46 | // falling back to default |
| 47 | config = new Config(); |
| 48 | } |
| 49 | // avoid accidentally joining other cluster |
| 50 | config.getGroupConfig().setName(UUID.randomUUID().toString()); |
| 51 | // quickly form single node cluster |
| 52 | config.getNetworkConfig().getJoin() |
| 53 | .getTcpIpConfig() |
| 54 | .setEnabled(true).setConnectionTimeoutSeconds(0); |
| 55 | config.getNetworkConfig().getJoin() |
| 56 | .getMulticastConfig() |
| 57 | .setEnabled(false); |
| 58 | return config; |
| 59 | } |
| 60 | |
| 61 | /** |
Yuta HIGUCHI | 151cad8 | 2015-02-04 23:26:50 -0800 | [diff] [blame] | 62 | * Creates an instance of dummy Hazelcast instance for testing. |
| 63 | * |
| 64 | * @return HazelcastInstance |
| 65 | */ |
| 66 | public HazelcastInstance initSingleInstance() { |
| 67 | return initInstances(1)[0]; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Creates some instances of dummy Hazelcast instances for testing. |
| 72 | * |
| 73 | * @param count number of instances to create |
| 74 | * @return array of HazelcastInstances |
| 75 | */ |
| 76 | public HazelcastInstance[] initInstances(int count) { |
| 77 | checkArgument(count > 0, "Cluster size must be > 0"); |
| 78 | factory = new TestHazelcastInstanceFactory(count); |
| 79 | return factory.newInstances(getTestConfig()); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Sets the Hazelast instance to return on #getHazelcastInstance(). |
Yuta HIGUCHI | b4139d8 | 2014-09-23 18:41:33 -0700 | [diff] [blame] | 84 | * |
| 85 | * @param instance Hazelast instance to return on #getHazelcastInstance() |
| 86 | */ |
Yuta HIGUCHI | 151cad8 | 2015-02-04 23:26:50 -0800 | [diff] [blame] | 87 | public void setHazelcastInstance(HazelcastInstance instance) { |
Yuta HIGUCHI | b4139d8 | 2014-09-23 18:41:33 -0700 | [diff] [blame] | 88 | this.instance = instance; |
| 89 | } |
| 90 | |
Yuta HIGUCHI | b4139d8 | 2014-09-23 18:41:33 -0700 | [diff] [blame] | 91 | @Override |
| 92 | public void activate() { |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 93 | // Hazelcast setup removed from original code. |
Yuta HIGUCHI | 151cad8 | 2015-02-04 23:26:50 -0800 | [diff] [blame] | 94 | checkState(this.instance != null, "HazelcastInstance needs to be set"); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void deactivate() { |
| 99 | // Hazelcast instance shutdown removed from original code. |
| 100 | factory.shutdownAll(); |
Yuta HIGUCHI | b4139d8 | 2014-09-23 18:41:33 -0700 | [diff] [blame] | 101 | } |
| 102 | } |