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