2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.datastore.node;
11 import org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory;
12 import org.opendaylight.controller.cluster.datastore.util.InstanceIdentifierUtils;
13 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
14 import org.opendaylight.yangtools.yang.data.api.codec.BitsCodec;
15 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
16 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
18 import org.opendaylight.yangtools.yang.model.util.IdentityrefType;
19 import org.opendaylight.yangtools.yang.model.util.InstanceIdentifier;
20 import org.opendaylight.yangtools.yang.model.util.Leafref;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 public class NodeValueCodec {
25 protected static final Logger logger = LoggerFactory
26 .getLogger(NodeValueCodec.class);
28 public static Object toTypeSafeValue(DataSchemaNode schema, TypeDefinition type, NormalizedNodeMessages.Node node){
30 String value = node.getValue();
32 if(schema != null && value != null){
33 TypeDefinition<?> baseType = type;
35 while (baseType.getBaseType() != null) {
36 baseType = baseType.getBaseType();
39 TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec =
40 TypeDefinitionAwareCodec.from(type);
42 if(codec instanceof BitsCodec){
43 if(value.contains("[]")){
46 value = value.replace("[", "");
47 value = value.replace("]", "");
48 value = value.replace(",", " ");
53 return codec.deserialize(value);
54 } else if(baseType instanceof Leafref) {
56 } else if(baseType instanceof IdentityrefType) {
57 return QNameFactory.create(value);
58 } else if(baseType instanceof InstanceIdentifier) {
59 return InstanceIdentifierUtils.fromSerializable(node.getInstanceIdentifierValue());
61 logger.error("Could not figure out how to transform value " + value + " for schemaType " + type);