blob: 45cc529492c27a3be71773b8f37a3057174e52a7 [file] [log] [blame]
Madan Jampani3289fbf2016-01-13 14:14:27 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampani3289fbf2016-01-13 14:14:27 -08003 *
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
Jordan Haltermane9c37092017-03-21 11:16:14 -070018import com.google.common.base.Throwables;
19import com.google.common.collect.Maps;
20import io.atomix.catalyst.transport.Address;
21import io.atomix.catalyst.transport.Client;
22import io.atomix.catalyst.transport.Server;
23import io.atomix.catalyst.transport.Transport;
24import org.onlab.packet.IpAddress;
25import org.onosproject.cluster.PartitionId;
26import org.onosproject.store.cluster.messaging.Endpoint;
27import org.onosproject.store.cluster.messaging.MessagingService;
Madan Jampani3289fbf2016-01-13 14:14:27 -080028
Madan Jampani2f9cc712016-02-15 19:36:21 -080029import java.net.InetAddress;
30import java.net.InetSocketAddress;
31import java.net.UnknownHostException;
32import java.util.Map;
33
Jordan Haltermane9c37092017-03-21 11:16:14 -070034import static com.google.common.base.MoreObjects.toStringHelper;
35import static com.google.common.base.Preconditions.checkNotNull;
Madan Jampani3289fbf2016-01-13 14:14:27 -080036
37/**
Jordan Haltermane9c37092017-03-21 11:16:14 -070038 * Copycat transport implementation built on {@link MessagingService}.
Madan Jampani3289fbf2016-01-13 14:14:27 -080039 */
40public class CopycatTransport implements Transport {
Madan Jampanif778c962016-01-31 22:56:38 -080041 private final PartitionId partitionId;
Madan Jampani3289fbf2016-01-13 14:14:27 -080042 private final MessagingService messagingService;
Madan Jampani2f9cc712016-02-15 19:36:21 -080043 private static final Map<Address, Endpoint> EP_LOOKUP_CACHE = Maps.newConcurrentMap();
44 private static final Map<Endpoint, Address> ADDRESS_LOOKUP_CACHE = Maps.newConcurrentMap();
Madan Jampani3289fbf2016-01-13 14:14:27 -080045
Jordan Haltermane9c37092017-03-21 11:16:14 -070046 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 Jampani3289fbf2016-01-13 14:14:27 -080056 }
57
58 @Override
59 public Client client() {
Jordan Haltermane9c37092017-03-21 11:16:14 -070060 return new CopycatTransportClient(partitionId, messagingService);
Madan Jampani3289fbf2016-01-13 14:14:27 -080061 }
62
63 @Override
64 public Server server() {
Jordan Haltermane9c37092017-03-21 11:16:14 -070065 return new CopycatTransportServer(partitionId, messagingService);
66 }
67
68 @Override
69 public String toString() {
70 return toStringHelper(this).toString();
Madan Jampani3289fbf2016-01-13 14:14:27 -080071 }
Madan Jampani2f9cc712016-02-15 19:36:21 -080072
73 /**
74 * Maps {@link Address address} to {@link Endpoint endpoint}.
Madan Jampani86cb2432016-02-17 11:07:56 -080075 * @param address address
Madan Jampani2f9cc712016-02-15 19:36:21 -080076 * @return end point
77 */
Jordan Haltermane9c37092017-03-21 11:16:14 -070078 static Endpoint toEndpoint(Address address) {
Madan Jampani2f9cc712016-02-15 19:36:21 -080079 return EP_LOOKUP_CACHE.computeIfAbsent(address, a -> {
80 try {
Madan Jampani6c177182016-02-17 18:31:08 -080081 return new Endpoint(IpAddress.valueOf(InetAddress.getByName(a.host())), a.port());
Madan Jampani2f9cc712016-02-15 19:36:21 -080082 } 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 Haltermane9c37092017-03-21 11:16:14 -070094 static Address toAddress(Endpoint endpoint) {
Madan Jampani2f9cc712016-02-15 19:36:21 -080095 return ADDRESS_LOOKUP_CACHE.computeIfAbsent(endpoint, ep -> {
96 try {
97 InetAddress host = InetAddress.getByAddress(endpoint.host().toOctets());
98 int port = endpoint.port();
Madan Jampani6c177182016-02-17 18:31:08 -080099 return new Address(new InetSocketAddress(host, port));
Madan Jampani2f9cc712016-02-15 19:36:21 -0800100 } catch (UnknownHostException e) {
101 Throwables.propagate(e);
102 return null;
103 }
104 });
105 }
Madan Jampani3289fbf2016-01-13 14:14:27 -0800106}