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