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