// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.provision.security;

import com.yahoo.config.provision.HostName;
import com.yahoo.config.provision.NodeType;

import java.util.Objects;
import java.util.Optional;

/**
 * The identity of a Vespa node
 *
 * @author bjorncs
 */
public class NodeIdentity {

    private final NodeType nodeType;
    private final String identityName;
    private final HostName hostname;

    private NodeIdentity(NodeType nodeType, String identityName, HostName hostname) {
        this.nodeType = nodeType;
        this.identityName = identityName;
        this.hostname = hostname;
    }

    public NodeType nodeType() {
        return nodeType;
    }


    public Optional<String> identityName() {
        return Optional.ofNullable(identityName);
    }

    public Optional<HostName> hostname() {
        return Optional.ofNullable(hostname);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        NodeIdentity that = (NodeIdentity) o;
        return nodeType == that.nodeType &&
                Objects.equals(identityName, that.identityName) &&
                Objects.equals(hostname, that.hostname);
    }

    @Override
    public int hashCode() {
        return Objects.hash(nodeType, identityName, hostname);
    }

    @Override
    public String toString() {
        return "NodeIdentity{" +
                "nodeType=" + nodeType +
                ", identityName='" + identityName + '\'' +
                ", hostname=" + hostname +
                '}';
    }

    public static class Builder {
        private final NodeType nodeType;
        private String identityName;
        private HostName hostname;

        public Builder(NodeType nodeType) {
            this.nodeType = nodeType;
        }

        public Builder identityName(String identityName) {
            this.identityName = identityName;
            return this;
        }

        public Builder hostname(HostName hostname) {
            this.hostname = hostname;
            return this;
        }

        public NodeIdentity build() {
            return new NodeIdentity(nodeType, identityName, hostname);
        }
    }
}

@@ -152,16 +152,65 @@
 urity;%0A%0A
+import com.yahoo.config.provision.ApplicationId;%0A
 import c
@@ -543,24 +543,71 @@
 me hostname;
+%0A    private final ApplicationId applicationId;
 %0A%0A    privat
@@ -670,32 +670,61 @@
 ostName hostname
+, ApplicationId applicationId
 ) %7B%0A        this
@@ -817,24 +817,68 @@
 = hostname;%0A
+        this.applicationId = applicationId;%0A
     %7D%0A%0A    p
@@ -1128,32 +1128,143 @@
 stname);%0A    %7D%0A%0A
+    public Optional%3CApplicationId%3E applicationId() %7B%0A        return Optional.ofNullable(applicationId);%0A    %7D%0A%0A
     @Override%0A  
@@ -1606,32 +1606,101 @@
 , that.hostname)
+ &&%0A                Objects.equals(applicationId, that.applicationId)
 ;%0A    %7D%0A%0A    @Ov
@@ -1786,32 +1786,47 @@
 tyName, hostname
+, applicationId
 );%0A    %7D%0A%0A    @O
@@ -2036,24 +2036,77 @@
  hostname +%0A
+                %22, applicationId=%22 + applicationId +%0A
             
@@ -2267,16 +2267,61 @@
 ostname;
+%0A        private ApplicationId applicationId;
 %0A%0A      
@@ -2667,32 +2667,184 @@
 his;%0A        %7D%0A%0A
+        public Builder applicationId(ApplicationId applicationId) %7B%0A            this.applicationId = applicationId;%0A            return this;%0A        %7D%0A%0A
         public N
@@ -2929,24 +2929,39 @@
 me, hostname
+, applicationId
 );%0A        %7D

Add ApplicationId to NodeIdentity