summaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/acl/FilterTableLineEditor.java
blob: 611aff246d926df344975ff5019a25655f5c1042 (plain) (blame)
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
package com.yahoo.vespa.hosted.node.admin.maintenance.acl;

import com.yahoo.vespa.hosted.node.admin.configserver.noderepository.Acl;
import com.yahoo.vespa.hosted.node.admin.task.util.file.LineEdit;
import com.yahoo.vespa.hosted.node.admin.task.util.file.LineEditor;
import com.yahoo.vespa.hosted.node.admin.task.util.network.IPVersion;

import java.util.LinkedList;
import java.util.List;

/**
 * An editor that assumes all rules in the filter table are exactly as the the wanted rules
 */
class FilterTableLineEditor implements LineEditor {

    private final LinkedList<String> wantedRules;

    private FilterTableLineEditor(List<String> wantedRules) {
        this.wantedRules = new LinkedList<>(wantedRules);
    }

    static FilterTableLineEditor from(Acl acl, IPVersion ipVersion) {
        List<String> rules = acl.toRules(ipVersion);
        return new FilterTableLineEditor(rules);
    }

    @Override
    public LineEdit edit(String line) {
        // We have already added all the lines we wanted, remove the remainer
        if (wantedRules.isEmpty()) return LineEdit.remove();

        String wantedRule = wantedRules.pop();
        return wantedRule.equals(line) ? LineEdit.none() : LineEdit.replaceWith(wantedRule);
    }

    @Override
    public List<String> onComplete() {
        return this.wantedRules;
    }
}