Improve segmented journal actor metrics
[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 static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
13
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.IOException;
20 import java.nio.charset.StandardCharsets;
21 import java.util.regex.Pattern;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.opendaylight.yangtools.concepts.Identifier;
24 import org.opendaylight.yangtools.concepts.WritableIdentifier;
25
26 /**
27  * An {@link Identifier} identifying a data store frontend type, which is able to access the data store backend.
28  * Frontend implementations need to define this identifier so that multiple clients existing on a member node can be
29  * discerned.
30  */
31 public final class FrontendType implements Comparable<FrontendType>, WritableIdentifier {
32     @java.io.Serial
33     private static final long serialVersionUID = 1L;
34     private static final String SIMPLE_STRING_REGEX = "^[a-zA-Z0-9-_.*+:=,!~';]+$";
35     private static final Pattern SIMPLE_STRING_PATTERN = Pattern.compile(SIMPLE_STRING_REGEX);
36
37     private final @NonNull String name;
38
39     @SuppressFBWarnings(value = "VO_VOLATILE_REFERENCE_TO_ARRAY",
40             justification = "The array elements are non-volatile but we don't access them.")
41     private volatile byte[] serialized;
42
43     private FrontendType(final String name) {
44         this.name = requireNonNull(name);
45     }
46
47     FrontendType(final String name, final byte[] serialized) {
48         this(name);
49         this.serialized = verifyNotNull(serialized);
50     }
51
52     /**
53      * Return a {@link FrontendType} corresponding to a string representation. Input string has constraints
54      * on what characters it can contain. It may contain the following:
55      * - US-ASCII letters and numbers
56      * - special characters: -_.*+:=,!~';
57      *
58      * @param name the input name
59      * @return A {@link FrontendType} instance
60      * @throws IllegalArgumentException if the string is null, empty or contains invalid characters
61      */
62     public static @NonNull FrontendType forName(final String name) {
63         checkArgument(!Strings.isNullOrEmpty(name));
64         checkArgument(SIMPLE_STRING_PATTERN.matcher(name).matches(),
65             "Supplied name %s does not patch pattern %s", name, SIMPLE_STRING_REGEX);
66         return new FrontendType(name);
67     }
68
69     public static @NonNull FrontendType readFrom(final DataInput in) throws IOException {
70         final byte[] serialized = new byte[in.readInt()];
71         in.readFully(serialized);
72         return new FrontendType(new String(serialized, StandardCharsets.UTF_8));
73     }
74
75     @Override
76     public void writeTo(final DataOutput out) throws IOException {
77         final byte[] local = getSerialized();
78         out.writeInt(local.length);
79         out.write(local);
80     }
81
82     public @NonNull String getName() {
83         return name;
84     }
85
86     public org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
87         . @NonNull FrontendType toYang() {
88         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
89                 .FrontendType(name);
90     }
91
92     @Override
93     public int hashCode() {
94         return name.hashCode();
95     }
96
97     @Override
98     public boolean equals(final Object obj) {
99         return this == obj || obj instanceof FrontendType && name.equals(((FrontendType)obj).name);
100     }
101
102     @Override
103     public int compareTo(final FrontendType obj) {
104         return this == obj ? 0 : name.compareTo(obj.name);
105     }
106
107     @Override
108     public String toString() {
109         return MoreObjects.toStringHelper(FrontendType.class).add("name", name).toString();
110     }
111
112     private byte[] getSerialized() {
113         byte[] local = serialized;
114         if (local == null) {
115             local = name.getBytes(StandardCharsets.UTF_8);
116             serialized = local;
117         }
118         return local;
119     }
120
121     @java.io.Serial
122     private Object writeReplace() {
123         return new FT(getSerialized());
124     }
125 }