blob: 5cb57577cdd918564e834f2c9575e4d28c19e934 [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;
tomdc66b382014-09-22 17:05:47 -070017
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070018import com.google.common.io.ByteStreams;
19import com.google.common.io.Files;
tom1a2908c2014-09-23 16:37:39 -070020import com.hazelcast.config.Config;
21import com.hazelcast.config.FileSystemXmlConfig;
tomdc66b382014-09-22 17:05:47 -070022import com.hazelcast.core.Hazelcast;
23import com.hazelcast.core.HazelcastInstance;
Yuta HIGUCHI497c8842014-09-25 14:23:34 -070024
tomdc66b382014-09-22 17:05:47 -070025import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Service;
Madan Jampaniafeebbd2015-05-19 15:26:01 -070029import org.onosproject.store.cluster.impl.ClusterDefinitionManager;
tomdc66b382014-09-22 17:05:47 -070030import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070033import java.io.File;
tom1a2908c2014-09-23 16:37:39 -070034import java.io.FileNotFoundException;
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070035import java.io.IOException;
36import java.io.InputStream;
tom0872a172014-09-23 11:24:26 -070037
tomdc66b382014-09-22 17:05:47 -070038/**
39 * Auxiliary bootstrap of distributed store.
40 */
Madan Jampani86940d92015-05-06 11:47:57 -070041@Component(immediate = false, enabled = false)
tomdc66b382014-09-22 17:05:47 -070042@Service
43public class StoreManager implements StoreService {
44
Yuta HIGUCHIb4139d82014-09-23 18:41:33 -070045 protected static final String HAZELCAST_XML_FILE = "etc/hazelcast.xml";
tom1a2908c2014-09-23 16:37:39 -070046
tomdc66b382014-09-22 17:05:47 -070047 private final Logger log = LoggerFactory.getLogger(getClass());
48
tom85ff08b2014-09-22 17:14:18 -070049 protected HazelcastInstance instance;
tomdc66b382014-09-22 17:05:47 -070050
51 @Activate
52 public void activate() {
tom1a2908c2014-09-23 16:37:39 -070053 try {
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070054 File hazelcastFile = new File(HAZELCAST_XML_FILE);
55 if (!hazelcastFile.exists()) {
56 createDefaultHazelcastFile(hazelcastFile);
57 }
58
tom1a2908c2014-09-23 16:37:39 -070059 Config config = new FileSystemXmlConfig(HAZELCAST_XML_FILE);
Yuta HIGUCHI48ee9922014-11-11 09:19:13 -080060
tom1a2908c2014-09-23 16:37:39 -070061 instance = Hazelcast.newHazelcastInstance(config);
tom1a2908c2014-09-23 16:37:39 -070062 log.info("Started");
63 } catch (FileNotFoundException e) {
64 log.error("Unable to configure Hazelcast", e);
65 }
tomdc66b382014-09-22 17:05:47 -070066 }
67
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070068 private void createDefaultHazelcastFile(File hazelcastFile) {
Madan Jampaniafeebbd2015-05-19 15:26:01 -070069 String ip = ClusterDefinitionManager.getSiteLocalAddress();
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070070 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
tomdc66b382014-09-22 17:05:47 -070082 @Deactivate
83 public void deactivate() {
tom0872a172014-09-23 11:24:26 -070084 instance.shutdown();
tomdc66b382014-09-22 17:05:47 -070085 log.info("Stopped");
86 }
87
88 @Override
89 public HazelcastInstance getHazelcastInstance() {
90 return instance;
91 }
tom0872a172014-09-23 11:24:26 -070092
tomdc66b382014-09-22 17:05:47 -070093}