aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/CopycatCommunicationProtocol.java
blob: 88ddae62279a675baa1c0c86bb041ab4ed4c3911 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.store.consistent.impl;

import java.net.URI;
import java.nio.ByteBuffer;
import java.util.concurrent.CompletableFuture;

import org.onlab.util.Tools;
import org.onosproject.cluster.ClusterService;
import org.onosproject.cluster.ControllerNode;
import org.onosproject.cluster.NodeId;
import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
import org.onosproject.store.cluster.messaging.MessageSubject;

import net.kuujo.copycat.protocol.AbstractProtocol;
import net.kuujo.copycat.protocol.ProtocolClient;
import net.kuujo.copycat.protocol.ProtocolHandler;
import net.kuujo.copycat.protocol.ProtocolServer;
import net.kuujo.copycat.util.Configurable;

/**
 * Protocol for Copycat communication that employs
 * {@code ClusterCommunicationService}.
 */
public class CopycatCommunicationProtocol extends AbstractProtocol {

    private static final MessageSubject COPYCAT_MESSAGE_SUBJECT =
            new MessageSubject("onos-copycat-message");

    protected ClusterService clusterService;
    protected ClusterCommunicationService clusterCommunicator;

    public CopycatCommunicationProtocol(ClusterService clusterService,
                                        ClusterCommunicationService clusterCommunicator) {
        this.clusterService = clusterService;
        this.clusterCommunicator = clusterCommunicator;
    }

    @Override
    public Configurable copy() {
        return this;
    }

    @Override
    public ProtocolClient createClient(URI uri) {
        NodeId nodeId = uriToNodeId(uri);
        if (nodeId == null) {
            throw new IllegalStateException("Unknown peer " + uri);
        }
        return new Client(nodeId);
    }

    @Override
    public ProtocolServer createServer(URI uri) {
        return new Server();
    }

    private class Server implements ProtocolServer {

        @Override
        public void handler(ProtocolHandler handler) {
            if (handler == null) {
                clusterCommunicator.removeSubscriber(COPYCAT_MESSAGE_SUBJECT);
            } else {
                clusterCommunicator.addSubscriber(COPYCAT_MESSAGE_SUBJECT,
                        ByteBuffer::wrap,
                        handler,
                        Tools::byteBuffertoArray);
                // FIXME: Tools::byteBuffertoArray involves a array copy.
            }
        }

        @Override
        public CompletableFuture<Void> listen() {
            return CompletableFuture.completedFuture(null);
        }

        @Override
        public CompletableFuture<Void> close() {
            clusterCommunicator.removeSubscriber(COPYCAT_MESSAGE_SUBJECT);
            return CompletableFuture.completedFuture(null);
        }
    }

    private class Client implements ProtocolClient {
        private final NodeId peer;

        public Client(NodeId peer) {
            this.peer = peer;
        }

        @Override
        public CompletableFuture<ByteBuffer> write(ByteBuffer request) {
            return clusterCommunicator.sendAndReceive(request,
                    COPYCAT_MESSAGE_SUBJECT,
                    Tools::byteBuffertoArray,
                    ByteBuffer::wrap,
                    peer);
        }

        @Override
        public CompletableFuture<Void> connect() {
            return CompletableFuture.completedFuture(null);
        }

        @Override
        public CompletableFuture<Void> close() {
            return CompletableFuture.completedFuture(null);
        }
    }

    private NodeId uriToNodeId(URI uri) {
        return clusterService.getNodes()
                             .stream()
                             .filter(node -> uri.getHost().equals(node.ip().toString()))
                             .map(ControllerNode::id)
                             .findAny()
                             .orElse(null);
    }
}