aboutsummaryrefslogtreecommitdiff
path: root/agent/src/main/java/moe/yuuta/dn42peering/agent/provision/BGPProvisioner.java
blob: 1ae8a2b23563b10795df647f18cc14b9806d2458 (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
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(@Nonnull List<BGPConfig> allDesired) {
        final String[] actualNamesRaw = new File("/etc/bird/peers").list((dir, name) -> name.matches("dn42_.*\\.conf"));
        final List<String> actualNames = Arrays.stream(actualNamesRaw == null ? new String[]{} : actualNamesRaw)
                .sorted()
                .collect(Collectors.toList());
        final String[] desiredNames = allDesired
                .stream()
                .map(desired -> generateBGPPath(desired.getId()))
                .sorted()
                .collect(Collectors.toList())
                .toArray(new String[]{});
        final List<Integer> toRemove = new ArrayList<>(actualNames.size());
        for (int i = 0; i < desiredNames.length; i ++) {
            toRemove.clear();
            for(int j = 0; j < actualNames.size(); j ++) {
                if(("/etc/bird/peers/" + actualNames.get(j)).equals(desiredNames[i])) {
                    toRemove.add(j);
                }
            }
            for (int j = 0; j < toRemove.size(); j ++) {
                actualNames.remove(toRemove.get(j).intValue());
            }
        }
        return Future.succeededFuture(actualNames.stream()
                .map(string -> new FileChange("/etc/bird/peers/" + string, null, FileChange.Action.DELETE.toString()))
                .collect(Collectors.toList()));
    }

    @Nonnull
    private static String generateBGPPath(long id) {
        return String.format("/etc/bird/peers/dn42_%d.conf", id);
    }

    @Nonnull
    private Future<Buffer> readConfig(long id) {
        return Future.future(f -> {
            vertx.fileSystem()
                    .readFile(generateBGPPath(id))
                    .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 BGPConfig config) {
        final Map<String, Object> params = new HashMap<>(3);
        params.put("name", config.getId());
        params.put("asn", config.getAsn());
        params.put("ipv4", config.getIpv4());
        params.put("ipv6", config.getIpv6().equals("") ? null : config.getIpv6());
        params.put("mpbgp", config.getMpbgp());
        params.put("dev", config.getInterface());
        return engine.render(params, "bird2.conf.ftlh");
    }

    @Nonnull
    private Future<List<Change>> calculateSingleChange(@Nonnull BGPConfig desiredConfig) {
        return CompositeFuture.all(readConfig(desiredConfig.getId()), renderConfig(desiredConfig))
                .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(desiredConfig.getId()),
                                desired,
                                FileChange.Action.CREATE_AND_WRITE.toString()));
                    } else if(!actual.equals(desired)) {
                        changes.add(new FileChange(generateBGPPath(desiredConfig.getId()),
                                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 CompositeFuture.join(allDesired.stream()
                        .map(this::calculateSingleChange)
                        .collect(Collectors.toList()))
                .compose(compositeFuture -> {
                    final List<Change> changes = new ArrayList<>();
                    for (int i = 0; i < compositeFuture.size(); i++)
                        changes.addAll(compositeFuture.resultAt(i));
                    return Future.succeededFuture(changes);
                })
                // Step 2: Calculate things to delete.
                .compose(changes -> {
                    return calculateDeleteChanges(allDesired).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);
                    });
                });
    }
}