summaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/pricing/PricingApiHandler.java
blob: b6b3c8584fd27ba5ef6f2260dd92d0c33be48ec8 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.restapi.pricing;

import com.yahoo.collections.Pair;
import com.yahoo.component.annotation.Inject;
import com.yahoo.config.provision.ClusterResources;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.container.jdisc.ThreadedHttpRequestHandler;
import com.yahoo.restapi.ErrorResponse;
import com.yahoo.restapi.Path;
import com.yahoo.restapi.SlimeJsonResponse;
import com.yahoo.slime.Cursor;
import com.yahoo.slime.Slime;
import com.yahoo.text.Text;
import com.yahoo.vespa.hosted.controller.Controller;
import com.yahoo.vespa.hosted.controller.api.integration.billing.Plan;
import com.yahoo.vespa.hosted.controller.api.integration.pricing.PriceInformation;
import com.yahoo.vespa.hosted.controller.api.integration.pricing.PricingInfo;
import com.yahoo.vespa.hosted.controller.restapi.ErrorResponses;
import com.yahoo.yolean.Exceptions;

import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import static com.yahoo.jdisc.http.HttpRequest.Method.GET;
import static com.yahoo.restapi.ErrorResponse.methodNotAllowed;
import static com.yahoo.vespa.hosted.controller.api.integration.pricing.PricingInfo.SupportLevel;
import static java.lang.Double.parseDouble;
import static java.lang.Integer.parseInt;
import static java.nio.charset.StandardCharsets.UTF_8;

/**
 * API for calculating price information
 *
 * @author hmusum
 */
@SuppressWarnings("unused") // Handler
public class PricingApiHandler extends ThreadedHttpRequestHandler {

    private static final Logger log = Logger.getLogger(PricingApiHandler.class.getName());
    private static final BigDecimal SCALED_ZERO = BigDecimal.ZERO.setScale(2);

    private final Controller controller;

    @Inject
    public PricingApiHandler(Context parentCtx, Controller controller) {
        super(parentCtx);
        this.controller = controller;
    }

    @Override
    public HttpResponse handle(HttpRequest request) {
        if (request.getMethod() != GET)
            return methodNotAllowed("Method '" + request.getMethod() + "' is not supported");

        try {
            return handleGET(request);
        } catch (IllegalArgumentException e) {
            return ErrorResponse.badRequest(Exceptions.toMessageString(e));
        } catch (RuntimeException e) {
            return ErrorResponses.logThrowing(request, log, e);
        }
    }

    private HttpResponse handleGET(HttpRequest request) {
        Path path = new Path(request.getUri());
        if (path.matches("/pricing/v1/pricing")) return pricing(request);

        return ErrorResponse.notFoundError(Text.format("No '%s' handler at '%s'", request.getMethod(),
                                                       request.getUri().getPath()));
    }

    private HttpResponse pricing(HttpRequest request) {
        String rawQuery = request.getUri().getRawQuery();
        PriceInformation price = parseQuery(rawQuery);
        return response(price);
    }

    private PriceInformation parseQuery(String rawQuery) {
        String[] elements = URLDecoder.decode(rawQuery, UTF_8).split("&");
        if (elements.length == 0) throw new IllegalArgumentException("no price information found in query");

        var supportLevel = SupportLevel.BASIC;
        var enclave = false;
        var committedSpend = 0d;
        var plan = controller.serviceRegistry().planRegistry().defaultPlan();  // fallback to default plan if not supplied
        List<ClusterResources> clusterResources = new ArrayList<>();

        for (Pair<String, String> entry : keysAndValues(elements)) {
            switch (entry.getFirst()) {
                case "committedSpend" -> committedSpend = parseDouble(entry.getSecond());
                case "enclave" -> enclave = Boolean.parseBoolean(entry.getSecond());
                case "planId" -> plan = plan(entry.getSecond())
                        .orElseThrow(() -> new IllegalArgumentException("Unknown plan id " + entry.getSecond()));
                case "supportLevel" -> supportLevel = SupportLevel.valueOf(entry.getSecond().toUpperCase());
                case "resources" -> clusterResources.add(clusterResources(entry.getSecond()));
            }
        }
        if (clusterResources.size() < 1) throw new IllegalArgumentException("No cluster resources found in query");

        PricingInfo pricingInfo = new PricingInfo(enclave, supportLevel, committedSpend);
        return controller.serviceRegistry().pricingController().price(clusterResources, pricingInfo, plan);
    }

    private ClusterResources clusterResources(String resourcesString) {
        String[] elements = resourcesString.split(",");
        if (elements.length == 0)
            throw new IllegalArgumentException("nothing found in cluster resources: " + resourcesString);

        var nodes = 0;
        var vcpu = 0d;
        var memoryGb = 0d;
        var diskGb = 0d;
        var gpuMemoryGb = 0d;

        for (var element : keysAndValues(elements)) {
            switch (element.getFirst()) {
                case "nodes" -> nodes = parseInt(element.getSecond());
                case "vcpu" -> vcpu = parseDouble(element.getSecond());
                case "memoryGb" -> memoryGb = parseDouble(element.getSecond());
                case "diskGb" -> diskGb = parseDouble(element.getSecond());
                case "gpuMemoryGb" -> gpuMemoryGb = parseDouble(element.getSecond());
            }
        }

        var nodeResources = new NodeResources(vcpu, memoryGb, diskGb, 0); // 0 bandwidth, not used in price calculation
        if (gpuMemoryGb > 0)
            nodeResources = nodeResources.with(new NodeResources.GpuResources(1, gpuMemoryGb));
        return new ClusterResources(nodes, 1, nodeResources);
    }

    private List<Pair<String, String>> keysAndValues(String[] elements) {
        return Arrays.stream(elements).map(element -> {
                    var index = element.indexOf("=");
                    if (index <= 0 ) throw new IllegalArgumentException("Error in query parameter, expected '=' between key and value: " + element);
                    return new Pair<>(element.substring(0, index), element.substring(index + 1));
                })
                .collect(Collectors.toList());
    }

    private Optional<Plan> plan(String element) {
        return controller.serviceRegistry().planRegistry().plan(element);
    }

    private static SlimeJsonResponse response(PriceInformation priceInfo) {
        var slime = new Slime();
        Cursor cursor = slime.setObject();

        var array = cursor.setArray("priceInfo");
        addItem(array, "List price", priceInfo.listPrice());
        addItem(array, "Enclave discount", priceInfo.enclaveDiscount());
        addItem(array, "Volume discount", priceInfo.volumeDiscount());
        addItem(array, "Committed spend", priceInfo.committedAmountDiscount());

        cursor.setString("totalAmount", priceInfo.totalAmount().toPlainString());

        return new SlimeJsonResponse(slime);
    }

    private static void addItem(Cursor array, String name, BigDecimal amount) {
        if (amount.compareTo(BigDecimal.ZERO) != 0) {
            var o = array.addObject();
            o.setString("description", name);
            o.setString("amount", SCALED_ZERO.add(amount).toPlainString());
        }
    }

}