Serialization/Deserialization and a host of other fixes
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / modification / MergeModification.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.utils.InstanceIdentifierUtils;
13 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
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  * MergeModification stores all the parameters required to merge data into the specified path
22  */
23 public class MergeModification extends AbstractModification {
24     private final NormalizedNode data;
25     private final SchemaContext schemaContext;
26
27
28     public MergeModification(YangInstanceIdentifier path, NormalizedNode data,
29         SchemaContext schemaContext) {
30         super(path);
31         this.data = data;
32         this.schemaContext = schemaContext;
33     }
34
35     @Override
36     public void apply(DOMStoreWriteTransaction transaction) {
37         transaction.merge(path, data);
38     }
39
40     @Override public Object toSerializable() {
41         NormalizedNodeMessages.Container encode =
42             new NormalizedNodeToNodeCodec(schemaContext).encode(
43                 path, data);
44
45         return PersistentMessages.Modification.newBuilder()
46             .setType(this.getClass().toString())
47             .setPath(InstanceIdentifierUtils.toSerializable(this.path))
48             .setData(encode.getNormalizedNode())
49             .build();
50
51     }
52
53     public static MergeModification fromSerializable(
54         Object serializable,
55         SchemaContext schemaContext) {
56         PersistentMessages.Modification o = (PersistentMessages.Modification) serializable;
57
58         YangInstanceIdentifier path = InstanceIdentifierUtils.fromSerializable(o.getPath());
59         NormalizedNode data = new NormalizedNodeToNodeCodec(schemaContext).decode(
60             path, o.getData());
61
62         return new MergeModification(path, data, schemaContext);
63     }
64
65 }