2e7e0619b9b578f465a83787e6252b033f41d9ec
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / messages / SchemaPathMessage.java
1 /*
2  * Copyright (c) 2016 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.netconf.topology.singleton.messages;
10
11 import com.google.common.collect.Iterables;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import java.io.Serializable;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20
21 public class SchemaPathMessage implements Serializable {
22     private static final long serialVersionUID = 1L;
23
24     @SuppressFBWarnings("SE_BAD_FIELD")
25     private final SchemaPath schemaPath;
26
27     public SchemaPathMessage(final SchemaPath schemaPath) {
28         this.schemaPath = schemaPath;
29     }
30
31     public SchemaPath getSchemaPath() {
32         return schemaPath;
33     }
34
35     private Object writeReplace() {
36         return new Proxy(this);
37     }
38
39     @Override
40     public String toString() {
41         return "SchemaPathMessage [schemaPath=" + schemaPath + "]";
42     }
43
44     private static class Proxy implements Externalizable {
45         private static final long serialVersionUID = 2L;
46
47         private SchemaPathMessage schemaPathMessage;
48
49         @SuppressWarnings("checkstyle:RedundantModifier")
50         public Proxy() {
51             //due to Externalizable
52         }
53
54         Proxy(final SchemaPathMessage schemaPathMessage) {
55             this.schemaPathMessage = schemaPathMessage;
56         }
57
58         @Override
59         public void writeExternal(final ObjectOutput out) throws IOException {
60             out.writeInt(Iterables.size(schemaPathMessage.getSchemaPath().getPathTowardsRoot()));
61
62             for (final QName qualifiedName : schemaPathMessage.getSchemaPath().getPathTowardsRoot()) {
63                 out.writeObject(qualifiedName);
64             }
65
66             out.writeBoolean(schemaPathMessage.getSchemaPath().isAbsolute());
67         }
68
69         @Override
70         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
71             final int sizePath = in.readInt();
72             final QName[] paths = new QName[sizePath];
73             for (int i = 0; i < sizePath; i++) {
74                 paths[i] = (QName) in.readObject();
75             }
76             final boolean absolute = in.readBoolean();
77             schemaPathMessage = new SchemaPathMessage(SchemaPath.create(absolute, paths));
78         }
79
80         private Object readResolve() {
81             return schemaPathMessage;
82         }
83     }
84
85 }