Merge "Bug 1569 - [DEV] Too small variable for OF-Port-Number"
[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.cluster.datastore.node.utils.PathUtils;
14 import org.opendaylight.controller.cluster.datastore.node.utils.serialization.NormalizedNodeSerializer;
15 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
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
33         NormalizedNodeMessages.Container.Builder builder = NormalizedNodeMessages.Container.newBuilder();
34         String parentPath = "";
35
36         if(id != null){
37             parentPath = PathUtils.getParentPath(PathUtils.toString(id));
38         }
39
40         builder.setParentPath(parentPath);
41         if(node != null) {
42             builder.setNormalizedNode(NormalizedNodeSerializer.serialize(node));
43         }
44
45         return builder.build();
46     }
47
48     public NormalizedNode<?,?> decode(YangInstanceIdentifier id, NormalizedNodeMessages.Node node){
49         if(node.getIntType() < 0 || node.getSerializedSize() == 0){
50             return null;
51         }
52         return NormalizedNodeSerializer.deSerialize(node);
53     }
54
55
56 }