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%2FNormalizedNodeToNodeCodec.java;h=5602b48e1b1664dc7d5b62abda1ad79bc35b7cbc;hb=2cf4749c41aa32c6b77064fc1ae0e231adc4a5f4;hp=4e76e37fa25c179ac3f2b196f97ee389261a8c97;hpb=287b1d1ecec3264c192b1007019bfcadf6cb4311;p=controller.git diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/NormalizedNodeToNodeCodec.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/NormalizedNodeToNodeCodec.java index 4e76e37fa2..5602b48e1b 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/NormalizedNodeToNodeCodec.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/NormalizedNodeToNodeCodec.java @@ -1,56 +1,123 @@ /* + * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved. * - * Copyright (c) 2014 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 - * + * 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; -import org.opendaylight.controller.cluster.datastore.node.utils.PathUtils; import org.opendaylight.controller.cluster.datastore.node.utils.serialization.NormalizedNodeSerializer; +import org.opendaylight.controller.cluster.datastore.node.utils.serialization.NormalizedNodeSerializer.DeSerializer; +import org.opendaylight.controller.cluster.datastore.node.utils.serialization.NormalizedNodeSerializer.Serializer; import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages; +import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Container; +import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.InstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class NormalizedNodeToNodeCodec { + public interface Encoded { + NormalizedNodeMessages.Container getEncodedNode(); + + NormalizedNodeMessages.InstanceIdentifier getEncodedPath(); + } + + public interface Decoded { + NormalizedNode getDecodedNode(); + + YangInstanceIdentifier getDecodedPath(); + } + private final SchemaContext ctx; - private static final Logger logger = LoggerFactory.getLogger(NormalizedNodeToNodeCodec.class); public NormalizedNodeToNodeCodec(final SchemaContext ctx){ this.ctx = ctx; + } + public NormalizedNodeMessages.Container encode(NormalizedNode node){ + return encode(null, node).getEncodedNode(); } - public NormalizedNodeMessages.Container encode(YangInstanceIdentifier id, NormalizedNode node){ + public Encoded encode(YangInstanceIdentifier path, NormalizedNode node) { + + NormalizedNodeMessages.InstanceIdentifier serializedPath = null; NormalizedNodeMessages.Container.Builder builder = NormalizedNodeMessages.Container.newBuilder(); - String parentPath = ""; - if(id != null){ - parentPath = PathUtils.getParentPath(PathUtils.toString(id)); - } + // Note: parent path is no longer used + builder.setParentPath(""); - builder.setParentPath(parentPath); if(node != null) { - builder.setNormalizedNode(NormalizedNodeSerializer.serialize(node)); + if(path == null) { + builder.setNormalizedNode(NormalizedNodeSerializer.serialize(node)); + } else { + Serializer serializer = NormalizedNodeSerializer.newSerializer(node); + builder.setNormalizedNode(serializer.serialize(path)); + serializedPath = serializer.getSerializedPath(); + } } - return builder.build(); + return new EncodedImpl(builder.build(), serializedPath); + } + + + public NormalizedNode decode(NormalizedNodeMessages.Node node){ + return decode(null, node).getDecodedNode(); } - public NormalizedNode decode(YangInstanceIdentifier id, NormalizedNodeMessages.Node node){ + public Decoded decode(NormalizedNodeMessages.InstanceIdentifier path, + NormalizedNodeMessages.Node node) { if(node.getIntType() < 0 || node.getSerializedSize() == 0){ - return null; + return new DecodedImpl(null, null); } - return NormalizedNodeSerializer.deSerialize(node); + + DeSerializer deSerializer = NormalizedNodeSerializer.newDeSerializer(path, node); + NormalizedNode decodedNode = deSerializer.deSerialize(); + return new DecodedImpl(decodedNode, deSerializer.getDeserializedPath()); } + private static class DecodedImpl implements Decoded { + + private final NormalizedNode decodedNode; + private final YangInstanceIdentifier decodedPath; + public DecodedImpl(NormalizedNode decodedNode, YangInstanceIdentifier decodedPath) { + this.decodedNode = decodedNode; + this.decodedPath = decodedPath; + } + + @Override + public NormalizedNode getDecodedNode() { + return decodedNode; + } + + @Override + public YangInstanceIdentifier getDecodedPath() { + return decodedPath; + } + } + + private static class EncodedImpl implements Encoded { + + private final Container encodedNode; + private final InstanceIdentifier encodedPath; + + EncodedImpl(Container encodedNode, InstanceIdentifier encodedPath) { + this.encodedNode = encodedNode; + this.encodedPath = encodedPath; + } + + @Override + public Container getEncodedNode() { + return encodedNode; + } + + @Override + public InstanceIdentifier getEncodedPath() { + return encodedPath; + } + } }