/* * Copyright (c) 2018 Pantheon Technologies, s.r.o. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.mdsal.binding.generator.impl; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; import java.util.Collection; import java.util.HashMap; import java.util.IdentityHashMap; import java.util.Map; import java.util.Map.Entry; import org.eclipse.jdt.annotation.NonNull; import org.opendaylight.mdsal.binding.model.api.JavaTypeName; import org.opendaylight.mdsal.binding.model.api.Type; import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder; import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase; import org.opendaylight.mdsal.binding.model.api.type.builder.TypeMemberBuilder; import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeTypes; import org.opendaylight.mdsal.binding.yang.types.RuntimeTypeProvider; import org.opendaylight.yangtools.concepts.Builder; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode; import org.opendaylight.yangtools.yang.model.api.DocumentedNode; import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus; import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.RpcDefinition; import org.opendaylight.yangtools.yang.model.api.SchemaNode; final class RuntimeTypeGenerator extends AbstractTypeGenerator { RuntimeTypeGenerator(final EffectiveModelContext context, final Map renames) { super(context, new RuntimeTypeProvider(context, renames), renames); } @NonNull BindingRuntimeTypes toTypeMapping() { final Map augmentationToSchema = new HashMap<>(); final Map typeToSchema = new HashMap<>(); final Multimap choiceToCases = HashMultimap.create(); final Map identities = new HashMap<>(); // Note: we are keying through WithStatus, but these nodes compare on semantics, so equivalent schema nodes // can result in two distinct types. We certainly need to keep them separate. final Map schemaToType = new IdentityHashMap<>(); /* * Fun parts are here. ModuleContext maps have Builders in them, we want plain types. We may encounter each * builder multiple times, hence we keep a builder->instance cache. */ final Map builderToType = new IdentityHashMap<>(); for (final ModuleContext ctx : moduleContexts()) { for (Entry e : ctx.getTypeToAugmentation().entrySet()) { augmentationToSchema.put(builtType(builderToType, e.getKey()), e.getValue()); } for (Entry e : ctx.getTypeToSchema().entrySet()) { final Type type = builtType(builderToType, e.getKey()); typeToSchema.put(type, e.getValue()); schemaToType.put(e.getValue(), type); } for (Entry e : ctx.getChoiceToCases().entries()) { choiceToCases.put(builtType(builderToType, e.getKey()), builtType(builderToType, e.getValue())); } for (Entry e : ctx.getIdentities().entrySet()) { identities.put(e.getKey(), builtType(builderToType, e.getValue())); } } return new BindingRuntimeTypes(schemaContext(), augmentationToSchema, typeToSchema, schemaToType, choiceToCases, identities); } private static Type builtType(final Map knownTypes, final Type type) { if (type instanceof Builder) { final Type existing = knownTypes.get(type); if (existing != null) { return existing; } final Type built = (Type) ((Builder)type).build(); knownTypes.put(type, built); return built; } return type; } @Override void addCodegenInformation(final GeneratedTypeBuilderBase genType, final Module module) { // No-op } @Override void addCodegenInformation(final GeneratedTypeBuilderBase genType, final Module module, final SchemaNode node) { // No-op } @Override void addCodegenInformation(final GeneratedTypeBuilder interfaceBuilder, final Module module, final String string, final Collection nodes) { // No-op } @Override void addComment(final TypeMemberBuilder genType, final DocumentedNode node) { // No-op } @Override void addRpcMethodComment(final TypeMemberBuilder genType, final RpcDefinition node) { // No-op } }