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