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; |
tom | dc66b38 | 2014-09-22 17:05:47 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 18 | import com.google.common.io.ByteStreams; |
| 19 | import com.google.common.io.Files; |
tom | 1a2908c | 2014-09-23 16:37:39 -0700 | [diff] [blame] | 20 | import com.hazelcast.config.Config; |
| 21 | import com.hazelcast.config.FileSystemXmlConfig; |
tom | dc66b38 | 2014-09-22 17:05:47 -0700 | [diff] [blame] | 22 | import com.hazelcast.core.Hazelcast; |
| 23 | import com.hazelcast.core.HazelcastInstance; |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 24 | |
tom | dc66b38 | 2014-09-22 17:05:47 -0700 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Activate; |
| 26 | import org.apache.felix.scr.annotations.Component; |
| 27 | import org.apache.felix.scr.annotations.Deactivate; |
| 28 | import org.apache.felix.scr.annotations.Service; |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 29 | import org.onosproject.store.cluster.impl.ClusterDefinitionManager; |
tom | dc66b38 | 2014-09-22 17:05:47 -0700 | [diff] [blame] | 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 33 | import java.io.File; |
tom | 1a2908c | 2014-09-23 16:37:39 -0700 | [diff] [blame] | 34 | import java.io.FileNotFoundException; |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 35 | import java.io.IOException; |
| 36 | import java.io.InputStream; |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 37 | |
tom | dc66b38 | 2014-09-22 17:05:47 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Auxiliary bootstrap of distributed store. |
| 40 | */ |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 41 | @Component(immediate = false, enabled = false) |
tom | dc66b38 | 2014-09-22 17:05:47 -0700 | [diff] [blame] | 42 | @Service |
| 43 | public class StoreManager implements StoreService { |
| 44 | |
Yuta HIGUCHI | b4139d8 | 2014-09-23 18:41:33 -0700 | [diff] [blame] | 45 | protected static final String HAZELCAST_XML_FILE = "etc/hazelcast.xml"; |
tom | 1a2908c | 2014-09-23 16:37:39 -0700 | [diff] [blame] | 46 | |
tom | dc66b38 | 2014-09-22 17:05:47 -0700 | [diff] [blame] | 47 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 48 | |
tom | 85ff08b | 2014-09-22 17:14:18 -0700 | [diff] [blame] | 49 | protected HazelcastInstance instance; |
tom | dc66b38 | 2014-09-22 17:05:47 -0700 | [diff] [blame] | 50 | |
| 51 | @Activate |
| 52 | public void activate() { |
tom | 1a2908c | 2014-09-23 16:37:39 -0700 | [diff] [blame] | 53 | try { |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 54 | File hazelcastFile = new File(HAZELCAST_XML_FILE); |
| 55 | if (!hazelcastFile.exists()) { |
| 56 | createDefaultHazelcastFile(hazelcastFile); |
| 57 | } |
| 58 | |
tom | 1a2908c | 2014-09-23 16:37:39 -0700 | [diff] [blame] | 59 | Config config = new FileSystemXmlConfig(HAZELCAST_XML_FILE); |
Yuta HIGUCHI | 48ee992 | 2014-11-11 09:19:13 -0800 | [diff] [blame] | 60 | |
tom | 1a2908c | 2014-09-23 16:37:39 -0700 | [diff] [blame] | 61 | instance = Hazelcast.newHazelcastInstance(config); |
tom | 1a2908c | 2014-09-23 16:37:39 -0700 | [diff] [blame] | 62 | log.info("Started"); |
| 63 | } catch (FileNotFoundException e) { |
| 64 | log.error("Unable to configure Hazelcast", e); |
| 65 | } |
tom | dc66b38 | 2014-09-22 17:05:47 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 68 | private void createDefaultHazelcastFile(File hazelcastFile) { |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 69 | String ip = ClusterDefinitionManager.getSiteLocalAddress(); |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 70 | String ipPrefix = ip.replaceFirst("\\.[0-9]*$", ".*"); |
| 71 | InputStream his = getClass().getResourceAsStream("/hazelcast.xml"); |
| 72 | try { |
| 73 | String hzCfg = new String(ByteStreams.toByteArray(his), "UTF-8"); |
| 74 | hzCfg = hzCfg.replaceFirst("@NAME", ip); |
| 75 | hzCfg = hzCfg.replaceFirst("@PREFIX", ipPrefix); |
| 76 | Files.write(hzCfg.getBytes("UTF-8"), hazelcastFile); |
| 77 | } catch (IOException e) { |
| 78 | log.error("Unable to write default hazelcast file", e); |
| 79 | } |
| 80 | } |
| 81 | |
tom | dc66b38 | 2014-09-22 17:05:47 -0700 | [diff] [blame] | 82 | @Deactivate |
| 83 | public void deactivate() { |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 84 | instance.shutdown(); |
tom | dc66b38 | 2014-09-22 17:05:47 -0700 | [diff] [blame] | 85 | log.info("Stopped"); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public HazelcastInstance getHazelcastInstance() { |
| 90 | return instance; |
| 91 | } |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 92 | |
tom | dc66b38 | 2014-09-22 17:05:47 -0700 | [diff] [blame] | 93 | } |