Merge changes I879118ce,I664b391e
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / modification / WriteModification.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.modification;
10
11 import org.opendaylight.controller.cluster.datastore.node.NormalizedNodeToNodeCodec;
12 import org.opendaylight.controller.cluster.datastore.node.NormalizedNodeToNodeCodec.Decoded;
13 import org.opendaylight.controller.cluster.datastore.node.NormalizedNodeToNodeCodec.Encoded;
14 import org.opendaylight.controller.protobuff.messages.persistent.PersistentMessages;
15 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
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
20 /**
21  * WriteModification stores all the parameters required to write data to the specified path
22  */
23 public class WriteModification extends AbstractModification {
24
25     protected final NormalizedNode data;
26     private final SchemaContext schemaContext;
27
28     public WriteModification(YangInstanceIdentifier path, NormalizedNode data, SchemaContext schemaContext) {
29         super(path);
30         this.data = data;
31         this.schemaContext = schemaContext;
32     }
33
34     @Override
35     public void apply(DOMStoreWriteTransaction transaction) {
36         transaction.write(path, data);
37     }
38
39     public NormalizedNode getData() {
40         return data;
41     }
42
43     @Override
44     public Object toSerializable() {
45         Encoded encoded = new NormalizedNodeToNodeCodec(schemaContext).encode(path, data);
46
47         return PersistentMessages.Modification.newBuilder()
48                 .setType(this.getClass().toString())
49                 .setPath(encoded.getEncodedPath())
50                 .setData(encoded.getEncodedNode().getNormalizedNode())
51                 .build();
52     }
53
54     public static WriteModification fromSerializable(Object serializable, SchemaContext schemaContext) {
55         PersistentMessages.Modification o = (PersistentMessages.Modification) serializable;
56         Decoded decoded = new NormalizedNodeToNodeCodec(schemaContext).decode(o.getPath(), o.getData());
57         return new WriteModification(decoded.getDecodedPath(), decoded.getDecodedNode(), schemaContext);
58     }
59 }