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

import com.yahoo.component.Version;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.vespa.config.server.NotFoundException;

import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Used during config request handling to route to the right config model
 * based on application id and version.
 * 
 * @author Vegard Sjonfjell
 */
public final class ApplicationMapper {

    private final Map<ApplicationId, ApplicationVersions> requestHandlers = new ConcurrentHashMap<>();

    private ApplicationVersions applicationVersions(ApplicationId applicationId) {
        ApplicationVersions versions = requestHandlers.get(applicationId);
        if (versions == null) throw new NotFoundException("No such application id: " + applicationId);

        return versions;
    }

    /**
     * Register an Application to an application id and specific vespa version
     */
    public void register(ApplicationId applicationId, ApplicationVersions applicationVersions) {
        requestHandlers.put(applicationId, applicationVersions);
    }

    /**
     * Remove all applications associated with this application id
     */
    public void remove(ApplicationId applicationId) {
        requestHandlers.remove(applicationId);
    }

    /**
     * Retrieve the Application corresponding to this application id and specified vespa version.
     *
     * @return the matching application, or null if none matches
     */
    public Application getForVersion(ApplicationId applicationId, Optional<Version> vespaVersion, Instant now) throws VersionDoesNotExistException {
        return applicationVersions(applicationId).getForVersionOrLatest(vespaVersion, now);
    }

    /** Returns whether this registry has an application for the given application id */
    public boolean hasApplication(ApplicationId applicationId, Instant now) {
        return hasApplicationForVersion(applicationId, Optional.empty(), now);
    }

    /** Returns whether this registry has an application for the given application id and vespa version */
    public boolean hasApplicationForVersion(ApplicationId applicationId, Optional<Version> vespaVersion, Instant now) {
        try {
            return getForVersion(applicationId, vespaVersion, now) != null;
        }
        catch (VersionDoesNotExistException | NotFoundException ex) {
            return false;
        }
    }

    /**
     * Get the number of applications registered
     */
    public int numApplications() {
        return requestHandlers.size();
    }

    public Set<ApplicationId> listApplicationIds() {
        return Collections.unmodifiableSet(requestHandlers.keySet());
    }

    public List<Application> listApplications(ApplicationId applicationId) {
        return requestHandlers.get(applicationId).applications();
    }

}