Remove config module archetype
[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 com.google.common.annotations.Beta;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Strings;
14 import com.google.common.base.Verify;
15 import java.io.DataInput;
16 import java.io.DataOutput;
17 import java.io.Externalizable;
18 import java.io.IOException;
19 import java.io.ObjectInput;
20 import java.io.ObjectOutput;
21 import java.nio.charset.StandardCharsets;
22 import java.util.regex.Pattern;
23 import javax.annotation.RegEx;
24 import org.opendaylight.yangtools.concepts.Identifier;
25 import org.opendaylight.yangtools.concepts.WritableIdentifier;
26
27 /**
28  * An {@link Identifier} identifying a data store frontend type, which is able to access the data store backend.
29  * Frontend implementations need to define this identifier so that multiple clients existing on a member node can be
30  * discerned.
31  *
32  * @author Robert Varga
33  */
34 @Beta
35 public final class FrontendType implements Comparable<FrontendType>, WritableIdentifier {
36     private static final class Proxy implements Externalizable {
37         private static final long serialVersionUID = 1L;
38         private byte[] serialized;
39
40         // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
41         // be able to create instances via reflection.
42         @SuppressWarnings("checkstyle:RedundantModifier")
43         public Proxy() {
44             // For Externalizable
45         }
46
47         Proxy(final byte[] serialized) {
48             this.serialized = Preconditions.checkNotNull(serialized);
49         }
50
51         @Override
52         public void writeExternal(final ObjectOutput out) throws IOException {
53             out.writeInt(serialized.length);
54             out.write(serialized);
55         }
56
57         @Override
58         public void readExternal(final ObjectInput in) throws IOException {
59             serialized = new byte[in.readInt()];
60             in.readFully(serialized);
61         }
62
63         private Object readResolve() {
64             // TODO: consider caching instances here
65             return new FrontendType(new String(serialized, StandardCharsets.UTF_8), serialized);
66         }
67     }
68
69     @RegEx
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     private static final long serialVersionUID = 1L;
73     private final String name;
74     private volatile byte[] serialized;
75
76     private FrontendType(final String name) {
77         this.name = Preconditions.checkNotNull(name);
78     }
79
80     FrontendType(final String name, final byte[] serialized) {
81         this(name);
82         this.serialized = Verify.verifyNotNull(serialized);
83     }
84
85     /**
86      * Return a {@link FrontendType} corresponding to a string representation. Input string has constraints
87      * on what characters it can contain. It may contain the following:
88      * - US-ASCII letters and numbers
89      * - special characters: -_.*+:=,!~';
90      *
91      * @param name the input name
92      * @return A {@link FrontendType} instance
93      * @throws IllegalArgumentException if the string is null, empty or contains invalid characters
94      */
95     public static FrontendType forName(final String name) {
96         Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
97         Preconditions.checkArgument(SIMPLE_STRING_PATTERN.matcher(name).matches(),
98             "Supplied name %s does not patch pattern %s", name, SIMPLE_STRING_REGEX);
99         return new FrontendType(name);
100     }
101
102     public static FrontendType readFrom(final DataInput in) throws IOException {
103         final byte[] serialized = new byte[in.readInt()];
104         in.readFully(serialized);
105         return new FrontendType(new String(serialized, StandardCharsets.UTF_8));
106     }
107
108     @Override
109     public void writeTo(final DataOutput out) throws IOException {
110         final byte[] local = getSerialized();
111         out.writeInt(local.length);
112         out.write(local);
113     }
114
115     public String getName() {
116         return name;
117     }
118
119     @Override
120     public int hashCode() {
121         return name.hashCode();
122     }
123
124     @Override
125     public boolean equals(final Object obj) {
126         return this == obj || obj instanceof FrontendType && name.equals(((FrontendType)obj).name);
127     }
128
129     @Override
130     public int compareTo(final FrontendType obj) {
131         return this == obj ? 0 : name.compareTo(obj.name);
132     }
133
134     @Override
135     public String toString() {
136         return MoreObjects.toStringHelper(FrontendType.class).add("name", name).toString();
137     }
138
139     private byte[] getSerialized() {
140         byte[] local = serialized;
141         if (local == null) {
142             local = name.getBytes(StandardCharsets.UTF_8);
143             serialized = local;
144         }
145         return local;
146     }
147
148     Object writeReplace() {
149         return new Proxy(getSerialized());
150     }
151 }