summaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/notify/Notifier.java
blob: 19d5cd8c1b311a49c6c3fd115e9318638221db29 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.notify;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Environment;
import com.yahoo.text.Text;
import com.yahoo.vespa.hosted.controller.api.integration.deployment.RunId;
import com.yahoo.vespa.hosted.controller.api.integration.organization.Mail;
import com.yahoo.vespa.hosted.controller.api.integration.organization.Mailer;
import com.yahoo.vespa.hosted.controller.api.integration.organization.MailerException;
import com.yahoo.vespa.hosted.controller.api.integration.zone.ZoneRegistry;
import com.yahoo.vespa.hosted.controller.notification.Notification;
import com.yahoo.vespa.hosted.controller.notification.NotificationSource;
import com.yahoo.vespa.hosted.controller.persistence.CuratorDb;
import com.yahoo.vespa.hosted.controller.tenant.CloudTenant;
import com.yahoo.vespa.hosted.controller.tenant.TenantContacts;

import java.net.URI;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * Notifier is responsible for dispatching user notifications to their chosen Contact points.
 *
 * @author enygaard
 */
public class Notifier {
    private final CuratorDb curatorDb;
    private final ZoneRegistry zoneRegistry;
    private final Mailer mailer;

    private static final Logger log = Logger.getLogger(Notifier.class.getName());

    public Notifier(CuratorDb curatorDb, ZoneRegistry zoneRegistry, Mailer mailer) {
        this.curatorDb = Objects.requireNonNull(curatorDb);
        this.zoneRegistry = Objects.requireNonNull(zoneRegistry);
        this.mailer = Objects.requireNonNull(mailer);
    }

    public void dispatch(List<Notification> notifications, NotificationSource source) {
        if (notifications.isEmpty()) {
            return;
        }
        if (skipSource(source)) {
            return;
        }
        var tenant = curatorDb.readTenant(source.tenant());
        tenant.stream().forEach(t -> {
            if (t instanceof CloudTenant) {
                var ct = (CloudTenant) t;
                ct.info().contacts().all().stream()
                        .filter(c -> c.audiences().contains(TenantContacts.Audience.NOTIFICATIONS))
                        .collect(Collectors.groupingBy(TenantContacts.Contact::type, Collectors.toList()))
                        .entrySet()
                        .forEach(e -> notifications.forEach(n -> dispatch(n, e.getKey(), e.getValue())));
            }
        });
    }

    private boolean skipSource(NotificationSource source) {
        // Limit sources to production systems only. Dev and test systems cause too much noise at the moment.
        if (source.zoneId().map(z -> z.environment() != Environment.prod).orElse(false)) {
            return true;
        } else if (source.jobType().map(t -> !t.isProduction()).orElse(false)) {
            return true;
        }
        return false;
    }

    public void dispatch(Notification notification) {
        dispatch(List.of(notification), notification.source());
    }

    private void dispatch(Notification notification, TenantContacts.Type type, Collection<? extends TenantContacts.Contact> contacts) {
        switch (type) {
            case EMAIL:
                dispatch(notification, contacts.stream().map(c -> (TenantContacts.EmailContact) c).collect(Collectors.toList()));
                break;
            default:
                throw new IllegalArgumentException("Unknown TenantContacts type " + type.name());
        }
    }

    private void dispatch(Notification notification, Collection<TenantContacts.EmailContact> contacts) {
        try {
            mailer.send(mailOf(notification, contacts.stream().map(c -> c.email()).collect(Collectors.toList())));
        } catch (MailerException e) {
            log.log(Level.SEVERE, "Failed sending email", e);
        }
    }

    private Mail mailOf(Notification n, Collection<String> recipients) {
        var source = n.source();
        var subject = Text.format("[%s] %s Vespa Notification for %s - %s", n.level().toString().toUpperCase(), n.type().name(), source.tenant(), source.application());
        var body = new StringBuilder();
        body.append("Source: ").append(n.source().toString()).append("\n")
                .append("\n")
                .append(String.join("\n", n.messages()))
                .append("\n")
                .append(url(source).toString());
        return new Mail(recipients, subject.toString(), body.toString());
    }

    private URI url(NotificationSource source) {
        if (source.application().isPresent()) {
            if (source.instance().isPresent()) {
                if (source.jobType().isPresent() && source.runNumber().isPresent()) {
                    return zoneRegistry.dashboardUrl(
                            new RunId(ApplicationId.from(source.tenant(),
                                    source.application().get(),
                                    source.instance().get()),
                                    source.jobType().get(),
                                    source.runNumber().getAsLong()));
                }
                return zoneRegistry.dashboardUrl(ApplicationId.from(source.tenant(), source.application().get(), source.instance().get()));
            }
            return zoneRegistry.dashboardUrl(source.tenant(), source.application().get());
        }
        return zoneRegistry.dashboardUrl(source.tenant());
    }

}