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;
44 Proxy(final byte[] serialized) {
45 this.serialized = Preconditions.checkNotNull(serialized);
49 public void writeExternal(final ObjectOutput out) throws IOException {
50 out.writeInt(serialized.length);
51 out.write(serialized);
55 public void readExternal(final ObjectInput in) throws IOException {
56 serialized = new byte[in.readInt()];
57 in.readFully(serialized);
60 private Object readResolve() {
61 // TODO: consider caching instances here
62 return new FrontendType(new String(serialized, StandardCharsets.UTF_8), serialized);
67 private static final String SIMPLE_STRING_REGEX = "^[a-zA-Z0-9-_.*+:=,!~';]+$";
68 private static final Pattern SIMPLE_STRING_PATTERN = Pattern.compile(SIMPLE_STRING_REGEX);
69 private static final long serialVersionUID = 1L;
70 private final String name;
71 private volatile byte[] serialized;
73 private FrontendType(final String name) {
74 this.name = Preconditions.checkNotNull(name);
77 FrontendType(final String name, final byte[] serialized) {
79 this.serialized = Verify.verifyNotNull(serialized);
83 * Return a {@link FrontendType} corresponding to a string representation. Input string has constraints
84 * on what characters it can contain. It may contain the following:
85 * - US-ASCII letters and numbers
86 * - special characters: -_.*+:=,!~';
88 * @return A {@link FrontendType} instance
89 * @throws IllegalArgumentException if the string is null, empty or contains invalid characters
91 public static FrontendType forName(final String name) {
92 Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
93 Preconditions.checkArgument(SIMPLE_STRING_PATTERN.matcher(name).matches(),
94 "Supplied name %s does not patch pattern %s", name, SIMPLE_STRING_REGEX);
95 return new FrontendType(name);
98 public static FrontendType readFrom(final DataInput in) throws IOException {
99 final byte[] serialized = new byte[in.readInt()];
100 in.readFully(serialized);
101 return new FrontendType(new String(serialized, StandardCharsets.UTF_8));
105 public void writeTo(final DataOutput out) throws IOException {
106 final byte[] serialized = getSerialized();
107 out.writeInt(serialized.length);
108 out.write(serialized);
111 public String getName() {
116 public int hashCode() {
117 return name.hashCode();
121 public boolean equals(final Object o) {
122 return this == o || (o instanceof FrontendType && name.equals(((FrontendType)o).name));
126 public int compareTo(final FrontendType o) {
127 return this == o ? 0 : name.compareTo(o.name);
131 public String toString() {
132 return MoreObjects.toStringHelper(FrontendType.class).add("name", name).toString();
135 private byte[] getSerialized() {
136 byte[] local = serialized;
138 local = name.getBytes(StandardCharsets.UTF_8);
144 Object writeReplace() {
145 return new Proxy(getSerialized());