public KeytabPrincipalFindOrCreateResult findOrCreate(KerberosKeytabEntity kerberosKeytabEntity,
                                                        HostEntity hostEntity, KerberosPrincipalEntity kerberosPrincipalEntity,
                                                        @Nullable List<KerberosKeytabPrincipalEntity> keytabList) {
    KeytabPrincipalFindOrCreateResult result = new KeytabPrincipalFindOrCreateResult();
    result.created = false;

    Long hostId = hostEntity == null ? null : hostEntity.getHostId();
    // The DB requests should be avoided due to heavy impact on the performance
    KerberosKeytabPrincipalEntity kkp = (keytabList == null || keytabList.isEmpty())
      ? findByNaturalKey(hostId, kerberosKeytabEntity.getKeytabPath(), kerberosPrincipalEntity.getPrincipalName())
      : keytabList.stream()
      .filter(keytab ->
        keytab != null
          && keytab.getHostId() != null
          && keytab.getKeytabPath() != null
          && keytab.getPrincipalName() != null
          && keytab.getHostId().equals(hostId)
          && keytab.getKeytabPath().equals(kerberosKeytabEntity.getKeytabPath())
          && keytab.getPrincipalName().equals(kerberosPrincipalEntity.getPrincipalName())
      )
      .findFirst()
      .orElse(null);

    if (kkp != null) {
      result.kkp = kkp;
      return result;
    }

    kkp = new KerberosKeytabPrincipalEntity(
        kerberosKeytabEntity,
        hostEntity,
        kerberosPrincipalEntity
    );
    create(kkp);

    kerberosKeytabEntity.addKerberosKeytabPrincipal(kkp);
    kerberosPrincipalEntity.addKerberosKeytabPrincipal(kkp);

    result.kkp = kkp;
    result.created = true;
    return result;
  }
Find or create {@link KerberosKeytabPrincipalEntity} with specified dependencies.

@param kerberosKeytabEntity {@link KerberosKeytabEntity} which owns this principal
@param hostEntity           {@link HostEntity} which owns this principal
@param kerberosPrincipalEntity      {@link KerberosPrincipalEntity} which related to this principal
@return evaluated entity
ambari-server/src/main/java/org/apache/ambari/server/orm/dao/KerberosKeytabPrincipalDAO.java