blob: 43cde78840ce0b20f6cb161cd57727c892e52b3a [file] [log] [blame]
Jordan Halterman00e92da2018-05-22 23:05:52 -07001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
16package org.onosproject.store.impl;
17
18import java.util.stream.Collectors;
19
20import io.atomix.cluster.discovery.BootstrapDiscoveryProvider;
21import io.atomix.core.Atomix;
22import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
27import org.apache.felix.scr.annotations.Service;
28import org.onosproject.cluster.ClusterMetadataService;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32/**
33 * Atomix manager.
34 */
35@Component(immediate = true)
36@Service(value = AtomixManager.class)
37public class AtomixManager {
38
39 private final Logger log = LoggerFactory.getLogger(getClass());
40
41 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
42 protected ClusterMetadataService metadataService;
43
44 private Atomix atomix;
45
46 /**
47 * Returns the Atomix instance.
48 *
49 * @return the Atomix instance
50 */
51 public Atomix getAtomix() {
52 return atomix;
53 }
54
55 @Activate
56 public void activate() {
57 atomix = createAtomix();
58 atomix.start().join();
59 log.info("Started");
60 }
61
62 @Deactivate
63 public void deactivate() {
64 atomix.stop().join();
65 log.info("Stopped");
66 }
67
68 private Atomix createAtomix() {
69 return Atomix.builder(getClass().getClassLoader())
70 .withClusterId(metadataService.getClusterMetadata().getName())
71 .withMemberId(metadataService.getLocalNode().id().id())
72 .withAddress(metadataService.getLocalNode().ip().toString(), metadataService.getLocalNode().tcpPort())
73 .withProperty("type", "onos")
74 .withMembershipProvider(BootstrapDiscoveryProvider.builder()
75 .withNodes(metadataService.getClusterMetadata().getStorageNodes().stream()
76 .map(node -> io.atomix.cluster.Node.builder()
77 .withId(node.id().id())
78 .withAddress(node.ip().toString(), node.tcpPort())
79 .build())
80 .collect(Collectors.toList()))
81 .build())
82 .build();
83 }
84}