aboutsummaryrefslogtreecommitdiff
path: root/agent/src/main/java/moe/yuuta/dn42peering/agent/provision/BGPProvisioner.java
blob: 81eec3360b8d1f1d190fc1a7b130b49b1de2dc6f (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
package moe.yuuta.dn42peering.agent.provision;

import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.file.FileSystemException;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import io.vertx.ext.web.common.template.TemplateEngine;
import io.vertx.ext.web.templ.freemarker.FreeMarkerTemplateEngine;
import moe.yuuta.dn42peering.agent.proto.BGPConfig;
import moe.yuuta.dn42peering.agent.proto.Node;

import javax.annotation.Nonnull;
import java.io.File;
import java.nio.file.NoSuchFileException;
import java.util.*;
import java.util.stream.Collectors;

public class BGPProvisioner implements IProvisioner<BGPConfig> {
    private final Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());

    private final TemplateEngine engine;
    private final Vertx vertx;

    public BGPProvisioner(@Nonnull Vertx vertx) {
        this(FreeMarkerTemplateEngine.create(vertx, "ftlh"), vertx);
    }

    public BGPProvisioner(@Nonnull TemplateEngine engine,
                          @Nonnull Vertx vertx) {
        this.engine = engine;
        this.vertx = vertx;
    }

    @Nonnull
    private Future<List<Change>> calculateDeleteChanges() {
        final String[] actualNames = new File("/etc/bird/peers").list((dir, name) -> name.matches("dn42_.*\\.conf"));
        if(actualNames == null)
            return Future.succeededFuture(Collections.emptyList());
        return Future.succeededFuture(Arrays.stream(actualNames)
                .map(string -> new FileChange("/etc/bird/peers/" + string, null, FileChange.Action.DELETE.toString()))
                .collect(Collectors.toList()));
    }

    @Nonnull
    private String generateBGPPath() {
        return vertx
                .getOrCreateContext()
                .config()
                .getString("bird_output_path", "/etc/bird/peers/dn42peers.conf");
    }

    @Nonnull
    private Future<Buffer> readConfig() {
        return Future.future(f -> {
            vertx.fileSystem()
                    .readFile(generateBGPPath())
                    .onFailure(err -> {
                        if(err instanceof FileSystemException &&
                        err.getCause() instanceof NoSuchFileException) {
                            f.complete(null);
                        } else {
                            f.fail(err);
                        }
                    })
                    .onSuccess(f::complete);
        });
    }

    @Nonnull
    private Future<Buffer> renderConfig(@Nonnull List<BGPConfig> configs) {
        final Map<String, Object> params = new HashMap<>(1);
        final List<Object> configParams = new ArrayList<>(configs.size());
        configs.forEach(config -> {
            final Map<String, Object> param = new HashMap<>(6);
            param.put("name", config.getId());
            param.put("asn", config.getAsn());
            param.put("ipv4", config.getIpv4());
            param.put("ipv6", config.getIpv6().equals("") ? null : config.getIpv6());
            param.put("mpbgp", config.getMpbgp());
            param.put("dev", config.getInterface());
            configParams.add(param);
        });
        params.put("sessions", configParams);
        return engine.render(params, "bird2.conf.ftlh");
    }

    @Nonnull
    private Future<List<Change>> calculateSingleChange(@Nonnull List<BGPConfig> desiredConfigs) {
        return CompositeFuture.all(readConfig(), renderConfig(desiredConfigs))
                .compose(future -> {
                    final Buffer actualBuff = future.resultAt(0);
                    final String actual = actualBuff == null ? null : actualBuff.toString();
                    final String desired = future.resultAt(1).toString();
                    final List<Change> changes = new ArrayList<>(1);
                    if(actual == null) {
                        changes.add(new FileChange(generateBGPPath(),
                                desired,
                                FileChange.Action.CREATE_AND_WRITE.toString()));
                    } else if(!actual.equals(desired)) {
                        changes.add(new FileChange(generateBGPPath(),
                                desired,
                                FileChange.Action.OVERWRITE.toString()));
                    }
                    return Future.succeededFuture(changes);
                });
    }

    @Nonnull
    @Override
    public Future<List<Change>> calculateChanges(@Nonnull Node node, @Nonnull List<BGPConfig> allDesired) {
        // All of these calculations can be done in parallel but we must wait all of them to finish.
        // The three major steps above must be done in sequence.
        // Step 1: Calculate individual BGP changes in parallel and combine them into a single future.
        return calculateSingleChange(allDesired)
                // Step 2: Calculate things to delete.
                .compose(changes -> {
                    return calculateDeleteChanges().compose(deleteChangeList -> {
                        changes.addAll(deleteChangeList);
                        return Future.succeededFuture(changes);
                    });
                })
                // Step 3: Reload at last
                .compose(changes -> {
                    return Future.succeededFuture(Collections.singletonList(
                            new CommandChange(new String[]{"birdc", "configure"}))).compose(reloadChangeList -> {
                        changes.addAll(reloadChangeList);
                        return Future.succeededFuture(changes);
                    });
                });
    }
}