aboutsummaryrefslogtreecommitdiff
path: root/central/src/main/java/moe/yuuta/dn42peering/manage/ManagementProvision.java
blob: 5c9b5314614bde76d470c53be8254513ad5fa4e2 (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
package moe.yuuta.dn42peering.manage;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import moe.yuuta.dn42peering.jaba.Pair;
import moe.yuuta.dn42peering.node.INodeService;
import moe.yuuta.dn42peering.node.Node;
import moe.yuuta.dn42peering.peer.IPeerService;
import moe.yuuta.dn42peering.peer.Peer;
import moe.yuuta.dn42peering.peer.ProvisionStatus;
import moe.yuuta.dn42peering.provision.BGPRequestCommon;
import moe.yuuta.dn42peering.provision.IProvisionRemoteService;
import moe.yuuta.dn42peering.provision.WGRequestCommon;

import javax.annotation.Nonnull;
import java.io.IOException;

class ManagementProvision {
    private static final Logger logger = LoggerFactory.getLogger(ManagementProvision.class.getSimpleName());

    @Nonnull
    public static Future<Void> reloadPeer(@Nonnull INodeService nodeService,
                                    @Nonnull IProvisionRemoteService provisionService,
                                    @Nonnull Peer existingPeer, @Nonnull Peer inPeer) {
        // Check if we can reload on the fly.
        // Otherwise, we can only deprovision and provision.
        // This will cause unnecessary wastes.
        boolean canReload = inPeer.getType() == existingPeer.getType() &&
                inPeer.getNode() == existingPeer.getNode();
        // wg-quick does not support switching local IP addresses.
        // However, switch between link local addresses and real IPv6 addresses require the change of
        // local v6 address. Therefore, in such cases, we have to do a full re-provision.
        if(canReload && // Only check if no other factors prevent us from reloading.
                inPeer.getType() == Peer.VPNType.WIREGUARD &&
                existingPeer.getType() == Peer.VPNType.WIREGUARD) {
            try {
                final boolean existingLL = existingPeer.isIPv6LinkLocal();
                final boolean newLL = inPeer.isIPv6LinkLocal();
                if(existingLL != newLL) {
                    canReload = false;
                }
            } catch (IOException e) {
                return Future.failedFuture(e);
            }
        }
        // wg-quick will also not clear EndPoint setting if we just reload it.
        if(canReload && // Only check if no other factors prevent us from reloading.
                inPeer.getType() == Peer.VPNType.WIREGUARD &&
                existingPeer.getType() == Peer.VPNType.WIREGUARD) {
            if(inPeer.getWgEndpoint() == null &&
                    existingPeer.getWgEndpoint() != null) {
                canReload = false;
            }
        }
        Future<Void> future;
        if (canReload) {
            future = Future.<Node>future(f -> nodeService.getNode(inPeer.getNode(), f))
                    .compose(node -> {
                        if(node == null || !node.getSupportedVPNTypes().contains(inPeer.getType())) {
                            return Future.failedFuture("The node does not exist");
                        }
                        return Future.succeededFuture(node);
                    })
                    .compose(node -> {
                        switch (existingPeer.getType()) {
                            case WIREGUARD:
                                final WGRequestCommon wgReq = inPeer.toWGRequest();
                                wgReq.setNode(node.toRPCNode());
                                return Future.<String>future(f -> provisionService.reloadWG(
                                        node.toRPCNode(),
                                        wgReq,
                                        f)
                                ).compose(device -> Future.succeededFuture(new Pair<>(node, device)));
                            default:
                                throw new UnsupportedOperationException("Bug: Unknown type.");
                        }
                    })
                    .compose(pair -> {
                        final BGPRequestCommon bgpReq = inPeer.toBGPRequest();
                        bgpReq.setNode(pair.a.toRPCNode());
                        bgpReq.setDevice(pair.b);
                        return Future.future(f -> provisionService.reloadBGP(
                                pair.a.toRPCNode(),
                                bgpReq,
                                f));
                    });
        } else {
            future = unprovisionPeer(nodeService, provisionService, existingPeer)
                    .compose(f -> provisionPeer(nodeService, provisionService, inPeer));
        }
        return future;
    }

    public static Future<Void> unprovisionPeer(@Nonnull INodeService nodeService,
                                         @Nonnull IProvisionRemoteService provisionService,
                                         @Nonnull Peer existingPeer) {
        return Future.<Node>future(f -> nodeService.getNode(existingPeer.getNode(), f))
                .compose(node -> {
                    if(node == null) {
                        return Future.failedFuture("The node does not exist");
                    }
                    return Future.succeededFuture(node);
                })
                .compose(node -> {
                    switch (existingPeer.getType()) {
                        case WIREGUARD:
                            return Future.<Void>future(f -> provisionService.deleteWG(
                                    node.toRPCNode(),
                                    new WGRequestCommon(null,
                                            (long)existingPeer.getId(),
                                            null,
                                            null,
                                            null,
                                            null,
                                            null,
                                            null,
                                            null),
                                    f))
                                    .compose(res -> Future.succeededFuture(node));
                        default:
                            throw new UnsupportedOperationException("Bug: Unknown type.");
                    }
                })
                .compose(node -> {
                    return Future.future(f -> provisionService.deleteBGP(
                            node.toRPCNode(),
                            new BGPRequestCommon(null,
                                    (long)existingPeer.getId(),
                                    null,
                                    null,
                                    null,
                                    null,
                                    null),
                            f));
                })
                ;
    }

    @Nonnull
    public static Future<Void> provisionPeer(@Nonnull INodeService nodeService,
                                       @Nonnull IProvisionRemoteService provisionService,
                                       @Nonnull Peer inPeer) {
        return Future.<Node>future(f -> nodeService.getNode(inPeer.getNode(), f))
                .compose(node -> {
                    if(node == null || !node.getSupportedVPNTypes().contains(inPeer.getType())) {
                        return Future.failedFuture("The node does not exist");
                    }
                    return Future.succeededFuture(node);
                })
                .compose(node -> {
                    switch (inPeer.getType()) {
                        case WIREGUARD:
                            final WGRequestCommon wgReq = inPeer.toWGRequest();
                            wgReq.setNode(node.toRPCNode());
                            return Future.<String>future(f -> provisionService.provisionWG(
                                    node.toRPCNode(),
                                    wgReq,
                                    f)
                            ).compose(device -> Future.succeededFuture(new Pair<>(node, device)));
                        default:
                            throw new UnsupportedOperationException("Bug: Unknown type.");
                    }
                })
                .compose(pair -> {
                    final BGPRequestCommon bgpReq = inPeer.toBGPRequest();
                    bgpReq.setNode(pair.a.toRPCNode());
                    bgpReq.setDevice(pair.b);
                    return Future.future(f -> provisionService.provisionBGP(
                            pair.a.toRPCNode(),
                            bgpReq,
                            f));
                });
    }

    public static void handleProvisionResult(@Nonnull IPeerService peerService,
                                       @Nonnull Peer inPeer,
                                       @Nonnull AsyncResult<Void> res) {
        if(res.succeeded()) {
            peerService.changeProvisionStatus(inPeer.getId(),
                    ProvisionStatus.PROVISIONED, ar -> {
                        if (ar.failed()) {
                            logger.error(String.format("Cannot update %d to provisioned.", inPeer.getId()), ar.cause());
                        }
                    });
        } else {
            logger.error(String.format("Cannot provision %d.", inPeer.getId()), res.cause());
            peerService.changeProvisionStatus(inPeer.getId(),
                    ProvisionStatus.FAIL, ar -> {
                        if (ar.failed()) {
                            logger.error(String.format("Cannot update %d to failed.", inPeer.getId()), ar.cause());
                        }
                    });
        }
    }
}