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