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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.yahoo.vespa.athenz.identityprovider.api.bindings.IdentityDocumentEntity;
import com.yahoo.vespa.athenz.identityprovider.api.bindings.VespaUniqueInstanceIdEntity;
import com.yahoo.vespa.athenz.identityprovider.api.bindings.SignedIdentityDocumentEntity;

/**
 * Utility class for mapping objects model types and their Jackson binding versions.
 *
 * @author bjorncs
 */
public class EntityBindingsMapper {

    private static final ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());

    private EntityBindingsMapper() {}

    public static String toAttestationData(SignedIdentityDocument model) {
        try {
            return mapper.writeValueAsString(toSignedIdentityDocumentEntity(model));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public static VespaUniqueInstanceId toVespaUniqueInstanceId(VespaUniqueInstanceIdEntity entity) {
        return new VespaUniqueInstanceId(
                entity.clusterIndex, entity.clusterId, entity.instance, entity.application, entity.tenant, entity.region, entity.environment);
    }

    public static VespaUniqueInstanceIdEntity toVespaUniqueInstanceIdEntity(VespaUniqueInstanceId model) {
        return new VespaUniqueInstanceIdEntity(
                model.tenant(), model.application(), model.environment(), model.region(),
                model.instance(), model.clusterId(), model.clusterIndex());
    }

    public static IdentityDocumentEntity toIdentityDocumentEntity(IdentityDocument model) {
        return new IdentityDocumentEntity(
                toVespaUniqueInstanceIdEntity(model.providerUniqueId()),
                model.configServerHostname(),
                model.instanceHostname(),
                model.createdAt(),
                model.ipAddresses());
    }

    public static SignedIdentityDocumentEntity toSignedIdentityDocumentEntity(SignedIdentityDocument model) {
        try {
            IdentityDocumentEntity identityDocumentEntity = toIdentityDocumentEntity(model.identityDocument());
            String rawDocument = mapper.writeValueAsString(identityDocumentEntity);
            return new SignedIdentityDocumentEntity(
                    rawDocument,
                    model.signature(),
                    model.signingKeyVersion(),
                    model.providerUniqueId().asDottedString(),
                    model.dnsSuffix(),
                    model.providerService().getFullName(),
                    model.ztsEndpoint(),
                    model.documentVersion());
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

}

@@ -338,16 +338,115 @@
 Module;%0A
+import com.yahoo.vespa.athenz.api.AthenzIdentity;%0Aimport com.yahoo.vespa.athenz.api.AthenzService;%0A
 import c
@@ -513,32 +513,32 @@
 DocumentEntity;%0A
-
 import com.yahoo
@@ -699,16 +699,70 @@
 tEntity;
+%0Aimport com.yahoo.vespa.athenz.utils.AthenzIdentities;
 %0A%0A/**%0A *
@@ -1475,16 +1475,16 @@
 anceId(%0A
-
         
@@ -1617,32 +1617,950 @@
 onment);%0A    %7D%0A%0A
+    private static IdentityDocument toIdentityDocument(IdentityDocumentEntity entity) %7B%0A        return new IdentityDocument(%0A                toVespaUniqueInstanceId(entity.providerUniqueId),%0A                entity.configServerHostname,%0A                entity.instanceHostname,%0A                entity.createdAt,%0A                entity.ipAddresses);%0A    %7D%0A%0A    public static SignedIdentityDocument toSignedIdentityDocument(SignedIdentityDocumentEntity entity) %7B%0A        return new SignedIdentityDocument(%0A                toIdentityDocument(entity.identityDocument),%0A                entity.signature,%0A                entity.signingKeyVersion,%0A                VespaUniqueInstanceId.fromDottedString(entity.providerUniqueId),%0A                entity.dnsSuffix,%0A                (AthenzService) AthenzIdentities.from(entity.providerService),%0A                entity.ztsEndpoint,%0A                entity.documentVersion);%0A    %7D%0A%0A
     public stati

Add mapping from SignedIdentityDocumentEntity to SignedIdentityDocument