2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.access.concepts;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.annotations.Beta;
15 import com.google.common.base.MoreObjects;
16 import com.google.common.base.Strings;
17 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
18 import java.io.DataInput;
19 import java.io.DataOutput;
20 import java.io.Externalizable;
21 import java.io.IOException;
22 import java.io.ObjectInput;
23 import java.io.ObjectOutput;
24 import java.nio.charset.StandardCharsets;
25 import java.util.regex.Pattern;
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.opendaylight.yangtools.concepts.Identifier;
28 import org.opendaylight.yangtools.concepts.WritableIdentifier;
31 * An {@link Identifier} identifying a data store frontend type, which is able to access the data store backend.
32 * Frontend implementations need to define this identifier so that multiple clients existing on a member node can be
35 * @author Robert Varga
38 public final class FrontendType implements Comparable<FrontendType>, WritableIdentifier {
39 private static final class Proxy implements Externalizable {
40 private static final long serialVersionUID = 1L;
41 private byte[] serialized;
43 // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
44 // be able to create instances via reflection.
45 @SuppressWarnings("checkstyle:RedundantModifier")
50 Proxy(final byte[] serialized) {
51 this.serialized = requireNonNull(serialized);
55 public void writeExternal(final ObjectOutput out) throws IOException {
56 out.writeInt(serialized.length);
57 out.write(serialized);
61 public void readExternal(final ObjectInput in) throws IOException {
62 serialized = new byte[in.readInt()];
63 in.readFully(serialized);
66 private Object readResolve() {
67 // TODO: consider caching instances here
68 return new FrontendType(new String(serialized, StandardCharsets.UTF_8), serialized);
72 private static final String SIMPLE_STRING_REGEX = "^[a-zA-Z0-9-_.*+:=,!~';]+$";
73 private static final Pattern SIMPLE_STRING_PATTERN = Pattern.compile(SIMPLE_STRING_REGEX);
74 private static final long serialVersionUID = 1L;
76 private final @NonNull String name;
78 @SuppressFBWarnings(value = "VO_VOLATILE_REFERENCE_TO_ARRAY",
79 justification = "The array elements are non-volatile but we don't access them.")
80 private volatile byte[] serialized;
82 private FrontendType(final String name) {
83 this.name = requireNonNull(name);
86 FrontendType(final String name, final byte[] serialized) {
88 this.serialized = verifyNotNull(serialized);
92 * Return a {@link FrontendType} corresponding to a string representation. Input string has constraints
93 * on what characters it can contain. It may contain the following:
94 * - US-ASCII letters and numbers
95 * - special characters: -_.*+:=,!~';
97 * @param name the input name
98 * @return A {@link FrontendType} instance
99 * @throws IllegalArgumentException if the string is null, empty or contains invalid characters
101 public static @NonNull FrontendType forName(final String name) {
102 checkArgument(!Strings.isNullOrEmpty(name));
103 checkArgument(SIMPLE_STRING_PATTERN.matcher(name).matches(),
104 "Supplied name %s does not patch pattern %s", name, SIMPLE_STRING_REGEX);
105 return new FrontendType(name);
108 public static @NonNull FrontendType readFrom(final DataInput in) throws IOException {
109 final byte[] serialized = new byte[in.readInt()];
110 in.readFully(serialized);
111 return new FrontendType(new String(serialized, StandardCharsets.UTF_8));
115 public void writeTo(final DataOutput out) throws IOException {
116 final byte[] local = getSerialized();
117 out.writeInt(local.length);
121 public @NonNull String getName() {
125 public org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
126 . @NonNull FrontendType toYang() {
127 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
132 public int hashCode() {
133 return name.hashCode();
137 public boolean equals(final Object obj) {
138 return this == obj || obj instanceof FrontendType && name.equals(((FrontendType)obj).name);
142 public int compareTo(final FrontendType obj) {
143 return this == obj ? 0 : name.compareTo(obj.name);
147 public String toString() {
148 return MoreObjects.toStringHelper(FrontendType.class).add("name", name).toString();
151 private byte[] getSerialized() {
152 byte[] local = serialized;
154 local = name.getBytes(StandardCharsets.UTF_8);
160 Object writeReplace() {
161 return new Proxy(getSerialized());