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
|
// 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.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
/**
* An in-memory name service for testing purposes.
*
* @author mpolden
*/
public class MemoryNameService implements NameService {
private final Map<RecordId, Record> records = new HashMap<>();
public Map<RecordId, Record> records() {
return Collections.unmodifiableMap(records);
}
@Override
public RecordId createCname(RecordName alias, RecordData canonicalName) {
RecordId id = new RecordId(UUID.randomUUID().toString());
records.put(id, new Record(id, Record.Type.CNAME, alias, canonicalName));
return id;
}
@Override
public Optional<Record> findRecord(Record.Type type, RecordName name) {
return records.values().stream()
.filter(record -> record.type() == type && record.name().equals(name))
.findFirst();
}
@Override
public Optional<Record> findRecord(Record.Type type, RecordData data) {
return records.values()
.stream()
.filter(record -> record.type() == type && record.value().equals(data))
.findFirst();
}
@Override
public void updateRecord(RecordId id, RecordData newData) {
records.computeIfPresent(id, (k, record) -> new Record(id, record.type(), record.name(), newData));
}
@Override
public void removeRecord(RecordId id) {
records.remove(id);
}
}
|