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