Fix checkstyle in mdsal-binding-dom-codec
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / 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.mdsal.binding.dom.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.AugmentationIdentifier;
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 and available in schema used by codec.
19  */
20 public class MissingSchemaException extends IllegalArgumentException {
21
22     private static final long serialVersionUID = 1L;
23
24     protected MissingSchemaException(final String msg) {
25         super(msg);
26     }
27
28     protected MissingSchemaException(final String msg, final Throwable cause) {
29         super(msg, cause);
30     }
31
32     private static MissingSchemaException create(final String format, final Object... args) {
33         return new MissingSchemaException(String.format(format, args));
34     }
35
36     static void checkModulePresent(final SchemaContext schemaContext, final QName name) {
37         if (schemaContext.findModuleByNamespaceAndRevision(name.getNamespace(), name.getRevision()) == null) {
38             throw MissingSchemaException.create("Module %s is not present in current schema context.",
39                 name.getModule());
40         }
41     }
42
43     static void checkModulePresent(final SchemaContext schemaContext, final PathArgument child) {
44         checkModulePresent(schemaContext, extractName(child));
45     }
46
47     private static QName extractName(final PathArgument child) {
48         if (child instanceof AugmentationIdentifier) {
49             final Set<QName> children = ((AugmentationIdentifier) child).getPossibleChildNames();
50             Preconditions.checkArgument(!children.isEmpty(),"Augmentation without childs must not be used in data");
51             return children.iterator().next();
52         }
53         return child.getNodeType();
54     }
55 }