// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.lb;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.HostName;
import com.yahoo.config.provision.NodeType;
import com.yahoo.vespa.hosted.provision.Node;
import com.yahoo.vespa.hosted.provision.NodeRepository;

import java.util.Comparator;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * This code mimics provisioning load balancers that already exist in the routing layer.
 * It will just map the load balancer request to the proxy nodes available in the node repository.
 *
 * @author ogronnesby
 */
public class SharedLoadBalancerService implements LoadBalancerService {
    private final NodeRepository nodeRepository;

    public SharedLoadBalancerService(NodeRepository nodeRepository) {
        this.nodeRepository = Objects.requireNonNull(nodeRepository, "Missing nodeRepository value");
    }

    @Override
    public LoadBalancerInstance create(ApplicationId application, ClusterSpec.Id cluster, Set<Real> reals) {
        final var proxyNodes = nodeRepository.getNodes(NodeType.proxy);
        proxyNodes.sort(Comparator.comparing(Node::hostname));

        if (proxyNodes.size() == 0) {
            throw new IllegalStateException("Missing proxy nodes in nodeRepository");
        }

        final var firstProxyNode = proxyNodes.get(0);
        final var networkNames = proxyNodes.stream()
                .flatMap(node -> node.ipAddresses().stream())
                .collect(Collectors.toSet());

        return new LoadBalancerInstance(
                HostName.from(firstProxyNode.hostname()),
                Optional.empty(),
                Set.of(4080, 4443),
                networkNames,
                reals
        );
    }

    @Override
    public void remove(ApplicationId application, ClusterSpec.Id cluster) {
        // Do nothing, we have no external state to modify
    }

    @Override
    public Protocol protocol() {
        return Protocol.dualstack;
    }
}

@@ -879,16 +879,117 @@
 rvice %7B%0A
+    private static final Comparator%3CNode%3E hostnameComparator = Comparator.comparing(Node::hostname);%0A
     priv
@@ -1428,16 +1428,24 @@
 es.sort(
+hostname
 Comparat
@@ -1450,34 +1450,8 @@
 ator
-.comparing(Node::hostname)
 );%0A%0A

Make comparator static member of class