Jordan Halterman | 84dd47d | 2018-03-12 13:56:57 -0700 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import java.util.Map; |
| 19 | import java.util.function.Function; |
| 20 | |
| 21 | import com.google.common.collect.Maps; |
| 22 | import org.onosproject.store.primitives.DistributedPrimitiveCreator; |
| 23 | import org.onosproject.store.service.AsyncAtomicCounter; |
| 24 | import org.onosproject.store.service.AsyncAtomicIdGenerator; |
| 25 | import org.onosproject.store.service.AsyncAtomicValue; |
| 26 | import org.onosproject.store.service.AsyncConsistentMultimap; |
| 27 | import org.onosproject.store.service.AsyncConsistentTreeMap; |
| 28 | import org.onosproject.store.service.AsyncDocumentTree; |
| 29 | import org.onosproject.store.service.DistributedPrimitive; |
| 30 | import org.onosproject.store.service.Serializer; |
| 31 | import org.onosproject.store.service.Topic; |
| 32 | import org.onosproject.store.service.WorkQueue; |
| 33 | |
| 34 | /** |
| 35 | * Primitive instance manager. |
| 36 | */ |
| 37 | public class DistributedPrimitiveManager { |
| 38 | private final Map<String, DistributedPrimitive> primitives = Maps.newConcurrentMap(); |
| 39 | private final DistributedPrimitiveCreator primitiveCreator; |
| 40 | |
| 41 | DistributedPrimitiveManager(DistributedPrimitiveCreator primitiveCreator) { |
| 42 | this.primitiveCreator = primitiveCreator; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Returns a cached primitive instance. |
| 47 | * |
| 48 | * @param name the primitive name |
| 49 | * @param factory the primitive factory |
| 50 | * @param <T> the primitive type |
| 51 | * @return the primitive instance |
| 52 | */ |
| 53 | @SuppressWarnings("unchecked") |
| 54 | private <T extends DistributedPrimitive> T getPrimitive(String name, Function<String, T> factory) { |
| 55 | return (T) primitives.computeIfAbsent(name, factory); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Returns an instance of {@code AsyncAtomicCounter} with specified name. |
| 60 | * @param name counter name |
| 61 | * |
| 62 | * @return AsyncAtomicCounter instance |
| 63 | */ |
| 64 | public AsyncAtomicCounter getAsyncAtomicCounter(String name) { |
| 65 | return getPrimitive(name, primitiveCreator::newAsyncCounter); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns an instance of {@code AsyncAtomicIdGenerator} with specified name. |
| 70 | * |
| 71 | * @param name ID generator name |
| 72 | * @return AsyncAtomicIdGenerator instance |
| 73 | */ |
| 74 | public AsyncAtomicIdGenerator getAsyncAtomicIdGenerator(String name) { |
| 75 | return getPrimitive(name, primitiveCreator::newAsyncIdGenerator); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Returns an instance of {@code WorkQueue} with specified name. |
| 80 | * |
| 81 | * @param <E> work element type |
| 82 | * @param name work queue name |
| 83 | * @param serializer serializer |
| 84 | * @return WorkQueue instance |
| 85 | */ |
| 86 | public <E> WorkQueue<E> getWorkQueue(String name, Serializer serializer) { |
| 87 | return getPrimitive(name, n -> primitiveCreator.newWorkQueue(n, serializer)); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns an instance of {@code AsyncDocumentTree} with specified name. |
| 92 | * |
| 93 | * @param <V> tree node value type |
| 94 | * @param name document tree name |
| 95 | * @param serializer serializer |
| 96 | * @return AsyncDocumentTree instance |
| 97 | */ |
| 98 | public <V> AsyncDocumentTree<V> getDocumentTree(String name, Serializer serializer) { |
| 99 | return getPrimitive(name, n -> primitiveCreator.newAsyncDocumentTree(n, serializer)); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns a set backed instance of {@code AsyncConsistentMultimap} with |
| 104 | * the specified name. |
| 105 | * |
| 106 | * @param name the multimap name |
| 107 | * @param serializer serializer |
| 108 | * @param <K> key type |
| 109 | * @param <V> value type |
| 110 | * @return set backed {@code AsyncConsistentMultimap} instance |
| 111 | */ |
| 112 | public <K, V> AsyncConsistentMultimap<K, V> getAsyncSetMultimap(String name, Serializer serializer) { |
| 113 | return getPrimitive(name, n -> primitiveCreator.newAsyncConsistentSetMultimap(n, serializer)); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Returns an instance of {@code AsyncConsistentTreeMap} with the specified |
| 118 | * name. |
| 119 | * |
| 120 | * @param name the treemap name |
| 121 | * @param serializer serializer |
| 122 | * @param <V> value type |
| 123 | * @return set backed {@code AsyncConsistentTreeMap} instance |
| 124 | */ |
| 125 | public <V> AsyncConsistentTreeMap<V> getAsyncTreeMap(String name, Serializer serializer) { |
| 126 | return getPrimitive(name, n -> primitiveCreator.newAsyncConsistentTreeMap(n, serializer)); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Returns an instance of {@code Topic} with specified name. |
| 131 | * |
| 132 | * @param <T> topic message type |
| 133 | * @param name topic name |
| 134 | * @param serializer serializer |
| 135 | * |
| 136 | * @return Topic instance |
| 137 | */ |
| 138 | public <T> Topic<T> getTopic(String name, Serializer serializer) { |
| 139 | AsyncAtomicValue<T> atomicValue = |
| 140 | getPrimitive(name, n -> primitiveCreator.newAsyncAtomicValue("topic-" + n, serializer)); |
| 141 | return new DefaultDistributedTopic<>(atomicValue); |
| 142 | } |
| 143 | } |