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.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;
22 public class NormalizedNodeToNodeCodec {
23 private final SchemaContext ctx;
24 private static final Logger logger = LoggerFactory.getLogger(NormalizedNodeToNodeCodec.class);
26 public NormalizedNodeToNodeCodec(final SchemaContext ctx){
31 public NormalizedNodeMessages.Container encode(YangInstanceIdentifier id, NormalizedNode node){
32 String parentPath = "";
35 parentPath = PathUtils.getParentPath(id.toString());
39 NormalizedNodeToProtocolBufferNode encoder = new NormalizedNodeToProtocolBufferNode();
40 encoder.encode(parentPath, node);
42 return encoder.getContainer();
47 public NormalizedNode<?,?> decode(YangInstanceIdentifier id, NormalizedNodeMessages.Node node){
48 NodeToNormalizedNodeBuilder currentOp = NodeToNormalizedNodeBuilder.from(ctx);
50 for(YangInstanceIdentifier.PathArgument pathArgument : id.getPathArguments()){
51 currentOp = currentOp.getChild(pathArgument);
54 QName nodeType = null;
56 if(id.getPath().size() < 1){
59 final YangInstanceIdentifier.PathArgument pathArgument = id.getPath().get(id.getPath().size() - 1);
60 if(pathArgument instanceof YangInstanceIdentifier.AugmentationIdentifier){
63 nodeType = pathArgument.getNodeType();
66 if((node != null)&& (!node.getType().isEmpty())){
67 return currentOp.normalize(nodeType, node);