d1ae264c3b912839a4332fc74766076859b754db
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / NormalizedNodeToNodeCodec.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.protobuff.messages.common.NormalizedNodeMessages;
14 import org.opendaylight.controller.cluster.datastore.node.utils.PathUtils;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class NormalizedNodeToNodeCodec {
23     private final SchemaContext ctx;
24     private static final Logger logger = LoggerFactory.getLogger(NormalizedNodeToNodeCodec.class);
25
26     public NormalizedNodeToNodeCodec(final SchemaContext ctx){
27         this.ctx = ctx;
28
29     }
30
31     public NormalizedNodeMessages.Container encode(YangInstanceIdentifier id, NormalizedNode node){
32         String parentPath = "";
33
34         if(id != null){
35             parentPath = PathUtils.getParentPath(PathUtils.toString(id));
36         }
37
38
39         NormalizedNodeToProtocolBufferNode encoder = new NormalizedNodeToProtocolBufferNode();
40         encoder.encode(parentPath, node);
41
42         return encoder.getContainer();
43
44
45     }
46
47     public NormalizedNode<?,?> decode(YangInstanceIdentifier id, NormalizedNodeMessages.Node node){
48             NodeToNormalizedNodeBuilder currentOp = NodeToNormalizedNodeBuilder.from(ctx);
49
50             for(YangInstanceIdentifier.PathArgument pathArgument : id.getPathArguments()){
51                 currentOp = currentOp.getChild(pathArgument);
52             }
53
54             QName nodeType = null;
55
56             if(id.getPath().size() < 1){
57                 nodeType = null;
58             } else {
59                 final YangInstanceIdentifier.PathArgument pathArgument = id.getPath().get(id.getPath().size() - 1);
60                 if(pathArgument instanceof YangInstanceIdentifier.AugmentationIdentifier){
61                     nodeType = null;
62                 } else {
63                     nodeType = pathArgument.getNodeType();
64                 }
65             }
66             if((node != null)&& (!node.getType().isEmpty())){
67                return currentOp.normalize(nodeType, node);
68             } else{
69               return null;
70             }
71     }
72
73
74 }