import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
-import javax.annotation.Nullable;
+import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.mdsal.binding.generator.api.BindingRuntimeTypes;
import org.opendaylight.mdsal.binding.generator.api.ClassLoadingStrategy;
import org.opendaylight.mdsal.binding.generator.impl.BindingGeneratorImpl;
import org.opendaylight.yangtools.concepts.Immutable;
import org.opendaylight.yangtools.yang.binding.Action;
import org.opendaylight.yangtools.yang.binding.Augmentation;
+import org.opendaylight.yangtools.yang.binding.Enumeration;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
new CacheLoader<QName, Class<?>>() {
@Override
public Class<?> load(final QName key) {
- final java.util.Optional<Type> identityType = runtimeTypes.findIdentity(key);
+ final Optional<Type> identityType = runtimeTypes.findIdentity(key);
checkArgument(identityType.isPresent(), "Supplied QName %s is not a valid identity", key);
try {
return strategy.loadClass(identityType.get());
}
}
- final AugmentationIdentifier identifier = new AugmentationIdentifier(childNames);
+ final AugmentationIdentifier identifier = AugmentationIdentifier.create(childNames);
final AugmentationSchemaNode proxy = new EffectiveAugmentationSchema(origSchema, realChilds);
return new SimpleEntry<>(identifier, proxy);
}
return found;
}
- private static Type referencedType(final Class<?> type) {
- return new ReferencedTypeImpl(JavaTypeName.create(type));
- }
-
/**
* Returns schema ({@link DataSchemaNode}, {@link AugmentationSchemaNode} or {@link TypeDefinition})
* from which supplied class was generated. Returned schema may be augmented with
for (final ChoiceSchemaNode choice : Iterables.filter(schema.getChildNodes(), ChoiceSchemaNode.class)) {
final ChoiceSchemaNode originalChoice = getOriginalSchema(choice);
- final java.util.Optional<Type> optType = runtimeTypes.findType(originalChoice);
+ final Optional<Type> optType = runtimeTypes.findType(originalChoice);
checkState(optType.isPresent(), "Failed to find generated type for choice %s", originalChoice);
final Type choiceType = optType.get();
*
* @param enumClass enum generated class
* @return mapped enum constants from yang with their corresponding values in generated binding classes
+ * @deprecated This method is not guaranteed to be accurate. Use {@link Enumeration#getName()} instead.
*/
+ @Deprecated(forRemoval = true)
public BiMap<String, String> getEnumMapping(final Class<?> enumClass) {
final Entry<GeneratedType, WithStatus> typeWithSchema = getTypeWithSchema(enumClass);
return getEnumMapping(typeWithSchema);
*
* @param enumClassName enum generated class name
* @return mapped enum constants from yang with their corresponding values in generated binding classes
+ * @deprecated This method is not guaranteed to be accurate. Use {@link Enumeration#getName()} instead.
*/
+ @Deprecated(forRemoval = true)
public BiMap<String, String> getEnumMapping(final String enumClassName) {
return getEnumMapping(findTypeWithSchema(enumClassName));
}
+ private static BiMap<String, String> getEnumMapping(final Entry<GeneratedType, WithStatus> typeWithSchema) {
+ final TypeDefinition<?> typeDef = (TypeDefinition<?>) typeWithSchema.getValue();
+
+ Preconditions.checkArgument(typeDef instanceof EnumTypeDefinition);
+ final EnumTypeDefinition enumType = (EnumTypeDefinition) typeDef;
+
+ final HashBiMap<String, String> mappedEnums = HashBiMap.create();
+
+ for (final EnumTypeDefinition.EnumPair enumPair : enumType.getValues()) {
+ mappedEnums.put(enumPair.getName(), BindingMapping.getClassName(enumPair.getName()));
+ }
+
+ // TODO cache these maps for future use
+ return mappedEnums;
+ }
+
private Entry<GeneratedType, WithStatus> findTypeWithSchema(final String className) {
// All we have is a straight FQCN, which we need to split into a hierarchical JavaTypeName. This involves
// some amount of guesswork -- we do that by peeling components at the dot and trying out, e.g. given
}
final Type type = new ReferencedTypeImpl(name);
- final java.util.Optional<WithStatus> optSchema = runtimeTypes.findSchema(type);
+ final Optional<WithStatus> optSchema = runtimeTypes.findSchema(type);
if (!optSchema.isPresent()) {
continue;
}
final WithStatus schema = optSchema.get();
- final java.util.Optional<Type> optDefinedType = runtimeTypes.findType(schema);
+ final Optional<Type> optDefinedType = runtimeTypes.findType(schema);
if (!optDefinedType.isPresent()) {
continue;
}
throw new IllegalArgumentException("Failed to find type for " + className);
}
- private static BiMap<String, String> getEnumMapping(final Entry<GeneratedType, WithStatus> typeWithSchema) {
- final TypeDefinition<?> typeDef = (TypeDefinition<?>) typeWithSchema.getValue();
-
- Preconditions.checkArgument(typeDef instanceof EnumTypeDefinition);
- final EnumTypeDefinition enumType = (EnumTypeDefinition) typeDef;
-
- final HashBiMap<String, String> mappedEnums = HashBiMap.create();
-
- for (final EnumTypeDefinition.EnumPair enumPair : enumType.getValues()) {
- mappedEnums.put(enumPair.getName(), BindingMapping.getClassName(enumPair.getName()));
- }
-
- // TODO cache these maps for future use
- return mappedEnums;
- }
-
public Set<Class<?>> getCases(final Class<?> choice) {
final Collection<Type> cazes = runtimeTypes.findCases(referencedType(choice));
final Set<Class<?>> ret = new HashSet<>(cazes.size());
public Class<?> getClassForSchema(final SchemaNode childSchema) {
final SchemaNode origSchema = getOriginalSchema(childSchema);
- final java.util.Optional<Type> clazzType = runtimeTypes.findType(origSchema);
+ final Optional<Type> clazzType = runtimeTypes.findType(origSchema);
checkArgument(clazzType.isPresent(), "Failed to find binding type for %s (original %s)",
childSchema, origSchema);
}
if (!augment.getChildNodes().isEmpty()) {
- final java.util.Optional<Type> augType = runtimeTypes.findType(augOrig);
+ final Optional<Type> augType = runtimeTypes.findType(augOrig);
if (augType.isPresent()) {
identifierToType.put(getAugmentationIdentifier(augment), augType.get());
}
}
private static AugmentationIdentifier getAugmentationIdentifier(final AugmentationSchemaNode augment) {
- final Set<QName> childNames = new HashSet<>();
- for (final DataSchemaNode child : augment.getChildNodes()) {
- childNames.add(child.getQName());
- }
- return new AugmentationIdentifier(childNames);
+ // FIXME: use DataSchemaContextNode.augmentationIdentifierFrom() once it does caching
+ return AugmentationIdentifier.create(augment.getChildNodes().stream().map(DataSchemaNode::getQName)
+ .collect(ImmutableSet.toImmutableSet()));
+ }
+
+ private static Type referencedType(final Class<?> type) {
+ return new ReferencedTypeImpl(JavaTypeName.create(type));
}
private static Type referencedType(final Type type) {