X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=binding%2Fmdsal-binding-runtime-api%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fmdsal%2Fbinding%2Fruntime%2Fapi%2FBindingRuntimeTypes.java;h=d0ac16642ab2fbe5979223eccbdfe1f15bd1d7fb;hb=8f2a259e8bf106c4b33acd8df3a7dbe19aaa43d0;hp=70622a9da979e995959e50f5ffbf95d84a463bc4;hpb=965b13e31aefdef16f70019b8883164111411d67;p=mdsal.git diff --git a/binding/mdsal-binding-runtime-api/src/main/java/org/opendaylight/mdsal/binding/runtime/api/BindingRuntimeTypes.java b/binding/mdsal-binding-runtime-api/src/main/java/org/opendaylight/mdsal/binding/runtime/api/BindingRuntimeTypes.java index 70622a9da9..d0ac16642a 100644 --- a/binding/mdsal-binding-runtime-api/src/main/java/org/opendaylight/mdsal/binding/runtime/api/BindingRuntimeTypes.java +++ b/binding/mdsal-binding-runtime-api/src/main/java/org/opendaylight/mdsal/binding/runtime/api/BindingRuntimeTypes.java @@ -7,172 +7,34 @@ */ package org.opendaylight.mdsal.binding.runtime.api; -import static java.util.Objects.requireNonNull; - import com.google.common.annotations.Beta; -import com.google.common.base.MoreObjects; -import com.google.common.collect.BiMap; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.Multimap; -import java.lang.invoke.MethodHandles; -import java.lang.invoke.VarHandle; import java.util.Collection; -import java.util.IdentityHashMap; -import java.util.Map; import java.util.Optional; -import org.eclipse.jdt.annotation.NonNull; import org.opendaylight.mdsal.binding.model.api.Type; import org.opendaylight.yangtools.concepts.Immutable; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode; import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus; -import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextProvider; -import org.opendaylight.yangtools.yang.model.api.SchemaNode; -import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * The result of BindingGenerator run. Contains mapping between Types and SchemaNodes. */ @Beta -public final class BindingRuntimeTypes implements EffectiveModelContextProvider, Immutable { - private static final Logger LOG = LoggerFactory.getLogger(BindingRuntimeTypes.class); - private static final VarHandle TYPE_TO_IDENTIFIER; - - static { - try { - TYPE_TO_IDENTIFIER = MethodHandles.lookup().findVarHandle(BindingRuntimeTypes.class, "typeToIdentifier", - ImmutableMap.class); - } catch (NoSuchFieldException | IllegalAccessException e) { - throw new ExceptionInInitializerError(e); - } - } - - private final @NonNull EffectiveModelContext schemaContext; - private final ImmutableMap typeToAugmentation; - private final ImmutableMap typeToSchema; - private final ImmutableMultimap choiceToCases; - private final ImmutableMap identities; - // Not Immutable as we use two different implementations - private final Map schemaToType; - - @SuppressWarnings("unused") - // Accessed via TYPE_TO_IDENTIFIER - private volatile ImmutableMap typeToIdentifier = ImmutableMap.of(); - - public BindingRuntimeTypes(final EffectiveModelContext schemaContext, - final Map typeToAugmentation, - final Map typeToSchema, final Map schemaToType, - final Multimap choiceToCases, final Map identities) { - this.schemaContext = requireNonNull(schemaContext); - this.typeToAugmentation = ImmutableMap.copyOf(typeToAugmentation); - this.typeToSchema = ImmutableMap.copyOf(typeToSchema); - this.choiceToCases = ImmutableMultimap.copyOf(choiceToCases); - this.identities = ImmutableMap.copyOf(identities); - - // Careful to use identity for SchemaNodes, but only if needed - // FIXME: 8.0.0: YT should be switching to identity for equals(), so this should become unnecessary - Map copy; - try { - copy = ImmutableMap.copyOf(schemaToType); - } catch (IllegalArgumentException e) { - LOG.debug("Equality-duplicates found in {}", schemaToType.keySet()); - copy = new IdentityHashMap<>(schemaToType); - } - - this.schemaToType = copy; - } - - public BindingRuntimeTypes(final EffectiveModelContext schemaContext, - final Map typeToAugmentation, - final BiMap typeToDefiningSchema, - final Multimap choiceToCases, final Map identities) { - this(schemaContext, typeToAugmentation, typeToDefiningSchema, typeToDefiningSchema.inverse(), choiceToCases, - identities); - } - - @Override - public EffectiveModelContext getEffectiveModelContext() { - return schemaContext; - } - - public Optional findAugmentation(final Type type) { - return Optional.ofNullable(typeToAugmentation.get(type)); - } - - public Optional findIdentity(final QName qname) { - return Optional.ofNullable(identities.get(qname)); - } - - public Optional findSchema(final Type type) { - return Optional.ofNullable(typeToSchema.get(type)); - } - - public Optional findSchemaNodeIdentifier(final Type type) { - final ImmutableMap local = (ImmutableMap) TYPE_TO_IDENTIFIER.getAcquire(this); - final Absolute existing = local.get(type); - return existing != null ? Optional.of(existing) : loadSchemaNodeIdentifier(local, type); - } - - public Optional findType(final WithStatus schema) { - return Optional.ofNullable(schemaToType.get(schema)); - } - - public Multimap getChoiceToCases() { - return choiceToCases; - } - - public Collection findCases(final Type choiceType) { - return choiceToCases.get(choiceType); - } +public interface BindingRuntimeTypes extends EffectiveModelContextProvider, Immutable { - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .add("typeToAugmentation", typeToAugmentation) - .add("typeToSchema", typeToSchema) - .add("choiceToCases", choiceToCases) - .add("identities", identities) - .toString(); - } + Optional findAugmentation(Type type); - private Optional loadSchemaNodeIdentifier(final ImmutableMap local, final Type type) { - final WithStatus schema = typeToSchema.get(type); - if (!(schema instanceof SchemaNode)) { - return Optional.empty(); - } + Optional findIdentity(QName qname); - // TODO: do not rely on getPath() here - final Absolute created = Absolute.of(ImmutableList.copyOf(((SchemaNode) schema).getPath().getPathFromRoot())) - .intern(); + Optional findSchema(Type type); - ImmutableMap prev = local; - while (true) { - // Compute next cache - final ImmutableMap next = - ImmutableMap.builderWithExpectedSize(prev.size() + 1) - .putAll(prev) - .put(type, created).build(); + Optional findType(WithStatus schema); - final Object witness = TYPE_TO_IDENTIFIER.compareAndExchangeRelease(this, prev, next); - if (witness == prev) { - // Cache populated successfully, we are all done now - return Optional.of(created); - } + Optional findOriginalAugmentationType(AugmentationSchemaNode augment); - // Remember cache for next computation - prev = (ImmutableMap) witness; - final Absolute raced = prev.get(type); - if (raced != null) { - // We have raced on this item, use it from cache - return Optional.of(raced); - } + Multimap getChoiceToCases(); - // We have raced on a different item, loop around and repeat - } - } + Collection findCases(Type choiceType); }