Binding2 runtime - Codecs impl #2
[mdsal.git] / binding2 / mdsal-binding2-dom-codec / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / codec / impl / MissingSchemaException.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.mdsal.binding.javav2.dom.codec.impl;
10
11 import com.google.common.base.Preconditions;
12 import java.util.Set;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
16 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
17
18 /**
19  * Thrown when codec was used with data which are not modeled
20  * and available in schema used by codec.
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     protected MissingSchemaException(final String msg, final Throwable cause) {
31         super(msg, cause);
32     }
33
34     private static MissingSchemaException create(final String format, final Object... args) {
35         return new MissingSchemaException(String.format(format, args));
36     }
37
38     public static void checkModulePresent(final SchemaContext schemaContext, final QName name) {
39         if(schemaContext.findModuleByNamespaceAndRevision(name.getNamespace(), name.getRevision()) == null) {
40             throw MissingSchemaException.create("Module %s is not present in current schema context.",name.getModule());
41         }
42     }
43
44     public static void checkModulePresent(final SchemaContext schemaContext, final YangInstanceIdentifier.PathArgument
45             child) {
46         checkModulePresent(schemaContext, extractName(child));
47     }
48
49     private static QName extractName(final PathArgument child) {
50         if(child instanceof YangInstanceIdentifier.AugmentationIdentifier) {
51             final Set<QName> children = ((YangInstanceIdentifier.AugmentationIdentifier) child).getPossibleChildNames();
52             Preconditions.checkArgument(!children.isEmpty(),"Augmentation without childs must not be used in data");
53             return children.iterator().next();
54         }
55         return child.getNodeType();
56     }
57 }