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

import com.yahoo.config.application.api.DeploymentInstanceSpec;
import com.yahoo.config.application.api.DeploymentSpec;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.SystemName;
import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.vespa.hosted.controller.api.integration.deployment.JobType;
import com.yahoo.vespa.hosted.controller.application.Deployment;
import com.yahoo.vespa.hosted.controller.application.JobStatus;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.collectingAndThen;

/**
 * This class provides helper methods for reading a deployment spec.
 *
 * @author mpolden
 */
public class DeploymentSteps {

    private final DeploymentInstanceSpec spec;
    private final Supplier<SystemName> system;

    public DeploymentSteps(DeploymentInstanceSpec spec, Supplier<SystemName> system) {
        this.spec = Objects.requireNonNull(spec, "spec cannot be null");
        this.system = Objects.requireNonNull(system, "system cannot be null");
    }

    /** Returns jobs for this, in the order they should run */
    public List<JobType> jobs() {
        return Stream.concat(production().isEmpty() ? Stream.of() : Stream.of(JobType.systemTest, JobType.stagingTest),
                             spec.steps().stream().flatMap(step -> toJobs(step).stream()))
                     .distinct()
                     .collect(Collectors.toUnmodifiableList());
    }

    /** Returns job status sorted according to deployment spec */
    public List<JobStatus> sortedJobs(Collection<JobStatus> jobStatus) {
        List<JobType> sortedJobs = jobs();
        return jobStatus.stream()
                        .sorted(comparingInt(job -> sortedJobs.indexOf(job.type())))
                        .collect(Collectors.toUnmodifiableList());
    }

    /** Returns deployments sorted according to declared zones */
    public List<Deployment> sortedDeployments(Collection<Deployment> deployments) {
        List<ZoneId> productionZones = spec.zones().stream()
                                           .filter(z -> z.region().isPresent())
                                           .map(z -> ZoneId.from(z.environment(), z.region().get()))
                                           .collect(Collectors.toUnmodifiableList());
        return deployments.stream()
                          .sorted(comparingInt(deployment -> productionZones.indexOf(deployment.zone())))
                          .collect(collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
    }

    /** Resolve jobs from step */
    public List<JobType> toJobs(DeploymentSpec.Step step) {
        return step.zones().stream()
                   .map(this::toJob)
                   .flatMap(Optional::stream)
                   .collect(Collectors.toUnmodifiableList());
    }

    /** Returns test jobs to run for this spec */
    public List<JobType> testJobs() {
        return jobs().stream().filter(JobType::isTest).collect(Collectors.toUnmodifiableList());
    }

    /** Returns declared production jobs in this */
    public List<JobType> productionJobs() {
        return toJobs(production());
    }

    /** Returns declared production steps in this */
    public List<DeploymentSpec.Step> production() {
        return spec.steps().stream()
                   .filter(step -> ! isTest(step))
                   .collect(Collectors.toUnmodifiableList());
    }

    private boolean isTest(DeploymentSpec.Step step) {
        return step.concerns(Environment.test) || step.concerns(Environment.staging);
    }

    /** Resolve job from deployment zone */
    private Optional<JobType> toJob(DeploymentSpec.DeclaredZone zone) {
        return JobType.from(system.get(), zone.environment(), zone.region().orElse(null));
    }

    /** Resolve jobs from steps */
    private List<JobType> toJobs(List<DeploymentSpec.Step> steps) {
        return steps.stream()
                    .flatMap(step -> toJobs(step).stream())
                    .collect(Collectors.toUnmodifiableList());
    }

}