Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame^] | 20 | import java.net.InetAddress; |
| 21 | import java.net.InetSocketAddress; |
| 22 | import java.net.UnknownHostException; |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import org.onlab.packet.IpAddress; |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 26 | import org.onosproject.cluster.PartitionId; |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame^] | 27 | import org.onosproject.store.cluster.messaging.Endpoint; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 28 | import org.onosproject.store.cluster.messaging.MessagingService; |
| 29 | |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame^] | 30 | import com.google.common.base.Throwables; |
| 31 | import com.google.common.collect.Maps; |
| 32 | |
| 33 | import io.atomix.catalyst.transport.Address; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 34 | import io.atomix.catalyst.transport.Client; |
| 35 | import io.atomix.catalyst.transport.Server; |
| 36 | import io.atomix.catalyst.transport.Transport; |
| 37 | |
| 38 | /** |
| 39 | * Custom {@link Transport transport} for Copycat interactions |
| 40 | * built on top of {@link MessagingService}. |
| 41 | * |
| 42 | * @see CopycatTransportServer |
| 43 | * @see CopycatTransportClient |
| 44 | */ |
| 45 | public class CopycatTransport implements Transport { |
| 46 | |
| 47 | /** |
| 48 | * Transport Mode. |
| 49 | */ |
| 50 | public enum Mode { |
| 51 | /** |
HIGUCHI Yuta | 556cf1e | 2016-01-13 18:04:58 -0800 | [diff] [blame] | 52 | * Signifies transport for client {@literal ->} server interaction. |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 53 | */ |
| 54 | CLIENT, |
| 55 | |
| 56 | /** |
HIGUCHI Yuta | 556cf1e | 2016-01-13 18:04:58 -0800 | [diff] [blame] | 57 | * Signified transport for server {@literal ->} server interaction. |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 58 | */ |
| 59 | SERVER |
| 60 | } |
| 61 | |
| 62 | private final Mode mode; |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 63 | private final PartitionId partitionId; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 64 | private final MessagingService messagingService; |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame^] | 65 | private static final Map<Address, Endpoint> EP_LOOKUP_CACHE = Maps.newConcurrentMap(); |
| 66 | private static final Map<Endpoint, Address> ADDRESS_LOOKUP_CACHE = Maps.newConcurrentMap(); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 67 | |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 68 | public CopycatTransport(Mode mode, PartitionId partitionId, MessagingService messagingService) { |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 69 | this.mode = checkNotNull(mode); |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 70 | this.partitionId = checkNotNull(partitionId); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 71 | this.messagingService = checkNotNull(messagingService); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public Client client() { |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 76 | return new CopycatTransportClient(partitionId, |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 77 | messagingService, |
| 78 | mode); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public Server server() { |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 83 | return new CopycatTransportServer(partitionId, |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 84 | messagingService); |
| 85 | } |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame^] | 86 | |
| 87 | /** |
| 88 | * Maps {@link Address address} to {@link Endpoint endpoint}. |
| 89 | * @param address |
| 90 | * @return end point |
| 91 | */ |
| 92 | public static Endpoint toEndpoint(Address address) { |
| 93 | return EP_LOOKUP_CACHE.computeIfAbsent(address, a -> { |
| 94 | try { |
| 95 | Endpoint endpoint = new Endpoint(IpAddress.valueOf(InetAddress.getByName(a.host())), a.port()); |
| 96 | ADDRESS_LOOKUP_CACHE.putIfAbsent(endpoint, address); |
| 97 | return endpoint; |
| 98 | } catch (UnknownHostException e) { |
| 99 | Throwables.propagate(e); |
| 100 | return null; |
| 101 | } |
| 102 | }); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Maps {@link Endpoint endpoint} to {@link Address address}. |
| 107 | * @param endpoint end point |
| 108 | * @return address |
| 109 | */ |
| 110 | public static Address toAddress(Endpoint endpoint) { |
| 111 | return ADDRESS_LOOKUP_CACHE.computeIfAbsent(endpoint, ep -> { |
| 112 | try { |
| 113 | InetAddress host = InetAddress.getByAddress(endpoint.host().toOctets()); |
| 114 | int port = endpoint.port(); |
| 115 | Address address = new Address(new InetSocketAddress(host, port)); |
| 116 | EP_LOOKUP_CACHE.putIfAbsent(address, endpoint); |
| 117 | return address; |
| 118 | } catch (UnknownHostException e) { |
| 119 | Throwables.propagate(e); |
| 120 | return null; |
| 121 | } |
| 122 | }); |
| 123 | } |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 124 | } |