2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
14 import org.opendaylight.mdsal.binding.dom.codec.api.BindingIdentityCodec;
15 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
16 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
17 import org.opendaylight.yangtools.concepts.Codec;
18 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
19 import org.opendaylight.yangtools.yang.common.QName;
21 final class IdentityCodec implements Codec<QName, Class<?>>, BindingIdentityCodec {
22 private final BindingRuntimeContext context;
24 IdentityCodec(final BindingRuntimeContext context) {
25 this.context = requireNonNull(context);
29 public Class<?> deserialize(final QName input) {
30 checkArgument(input != null, "Input must not be null.");
31 return context.getIdentityClass(input);
35 public QName serialize(final Class<?> input) {
36 checkArgument(BaseIdentity.class.isAssignableFrom(input), "%s is not an identity", input);
37 return BindingReflections.findQName(input);
41 public Class<? extends BaseIdentity> toBinding(final QName qname) {
42 final Class<?> identity = context.getIdentityClass(requireNonNull(qname));
43 checkArgument(BaseIdentity.class.isAssignableFrom(identity), "%s resolves to non-identity %s", qname, identity);
44 return identity.asSubclass(BaseIdentity.class);
48 public QName fromBinding(final Class<? extends BaseIdentity> bindingClass) {
49 final QName qname = BindingReflections.getQName(bindingClass);
50 checkState(qname != null, "Failed to resolve QName of %s", bindingClass);