blob: b5b1f214b82aeaee0a9859a10d31ef1d5e8471b8 [file] [log] [blame]
Madan Jampani3289fbf2016-01-13 14:14:27 -08001/*
2 * Copyright 2016 Open Networking Laboratory
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 */
16package org.onosproject.store.primitives.impl;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19
Madan Jampani2f9cc712016-02-15 19:36:21 -080020import java.net.InetAddress;
21import java.net.InetSocketAddress;
22import java.net.UnknownHostException;
23import java.util.Map;
24
25import org.onlab.packet.IpAddress;
Madan Jampanif778c962016-01-31 22:56:38 -080026import org.onosproject.cluster.PartitionId;
Madan Jampani2f9cc712016-02-15 19:36:21 -080027import org.onosproject.store.cluster.messaging.Endpoint;
Madan Jampani3289fbf2016-01-13 14:14:27 -080028import org.onosproject.store.cluster.messaging.MessagingService;
29
Madan Jampani2f9cc712016-02-15 19:36:21 -080030import com.google.common.base.Throwables;
31import com.google.common.collect.Maps;
32
33import io.atomix.catalyst.transport.Address;
Madan Jampani3289fbf2016-01-13 14:14:27 -080034import io.atomix.catalyst.transport.Client;
35import io.atomix.catalyst.transport.Server;
36import 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 */
45public class CopycatTransport implements Transport {
46
47 /**
48 * Transport Mode.
49 */
50 public enum Mode {
51 /**
HIGUCHI Yuta556cf1e2016-01-13 18:04:58 -080052 * Signifies transport for client {@literal ->} server interaction.
Madan Jampani3289fbf2016-01-13 14:14:27 -080053 */
54 CLIENT,
55
56 /**
HIGUCHI Yuta556cf1e2016-01-13 18:04:58 -080057 * Signified transport for server {@literal ->} server interaction.
Madan Jampani3289fbf2016-01-13 14:14:27 -080058 */
59 SERVER
60 }
61
62 private final Mode mode;
Madan Jampanif778c962016-01-31 22:56:38 -080063 private final PartitionId partitionId;
Madan Jampani3289fbf2016-01-13 14:14:27 -080064 private final MessagingService messagingService;
Madan Jampani2f9cc712016-02-15 19:36:21 -080065 private static final Map<Address, Endpoint> EP_LOOKUP_CACHE = Maps.newConcurrentMap();
66 private static final Map<Endpoint, Address> ADDRESS_LOOKUP_CACHE = Maps.newConcurrentMap();
Madan Jampani3289fbf2016-01-13 14:14:27 -080067
Madan Jampanif778c962016-01-31 22:56:38 -080068 public CopycatTransport(Mode mode, PartitionId partitionId, MessagingService messagingService) {
Madan Jampani3289fbf2016-01-13 14:14:27 -080069 this.mode = checkNotNull(mode);
Madan Jampanif778c962016-01-31 22:56:38 -080070 this.partitionId = checkNotNull(partitionId);
Madan Jampani3289fbf2016-01-13 14:14:27 -080071 this.messagingService = checkNotNull(messagingService);
72 }
73
74 @Override
75 public Client client() {
Madan Jampanif778c962016-01-31 22:56:38 -080076 return new CopycatTransportClient(partitionId,
Madan Jampani3289fbf2016-01-13 14:14:27 -080077 messagingService,
78 mode);
79 }
80
81 @Override
82 public Server server() {
Madan Jampanif778c962016-01-31 22:56:38 -080083 return new CopycatTransportServer(partitionId,
Madan Jampani3289fbf2016-01-13 14:14:27 -080084 messagingService);
85 }
Madan Jampani2f9cc712016-02-15 19:36:21 -080086
87 /**
88 * Maps {@link Address address} to {@link Endpoint endpoint}.
89 * @param address
90 * @return end point
91 */
92 public static Endpoint toEndpoint(Address address) {
93 return EP_LOOKUP_CACHE.computeIfAbsent(address, a -> {
94 try {
95 Endpoint endpoint = new Endpoint(IpAddress.valueOf(InetAddress.getByName(a.host())), a.port());
96 ADDRESS_LOOKUP_CACHE.putIfAbsent(endpoint, address);
97 return endpoint;
98 } catch (UnknownHostException e) {
99 Throwables.propagate(e);
100 return null;
101 }
102 });
103 }
104
105 /**
106 * Maps {@link Endpoint endpoint} to {@link Address address}.
107 * @param endpoint end point
108 * @return address
109 */
110 public static Address toAddress(Endpoint endpoint) {
111 return ADDRESS_LOOKUP_CACHE.computeIfAbsent(endpoint, ep -> {
112 try {
113 InetAddress host = InetAddress.getByAddress(endpoint.host().toOctets());
114 int port = endpoint.port();
115 Address address = new Address(new InetSocketAddress(host, port));
116 EP_LOOKUP_CACHE.putIfAbsent(address, endpoint);
117 return address;
118 } catch (UnknownHostException e) {
119 Throwables.propagate(e);
120 return null;
121 }
122 });
123 }
Madan Jampani3289fbf2016-01-13 14:14:27 -0800124}