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