blob: 293deef71a7974294400866fc117eb020f54c6c0 [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 */
16package org.onosproject.store.primitives.impl;
17
18import io.atomix.Atomix;
19import io.atomix.AtomixClient;
20import io.atomix.catalyst.transport.Transport;
21import io.atomix.resource.ResourceType;
22import io.atomix.variables.DistributedLong;
23
24import java.util.Collection;
Madan Jampanie14a09c2016-02-11 10:43:21 -080025import java.util.Set;
Madan Jampani15b8ef52016-02-02 17:35:05 -080026import java.util.concurrent.CompletableFuture;
27
28import org.onlab.util.HexString;
29import org.onosproject.store.primitives.DistributedPrimitiveCreator;
30import org.onosproject.store.primitives.resources.impl.AtomixConsistentMap;
31import org.onosproject.store.primitives.resources.impl.AtomixCounter;
Madan Jampani39fff102016-02-14 13:17:28 -080032import org.onosproject.store.primitives.resources.impl.AtomixLeaderElector;
Madan Jampani15b8ef52016-02-02 17:35:05 -080033import org.onosproject.store.serializers.KryoNamespaces;
34import org.onosproject.store.service.AsyncAtomicCounter;
35import org.onosproject.store.service.AsyncAtomicValue;
36import org.onosproject.store.service.AsyncConsistentMap;
37import org.onosproject.store.service.AsyncDistributedSet;
38import org.onosproject.store.service.AsyncLeaderElector;
39import org.onosproject.store.service.DistributedQueue;
40import org.onosproject.store.service.Serializer;
41
42import com.google.common.base.Supplier;
43import com.google.common.base.Suppliers;
44import com.google.common.collect.ImmutableSet;
45
46/**
47 * StoragePartition client.
48 */
49public class StoragePartitionClient implements DistributedPrimitiveCreator, Managed<StoragePartitionClient> {
50
51 private final StoragePartition partition;
52 private final Transport transport;
53 private final io.atomix.catalyst.serializer.Serializer serializer;
54 private final Collection<ResourceType> resourceTypes;
55 private Atomix client;
56 private static final String ATOMIC_VALUES_CONSISTENT_MAP_NAME = "onos-atomic-values";
57 private final Supplier<AsyncConsistentMap<String, byte[]>> onosAtomicValuesMap =
58 Suppliers.memoize(() -> newAsyncConsistentMap(ATOMIC_VALUES_CONSISTENT_MAP_NAME,
59 Serializer.using(KryoNamespaces.BASIC)));
60
61 public StoragePartitionClient(StoragePartition partition,
62 io.atomix.catalyst.serializer.Serializer serializer,
63 Transport transport,
64 Collection<ResourceType> resourceTypes) {
65 this.partition = partition;
66 this.serializer = serializer;
67 this.transport = transport;
68 this.resourceTypes = ImmutableSet.copyOf(resourceTypes);
69 }
70
71 @Override
72 public CompletableFuture<Void> open() {
73 if (client != null && client.isOpen()) {
74 return CompletableFuture.completedFuture(null);
75 }
76 synchronized (StoragePartitionClient.this) {
77 client = AtomixClient.builder(partition.getMemberAddresses())
78 .withSerializer(serializer.clone())
79 .withResourceResolver(r -> {
80 resourceTypes.forEach(r::register);
81 })
82 .withTransport(transport)
83 .build();
84 }
85 return client.open().thenApply(v -> null);
86 }
87
88 @Override
89 public CompletableFuture<Void> close() {
90 return client != null ? client.close() : CompletableFuture.completedFuture(null);
91 }
92
93 @Override
94 public <K, V> AsyncConsistentMap<K, V> newAsyncConsistentMap(String name, Serializer serializer) {
95 AsyncConsistentMap<String, byte[]> rawMap =
96 new DelegatingAsyncConsistentMap<String, byte[]>(client.get(name, AtomixConsistentMap.class).join()) {
97 @Override
98 public String name() {
99 return name;
100 }
101 };
102 AsyncConsistentMap<K, V> transcodedMap = DistributedPrimitives.<K, V, String, byte[]>newTranscodingMap(rawMap,
103 key -> HexString.toHexString(serializer.encode(key)),
104 string -> serializer.decode(HexString.fromHexString(string)),
105 value -> value == null ? null : serializer.encode(value),
106 bytes -> serializer.decode(bytes));
107
108 return DistributedPrimitives.newCachingMap(transcodedMap);
109 }
110
111 @Override
112 public <E> AsyncDistributedSet<E> newAsyncDistributedSet(String name, Serializer serializer) {
113 return DistributedPrimitives.newSetFromMap(this.<E, Boolean>newAsyncConsistentMap(name, serializer));
114 }
115
116 @Override
117 public AsyncAtomicCounter newAsyncCounter(String name) {
118 DistributedLong distributedLong = client.get(name, DistributedLong.class).join();
119 return new AtomixCounter(name, distributedLong);
120 }
121
122 @Override
123 public <V> AsyncAtomicValue<V> newAsyncAtomicValue(String name, Serializer serializer) {
124 return new DefaultAsyncAtomicValue<>(name,
125 serializer,
126 onosAtomicValuesMap.get());
127 }
128
129 @Override
130 public <E> DistributedQueue<E> newDistributedQueue(String name, Serializer serializer) {
131 // TODO: Implement
132 throw new UnsupportedOperationException();
133 }
134
135 @Override
136 public AsyncLeaderElector newAsyncLeaderElector(String name) {
Madan Jampani39fff102016-02-14 13:17:28 -0800137 return client.get(name, AtomixLeaderElector.class).join();
Madan Jampani15b8ef52016-02-02 17:35:05 -0800138 }
139
140 @Override
Madan Jampanie14a09c2016-02-11 10:43:21 -0800141 public Set<String> getAsyncConsistentMapNames() {
142 return client.keys(AtomixConsistentMap.class).join();
143 }
144
145 @Override
146 public Set<String> getAsyncAtomicCounterNames() {
147 return client.keys(DistributedLong.class).join();
148 }
149
150 @Override
Madan Jampani15b8ef52016-02-02 17:35:05 -0800151 public boolean isOpen() {
152 return client.isOpen();
153 }
154
155 @Override
156 public boolean isClosed() {
157 return client.isClosed();
158 }
159}