blob: fc94dd66350d2cf5310f0fed216ba18da6b6665e [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;
Madan Jampani2f9cc712016-02-15 19:36:21 -080030import java.net.UnknownHostException;
31import java.util.Map;
32
Jordan Haltermane9c37092017-03-21 11:16:14 -070033import static com.google.common.base.MoreObjects.toStringHelper;
34import static com.google.common.base.Preconditions.checkNotNull;
Madan Jampani3289fbf2016-01-13 14:14:27 -080035
36/**
Jordan Haltermane9c37092017-03-21 11:16:14 -070037 * Copycat transport implementation built on {@link MessagingService}.
Madan Jampani3289fbf2016-01-13 14:14:27 -080038 */
39public class CopycatTransport implements Transport {
Madan Jampanif778c962016-01-31 22:56:38 -080040 private final PartitionId partitionId;
Madan Jampani3289fbf2016-01-13 14:14:27 -080041 private final MessagingService messagingService;
Madan Jampani2f9cc712016-02-15 19:36:21 -080042 private static final Map<Address, Endpoint> EP_LOOKUP_CACHE = Maps.newConcurrentMap();
Madan Jampani3289fbf2016-01-13 14:14:27 -080043
Jordan Haltermane9c37092017-03-21 11:16:14 -070044 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 Jampani3289fbf2016-01-13 14:14:27 -080054 }
55
56 @Override
57 public Client client() {
Jordan Haltermane9c37092017-03-21 11:16:14 -070058 return new CopycatTransportClient(partitionId, messagingService);
Madan Jampani3289fbf2016-01-13 14:14:27 -080059 }
60
61 @Override
62 public Server server() {
Jordan Haltermane9c37092017-03-21 11:16:14 -070063 return new CopycatTransportServer(partitionId, messagingService);
64 }
65
66 @Override
67 public String toString() {
68 return toStringHelper(this).toString();
Madan Jampani3289fbf2016-01-13 14:14:27 -080069 }
Madan Jampani2f9cc712016-02-15 19:36:21 -080070
71 /**
72 * Maps {@link Address address} to {@link Endpoint endpoint}.
Madan Jampani86cb2432016-02-17 11:07:56 -080073 * @param address address
Madan Jampani2f9cc712016-02-15 19:36:21 -080074 * @return end point
75 */
Jordan Haltermane9c37092017-03-21 11:16:14 -070076 static Endpoint toEndpoint(Address address) {
Madan Jampani2f9cc712016-02-15 19:36:21 -080077 return EP_LOOKUP_CACHE.computeIfAbsent(address, a -> {
78 try {
Madan Jampani6c177182016-02-17 18:31:08 -080079 return new Endpoint(IpAddress.valueOf(InetAddress.getByName(a.host())), a.port());
Madan Jampani2f9cc712016-02-15 19:36:21 -080080 } catch (UnknownHostException e) {
81 Throwables.propagate(e);
82 return null;
83 }
84 });
85 }
Madan Jampani3289fbf2016-01-13 14:14:27 -080086}