X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-clustering-commons%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fnode%2Futils%2Fstream%2FLithiumNormalizedNodeOutputStreamWriter.java;fp=opendaylight%2Fmd-sal%2Fsal-clustering-commons%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fnode%2Futils%2Fstream%2FLithiumNormalizedNodeOutputStreamWriter.java;h=e777948ca5ba552a295b9cd61cd741557da780d9;hb=657b0b025a92f9d0ad6647d79950071031d7b0b4;hp=0000000000000000000000000000000000000000;hpb=69e914b3889a82fa2f7daa8d55e1aa0de23fb454;p=controller.git diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/LithiumNormalizedNodeOutputStreamWriter.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/LithiumNormalizedNodeOutputStreamWriter.java new file mode 100644 index 0000000000..e777948ca5 --- /dev/null +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/LithiumNormalizedNodeOutputStreamWriter.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. 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.controller.cluster.datastore.node.utils.stream; + +import java.io.DataOutput; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.common.Revision; + +/** + * NormalizedNodeOutputStreamWriter will be used by distributed datastore to send normalized node in + * a stream. + * A stream writer wrapper around this class will write node objects to stream in recursive manner. + * for example - If you have a ContainerNode which has a two LeafNode as children, then + * you will first call + * {@link #startContainerNode(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier, int)}, + * then will call + * {@link #leafNode(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier, Object)} twice + * and then, {@link #endNode()} to end container node. + * + *

Based on the each node, the node type is also written to the stream, that helps in reconstructing the object, + * while reading. + */ +class LithiumNormalizedNodeOutputStreamWriter extends AbstractNormalizedNodeDataOutput { + private final Map stringCodeMap = new HashMap<>(); + + LithiumNormalizedNodeOutputStreamWriter(final DataOutput output) { + super(output); + } + + @Override + protected short streamVersion() { + return TokenTypes.LITHIUM_VERSION; + } + + @Override + protected void writeQName(final QName qname) throws IOException { + writeString(qname.getLocalName()); + writeString(qname.getNamespace().toString()); + writeString(qname.getRevision().map(Revision::toString).orElse(null)); + } + + @Override + protected final void writeString(final String string) throws IOException { + if (string != null) { + final Integer value = stringCodeMap.get(string); + if (value == null) { + stringCodeMap.put(string, stringCodeMap.size()); + writeByte(TokenTypes.IS_STRING_VALUE); + writeUTF(string); + } else { + writeByte(TokenTypes.IS_CODE_VALUE); + writeInt(value); + } + } else { + writeByte(TokenTypes.IS_NULL_VALUE); + } + } +}