Merge "Bug 1386: Avoid commit deadlock"
[controller.git] / opendaylight / md-sal / sal-protocolbuffer-encoding / src / main / java / org / opendaylight / controller / cluster / datastore / node / NodeValueCodec.java
1 /*
2  *
3  *  Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
4  *
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
8  *
9  */
10
11 package org.opendaylight.controller.cluster.datastore.node;
12
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;
25
26 public class NodeValueCodec {
27     protected static final Logger logger = LoggerFactory
28         .getLogger(NodeValueCodec.class);
29
30     public static Object toTypeSafeValue(DataSchemaNode schema, TypeDefinition type, NormalizedNodeMessages.Node node){
31
32         String value = node.getValue();
33
34         if(schema != null && value != null){
35             TypeDefinition<?> baseType = type;
36
37             while (baseType.getBaseType() != null) {
38                 baseType = baseType.getBaseType();
39             }
40
41             TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec =
42                 TypeDefinitionAwareCodec.from(type);
43
44             if(codec instanceof BitsCodec){
45                 if(value.contains("[]")){
46                     value = "";
47                 } else {
48                     value = value.replace("[", "");
49                     value = value.replace("]", "");
50                     value = value.replace(",", " ");
51                 }
52             }
53
54             if (codec != null) {
55                 return codec.deserialize(value);
56             } else if(baseType instanceof Leafref) {
57                 return value;
58             } else if(baseType instanceof IdentityrefType) {
59                 return QNameFactory.create(value);
60             } else if(baseType instanceof InstanceIdentifierType) {
61                 return InstanceIdentifierUtils.fromSerializable(node.getInstanceIdentifierValue());
62             } else {
63                 logger.error("Could not figure out how to transform value " + value +  " for schemaType " + type);
64             }
65         }
66
67         return value;
68     }
69 }