/*
 * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid;

import java.util.Map;
import java.util.Set;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;

public class DataValidationException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    public DataValidationException(final String message) {
        super(message);
    }

    public static void checkLegalChild(final boolean isLegal, final YangInstanceIdentifier.PathArgument child, final DataNodeContainer schema,
            final Set<QName> childNodes, final Set<YangInstanceIdentifier.AugmentationIdentifier> augments) {
        if (!isLegal) {
            throw new IllegalChildException(child, schema, childNodes, augments);
        }
    }

    public static void checkLegalChild(final boolean isLegal, final YangInstanceIdentifier.PathArgument child, final DataSchemaNode schema,
            final Set<QName> childNodes) {
        if (!isLegal) {
            throw new IllegalChildException(child, schema, childNodes);
        }
    }

    public static void checkLegalChild(final boolean isLegal, final YangInstanceIdentifier.PathArgument child, final ChoiceNode schema) {
        if (!isLegal) {
            throw new IllegalChildException(child, schema);
        }
    }

    public static void checkLegalData(final boolean isLegal, final String messageTemplate, final Object... messageAttrs) {
        if (!isLegal) {
            throw new DataValidationException(String.format(messageTemplate, messageAttrs));
        }
    }

    public static void checkListKey(final DataContainerChild<?, ?> childNode, final Map<QName, Object> keyValues, final QName keyQName,
            final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeId) {
        checkListKey(childNode, keyQName, nodeId);

        Object expectedValue = nodeId.getKeyValues().get(keyQName);
        Object actualValue = childNode.getValue();
    }

    public static void checkListKey(final DataContainerChild<?, ?> childNode, final QName keyQName, final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeId) {
        if (childNode == null) {
            throw new IllegalListKeyException(keyQName, nodeId);
        }
    }

    static final class IllegalChildException extends DataValidationException {
        private static final long serialVersionUID = 1L;

        public IllegalChildException(final YangInstanceIdentifier.PathArgument child, final DataNodeContainer schema,
                final Set<QName> childNodes, final Set<YangInstanceIdentifier.AugmentationIdentifier> augments) {
            super(String.format("Unknown child node: %s, does not belong to: %s as a direct child. "
                    + "Direct child nodes: %s, augmented child nodes: %s", child, schema, childNodes, augments));
        }

        public IllegalChildException(final YangInstanceIdentifier.PathArgument child, final ChoiceNode schema) {
            super(String.format("Unknown child node: %s, not detected in choice: %s", child, schema));
        }

        public IllegalChildException(final YangInstanceIdentifier.PathArgument child, final DataSchemaNode schema, final Set<QName> childNodes) {
            super(String.format("Unknown child node: %s, does not belong to: %s as a child. "
                    + "Child nodes: %s", child, schema, childNodes));
        }
    }

    static final class IllegalListKeyException extends DataValidationException {
        private static final long serialVersionUID = 1L;

        public IllegalListKeyException(final QName keyQName, final YangInstanceIdentifier.NodeIdentifierWithPredicates id) {
            super(String.format("Key value not present for key: %s, in: %s", keyQName, id));
        }

        public IllegalListKeyException(final QName keyQName, final YangInstanceIdentifier.NodeIdentifierWithPredicates id, final Object actualValue, final Object expectedValue) {
            super(String.format("Illegal value for key: %s, in: %s, actual value: %s, expected value from key: %s", keyQName, id, actualValue, expectedValue));
        }
    }
}

@@ -401,16 +401,42 @@
 il.Map;%0A
+import java.util.Objects;%0A
 import j
@@ -2492,24 +2492,30 @@
 );%0A%0A        
+final 
 Object expec
@@ -2517,21 +2517,16 @@
 expected
-Value
  = nodeI
@@ -2565,16 +2565,22 @@
         
+final 
 Object a
@@ -2584,21 +2584,16 @@
 t actual
-Value
  = child
@@ -2609,16 +2609,158 @@
 alue();%0A
+        if (!Objects.equals(expected, actual)) %7B%0A            throw new IllegalListKeyException(keyQName, nodeId, actual, expected);%0A        %7D%0A
     %7D%0A%0A 
@@ -3037,32 +3037,40 @@
    %7D%0A    %7D%0A%0A    
+private 
 static final cla
@@ -3187,37 +3187,38 @@
 = 1L;%0A%0A        p
-ublic
+rivate
  IllegalChildExc
@@ -3646,37 +3646,38 @@
     %7D%0A%0A        p
-ublic
+rivate
  IllegalChildExc
@@ -3874,37 +3874,38 @@
     %7D%0A%0A        p
-ublic
+rivate
  IllegalChildExc
@@ -4205,16 +4205,24 @@
  %7D%0A%0A    
+private 
 static f
@@ -4349,37 +4349,38 @@
 = 1L;%0A%0A        p
-ublic
+rivate
  IllegalListKeyE
@@ -4587,21 +4587,22 @@
        p
-ublic
+rivate
  Illegal

Fix checkListKey not checking actual/expected values