Add AbstractCodecFactory.LeafrefResolver
[yangtools.git] / yang / yang-data-codec-xml / src / main / java / org / opendaylight / yangtools / yang / data / codec / xml / XmlCodecFactory.java
index 726f013912bab857120cf4904ab4d3923bf34c54..18a2d4c4da3303e92168ccf175196c25c37bdc70 100644 (file)
 
 package org.opendaylight.yangtools.yang.data.codec.xml;
 
+import static com.google.common.base.Verify.verifyNotNull;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.annotations.Beta;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Verify;
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
-import java.util.AbstractMap.SimpleImmutableEntry;
-import java.util.Map.Entry;
-import javax.annotation.Nonnull;
-import javax.annotation.concurrent.ThreadSafe;
-import javax.xml.namespace.NamespaceContext;
-import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamWriter;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
+import java.util.List;
+import org.opendaylight.yangtools.rcf8528.data.util.EmptyMountPointContext;
+import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
+import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.data.impl.codec.AbstractIntegerStringCodec;
+import org.opendaylight.yangtools.yang.data.impl.codec.BinaryStringCodec;
+import org.opendaylight.yangtools.yang.data.impl.codec.BitsStringCodec;
+import org.opendaylight.yangtools.yang.data.impl.codec.BooleanStringCodec;
+import org.opendaylight.yangtools.yang.data.impl.codec.DecimalStringCodec;
+import org.opendaylight.yangtools.yang.data.impl.codec.EnumStringCodec;
+import org.opendaylight.yangtools.yang.data.impl.codec.StringStringCodec;
+import org.opendaylight.yangtools.yang.data.util.codec.AbstractCodecFactory;
+import org.opendaylight.yangtools.yang.data.util.codec.SharedCodecCache;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+import org.opendaylight.yangtools.yang.model.api.TypeAware;
+import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.UnknownTypeDefinition;
 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
+/**
+ * A thread-safe factory for instantiating {@link XmlCodec}s.
+ */
 @Beta
-@ThreadSafe
-public final class XmlCodecFactory {
-
-    private static final Logger LOG = LoggerFactory.getLogger(XmlCodecFactory.class);
-    private static final XmlCodec<Object> NULL_CODEC = new XmlCodec<Object>() {
-        @Override
-        public Object deserialize(final String input) {
-            return null;
-        }
-
-        @Override
-        public String serialize(final Object input) {
-            return null;
-        }
-
-        @Override
-        public void serializeToWriter(final XMLStreamWriter writer, final Object value) throws XMLStreamException {
-            // NOOP since codec is unkwown.
-            LOG.warn("Call of the serializeToWriter method on XmlCodecFactory.NULL_CODEC object. No operation " +
-                    "performed.");
-        }
-    };
-
-    private final LoadingCache<Entry<TypedSchemaNode, NamespaceContext>, XmlCodec<?>> codecs = CacheBuilder.newBuilder()
-            .softValues().build(new CacheLoader<Entry<TypedSchemaNode, NamespaceContext>, XmlCodec<?>>() {
-                @Override
-                public XmlCodec<?> load(@Nonnull final Entry<TypedSchemaNode, NamespaceContext> pair) {
-                    final TypedSchemaNode schemaNode = pair.getKey();
-                    final TypeDefinition<?> type = schemaNode.getType();
-                    return createCodec(schemaNode, type, pair.getValue());
-                }
-            });
-
-    private final SchemaContext schemaContext;
-
-    private XmlCodecFactory(final SchemaContext context) {
-        this.schemaContext = Preconditions.checkNotNull(context);
+public final class XmlCodecFactory extends AbstractCodecFactory<XmlCodec<?>> {
+    private final MountPointContext mountCtx;
+
+    private XmlCodecFactory(final MountPointContext mountCtx) {
+        super(mountCtx.getEffectiveModelContext(), new SharedCodecCache<>());
+        this.mountCtx = requireNonNull(mountCtx);
+    }
+
+    MountPointContext mountPointContext() {
+        return mountCtx;
     }
 
     /**
      * Instantiate a new codec factory attached to a particular context.
      *
-     * @param context SchemaContext to which the factory should be bound
+     * @param context MountPointContext to which the factory should be bound
      * @return A codec factory instance.
      */
-    public static XmlCodecFactory create(final SchemaContext context) {
+    public static XmlCodecFactory create(final MountPointContext context) {
         return new XmlCodecFactory(context);
     }
 
-    private XmlCodec<?> createCodec(final DataSchemaNode key, final TypeDefinition<?> type,
-                                    final NamespaceContext namespaceContext) {
-        if (type instanceof LeafrefTypeDefinition) {
-            return createReferencedTypeCodec(key, (LeafrefTypeDefinition) type, namespaceContext);
-        } else if (type instanceof IdentityrefTypeDefinition) {
-            return createIdentityrefTypeCodec(key, namespaceContext);
-        } else if (type instanceof UnionTypeDefinition) {
-            return createUnionTypeCodec(key, (UnionTypeDefinition)type, namespaceContext);
-        }
-        return createFromSimpleType(key, type, namespaceContext);
+    /**
+     * Instantiate a new codec factory attached to a particular context.
+     *
+     * @param context SchemaContext to which the factory should be bound
+     * @return A codec factory instance.
+     */
+    public static XmlCodecFactory create(final EffectiveModelContext context) {
+        return create(new EmptyMountPointContext(context));
     }
 
-    private XmlCodec<?> createReferencedTypeCodec(final DataSchemaNode schema, final LeafrefTypeDefinition type,
-                                                  final NamespaceContext namespaceContext) {
-        // FIXME: Verify if this does indeed support leafref of leafref
-        final TypeDefinition<?> referencedType =
-                SchemaContextUtil.getBaseTypeForLeafRef(type, getSchemaContext(), schema);
-        Verify.verifyNotNull(referencedType, "Unable to find base type for leafref node '%s'.", schema.getPath());
-        return createCodec(schema, referencedType, namespaceContext);
+    @Override
+    protected XmlCodec<?> binaryCodec(final BinaryTypeDefinition type) {
+        return new QuotedXmlCodec<>(BinaryStringCodec.from(type));
     }
 
-    public XmlCodec<QName> createIdentityrefTypeCodec(final DataSchemaNode schema,
-                                                      final NamespaceContext namespaceContext) {
-        final XmlCodec<QName> xmlStringIdentityrefCodec =
-                new XmlStringIdentityrefCodec(getSchemaContext(), schema.getQName().getModule(), namespaceContext);
-        return xmlStringIdentityrefCodec;
+    @Override
+    protected XmlCodec<?> booleanCodec(final BooleanTypeDefinition type) {
+        return new BooleanXmlCodec(BooleanStringCodec.from(type));
     }
 
-    private XmlCodec<Object> createUnionTypeCodec(final DataSchemaNode schema, final UnionTypeDefinition type,
-                                                  final NamespaceContext namespaceContext) {
-        final XmlCodec<Object> xmlStringUnionCodec = new XmlStringUnionCodec(schema, type, this, namespaceContext);
-        return xmlStringUnionCodec;
+    @Override
+    protected XmlCodec<?> bitsCodec(final BitsTypeDefinition type) {
+        return new QuotedXmlCodec<>(BitsStringCodec.from(type));
     }
 
-    private XmlCodec<?> createFromSimpleType(
-        final DataSchemaNode schema, final TypeDefinition<?> type,
-        final NamespaceContext namespaceContext) {
-        if (type instanceof InstanceIdentifierTypeDefinition) {
-            final XmlCodec<YangInstanceIdentifier> iidCodec = new XmlStringInstanceIdentifierCodec(schemaContext, this,
-                    namespaceContext);
-            return iidCodec;
-        }
-        if (type instanceof EmptyTypeDefinition) {
-            return XmlEmptyCodec.INSTANCE;
-        }
+    @Override
+    protected XmlCodec<?> emptyCodec(final EmptyTypeDefinition type) {
+        return EmptyXmlCodec.INSTANCE;
+    }
+
+    @Override
+    protected XmlCodec<?> enumCodec(final EnumTypeDefinition type) {
+        return new QuotedXmlCodec<>(EnumStringCodec.from(type));
+    }
+
+    @Override
+    protected XmlCodec<?> identityRefCodec(final IdentityrefTypeDefinition type, final QNameModule module) {
+        return new IdentityrefXmlCodec(getEffectiveModelContext(), module);
+    }
+
+    @Override
+    protected XmlCodec<?> instanceIdentifierCodec(final InstanceIdentifierTypeDefinition type) {
+        return new XmlStringInstanceIdentifierCodec(getEffectiveModelContext(), this);
+    }
+
+    @Override
+    protected XmlCodec<?> int8Codec(final Int8TypeDefinition type) {
+        return new NumberXmlCodec<>(AbstractIntegerStringCodec.from(type));
+    }
+
+    @Override
+    protected XmlCodec<?> int16Codec(final Int16TypeDefinition type) {
+        return new NumberXmlCodec<>(AbstractIntegerStringCodec.from(type));
+    }
+
+    @Override
+    protected XmlCodec<?> int32Codec(final Int32TypeDefinition type) {
+        return new NumberXmlCodec<>(AbstractIntegerStringCodec.from(type));
+    }
+
+    @Override
+    protected XmlCodec<?> int64Codec(final Int64TypeDefinition type) {
+        return new NumberXmlCodec<>(AbstractIntegerStringCodec.from(type));
+    }
+
+    @Override
+    protected XmlCodec<?> decimalCodec(final DecimalTypeDefinition type) {
+        return new NumberXmlCodec<>(DecimalStringCodec.from(type));
+    }
+
+    @Override
+    protected XmlCodec<?> stringCodec(final StringTypeDefinition type) {
+        return new QuotedXmlCodec<>(StringStringCodec.from(type));
+    }
+
+    @Override
+    protected XmlCodec<?> uint8Codec(final Uint8TypeDefinition type) {
+        return new NumberXmlCodec<>(AbstractIntegerStringCodec.from(type));
+    }
+
+    @Override
+    protected XmlCodec<?> uint16Codec(final Uint16TypeDefinition type) {
+        return new NumberXmlCodec<>(AbstractIntegerStringCodec.from(type));
+    }
+
+    @Override
+    protected XmlCodec<?> uint32Codec(final Uint32TypeDefinition type) {
+        return new NumberXmlCodec<>(AbstractIntegerStringCodec.from(type));
+    }
 
-        final TypeDefinitionAwareCodec<Object, ?> codec = TypeDefinitionAwareCodec.from(type);
-        if (codec == null) {
-            LOG.debug("Codec for type \"{}\" is not implemented yet.", type.getQName().getLocalName());
-            return NULL_CODEC;
-        }
-        return AbstractXmlCodec.create(codec);
+    @Override
+    protected XmlCodec<?> uint64Codec(final Uint64TypeDefinition type) {
+        return new NumberXmlCodec<>(AbstractIntegerStringCodec.from(type));
     }
 
-    SchemaContext getSchemaContext() {
-        return schemaContext;
+    @Override
+    protected XmlCodec<?> unionCodec(final UnionTypeDefinition type, final List<XmlCodec<?>> codecs) {
+        return UnionXmlCodec.create(type, codecs);
     }
 
-    XmlCodec<?> codecFor(final DataSchemaNode schema, final NamespaceContext namespaceContext) {
-        Preconditions.checkArgument(schema instanceof TypedSchemaNode, "Unsupported node type %s", schema.getClass());
-        return codecs.getUnchecked(new SimpleImmutableEntry<>((TypedSchemaNode)schema, namespaceContext));
+    @Override
+    protected XmlCodec<?> unknownCodec(final UnknownTypeDefinition type) {
+        return NullXmlCodec.INSTANCE;
     }
 
-    XmlCodec<?> codecFor(final DataSchemaNode schema, final TypeDefinition<?> unionSubType,
-                         final NamespaceContext namespaceContext) {
-        return createCodec(schema, unionSubType, namespaceContext);
+    <T extends SchemaNode & TypeAware> XmlCodec<?> codecFor(final T currentNode) {
+        return codecFor(currentNode, type -> verifyNotNull(
+            SchemaContextUtil.getBaseTypeForLeafRef(type, getEffectiveModelContext(), currentNode),
+            "Unable to find base type for leafref node %s type %s.", currentNode, type));
     }
 }