Fix FindBugs warnings in cds-access-api and enable enforcement
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / FrontendType.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 package org.opendaylight.controller.cluster.access.concepts;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Strings;
14 import com.google.common.base.Verify;
15 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16 import java.io.DataInput;
17 import java.io.DataOutput;
18 import java.io.Externalizable;
19 import java.io.IOException;
20 import java.io.ObjectInput;
21 import java.io.ObjectOutput;
22 import java.nio.charset.StandardCharsets;
23 import java.util.regex.Pattern;
24 import javax.annotation.RegEx;
25 import org.opendaylight.yangtools.concepts.Identifier;
26 import org.opendaylight.yangtools.concepts.WritableIdentifier;
27
28 /**
29  * An {@link Identifier} identifying a data store frontend type, which is able to access the data store backend.
30  * Frontend implementations need to define this identifier so that multiple clients existing on a member node can be
31  * discerned.
32  *
33  * @author Robert Varga
34  */
35 @Beta
36 public final class FrontendType implements Comparable<FrontendType>, WritableIdentifier {
37     private static final class Proxy implements Externalizable {
38         private static final long serialVersionUID = 1L;
39         private byte[] serialized;
40
41         // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
42         // be able to create instances via reflection.
43         @SuppressWarnings("checkstyle:RedundantModifier")
44         public Proxy() {
45             // For Externalizable
46         }
47
48         Proxy(final byte[] serialized) {
49             this.serialized = Preconditions.checkNotNull(serialized);
50         }
51
52         @Override
53         public void writeExternal(final ObjectOutput out) throws IOException {
54             out.writeInt(serialized.length);
55             out.write(serialized);
56         }
57
58         @Override
59         public void readExternal(final ObjectInput in) throws IOException {
60             serialized = new byte[in.readInt()];
61             in.readFully(serialized);
62         }
63
64         private Object readResolve() {
65             // TODO: consider caching instances here
66             return new FrontendType(new String(serialized, StandardCharsets.UTF_8), serialized);
67         }
68     }
69
70     @RegEx
71     private static final String SIMPLE_STRING_REGEX = "^[a-zA-Z0-9-_.*+:=,!~';]+$";
72     private static final Pattern SIMPLE_STRING_PATTERN = Pattern.compile(SIMPLE_STRING_REGEX);
73     private static final long serialVersionUID = 1L;
74     private final String name;
75
76     @SuppressFBWarnings(value = "VO_VOLATILE_REFERENCE_TO_ARRAY",
77             justification = "The array elements are non-volatile but we don't access them.")
78     private volatile byte[] serialized;
79
80     private FrontendType(final String name) {
81         this.name = Preconditions.checkNotNull(name);
82     }
83
84     FrontendType(final String name, final byte[] serialized) {
85         this(name);
86         this.serialized = Verify.verifyNotNull(serialized);
87     }
88
89     /**
90      * Return a {@link FrontendType} corresponding to a string representation. Input string has constraints
91      * on what characters it can contain. It may contain the following:
92      * - US-ASCII letters and numbers
93      * - special characters: -_.*+:=,!~';
94      *
95      * @param name the input name
96      * @return A {@link FrontendType} instance
97      * @throws IllegalArgumentException if the string is null, empty or contains invalid characters
98      */
99     public static FrontendType forName(final String name) {
100         Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
101         Preconditions.checkArgument(SIMPLE_STRING_PATTERN.matcher(name).matches(),
102             "Supplied name %s does not patch pattern %s", name, SIMPLE_STRING_REGEX);
103         return new FrontendType(name);
104     }
105
106     public static FrontendType readFrom(final DataInput in) throws IOException {
107         final byte[] serialized = new byte[in.readInt()];
108         in.readFully(serialized);
109         return new FrontendType(new String(serialized, StandardCharsets.UTF_8));
110     }
111
112     @Override
113     public void writeTo(final DataOutput out) throws IOException {
114         final byte[] local = getSerialized();
115         out.writeInt(local.length);
116         out.write(local);
117     }
118
119     public String getName() {
120         return name;
121     }
122
123     @Override
124     public int hashCode() {
125         return name.hashCode();
126     }
127
128     @Override
129     public boolean equals(final Object obj) {
130         return this == obj || obj instanceof FrontendType && name.equals(((FrontendType)obj).name);
131     }
132
133     @Override
134     public int compareTo(final FrontendType obj) {
135         return this == obj ? 0 : name.compareTo(obj.name);
136     }
137
138     @Override
139     public String toString() {
140         return MoreObjects.toStringHelper(FrontendType.class).add("name", name).toString();
141     }
142
143     private byte[] getSerialized() {
144         byte[] local = serialized;
145         if (local == null) {
146             local = name.getBytes(StandardCharsets.UTF_8);
147             serialized = local;
148         }
149         return local;
150     }
151
152     Object writeReplace() {
153         return new Proxy(getSerialized());
154     }
155 }