Fix findbugs violations in netconf
[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     private static class Proxy implements Externalizable {
40         private static final long serialVersionUID = 2L;
41
42         private SchemaPathMessage schemaPathMessage;
43
44         @SuppressWarnings("checkstyle:RedundantModifier")
45         public Proxy() {
46             //due to Externalizable
47         }
48
49         Proxy(final SchemaPathMessage schemaPathMessage) {
50             this.schemaPathMessage = schemaPathMessage;
51         }
52
53         @Override
54         public void writeExternal(final ObjectOutput out) throws IOException {
55             out.writeInt(Iterables.size(schemaPathMessage.getSchemaPath().getPathTowardsRoot()));
56
57             for (final QName qualifiedName : schemaPathMessage.getSchemaPath().getPathTowardsRoot()) {
58                 out.writeObject(qualifiedName);
59             }
60
61             out.writeBoolean(schemaPathMessage.getSchemaPath().isAbsolute());
62         }
63
64         @Override
65         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
66             final int sizePath = in.readInt();
67             final QName[] paths = new QName[sizePath];
68             for (int i = 0; i < sizePath; i++) {
69                 paths[i] = (QName) in.readObject();
70             }
71             final boolean absolute = in.readBoolean();
72             schemaPathMessage = new SchemaPathMessage(SchemaPath.create(absolute, paths));
73         }
74
75         private Object readResolve() {
76             return schemaPathMessage;
77         }
78     }
79
80 }