Bug 3067: Improved error reporting in Binding Data Codec
[mdsal.git] / code-generator / binding-data-codec / src / main / java / org / opendaylight / yangtools / binding / data / codec / impl / MissingSchemaException.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.binding.data.codec.impl;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Set;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
16
17 /**
18  * Thrown when codec was used with data which are not modeled
19  * and available in schema used by codec.
20  *
21  */
22 public class MissingSchemaException extends IllegalArgumentException {
23
24     private static final long serialVersionUID = 1L;
25
26     protected MissingSchemaException(final String msg) {
27         super(msg);
28     }
29
30     private static MissingSchemaException create(final String format, final Object... args) {
31         return new MissingSchemaException(String.format(format, args));
32     }
33
34     static void checkModulePresent(final SchemaContext schemaContext, final QName name) {
35         if(schemaContext.findModuleByNamespaceAndRevision(name.getNamespace(), name.getRevision()) == null) {
36             throw MissingSchemaException.create("Module %s is not present in current schema context.",name.getModule());
37         }
38     }
39
40     static void checkModulePresent(final SchemaContext schemaContext, final YangInstanceIdentifier.PathArgument child) {
41         checkModulePresent(schemaContext, extractName(child));
42     }
43
44     private static QName extractName(final PathArgument child) {
45         if(child instanceof YangInstanceIdentifier.AugmentationIdentifier) {
46             final Set<QName> children = ((YangInstanceIdentifier.AugmentationIdentifier) child).getPossibleChildNames();
47             Preconditions.checkArgument(!children.isEmpty(),"Augmentation without childs must not be used in data");
48             return children.iterator().next();
49         }
50         return child.getNodeType();
51     }
52
53
54
55
56 }