Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize serialization/deserialization #2

Open
jacksitlab opened this issue Jan 4, 2021 · 0 comments
Open

optimize serialization/deserialization #2

jacksitlab opened this issue Jan 4, 2021 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@jacksitlab
Copy link
Collaborator

With additional import of odl OSGi-Service
<reference id="parserFactory" interface="org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory" />
it should be much easier to serialize data like ODL does

import com.google.gson.stream.JsonWriter;
import org.opendaylight.binding.runtime.spi.BindingRuntimeHelpers;
import org.opendaylight.mdsal.binding.dom.codec.impl.BindingCodecContext;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.config.rev201208.Configuration;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.config.rev201208.ConfigurationBuilder;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.config.rev201208.configuration.EntryBuilder;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.config.rev201208.configuration.EntryKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.common.Uint8;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
import org.opendaylight.yangtools.yang.model.parser.api.YangParser;
import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
import org.opendaylight.yangtools.yang.parser.impl.YangParserFactoryImpl;
...

private void serialize(Configuration configuration) {
    final YangParser parser = this.yangParserFactory.createParser();
    final EffectiveModelContext schemaContext = parser.buildEffectiveModel();
    final NormalizedNode<?, ?> data = BINDING_CONTEXT.toNormalizedNode(
                InstanceIdentifier.create(Configuration.class), configuration).getValue();
    final String json = toJson(data, schemaContext);
}
    private static String toJson(final NormalizedNode<?, ?> node, final EffectiveModelContext schemaContext) {
        final JSONCodecFactory codecFactory = JSONCodecFactorySupplier.RFC7951.createSimple(schemaContext);
        try (StringWriter stringWriter = new StringWriter();
             JsonWriter jsonWriter = new JsonWriter(stringWriter);
             NormalizedNodeWriter nodeStreamer = NormalizedNodeWriter.forStreamWriter(
                     JSONNormalizedNodeStreamWriter.createNestedWriter(codecFactory, SchemaPath.ROOT,
                             schemaContext.getQName().getNamespace(), jsonWriter))) {
            jsonWriter.beginObject();
            nodeStreamer.write(node);
            jsonWriter.endObject();
            return stringWriter.toString();
        } catch (IOException e) {
            throw new IllegalStateException("Failed to convert input node to JSON: " + node, e);
        }
    }

Task is to replace our internal yangtools independent serializer with this one, to solve not covered bugs in the existing serializer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants