2 * Copyright (c) 2017 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.javav2.java.api.generator.rangeGenerators;
10 import com.google.common.base.Preconditions;
11 import javax.annotation.Nonnull;
12 import org.opendaylight.mdsal.binding.javav2.model.api.ConcreteType;
13 import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedProperty;
14 import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedTransferObject;
15 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
18 * Random utility methods for dealing with {@link Type} objects.
20 final class TypeUtils {
21 private static final String VALUE_PROP = "value";
24 throw new UnsupportedOperationException();
28 * Given a {@link Type} object lookup the base Java type which sits at the top
29 * of its type hierarchy.
31 * @param type Input Type object
32 * @return Resolved {@link ConcreteType} instance.
34 static ConcreteType getBaseYangType(@Nonnull final Type type) {
35 // Already the correct type
36 if (type instanceof ConcreteType) {
37 return (ConcreteType) type;
40 Preconditions.checkArgument(type instanceof GeneratedTransferObject, "Unsupported type %s", type);
42 // Need to walk up the GTO chain to the root
43 GeneratedTransferObject rootGto = (GeneratedTransferObject) type;
44 while (rootGto.getSuperType() != null) {
45 rootGto = rootGto.getSuperType();
48 // Look for the 'value' property and return its type
49 for (GeneratedProperty s : rootGto.getProperties()) {
50 if (VALUE_PROP.equals(s.getName())) {
51 return (ConcreteType) s.getReturnType();
55 // Should never happen
56 throw new IllegalArgumentException(String.format("Type %s root %s properties %s do not include \"%s\"",
57 type, rootGto, rootGto.getProperties(), VALUE_PROP));