blob: 04dfb38f0f28c53a7c9e42dcdabb3e1f156b1480 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.hz;
Yuta HIGUCHIb4139d82014-09-23 18:41:33 -070017
Yuta HIGUCHI151cad82015-02-04 23:26:50 -080018import static com.google.common.base.Preconditions.checkArgument;
19import static com.google.common.base.Preconditions.checkState;
20
Yuta HIGUCHIb4139d82014-09-23 18:41:33 -070021import java.io.FileNotFoundException;
22import java.util.UUID;
23
24import com.hazelcast.config.Config;
25import com.hazelcast.config.FileSystemXmlConfig;
26import com.hazelcast.core.HazelcastInstance;
Yuta HIGUCHI151cad82015-02-04 23:26:50 -080027import com.hazelcast.test.TestHazelcastInstanceFactory;
Yuta HIGUCHIb4139d82014-09-23 18:41:33 -070028
29/**
30 * Dummy StoreManager to use specified Hazelcast instance.
31 */
32public class TestStoreManager extends StoreManager {
33
Yuta HIGUCHI151cad82015-02-04 23:26:50 -080034 private TestHazelcastInstanceFactory factory;
35
Yuta HIGUCHIb4139d82014-09-23 18:41:33 -070036 /**
37 * Gets the Hazelcast Config for testing.
38 *
Yuta HIGUCHI151cad82015-02-04 23:26:50 -080039 * @return Hazelcast Configuration for testing
Yuta HIGUCHIb4139d82014-09-23 18:41:33 -070040 */
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 HIGUCHI151cad82015-02-04 23:26:50 -080062 * 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 HIGUCHIb4139d82014-09-23 18:41:33 -070084 *
85 * @param instance Hazelast instance to return on #getHazelcastInstance()
86 */
Yuta HIGUCHI151cad82015-02-04 23:26:50 -080087 public void setHazelcastInstance(HazelcastInstance instance) {
Yuta HIGUCHIb4139d82014-09-23 18:41:33 -070088 this.instance = instance;
89 }
90
Yuta HIGUCHIb4139d82014-09-23 18:41:33 -070091 @Override
92 public void activate() {
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070093 // Hazelcast setup removed from original code.
Yuta HIGUCHI151cad82015-02-04 23:26:50 -080094 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 HIGUCHIb4139d82014-09-23 18:41:33 -0700101 }
102}