Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 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}. |
Madan Jampani | 86cb243 | 2016-02-17 11:07:56 -0800 | [diff] [blame] | 89 | * @param address address |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 90 | * @return end point |
| 91 | */ |
| 92 | public static Endpoint toEndpoint(Address address) { |
| 93 | return EP_LOOKUP_CACHE.computeIfAbsent(address, a -> { |
| 94 | try { |
Madan Jampani | 6c17718 | 2016-02-17 18:31:08 -0800 | [diff] [blame] | 95 | return new Endpoint(IpAddress.valueOf(InetAddress.getByName(a.host())), a.port()); |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 96 | } catch (UnknownHostException e) { |
| 97 | Throwables.propagate(e); |
| 98 | return null; |
| 99 | } |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Maps {@link Endpoint endpoint} to {@link Address address}. |
| 105 | * @param endpoint end point |
| 106 | * @return address |
| 107 | */ |
| 108 | public static Address toAddress(Endpoint endpoint) { |
| 109 | return ADDRESS_LOOKUP_CACHE.computeIfAbsent(endpoint, ep -> { |
| 110 | try { |
| 111 | InetAddress host = InetAddress.getByAddress(endpoint.host().toOctets()); |
| 112 | int port = endpoint.port(); |
Madan Jampani | 6c17718 | 2016-02-17 18:31:08 -0800 | [diff] [blame] | 113 | return new Address(new InetSocketAddress(host, port)); |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 114 | } catch (UnknownHostException e) { |
| 115 | Throwables.propagate(e); |
| 116 | return null; |
| 117 | } |
| 118 | }); |
| 119 | } |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 120 | } |