Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import java.io.File; |
| 19 | import java.util.function.Supplier; |
| 20 | import java.util.stream.Collectors; |
| 21 | |
| 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.apache.felix.scr.annotations.Service; |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 28 | import org.onosproject.cluster.ClusterService; |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 29 | import org.onosproject.cluster.ControllerNode; |
| 30 | import org.onosproject.cluster.DefaultPartition; |
| 31 | import org.onosproject.cluster.PartitionId; |
| 32 | import org.onosproject.persistence.PersistenceService; |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 33 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 34 | import org.onosproject.store.primitives.DistributedPrimitiveCreator; |
| 35 | import org.onosproject.store.serializers.KryoNamespaces; |
| 36 | import org.onosproject.store.service.AsyncAtomicValue; |
| 37 | import org.onosproject.store.service.AsyncConsistentMultimap; |
| 38 | import org.onosproject.store.service.AsyncConsistentTreeMap; |
| 39 | import org.onosproject.store.service.AsyncDocumentTree; |
| 40 | import org.onosproject.store.service.AtomicCounterBuilder; |
| 41 | import org.onosproject.store.service.AtomicCounterMapBuilder; |
| 42 | import org.onosproject.store.service.AtomicIdGeneratorBuilder; |
| 43 | import org.onosproject.store.service.AtomicValueBuilder; |
| 44 | import org.onosproject.store.service.ConsistentMapBuilder; |
| 45 | import org.onosproject.store.service.ConsistentMultimapBuilder; |
| 46 | import org.onosproject.store.service.ConsistentTreeMapBuilder; |
| 47 | import org.onosproject.store.service.CoordinationService; |
| 48 | import org.onosproject.store.service.DistributedSetBuilder; |
| 49 | import org.onosproject.store.service.DocumentTreeBuilder; |
| 50 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; |
| 51 | import org.onosproject.store.service.LeaderElectorBuilder; |
| 52 | import org.onosproject.store.service.Serializer; |
| 53 | import org.onosproject.store.service.Topic; |
| 54 | import org.onosproject.store.service.TransactionContextBuilder; |
| 55 | import org.onosproject.store.service.WorkQueue; |
| 56 | import org.slf4j.Logger; |
| 57 | |
| 58 | import static org.onosproject.security.AppGuard.checkPermission; |
| 59 | import static org.onosproject.security.AppPermission.Type.STORAGE_WRITE; |
Thomas Vachuska | 58bf491 | 2017-10-31 12:00:32 -0700 | [diff] [blame^] | 60 | import static org.onosproject.store.primitives.impl.PartitionManager.PARTITIONS_DIR; |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 61 | import static org.slf4j.LoggerFactory.getLogger; |
| 62 | |
| 63 | /** |
| 64 | * Implementation of {@code CoordinationService} that uses a {@link StoragePartition} that spans all the nodes |
| 65 | * in the cluster regardless of version. |
| 66 | */ |
| 67 | @Service |
| 68 | @Component(immediate = true) |
| 69 | public class CoordinationManager implements CoordinationService { |
| 70 | |
| 71 | private final Logger log = getLogger(getClass()); |
| 72 | |
| 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 74 | protected ClusterService clusterService; |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 75 | |
| 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 77 | protected ClusterCommunicationService clusterCommunicator; |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 78 | |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 80 | protected PersistenceService persistenceService; |
| 81 | |
| 82 | private StoragePartition partition; |
| 83 | private DistributedPrimitiveCreator primitiveCreator; |
| 84 | |
| 85 | @Activate |
| 86 | public void activate() { |
| 87 | partition = new StoragePartition( |
| 88 | new DefaultPartition( |
| 89 | PartitionId.SHARED, |
| 90 | clusterService.getNodes() |
| 91 | .stream() |
| 92 | .map(ControllerNode::id) |
| 93 | .collect(Collectors.toSet())), |
| 94 | null, |
| 95 | null, |
| 96 | clusterCommunicator, |
| 97 | clusterService, |
Thomas Vachuska | 58bf491 | 2017-10-31 12:00:32 -0700 | [diff] [blame^] | 98 | new File(PARTITIONS_DIR + "/coordination")); |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 99 | partition.open().join(); |
| 100 | primitiveCreator = partition.client(); |
| 101 | log.info("Started"); |
| 102 | } |
| 103 | |
| 104 | @Deactivate |
| 105 | public void deactivate() { |
| 106 | log.info("Stopped"); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public <K, V> EventuallyConsistentMapBuilder<K, V> eventuallyConsistentMapBuilder() { |
| 111 | checkPermission(STORAGE_WRITE); |
| 112 | return new EventuallyConsistentMapBuilderImpl<>(clusterService, |
| 113 | clusterCommunicator, |
| 114 | persistenceService); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public <K, V> ConsistentMapBuilder<K, V> consistentMapBuilder() { |
| 119 | checkPermission(STORAGE_WRITE); |
| 120 | return new DefaultConsistentMapBuilder<>(primitiveCreator); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public <V> DocumentTreeBuilder<V> documentTreeBuilder() { |
| 125 | checkPermission(STORAGE_WRITE); |
| 126 | return new DefaultDocumentTreeBuilder<>(primitiveCreator); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public <V> ConsistentTreeMapBuilder<V> consistentTreeMapBuilder() { |
| 131 | return new DefaultConsistentTreeMapBuilder<>(primitiveCreator); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public <K, V> ConsistentMultimapBuilder<K, V> consistentMultimapBuilder() { |
| 136 | checkPermission(STORAGE_WRITE); |
| 137 | return new DefaultConsistentMultimapBuilder<>(primitiveCreator); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public <K> AtomicCounterMapBuilder<K> atomicCounterMapBuilder() { |
| 142 | checkPermission(STORAGE_WRITE); |
| 143 | return new DefaultAtomicCounterMapBuilder<>(primitiveCreator); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public <E> DistributedSetBuilder<E> setBuilder() { |
| 148 | checkPermission(STORAGE_WRITE); |
| 149 | return new DefaultDistributedSetBuilder<>(() -> this.<E, Boolean>consistentMapBuilder()); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public AtomicCounterBuilder atomicCounterBuilder() { |
| 154 | checkPermission(STORAGE_WRITE); |
| 155 | return new DefaultAtomicCounterBuilder(primitiveCreator); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public AtomicIdGeneratorBuilder atomicIdGeneratorBuilder() { |
| 160 | checkPermission(STORAGE_WRITE); |
| 161 | return new DefaultAtomicIdGeneratorBuilder(primitiveCreator); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public <V> AtomicValueBuilder<V> atomicValueBuilder() { |
| 166 | checkPermission(STORAGE_WRITE); |
| 167 | Supplier<ConsistentMapBuilder<String, byte[]>> mapBuilderSupplier = |
| 168 | () -> this.<String, byte[]>consistentMapBuilder() |
| 169 | .withName("onos-atomic-values") |
| 170 | .withSerializer(Serializer.using(KryoNamespaces.BASIC)); |
| 171 | return new DefaultAtomicValueBuilder<>(mapBuilderSupplier); |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public TransactionContextBuilder transactionContextBuilder() { |
| 176 | throw new UnsupportedOperationException(); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public LeaderElectorBuilder leaderElectorBuilder() { |
| 181 | checkPermission(STORAGE_WRITE); |
| 182 | return new DefaultLeaderElectorBuilder(primitiveCreator); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public <E> WorkQueue<E> getWorkQueue(String name, Serializer serializer) { |
| 187 | checkPermission(STORAGE_WRITE); |
| 188 | return primitiveCreator.newWorkQueue(name, serializer); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public <V> AsyncDocumentTree<V> getDocumentTree(String name, Serializer serializer) { |
| 193 | checkPermission(STORAGE_WRITE); |
| 194 | return primitiveCreator.newAsyncDocumentTree(name, serializer); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public <K, V> AsyncConsistentMultimap<K, V> getAsyncSetMultimap( |
| 199 | String name, Serializer serializer) { |
| 200 | checkPermission(STORAGE_WRITE); |
| 201 | return primitiveCreator.newAsyncConsistentSetMultimap(name, |
| 202 | serializer); |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public <V> AsyncConsistentTreeMap<V> getAsyncTreeMap( |
| 207 | String name, Serializer serializer) { |
| 208 | checkPermission(STORAGE_WRITE); |
| 209 | return primitiveCreator.newAsyncConsistentTreeMap(name, serializer); |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public <T> Topic<T> getTopic(String name, Serializer serializer) { |
| 214 | AsyncAtomicValue<T> atomicValue = this.<T>atomicValueBuilder() |
| 215 | .withName("topic-" + name) |
| 216 | .withSerializer(serializer) |
| 217 | .build(); |
| 218 | return new DefaultDistributedTopic<>(atomicValue); |
| 219 | } |
| 220 | } |