summaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/MemoryNameService.java
blob: 80b3a0bf25ca4be360560cb551e415de780eb129 (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
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.integration.dns;


import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * An in-memory name service for testing purposes.
 *
 * @author mpolden
 */
public class MemoryNameService implements NameService {

    private final Set<Record> records = new TreeSet<>();

    public Set<Record> records() {
        return Collections.unmodifiableSet(records);
    }

    @Override
    public Record createCname(RecordName name, RecordData canonicalName) {
        var record = new Record(Record.Type.CNAME, name, canonicalName);
        records.add(record);
        return record;
    }

    @Override
    public List<Record> createAlias(RecordName name, Set<AliasTarget> targets) {
        var records = targets.stream()
                             .sorted((a, b) -> Comparator.comparing(AliasTarget::name).compare(a, b))
                             .map(target -> new Record(Record.Type.ALIAS, name, target.asData()))
                             .collect(Collectors.toList());
        // Satisfy idempotency contract of interface
        removeRecords(findRecords(Record.Type.ALIAS, name));
        this.records.addAll(records);
        return records;
    }

    @Override
    public List<Record> createTxtRecords(RecordName name, List<RecordData> txtData) {
        var records = txtData.stream()
                             .map(data -> new Record(Record.Type.TXT, name, data))
                             .collect(Collectors.toList());
        this.records.addAll(records);
        return records;
    }

    @Override
    public List<Record> findRecords(Record.Type type, RecordName name) {
        return records.stream()
                      .filter(record -> record.type() == type && record.name().equals(name))
                      .collect(Collectors.toUnmodifiableList());
    }

    @Override
    public List<Record> findRecords(Record.Type type, RecordData data) {
        return records.stream()
                      .filter(record -> record.type() == type && record.data().equals(data))
                      .collect(Collectors.toUnmodifiableList());
    }

    @Override
    public void updateRecord(Record record, RecordData newData) {
        var records = findRecords(record.type(), record.name());
        if (records.isEmpty()) {
            throw new IllegalArgumentException("No record with data '" + newData.asString() + "' exists");
        }
        if (records.size() > 1) {
            throw new IllegalArgumentException("Cannot update multi-value record '" + record.name().asString() +
                                               "' with '" + newData.asString() + "'");
        }
        var existing = records.get(0);
        this.records.remove(existing);
        this.records.add(new Record(existing.type(), existing.name(), newData));
    }

    @Override
    public void removeRecords(List<Record> records) {
        this.records.removeAll(records);
    }

}