Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 1 | /* |
Thomas Vachuska | b6d3167 | 2018-07-27 17:03:46 -0700 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [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 | */ |
| 16 | |
Thomas Vachuska | b6d3167 | 2018-07-27 17:03:46 -0700 | [diff] [blame] | 17 | package org.onosproject.store.atomix.primitives.impl; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 18 | |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 19 | import io.atomix.cluster.MemberId; |
| 20 | import io.atomix.primitive.partition.PartitionGroup; |
| 21 | import io.atomix.protocols.raft.MultiRaftProtocol; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 22 | import org.onosproject.cluster.NodeId; |
| 23 | import org.onosproject.cluster.PartitionId; |
| 24 | import org.onosproject.event.AbstractListenerManager; |
Thomas Vachuska | b6d3167 | 2018-07-27 17:03:46 -0700 | [diff] [blame] | 25 | import org.onosproject.store.atomix.impl.AtomixManager; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 26 | import org.onosproject.store.primitives.PartitionAdminService; |
| 27 | import org.onosproject.store.primitives.PartitionEvent; |
| 28 | import org.onosproject.store.primitives.PartitionEventListener; |
| 29 | import org.onosproject.store.primitives.PartitionService; |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 30 | import org.onosproject.store.service.PartitionClientInfo; |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 31 | import org.onosproject.store.service.PartitionInfo; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 32 | import org.osgi.service.component.annotations.Activate; |
| 33 | import org.osgi.service.component.annotations.Component; |
| 34 | import org.osgi.service.component.annotations.Deactivate; |
| 35 | import org.osgi.service.component.annotations.Reference; |
| 36 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 39 | import java.util.List; |
| 40 | import java.util.Set; |
| 41 | import java.util.stream.Collectors; |
| 42 | |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 43 | import static org.onosproject.security.AppGuard.checkPermission; |
| 44 | import static org.onosproject.security.AppPermission.Type.PARTITION_READ; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 45 | import static org.slf4j.LoggerFactory.getLogger; |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 46 | |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 47 | /** |
| 48 | * Implementation of {@code PartitionService} and {@code PartitionAdminService}. |
| 49 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 50 | @Component(service = { PartitionService.class, PartitionAdminService.class }) |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 51 | public class PartitionManager extends AbstractListenerManager<PartitionEvent, PartitionEventListener> |
| 52 | implements PartitionService, PartitionAdminService { |
| 53 | |
| 54 | private final Logger log = getLogger(getClass()); |
| 55 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 57 | protected AtomixManager atomixManager; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 58 | |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 59 | private PartitionGroup partitionGroup; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 60 | |
| 61 | @Activate |
| 62 | public void activate() { |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 63 | partitionGroup = atomixManager.getAtomix().getPartitionService().getPartitionGroup(MultiRaftProtocol.TYPE); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 64 | eventDispatcher.addSink(PartitionEvent.class, listenerRegistry); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 65 | log.info("Started"); |
| 66 | } |
| 67 | |
Madan Jampani | 86cb243 | 2016-02-17 11:07:56 -0800 | [diff] [blame] | 68 | @Deactivate |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 69 | public void deactivate() { |
| 70 | eventDispatcher.removeSink(PartitionEvent.class); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 71 | log.info("Stopped"); |
| 72 | } |
| 73 | |
| 74 | @Override |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 75 | public int getNumberOfPartitions() { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 76 | checkPermission(PARTITION_READ); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 77 | return partitionGroup.getPartitions().size(); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public Set<PartitionId> getAllPartitionIds() { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 82 | checkPermission(PARTITION_READ); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 83 | return partitionGroup.getPartitionIds().stream() |
| 84 | .map(partitionId -> PartitionId.from(partitionId.id())) |
| 85 | .collect(Collectors.toSet()); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 89 | public Set<NodeId> getConfiguredMembers(PartitionId partitionId) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 90 | checkPermission(PARTITION_READ); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 91 | io.atomix.primitive.partition.PartitionId atomixPartitionId = |
| 92 | io.atomix.primitive.partition.PartitionId.from(partitionGroup.name(), partitionId.id()); |
| 93 | return partitionGroup.getPartition(atomixPartitionId).members() |
| 94 | .stream() |
| 95 | .map(member -> NodeId.nodeId(member.id())) |
| 96 | .collect(Collectors.toSet()); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 100 | public List<PartitionInfo> partitionInfo() { |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 101 | checkPermission(PARTITION_READ); |
| 102 | return partitionGroup.getPartitions() |
| 103 | .stream() |
| 104 | .map(partition -> { |
| 105 | MemberId primary = partition.primary(); |
| 106 | return new PartitionInfo( |
| 107 | PartitionId.from(partition.id().id()), |
| 108 | partition.term(), |
| 109 | partition.members().stream().map(member -> member.id()).collect(Collectors.toList()), |
| 110 | primary != null ? primary.id() : null); |
| 111 | }) |
| 112 | .collect(Collectors.toList()); |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 113 | } |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 114 | |
| 115 | @Override |
| 116 | public List<PartitionClientInfo> partitionClientInfo() { |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 117 | checkPermission(PARTITION_READ); |
| 118 | return partitionGroup.getPartitions() |
| 119 | .stream() |
| 120 | .map(partition -> new PartitionClientInfo( |
| 121 | PartitionId.from(partition.id().id()), |
| 122 | partition.members().stream() |
| 123 | .map(member -> NodeId.nodeId(member.id())) |
| 124 | .collect(Collectors.toList()))) |
| 125 | .collect(Collectors.toList()); |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 126 | } |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 127 | } |