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.base.MoreObjects;
15 import com.google.common.base.Strings;
16 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17 import java.io.DataInput;
18 import java.io.DataOutput;
19 import java.io.Externalizable;
20 import java.io.IOException;
21 import java.io.ObjectInput;
22 import java.io.ObjectOutput;
23 import java.nio.charset.StandardCharsets;
24 import java.util.regex.Pattern;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.opendaylight.yangtools.concepts.Identifier;
27 import org.opendaylight.yangtools.concepts.WritableIdentifier;
30 * An {@link Identifier} identifying a data store frontend type, which is able to access the data store backend.
31 * Frontend implementations need to define this identifier so that multiple clients existing on a member node can be
34 public final class FrontendType implements Comparable<FrontendType>, WritableIdentifier {
35 interface SerialForm extends Externalizable {
36 @NonNull FrontendType type();
38 void setType(@NonNull FrontendType type);
44 default void writeExternal(final ObjectOutput out) throws IOException {
45 final var serialized = type().getSerialized();
46 out.writeInt(serialized.length);
47 out.write(serialized);
51 default void readExternal(final ObjectInput in) throws IOException {
52 final var serialized = new byte[in.readInt()];
53 in.readFully(serialized);
54 // TODO: consider caching instances here
55 setType(new FrontendType(new String(serialized, StandardCharsets.UTF_8), serialized));
59 private static final class Proxy implements SerialForm {
61 private static final long serialVersionUID = 1L;
63 private FrontendType type;
65 // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
66 // be able to create instances via reflection.
67 @SuppressWarnings("checkstyle:RedundantModifier")
72 Proxy(final FrontendType type) {
73 this.type = requireNonNull(type);
77 public FrontendType type() {
78 return verifyNotNull(type);
82 public void setType(final FrontendType type) {
83 this.type = requireNonNull(type);
87 public Object readResolve() {
93 private static final long serialVersionUID = 1L;
94 private static final String SIMPLE_STRING_REGEX = "^[a-zA-Z0-9-_.*+:=,!~';]+$";
95 private static final Pattern SIMPLE_STRING_PATTERN = Pattern.compile(SIMPLE_STRING_REGEX);
97 private final @NonNull String name;
99 @SuppressFBWarnings(value = "VO_VOLATILE_REFERENCE_TO_ARRAY",
100 justification = "The array elements are non-volatile but we don't access them.")
101 private volatile byte[] serialized;
103 private FrontendType(final String name) {
104 this.name = requireNonNull(name);
107 FrontendType(final String name, final byte[] serialized) {
109 this.serialized = verifyNotNull(serialized);
113 * Return a {@link FrontendType} corresponding to a string representation. Input string has constraints
114 * on what characters it can contain. It may contain the following:
115 * - US-ASCII letters and numbers
116 * - special characters: -_.*+:=,!~';
118 * @param name the input name
119 * @return A {@link FrontendType} instance
120 * @throws IllegalArgumentException if the string is null, empty or contains invalid characters
122 public static @NonNull FrontendType forName(final String name) {
123 checkArgument(!Strings.isNullOrEmpty(name));
124 checkArgument(SIMPLE_STRING_PATTERN.matcher(name).matches(),
125 "Supplied name %s does not patch pattern %s", name, SIMPLE_STRING_REGEX);
126 return new FrontendType(name);
129 public static @NonNull FrontendType readFrom(final DataInput in) throws IOException {
130 final byte[] serialized = new byte[in.readInt()];
131 in.readFully(serialized);
132 return new FrontendType(new String(serialized, StandardCharsets.UTF_8));
136 public void writeTo(final DataOutput out) throws IOException {
137 final byte[] local = getSerialized();
138 out.writeInt(local.length);
142 public @NonNull String getName() {
146 public org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
147 . @NonNull FrontendType toYang() {
148 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
153 public int hashCode() {
154 return name.hashCode();
158 public boolean equals(final Object obj) {
159 return this == obj || obj instanceof FrontendType && name.equals(((FrontendType)obj).name);
163 public int compareTo(final FrontendType obj) {
164 return this == obj ? 0 : name.compareTo(obj.name);
168 public String toString() {
169 return MoreObjects.toStringHelper(FrontendType.class).add("name", name).toString();
172 private byte[] getSerialized() {
173 byte[] local = serialized;
175 local = name.getBytes(StandardCharsets.UTF_8);
182 Object writeReplace() {
183 return new Proxy(this);