aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/configchange/ConfigChangeActionsSlimeConverter.java
blob: f46c6893dc15cd7fd4e5c6746a17ba5c12e91439 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.configchange;

import com.yahoo.config.model.api.ServiceInfo;
import com.yahoo.slime.Cursor;

import java.util.Set;

/**
 * Class used to convert a ConfigChangeActions instance to Slime.
 *
 * @author geirst
 */
public class ConfigChangeActionsSlimeConverter {
    private final ConfigChangeActions actions;

    public ConfigChangeActionsSlimeConverter(ConfigChangeActions actions) {
        this.actions = actions;
    }

    public void toSlime(Cursor root) {
        Cursor actionsCursor = root.setObject("configChangeActions");
        restartActionsToSlime(actionsCursor);
        refeedActionsToSlime(actionsCursor);
        reindexActionsToSlime(actionsCursor);
    }

    private void restartActionsToSlime(Cursor actionsCursor) {
        Cursor restartCursor = actionsCursor.setArray("restart");
        for (RestartActions.Entry entry : actions.getRestartActions().getEntries()) {
            Cursor entryCursor = restartCursor.addObject();
            entryCursor.setString("clusterName", entry.getClusterName());
            entryCursor.setString("clusterType", entry.getClusterType());
            entryCursor.setString("serviceType", entry.getServiceType());
            messagesToSlime(entryCursor, entry.getMessages());
            servicesToSlime(entryCursor, entry.getServices());
        }
    }

    private void refeedActionsToSlime(Cursor actionsCursor) {
        Cursor refeedCursor = actionsCursor.setArray("refeed");
        for (RefeedActions.Entry entry : actions.getRefeedActions().getEntries()) {
            Cursor entryCursor = refeedCursor.addObject();
            entryCursor.setString("name", entry.name());
            entryCursor.setString("documentType", entry.getDocumentType());
            entryCursor.setString("clusterName", entry.getClusterName());
            messagesToSlime(entryCursor, entry.getMessages());
            servicesToSlime(entryCursor, entry.getServices());
        }
    }

    private void reindexActionsToSlime(Cursor actionsCursor) {
        Cursor refeedCursor = actionsCursor.setArray("reindex");
        for (ReindexActions.Entry entry : actions.getReindexActions().getEntries()) {
            Cursor entryCursor = refeedCursor.addObject();
            entryCursor.setString("name", entry.name());
            entryCursor.setString("documentType", entry.getDocumentType());
            entryCursor.setString("clusterName", entry.getClusterName());
            messagesToSlime(entryCursor, entry.getMessages());
            servicesToSlime(entryCursor, entry.getServices());
        }
    }

    private static void messagesToSlime(Cursor entryCursor, Set<String> messages) {
        Cursor messagesCursor = entryCursor.setArray("messages");
        for (String message : messages) {
            messagesCursor.addString(message);
        }
    }

    private static void servicesToSlime(Cursor entryCursor, Set<ServiceInfo> services) {
        Cursor servicesCursor = entryCursor.setArray("services");
        for (ServiceInfo service : services) {
            Cursor serviceCursor = servicesCursor.addObject();
            serviceCursor.setString("serviceName", service.getServiceName());
            serviceCursor.setString("serviceType", service.getServiceType());
            serviceCursor.setString("configId", service.getConfigId());
            serviceCursor.setString("hostName", service.getHostName());
        }
    }
}