More cds-access-api cleanup
[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.Externalizable;
20 import java.io.IOException;
21 import java.io.ObjectInput;
22 import java.io.ObjectOutput;
23 import java.io.Serial;
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;
29
30 /**
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
33  * discerned.
34  */
35 public final class FrontendType implements Comparable<FrontendType>, WritableIdentifier {
36     private static final class Proxy implements Externalizable {
37         @Serial
38         private static final long serialVersionUID = 1L;
39
40         private byte[] serialized;
41
42         // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
43         // be able to create instances via reflection.
44         @SuppressWarnings("checkstyle:RedundantModifier")
45         public Proxy() {
46             // For Externalizable
47         }
48
49         Proxy(final byte[] serialized) {
50             this.serialized = requireNonNull(serialized);
51         }
52
53         @Override
54         public void writeExternal(final ObjectOutput out) throws IOException {
55             out.writeInt(serialized.length);
56             out.write(serialized);
57         }
58
59         @Override
60         public void readExternal(final ObjectInput in) throws IOException {
61             serialized = new byte[in.readInt()];
62             in.readFully(serialized);
63         }
64
65         @Serial
66         private Object readResolve() {
67             // TODO: consider caching instances here
68             return new FrontendType(new String(serialized, StandardCharsets.UTF_8), serialized);
69         }
70     }
71
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     @Serial
75     private static final long serialVersionUID = 1L;
76
77     private final @NonNull String name;
78
79     @SuppressFBWarnings(value = "VO_VOLATILE_REFERENCE_TO_ARRAY",
80             justification = "The array elements are non-volatile but we don't access them.")
81     private volatile byte[] serialized;
82
83     private FrontendType(final String name) {
84         this.name = requireNonNull(name);
85     }
86
87     FrontendType(final String name, final byte[] serialized) {
88         this(name);
89         this.serialized = verifyNotNull(serialized);
90     }
91
92     /**
93      * Return a {@link FrontendType} corresponding to a string representation. Input string has constraints
94      * on what characters it can contain. It may contain the following:
95      * - US-ASCII letters and numbers
96      * - special characters: -_.*+:=,!~';
97      *
98      * @param name the input name
99      * @return A {@link FrontendType} instance
100      * @throws IllegalArgumentException if the string is null, empty or contains invalid characters
101      */
102     public static @NonNull FrontendType forName(final String name) {
103         checkArgument(!Strings.isNullOrEmpty(name));
104         checkArgument(SIMPLE_STRING_PATTERN.matcher(name).matches(),
105             "Supplied name %s does not patch pattern %s", name, SIMPLE_STRING_REGEX);
106         return new FrontendType(name);
107     }
108
109     public static @NonNull FrontendType readFrom(final DataInput in) throws IOException {
110         final byte[] serialized = new byte[in.readInt()];
111         in.readFully(serialized);
112         return new FrontendType(new String(serialized, StandardCharsets.UTF_8));
113     }
114
115     @Override
116     public void writeTo(final DataOutput out) throws IOException {
117         final byte[] local = getSerialized();
118         out.writeInt(local.length);
119         out.write(local);
120     }
121
122     public @NonNull String getName() {
123         return name;
124     }
125
126     public org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
127         . @NonNull FrontendType toYang() {
128         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024
129                 .FrontendType(name);
130     }
131
132     @Override
133     public int hashCode() {
134         return name.hashCode();
135     }
136
137     @Override
138     public boolean equals(final Object obj) {
139         return this == obj || obj instanceof FrontendType && name.equals(((FrontendType)obj).name);
140     }
141
142     @Override
143     public int compareTo(final FrontendType obj) {
144         return this == obj ? 0 : name.compareTo(obj.name);
145     }
146
147     @Override
148     public String toString() {
149         return MoreObjects.toStringHelper(FrontendType.class).add("name", name).toString();
150     }
151
152     private byte[] getSerialized() {
153         byte[] local = serialized;
154         if (local == null) {
155             local = name.getBytes(StandardCharsets.UTF_8);
156             serialized = local;
157         }
158         return local;
159     }
160
161     @Serial
162     Object writeReplace() {
163         return new Proxy(getSerialized());
164     }
165 }