aboutsummaryrefslogtreecommitdiff
path: root/central/src/main/java/moe/yuuta/dn42peering/peer/PeerServiceImpl.java
blob: 5784da28c1f62761142ac10304696140234e2759 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package moe.yuuta.dn42peering.peer;

import io.vertx.core.*;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import io.vertx.sqlclient.Pool;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;
import io.vertx.sqlclient.SqlResult;
import io.vertx.sqlclient.templates.SqlTemplate;
import moe.yuuta.dn42peering.database.DatabaseUtils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.*;

class PeerServiceImpl implements IPeerService {
    private final Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());

    private final Vertx vertx;
    private final Pool pool;

    PeerServiceImpl(@Nonnull Vertx vertx, @Nonnull Pool sql) {
        this.vertx = vertx;
        this.pool = sql;
    }

    @Nonnull
    @Override
    public IPeerService listUnderASN(@Nonnull String asn, @Nonnull Handler<AsyncResult<List<Peer>>> handler) {
        SqlTemplate
                .forQuery(pool, "SELECT id, type, asn, ipv4, ipv6, " +
                        "wg_endpoint, wg_endpoint_port, " +
                        "wg_self_pubkey, wg_self_privkey, wg_peer_pubkey, wg_preshared_secret, " +
                        "provision_status, mpbgp, node FROM peer " +
                        "WHERE asn = #{asn}")
                .mapTo(PeerRowMapper.INSTANCE)
                .execute(Collections.singletonMap("asn", asn))
                .compose(peers -> {
                    final List<Peer> peerList = new ArrayList<>();
                    for (Peer peer : peers) peerList.add(peer);
                    return Future.succeededFuture(peerList);
                })
                .onFailure(err -> err.printStackTrace())
                .onComplete(handler);
        return this;
    }

    @Nonnull
    @Override
    public IPeerService existsUnderASN(@Nonnull String asn, @Nonnull Handler<AsyncResult<Boolean>> handler) {
        SqlTemplate
                .forQuery(pool, "SELECT COUNT(id) FROM peer " +
                        "WHERE asn = #{asn}")
                .execute(Collections.singletonMap("asn", asn))
                .compose(rows -> Future.succeededFuture(rows.iterator().next().getInteger(0) > 0))
                .onComplete(handler);
        return this;
    }

    @Nonnull
    @Override
    public IPeerService updateTo(@Nonnull Peer peer, @Nonnull Handler<AsyncResult<Void>> handler) {
        SqlTemplate
                .forUpdate(pool, "UPDATE peer SET " +
                        "type = #{type}, " +
                        "ipv4 = #{ipv4}, " +
                        "ipv6 = #{ipv6}, " +
                        "wg_endpoint = #{wg_endpoint}, " +
                        "wg_endpoint_port = #{wg_endpoint_port}, " +
                        "wg_self_pubkey = #{wg_self_pubkey}, " +
                        "wg_self_privkey = #{wg_self_privkey}, " +
                        "wg_peer_pubkey = #{wg_peer_pubkey}, " +
                        "wg_preshared_secret = #{wg_preshared_secret}, " +
                        "provision_status = #{provision_status}, " +
                        "mpbgp = #{mpbgp}, " +
                        "node = #{node} " +
                        "WHERE id = #{id} AND asn = #{asn}")
                .mapFrom(PeerParametersMapper.INSTANCE)
                .execute(peer)
                .<Void>compose(res -> Future.succeededFuture(null))
                .onComplete(handler);
        return this;
    }

    @Nonnull
    @Override
    public IPeerService addNew(@Nonnull Peer peer, @Nonnull Handler<AsyncResult<Long>> handler) {
        peer.setId(0);
        Future.<RowSet<Peer>>future(f -> SqlTemplate
                .forQuery(pool, "INSERT INTO peer VALUES (#{id}, #{type}, #{asn}, " +
                        "#{ipv4}, #{ipv6}, " +
                        "#{wg_endpoint}, #{wg_endpoint_port}, " +
                        "#{wg_self_pubkey}, #{wg_self_privkey}, " +
                        "#{wg_peer_pubkey}, #{wg_preshared_secret}, " +
                        "#{provision_status}, #{mpbgp}, #{node}" +
                        ")")
                .mapFrom(PeerParametersMapper.INSTANCE)
                .mapTo(PeerRowMapper.INSTANCE)
                .execute(peer, f))
                .compose(rows -> Future.succeededFuture(rows.property(DatabaseUtils.LAST_INSERTED_ID)))
                .onComplete(handler);
        return this;
    }

    @Nonnull
    @Override
    public IPeerService isIPConflict(@Nonnull Peer.VPNType type, @Nullable String ipv4, @Nullable String ipv6, @Nonnull Handler<AsyncResult<Boolean>> handler) {
        final List<Future> futures = new ArrayList<>(2);
        if (ipv4 != null) {
            final Map<String, Object> params = new HashMap<>(3);
            params.put("type", type.toString());
            params.put("ip", ipv4);

            futures.add(Future.<RowSet<Row>>future(f -> SqlTemplate
                    .forQuery(pool, "SELECT COUNT(id) FROM peer WHERE type = #{type} AND ipv4 = #{ip}")
                    .execute(params, f))
                    .compose(rows -> Future.succeededFuture(rows.iterator().next().getInteger(0) > 0)));
        }
        if (ipv6 != null) {
            final Map<String, Object> params = new HashMap<>(3);
            params.put("type", type.toString());
            params.put("ip", ipv6);

            futures.add(Future.<RowSet<Row>>future(f -> SqlTemplate
                    .forQuery(pool, "SELECT COUNT(id) FROM peer WHERE type = #{type} AND ipv6 = #{ip}")
                    .execute(params, f))
                    .compose(rows -> Future.succeededFuture(rows.iterator().next().getInteger(0) > 0)));
        }
        if(futures.isEmpty()) {
            Future.succeededFuture(false).onComplete(handler);
            return this;
        }
        CompositeFuture.all(futures)
                .compose(future -> {
                    final List<Boolean> res = future.list();
                    for (boolean b : res) {
                        if (b) return Future.succeededFuture(true);
                    }
                    return Future.succeededFuture(false);
                })
                .onComplete(handler);
        return this;
    }

    @Nonnull
    @Override
    public IPeerService getSingle(@Nonnull String asn, String id, @Nonnull Handler<AsyncResult<Peer>> handler) {
        final Map<String, Object> params = new HashMap<>(2);
        params.put("id", id);
        params.put("asn", asn);
        Future.<RowSet<Peer>>future(f -> SqlTemplate
                .forQuery(pool, "SELECT id, type, asn, " +
                        "ipv4, ipv6, " +
                        "wg_endpoint, wg_endpoint_port, " +
                        "wg_self_pubkey, wg_self_privkey, " +
                        "wg_peer_pubkey, wg_preshared_secret, " +
                        "provision_status, mpbgp, node " +
                        "FROM peer " +
                        "WHERE id = #{id} AND asn = #{asn}")
                .mapTo(PeerRowMapper.INSTANCE)
                .execute(params, f))
                .compose(peers -> {
                    if(peers.iterator().hasNext())
                        return Future.succeededFuture(peers.iterator().next());
                    return Future.succeededFuture(null);
                })
                .onComplete(handler);
        return this;
    }

    @Nonnull
    @Override
    public IPeerService deletePeer(@Nonnull String asn, String id, @Nonnull Handler<AsyncResult<Void>> handler) {
        final Map<String, Object> params = new HashMap<>(2);
        params.put("id", id);
        params.put("asn", asn);
        Future.<SqlResult<Void>>future(f -> SqlTemplate
                .forUpdate(pool, "DELETE FROM peer WHERE id = #{id} AND asn = #{asn}")
                .execute(params, f))
        .<Void>compose(voidSqlResult -> Future.succeededFuture(null))
        .onComplete(handler);
        return this;
    }

    @Nonnull
    @Override
    public IPeerService changeProvisionStatus(int id, @Nonnull ProvisionStatus provisionStatus, @Nonnull Handler<AsyncResult<Void>> handler) {
        final Map<String, Object> params = new HashMap<>(2);
        params.put("id", id);
        params.put("provision_status", provisionStatus);
        SqlTemplate
                .forUpdate(pool, "UPDATE peer SET provision_status = #{provision_status} WHERE id = #{id}")
                .execute(params)
                .compose(res -> Future.<Void>succeededFuture(null))
                .onComplete(handler);
        return this;
    }
}