aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/persistence/AuditLogSerializer.java
blob: c94c04fc2448f3743dd077de429f15f122731b2d (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.persistence;

import com.yahoo.slime.ArrayTraverser;
import com.yahoo.slime.Cursor;
import com.yahoo.slime.Inspector;
import com.yahoo.slime.Slime;
import com.yahoo.slime.SlimeUtils;
import com.yahoo.vespa.hosted.controller.auditlog.AuditLog;

import java.util.ArrayList;
import java.util.List;

/**
 * Slime serializer for {@link AuditLog}.
 *
 * @author mpolden
 */
public class AuditLogSerializer {

    // WARNING: Since there are multiple servers in a ZooKeeper cluster and they upgrade one by one
    //          (and rewrite all nodes on startup), changes to the serialized format must be made
    //          such that what is serialized on version N+1 can be read by version N:
    //          - ADDING FIELDS: Always ok
    //          - REMOVING FIELDS: Stop reading the field first. Stop writing it on a later version.
    //          - CHANGING THE FORMAT OF A FIELD: Don't do it bro.

    private static final String entriesField = "entries";
    private static final String atField = "at";
    private static final String principalField = "principal";
    private static final String methodField = "method";
    private static final String resourceField = "resource";
    private static final String dataField = "data";

    public Slime toSlime(AuditLog log) {
        Slime slime = new Slime();
        Cursor root = slime.setObject();
        Cursor entryArray = root.setArray(entriesField);
        log.entries().forEach(entry -> {
            Cursor entryObject = entryArray.addObject();
            entryObject.setLong(atField, entry.at().toEpochMilli());
            entryObject.setString(principalField, entry.principal());
            entryObject.setString(methodField, asString(entry.method()));
            entryObject.setString(resourceField, entry.resource());
            entry.data().ifPresent(data -> entryObject.setString(dataField, data));
        });
        return slime;
    }

    public AuditLog fromSlime(Slime slime) {
        List<AuditLog.Entry> entries = new ArrayList<>();
        Cursor root = slime.get();
        root.field(entriesField).traverse((ArrayTraverser) (i, entryObject) -> {
            entries.add(new AuditLog.Entry(
                    SlimeUtils.instant(entryObject.field(atField)),
                    entryObject.field(principalField).asString(),
                    methodFrom(entryObject.field(methodField)),
                    entryObject.field(resourceField).asString(),
                    SlimeUtils.optionalString(entryObject.field(dataField))
            ));
        });
        return new AuditLog(entries);
    }

    private static String asString(AuditLog.Entry.Method method) {
        switch (method) {
            case POST: return "POST";
            case PATCH: return "PATCH";
            case PUT: return "PUT";
            case DELETE: return "DELETE";
            default: throw new IllegalArgumentException("No serialization defined for method " + method);
        }
    }

    private static AuditLog.Entry.Method methodFrom(Inspector field) {
        switch (field.asString()) {
            case "POST": return AuditLog.Entry.Method.POST;
            case "PATCH": return AuditLog.Entry.Method.PATCH;
            case "PUT": return AuditLog.Entry.Method.PUT;
            case "DELETE": return AuditLog.Entry.Method.DELETE;
            default: throw new IllegalArgumentException("Unknown serialized value '" + field.asString() + "'");
        }
    }

}