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