aboutsummaryrefslogtreecommitdiff
path: root/central/src/main/java/moe/yuuta/dn42peering/node/NodeServiceImpl.java
blob: bc7e5bf20a989a4b923c0a460cf5d622eb2d2d8e (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
package moe.yuuta.dn42peering.node;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.mysqlclient.MySQLException;
import io.vertx.sqlclient.Pool;
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 java.util.*;

class NodeServiceImpl implements INodeService {
    private final Pool pool;
    private final Vertx vertx;

    NodeServiceImpl(@Nonnull Vertx vertx, @Nonnull Pool mySQLPool) {
        this.vertx = vertx;
        this.pool = mySQLPool;
    }

    @Nonnull
    @Override
    public INodeService listNodes(@Nonnull Handler<AsyncResult<List<Node>>> handler) {
        SqlTemplate
                .forQuery(pool, "SELECT id, public_ip, dn42_ip4, dn42_ip6, dn42_ip6_nonll, asn, " +
                        "internal_ip, internal_port, name, notice, vpn_type_wg " +
                        "FROM node")
                .mapTo(NodeRowMapper.INSTANCE)
                .execute(null)
                .compose(nodeRowMappers -> {
                    final List<Node> nodes = new ArrayList<>(10);
                    for (Node node : nodeRowMappers)
                        nodes.add(node);
                    return Future.succeededFuture(nodes);
                })
                .onComplete(handler);
        return this;
    }

    @Nonnull
    @Override
    public INodeService getNode(int id, @Nonnull Handler<AsyncResult<Node>> handler) {
        SqlTemplate
                .forQuery(pool, "SELECT id, public_ip, asn, " +
                        "dn42_ip4, dn42_ip6, dn42_ip6_nonll, " +
                        "internal_ip, internal_port, name, notice, vpn_type_wg " +
                        "FROM node " +
                        "WHERE id = #{id}")
                .mapTo(NodeRowMapper.INSTANCE)
                .execute(Collections.singletonMap("id", id))
                .compose(nodeRowMappers -> {
                    if (nodeRowMappers.iterator().hasNext()) {
                        return Future.succeededFuture(nodeRowMappers.iterator().next());
                    } else {
                        return Future.succeededFuture(null);
                    }
                })
                .onComplete(handler);
        return this;
    }

    @Nonnull
    @Override
    public INodeService addNew(@Nonnull Node node,
                               @Nonnull Handler<AsyncResult<Long>> handler) {
        node.setId(0);
        Future.<Long>future(f -> {
            Future.<RowSet<Node>>future(f1 -> SqlTemplate
                    .forUpdate(pool, "INSERT INTO node (id, asn, name, notice, " +
                            "public_ip, " +
                            "dn42_ip4, dn42_ip6, dn42_ip6_nonll," +
                            "internal_ip, internal_port," +
                            "vpn_type_wg) " +
                            "VALUES (#{id}, #{asn}, #{name}, #{notice}, " +
                            "#{public_ip}, " +
                            "#{dn42_ip4}, #{dn42_ip6}, #{dn42_ip6_nonll}, " +
                            "#{internal_ip}, #{internal_port}, " +
                            "#{vpn_type_wg}" +
                            ")")
                    .mapFrom(NodeParametersMapper.INSTANCE)
                    .mapTo(NodeRowMapper.INSTANCE)
                    .execute(node, f1))
                    .compose(rows -> Future.succeededFuture(rows.property(DatabaseUtils.LAST_INSERTED_ID)))
                    .onFailure(err -> {
                        if (err instanceof MySQLException) {
                            if (((MySQLException) err).getErrorCode() == 1062 /* Duplicate */) {
                                f.fail(new DuplicateNodeException());
                                return;
                            }
                            f.fail(err);
                        }
                    })
                    .onSuccess(f::complete);
        }).onComplete(handler);
        return this;
    }

    @Nonnull
    @Override
    public INodeService updateTo(@Nonnull Node node, @Nonnull Handler<AsyncResult<Long>> handler) {
        Future.<Long>future(f -> {
            Future.<RowSet<Node>>future(f1 -> SqlTemplate
                    .forUpdate(pool, "UPDATE node SET " +
                            "asn = #{asn}," +
                            "name = #{name}," +
                            "notice = #{notice}," +
                            "public_ip = #{public_ip}," +
                            "dn42_ip4 = #{dn42_ip4}," +
                            "dn42_ip6 = #{dn42_ip6}," +
                            "dn42_ip6_nonll = #{dn42_ip6_nonll}," +
                            "internal_ip = #{internal_ip}," +
                            "internal_port = #{internal_port}," +
                            "vpn_type_wg = #{vpn_type_wg} " +
                            "WHERE id = #{id}")
                    .mapFrom(NodeParametersMapper.INSTANCE)
                    .mapTo(NodeRowMapper.INSTANCE)
                    .execute(node, f1))
                    .compose(rows -> Future.succeededFuture(rows.property(DatabaseUtils.LAST_INSERTED_ID)))
                    .onFailure(err -> {
                        if (err instanceof MySQLException) {
                            if (((MySQLException) err).getErrorCode() == 1062 /* Duplicate */) {
                                f.fail(new DuplicateNodeException());
                                return;
                            }
                            f.fail(err);
                        }
                    })
                    .onSuccess(f::complete);
        }).onComplete(handler);
        return this;
    }

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