aboutsummaryrefslogtreecommitdiffstats
path: root/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/ServiceModel.java
blob: a9071e3ab945335f3a4045411a8e19ffe48ce4f9 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.service.monitor;

import com.yahoo.vespa.applicationmodel.ApplicationInstance;
import com.yahoo.vespa.applicationmodel.ApplicationInstanceReference;
import com.yahoo.vespa.applicationmodel.HostName;
import com.yahoo.vespa.applicationmodel.ServiceInstance;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * The ServiceModel is almost a mirror of the SuperModel, except that it
 * also gives ServiceStatus on each service, and there may be
 * artificial applications like the config server "application".
 */
// @Immutable
public class ServiceModel {
    private final Map<ApplicationInstanceReference, ApplicationInstance> applications;

    public ServiceModel(Map<ApplicationInstanceReference, ApplicationInstance> applications) {
        this.applications = Collections.unmodifiableMap(applications);
    }

    public Map<ApplicationInstanceReference, ApplicationInstance> getAllApplicationInstances() {
        return applications;
    }

    public Optional<ApplicationInstance> getApplicationInstance(ApplicationInstanceReference reference) {
        return Optional.ofNullable(applications.get(reference));
    }

    public Map<HostName, List<ServiceInstance>> getServiceInstancesByHostName() {
        return applications.values().stream()
                .flatMap(application -> application.serviceClusters().stream())
                .flatMap(cluster -> cluster.serviceInstances().stream())
                .collect(Collectors.groupingBy(ServiceInstance::hostName, Collectors.toList()));
    }
}