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 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame^] | 18 | import com.google.common.base.Throwables; |
| 19 | import com.google.common.collect.Maps; |
| 20 | import io.atomix.catalyst.transport.Address; |
| 21 | import io.atomix.catalyst.transport.Client; |
| 22 | import io.atomix.catalyst.transport.Server; |
| 23 | import io.atomix.catalyst.transport.Transport; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onosproject.cluster.PartitionId; |
| 26 | import org.onosproject.store.cluster.messaging.Endpoint; |
| 27 | import org.onosproject.store.cluster.messaging.MessagingService; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 28 | |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 29 | import java.net.InetAddress; |
| 30 | import java.net.InetSocketAddress; |
| 31 | import java.net.UnknownHostException; |
| 32 | import java.util.Map; |
| 33 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame^] | 34 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 35 | import static com.google.common.base.Preconditions.checkNotNull; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame^] | 38 | * Copycat transport implementation built on {@link MessagingService}. |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 39 | */ |
| 40 | public class CopycatTransport implements Transport { |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 41 | private final PartitionId partitionId; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 42 | private final MessagingService messagingService; |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 43 | private static final Map<Address, Endpoint> EP_LOOKUP_CACHE = Maps.newConcurrentMap(); |
| 44 | private static final Map<Endpoint, Address> ADDRESS_LOOKUP_CACHE = Maps.newConcurrentMap(); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 45 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame^] | 46 | static final byte MESSAGE = 0x01; |
| 47 | static final byte CONNECT = 0x02; |
| 48 | static final byte CLOSE = 0x03; |
| 49 | |
| 50 | static final byte SUCCESS = 0x01; |
| 51 | static final byte FAILURE = 0x02; |
| 52 | |
| 53 | public CopycatTransport(PartitionId partitionId, MessagingService messagingService) { |
| 54 | this.partitionId = checkNotNull(partitionId, "partitionId cannot be null"); |
| 55 | this.messagingService = checkNotNull(messagingService, "messagingService cannot be null"); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public Client client() { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame^] | 60 | return new CopycatTransportClient(partitionId, messagingService); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public Server server() { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame^] | 65 | return new CopycatTransportServer(partitionId, messagingService); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public String toString() { |
| 70 | return toStringHelper(this).toString(); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 71 | } |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Maps {@link Address address} to {@link Endpoint endpoint}. |
Madan Jampani | 86cb243 | 2016-02-17 11:07:56 -0800 | [diff] [blame] | 75 | * @param address address |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 76 | * @return end point |
| 77 | */ |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame^] | 78 | static Endpoint toEndpoint(Address address) { |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 79 | return EP_LOOKUP_CACHE.computeIfAbsent(address, a -> { |
| 80 | try { |
Madan Jampani | 6c17718 | 2016-02-17 18:31:08 -0800 | [diff] [blame] | 81 | return new Endpoint(IpAddress.valueOf(InetAddress.getByName(a.host())), a.port()); |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 82 | } catch (UnknownHostException e) { |
| 83 | Throwables.propagate(e); |
| 84 | return null; |
| 85 | } |
| 86 | }); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Maps {@link Endpoint endpoint} to {@link Address address}. |
| 91 | * @param endpoint end point |
| 92 | * @return address |
| 93 | */ |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame^] | 94 | static Address toAddress(Endpoint endpoint) { |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 95 | return ADDRESS_LOOKUP_CACHE.computeIfAbsent(endpoint, ep -> { |
| 96 | try { |
| 97 | InetAddress host = InetAddress.getByAddress(endpoint.host().toOctets()); |
| 98 | int port = endpoint.port(); |
Madan Jampani | 6c17718 | 2016-02-17 18:31:08 -0800 | [diff] [blame] | 99 | return new Address(new InetSocketAddress(host, port)); |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 100 | } catch (UnknownHostException e) { |
| 101 | Throwables.propagate(e); |
| 102 | return null; |
| 103 | } |
| 104 | }); |
| 105 | } |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 106 | } |