BUG-5280: validate FrontendType regular expression
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / FrontendType.java
index 27db82068dc6b38e5974a185e8c32cd9ec3ee5a7..471b489046fb20fab5b0789275dcb67391bcf7a2 100644 (file)
@@ -8,8 +8,19 @@
 package org.opendaylight.controller.cluster.access.concepts;
 
 import com.google.common.annotations.Beta;
 package org.opendaylight.controller.cluster.access.concepts;
 
 import com.google.common.annotations.Beta;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import com.google.common.base.Verify;
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.nio.charset.StandardCharsets;
 import java.util.regex.Pattern;
 import java.util.regex.Pattern;
-import javax.annotation.Nonnull;
+import javax.annotation.RegEx;
 import org.opendaylight.yangtools.concepts.Identifier;
 
 /**
 import org.opendaylight.yangtools.concepts.Identifier;
 
 /**
@@ -20,16 +31,116 @@ import org.opendaylight.yangtools.concepts.Identifier;
  * @author Robert Varga
  */
 @Beta
  * @author Robert Varga
  */
 @Beta
-public interface FrontendType extends Identifier {
-    Pattern SIMPLE_STRING_PATTERN = Pattern.compile("[a-zA-Z-_.*+:=,!~';]+");
+public final class FrontendType implements Comparable<FrontendType>, Identifier, WritableObject {
+    private static final class Proxy implements Externalizable {
+        private static final long serialVersionUID = 1L;
+        private byte[] serialized;
+
+        public Proxy() {
+            // For Externalizable
+        }
+
+        Proxy(final byte[] serialized) {
+            this.serialized = Preconditions.checkNotNull(serialized);
+        }
+
+        @Override
+        public void writeExternal(final ObjectOutput out) throws IOException {
+            out.writeInt(serialized.length);
+            out.write(serialized);
+        }
+
+        @Override
+        public void readExternal(final ObjectInput in) throws IOException {
+            serialized = new byte[in.readInt()];
+            in.readFully(serialized);
+        }
+
+        private Object readResolve() {
+            // TODO: consider caching instances here
+            return new FrontendType(new String(serialized, StandardCharsets.UTF_8), serialized);
+        }
+    }
+
+    @RegEx
+    private static final String SIMPLE_STRING_REGEX = "^[a-zA-Z0-9-_.*+:=,!~';]+$";
+    private static final Pattern SIMPLE_STRING_PATTERN = Pattern.compile(SIMPLE_STRING_REGEX);
+    private static final long serialVersionUID = 1L;
+    private final String name;
+    private volatile byte[] serialized;
+
+    private FrontendType(final String name) {
+        this.name = Preconditions.checkNotNull(name);
+    }
+
+    FrontendType(final String name, final byte[] serialized) {
+        this(name);
+        this.serialized = Verify.verifyNotNull(serialized);
+    }
 
     /**
 
     /**
-     * Return a string representation of this frontend type. Unlike {@link #toString()}, returned string has
-     * restricted rules on what it can contain:
-     * - US-ASCII letters
+     * Return a {@link FrontendType} corresponding to a string representation. Input string has constraints
+     * on what characters it can contain. It may contain the following:
+     * - US-ASCII letters and numbers
      * - special characters: -_.*+:=,!~';
      *
      * - special characters: -_.*+:=,!~';
      *
-     * A validation pattern for this string is available as {@link #SIMPLE_STRING_PATTERN}.
+     * @return A {@link FrontendType} instance
+     * @throws IllegalArgumentException if the string is null, empty or contains invalid characters
      */
      */
-    @Nonnull String toSimpleString();
+    public static FrontendType forName(final String name) {
+        Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
+        Preconditions.checkArgument(SIMPLE_STRING_PATTERN.matcher(name).matches(),
+            "Supplied name %s does not patch pattern %s", name, SIMPLE_STRING_REGEX);
+        return new FrontendType(name);
+    }
+
+    public static FrontendType readFrom(final DataInput in) throws IOException {
+        final byte[] serialized = new byte[in.readInt()];
+        in.readFully(serialized);
+        return new FrontendType(new String(serialized, StandardCharsets.UTF_8));
+    }
+
+    @Override
+    public void writeTo(final DataOutput out) throws IOException {
+        final byte[] serialized = getSerialized();
+        out.writeInt(serialized.length);
+        out.write(serialized);
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    @Override
+    public int hashCode() {
+        return name.hashCode();
+    }
+
+    @Override
+    public boolean equals(final Object o) {
+        return this == o || (o instanceof FrontendType && name.equals(((FrontendType)o).name));
+    }
+
+    @Override
+    public int compareTo(final FrontendType o) {
+        return this == o ? 0 : name.compareTo(o.name);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(FrontendType.class).add("name", name).toString();
+    }
+
+    private byte[] getSerialized() {
+        byte[] local = serialized;
+        if (local == null) {
+            local = name.getBytes(StandardCharsets.UTF_8);
+            serialized = local;
+        }
+        return local;
+    }
+
+    Object writeReplace() {
+        return new Proxy(getSerialized());
+    }
 }
 }