43adf9ef3b048e4de0c3f64e10b3322f20f4d358
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / MissingSchemaForClassException.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 org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
12 import org.opendaylight.yangtools.yang.binding.Augmentation;
13
14 /**
15  * Thrown when Java Binding class was used in data for which codec does not
16  * have schema.
17  *
18  * By serialization /  deserialization of this exception {@link #getBindingClass()}
19  * will return null.
20  */
21 public class MissingSchemaForClassException extends MissingSchemaException {
22
23     private static final long serialVersionUID = 1L;
24
25     private final transient Class<?> bindingClass;
26
27     private MissingSchemaForClassException(final Class<?> clz) {
28         super(String.format("Schema is not available for %s", clz));
29         this.bindingClass = Preconditions.checkNotNull(clz);
30     }
31
32     static MissingSchemaForClassException forClass(final Class<?> clz) {
33         return new MissingSchemaForClassException(clz);
34     }
35
36     public Class<?> getBindingClass() {
37         return bindingClass;
38     }
39
40     public static void check(final BindingRuntimeContext runtimeContext, final Class<?> bindingClass) {
41         final Object schema;
42         if (Augmentation.class.isAssignableFrom(bindingClass)) {
43             schema = runtimeContext.getAugmentationDefinition(bindingClass);
44         } else {
45             schema = runtimeContext.getSchemaDefinition(bindingClass);
46         }
47         if(schema == null) {
48             throw MissingSchemaForClassException.forClass(bindingClass);
49         }
50     }
51 }