Merge "Fix the build errors due to the class change of InstanceIdentifier to YangInst...
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / WriteData.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.messages;
10
11 import org.opendaylight.controller.cluster.datastore.node.NormalizedNodeToNodeCodec;
12 import org.opendaylight.controller.cluster.datastore.utils.InstanceIdentifierUtils;
13 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
14 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
15 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18
19 public class WriteData extends ModifyData{
20
21   public static final Class SERIALIZABLE_CLASS = ShardTransactionMessages.WriteData.class;
22
23   public WriteData(InstanceIdentifier path, NormalizedNode<?, ?> data, SchemaContext schemaContext) {
24     super(path, data, schemaContext);
25   }
26
27     @Override public Object toSerializable() {
28
29         NormalizedNodeMessages.Node normalizedNode =
30             new NormalizedNodeToNodeCodec(schemaContext).encode(
31                 InstanceIdentifierUtils.from(path.toString()), data)
32                 .getNormalizedNode();
33         return ShardTransactionMessages.WriteData.newBuilder()
34             .setInstanceIdentifierPathArguments(path.toString())
35             .setNormalizedNode(normalizedNode).build();
36
37     }
38
39     public static WriteData fromSerializable(Object serializable, SchemaContext schemaContext){
40         ShardTransactionMessages.WriteData o = (ShardTransactionMessages.WriteData) serializable;
41         InstanceIdentifier identifier = InstanceIdentifierUtils.from(o.getInstanceIdentifierPathArguments());
42
43         NormalizedNode<?, ?> normalizedNode =
44             new NormalizedNodeToNodeCodec(schemaContext)
45                 .decode(identifier, o.getNormalizedNode());
46
47         return new WriteData(identifier, normalizedNode, schemaContext);
48     }
49
50 }