blob: 14e1d9689f7b3d9a983af114d7d03331b84d77ab [file] [log] [blame]
Madan Jampani15b8ef52016-02-02 17:35:05 -08001/*
2 * Copyright 2016 Open Networking Laboratory
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 */
16
17package org.onosproject.store.primitives.impl;
18
19import static org.slf4j.LoggerFactory.getLogger;
20
21import java.io.File;
Madan Jampanie14a09c2016-02-11 10:43:21 -080022import java.util.List;
Madan Jampani15b8ef52016-02-02 17:35:05 -080023import java.util.Map;
Madan Jampanie14a09c2016-02-11 10:43:21 -080024import java.util.Optional;
Madan Jampani15b8ef52016-02-02 17:35:05 -080025import java.util.Set;
26import java.util.concurrent.CompletableFuture;
Madan Jampanie14a09c2016-02-11 10:43:21 -080027import java.util.stream.Collectors;
Madan Jampani15b8ef52016-02-02 17:35:05 -080028
29import org.apache.felix.scr.annotations.Activate;
30import org.apache.felix.scr.annotations.Component;
31import org.apache.felix.scr.annotations.Reference;
32import org.apache.felix.scr.annotations.ReferenceCardinality;
33import org.apache.felix.scr.annotations.Service;
34import org.onlab.util.Tools;
35import org.onosproject.cluster.ClusterMetadataService;
36import org.onosproject.cluster.ClusterService;
37import org.onosproject.cluster.NodeId;
38import org.onosproject.cluster.PartitionId;
39import org.onosproject.event.AbstractListenerManager;
40import org.onosproject.store.cluster.messaging.MessagingService;
41import org.onosproject.store.primitives.DistributedPrimitiveCreator;
42import org.onosproject.store.primitives.PartitionAdminService;
43import org.onosproject.store.primitives.PartitionEvent;
44import org.onosproject.store.primitives.PartitionEventListener;
45import org.onosproject.store.primitives.PartitionService;
Madan Jampanie14a09c2016-02-11 10:43:21 -080046import org.onosproject.store.service.PartitionInfo;
Madan Jampani15b8ef52016-02-02 17:35:05 -080047import org.slf4j.Logger;
48
49import com.google.common.collect.ImmutableSet;
50import com.google.common.collect.Maps;
51
52/**
53 * Implementation of {@code PartitionService} and {@code PartitionAdminService}.
54 */
55@Component
56@Service
57public class PartitionManager extends AbstractListenerManager<PartitionEvent, PartitionEventListener>
58 implements PartitionService, PartitionAdminService {
59
60 private final Logger log = getLogger(getClass());
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected MessagingService messagingService;
64
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected ClusterMetadataService metadataService;
67
68 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected ClusterService clusterService;
70
71 Map<PartitionId, StoragePartition> partitions = Maps.newConcurrentMap();
72
73 @Activate
74 public void activate() {
75 eventDispatcher.addSink(PartitionEvent.class, listenerRegistry);
76
77 metadataService.getClusterMetadata()
78 .getPartitions()
79 .stream()
80 .forEach(partition -> partitions.put(partition.getId(), new StoragePartition(partition,
81 messagingService,
82 clusterService,
83 CatalystSerializers.getSerializer(),
84 new File(System.getProperty("karaf.data") + "/data/" + partition.getId()))));
85
86 CompletableFuture<Void> openFuture = CompletableFuture.allOf(partitions.values()
87 .stream()
88 .map(StoragePartition::open)
89 .toArray(CompletableFuture[]::new));
90 openFuture.join();
91 log.info("Started");
92 }
93
94 public void deactivate() {
95 eventDispatcher.removeSink(PartitionEvent.class);
96
97 CompletableFuture<Void> closeFuture = CompletableFuture.allOf(partitions.values()
98 .stream()
99 .map(StoragePartition::close)
100 .toArray(CompletableFuture[]::new));
101 closeFuture.join();
102 log.info("Stopped");
103 }
104
105 @Override
106 public CompletableFuture<Void> leave(PartitionId partitionId) {
107 // TODO: Implement
108 return Tools.exceptionalFuture(new UnsupportedOperationException());
109 }
110
111 @Override
112 public CompletableFuture<Void> join(PartitionId partitionId) {
113 // TODO: Implement
114 return Tools.exceptionalFuture(new UnsupportedOperationException());
115 }
116
117 @Override
118 public int getNumberOfPartitions() {
119 return partitions.size();
120 }
121
122 @Override
123 public Set<PartitionId> getAllPartitionIds() {
124 return partitions.keySet();
125 }
126
127 @Override
128 public DistributedPrimitiveCreator getDistributedPrimitiveCreator(PartitionId partitionId) {
129 return partitions.get(partitionId).client();
130 }
131
132 @Override
133 public Set<NodeId> getConfiguredMembers(PartitionId partitionId) {
134 StoragePartition partition = partitions.get(partitionId);
135 return ImmutableSet.copyOf(partition.getMembers());
136 }
137
138 @Override
139 public Set<NodeId> getActiveMembersMembers(PartitionId partitionId) {
140 // TODO: This needs to query metadata to determine currently active
141 // members of partition
142 return getConfiguredMembers(partitionId);
143 }
Madan Jampanie14a09c2016-02-11 10:43:21 -0800144
145 @Override
146 public List<PartitionInfo> partitionInfo() {
147 return partitions.values()
148 .stream()
149 .map(StoragePartition::info)
150 .filter(Optional::isPresent)
151 .map(Optional::get)
152 .collect(Collectors.toList());
153 }
Madan Jampani15b8ef52016-02-02 17:35:05 -0800154}