package com.yahoo.vespa.hosted.controller.application;

import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.vespa.hosted.controller.rotation.RotationId;

import java.util.Objects;

/**
 * Contains the tuple of [clusterId, endpointId, rotationId], to keep track
 * of which services have assigned which rotations under which name.
 *
 * @author ogronnesby
 */
public class AssignedRotation {
    private final ClusterSpec.Id clusterId;
    private final EndpointId endpointId;
    private final RotationId rotationId;

    public AssignedRotation(ClusterSpec.Id clusterId, EndpointId endpointId, RotationId rotationId) {
        this.clusterId = requireNonEmpty(clusterId, clusterId.value(), "clusterId");
        this.endpointId = Objects.requireNonNull(endpointId);
        this.rotationId = Objects.requireNonNull(rotationId);
    }

    public ClusterSpec.Id clusterId() { return clusterId; }
    public EndpointId endpointId() { return endpointId; }
    public RotationId rotationId() { return rotationId; }

    @Override
    public String toString() {
        return "AssignedRotation{" +
                "clusterId=" + clusterId +
                ", endpointId='" + endpointId + '\'' +
                ", rotationId=" + rotationId +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        AssignedRotation that = (AssignedRotation) o;
        return clusterId.equals(that.clusterId) &&
                endpointId.equals(that.endpointId) &&
                rotationId.equals(that.rotationId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(clusterId, endpointId, rotationId);
    }

    private static <T> T requireNonEmpty(T object, String value, String field) {
        Objects.requireNonNull(object);
        Objects.requireNonNull(value);
        if (value.isEmpty()) {
            throw new IllegalArgumentException("Field '" + field + "' was empty");
        }
        return object;
    }
}

@@ -96,16 +96,62 @@
 erSpec;%0A
+import com.yahoo.config.provision.RegionName;%0A
 import c
@@ -230,16 +230,38 @@
 Objects;
+%0Aimport java.util.Set;
 %0A%0A/**%0A *
@@ -317,16 +317,27 @@
 tationId
+, regions%5B%5D
 %5D, to ke
@@ -600,16 +600,59 @@
 ationId;
+%0A    private final Set%3CRegionName%3E regions;
 %0A%0A    pu
@@ -743,16 +743,41 @@
 tationId
+, Set%3CRegionName%3E regions
 ) %7B%0A    
@@ -973,32 +973,100 @@
 ll(rotationId);%0A
+        this.regions = Set.copyOf(Objects.requireNonNull(regions));%0A
     %7D%0A%0A    publi
@@ -1231,16 +1231,73 @@
 ionId; %7D
+%0A    public Set%3CRegionName%3E regions() %7B return regions; %7D
 %0A%0A    @O
@@ -1513,24 +1513,65 @@
 otationId +%0A
+                %22, regions=%22 + regions +%0A
             
@@ -1942,32 +1942,80 @@
 that.rotationId)
+ &&%0A                regions.equals(that.regions)
 ;%0A    %7D%0A%0A    @Ov
@@ -2106,24 +2106,33 @@
 , rotationId
+, regions
 );%0A    %7D%0A%0A  

Put information on assigned regions in AssignedRotation class