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 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 java.io.DataInput;
16 import java.io.DataOutput;
17 import java.io.Externalizable;
18 import java.io.IOException;
19 import java.io.ObjectInput;
20 import java.io.ObjectOutput;
21 import java.nio.charset.StandardCharsets;
22 import java.util.regex.Pattern;
23 import javax.annotation.RegEx;
24 import org.opendaylight.yangtools.concepts.Identifier;
25 import org.opendaylight.yangtools.concepts.WritableIdentifier;
28 * An {@link Identifier} identifying a data store frontend type, which is able to access the data store backend.
29 * Frontend implementations need to define this identifier so that multiple clients existing on a member node can be
32 * @author Robert Varga
35 public final class FrontendType implements Comparable<FrontendType>, WritableIdentifier {
36 private static final class Proxy implements Externalizable {
37 private static final long serialVersionUID = 1L;
38 private byte[] serialized;
40 // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
41 // be able to create instances via reflection.
42 @SuppressWarnings("checkstyle:RedundantModifier")
47 Proxy(final byte[] serialized) {
48 this.serialized = Preconditions.checkNotNull(serialized);
52 public void writeExternal(final ObjectOutput out) throws IOException {
53 out.writeInt(serialized.length);
54 out.write(serialized);
58 public void readExternal(final ObjectInput in) throws IOException {
59 serialized = new byte[in.readInt()];
60 in.readFully(serialized);
63 private Object readResolve() {
64 // TODO: consider caching instances here
65 return new FrontendType(new String(serialized, StandardCharsets.UTF_8), serialized);
70 private static final String SIMPLE_STRING_REGEX = "^[a-zA-Z0-9-_.*+:=,!~';]+$";
71 private static final Pattern SIMPLE_STRING_PATTERN = Pattern.compile(SIMPLE_STRING_REGEX);
72 private static final long serialVersionUID = 1L;
73 private final String name;
74 private volatile byte[] serialized;
76 private FrontendType(final String name) {
77 this.name = Preconditions.checkNotNull(name);
80 FrontendType(final String name, final byte[] serialized) {
82 this.serialized = Verify.verifyNotNull(serialized);
86 * Return a {@link FrontendType} corresponding to a string representation. Input string has constraints
87 * on what characters it can contain. It may contain the following:
88 * - US-ASCII letters and numbers
89 * - special characters: -_.*+:=,!~';
91 * @param name the input name
92 * @return A {@link FrontendType} instance
93 * @throws IllegalArgumentException if the string is null, empty or contains invalid characters
95 public static FrontendType forName(final String name) {
96 Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
97 Preconditions.checkArgument(SIMPLE_STRING_PATTERN.matcher(name).matches(),
98 "Supplied name %s does not patch pattern %s", name, SIMPLE_STRING_REGEX);
99 return new FrontendType(name);
102 public static FrontendType readFrom(final DataInput in) throws IOException {
103 final byte[] serialized = new byte[in.readInt()];
104 in.readFully(serialized);
105 return new FrontendType(new String(serialized, StandardCharsets.UTF_8));
109 public void writeTo(final DataOutput out) throws IOException {
110 final byte[] local = getSerialized();
111 out.writeInt(local.length);
115 public String getName() {
120 public int hashCode() {
121 return name.hashCode();
125 public boolean equals(final Object obj) {
126 return this == obj || obj instanceof FrontendType && name.equals(((FrontendType)obj).name);
130 public int compareTo(final FrontendType obj) {
131 return this == obj ? 0 : name.compareTo(obj.name);
135 public String toString() {
136 return MoreObjects.toStringHelper(FrontendType.class).add("name", name).toString();
139 private byte[] getSerialized() {
140 byte[] local = serialized;
142 local = name.getBytes(StandardCharsets.UTF_8);
148 Object writeReplace() {
149 return new Proxy(getSerialized());