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%2FNodeValueCodec.java;fp=opendaylight%2Fmd-sal%2Fsal-clustering-commons%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fnode%2FNodeValueCodec.java;h=b6dbefbbac16506d6ed17af63bd2d208707ecaea;hb=6b9c0bb57b65343df31fd68d178821a22afd0fe2;hp=0000000000000000000000000000000000000000;hpb=cc6c063be5f143cd601208ef26604d90a25bd1a5;p=controller.git diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/NodeValueCodec.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/NodeValueCodec.java new file mode 100644 index 0000000000..b6dbefbbac --- /dev/null +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/NodeValueCodec.java @@ -0,0 +1,69 @@ +/* + * + * 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 + * + */ + +package org.opendaylight.controller.cluster.datastore.node; + +import org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory; +import org.opendaylight.controller.cluster.datastore.util.InstanceIdentifierUtils; +import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages; +import org.opendaylight.yangtools.yang.data.api.codec.BitsCodec; +import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.TypeDefinition; +import org.opendaylight.yangtools.yang.model.util.IdentityrefType; +import org.opendaylight.yangtools.yang.model.util.InstanceIdentifierType; +import org.opendaylight.yangtools.yang.model.util.Leafref; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class NodeValueCodec { + protected static final Logger logger = LoggerFactory + .getLogger(NodeValueCodec.class); + + public static Object toTypeSafeValue(DataSchemaNode schema, TypeDefinition type, NormalizedNodeMessages.Node node){ + + String value = node.getValue(); + + if(schema != null && value != null){ + TypeDefinition baseType = type; + + while (baseType.getBaseType() != null) { + baseType = baseType.getBaseType(); + } + + TypeDefinitionAwareCodec> codec = + TypeDefinitionAwareCodec.from(type); + + if(codec instanceof BitsCodec){ + if(value.contains("[]")){ + value = ""; + } else { + value = value.replace("[", ""); + value = value.replace("]", ""); + value = value.replace(",", " "); + } + } + + if (codec != null) { + return codec.deserialize(value); + } else if(baseType instanceof Leafref) { + return value; + } else if(baseType instanceof IdentityrefType) { + return QNameFactory.create(value); + } else if(baseType instanceof InstanceIdentifierType) { + return InstanceIdentifierUtils.fromSerializable(node.getInstanceIdentifierValue()); + } else { + logger.error("Could not figure out how to transform value " + value + " for schemaType " + type); + } + } + + return value; + } +}