BUG-5280: introduce Request/Response and related concepts
[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         public Proxy() {
41             // For Externalizable
42         }
43
44         Proxy(final byte[] serialized) {
45             this.serialized = Preconditions.checkNotNull(serialized);
46         }
47
48         @Override
49         public void writeExternal(final ObjectOutput out) throws IOException {
50             out.writeInt(serialized.length);
51             out.write(serialized);
52         }
53
54         @Override
55         public void readExternal(final ObjectInput in) throws IOException {
56             serialized = new byte[in.readInt()];
57             in.readFully(serialized);
58         }
59
60         private Object readResolve() {
61             // TODO: consider caching instances here
62             return new FrontendType(new String(serialized, StandardCharsets.UTF_8), serialized);
63         }
64     }
65
66     @RegEx
67     private static final String SIMPLE_STRING_REGEX = "^[a-zA-Z0-9-_.*+:=,!~';]+$";
68     private static final Pattern SIMPLE_STRING_PATTERN = Pattern.compile(SIMPLE_STRING_REGEX);
69     private static final long serialVersionUID = 1L;
70     private final String name;
71     private volatile byte[] serialized;
72
73     private FrontendType(final String name) {
74         this.name = Preconditions.checkNotNull(name);
75     }
76
77     FrontendType(final String name, final byte[] serialized) {
78         this(name);
79         this.serialized = Verify.verifyNotNull(serialized);
80     }
81
82     /**
83      * Return a {@link FrontendType} corresponding to a string representation. Input string has constraints
84      * on what characters it can contain. It may contain the following:
85      * - US-ASCII letters and numbers
86      * - special characters: -_.*+:=,!~';
87      *
88      * @return A {@link FrontendType} instance
89      * @throws IllegalArgumentException if the string is null, empty or contains invalid characters
90      */
91     public static FrontendType forName(final String name) {
92         Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
93         Preconditions.checkArgument(SIMPLE_STRING_PATTERN.matcher(name).matches(),
94             "Supplied name %s does not patch pattern %s", name, SIMPLE_STRING_REGEX);
95         return new FrontendType(name);
96     }
97
98     public static FrontendType readFrom(final DataInput in) throws IOException {
99         final byte[] serialized = new byte[in.readInt()];
100         in.readFully(serialized);
101         return new FrontendType(new String(serialized, StandardCharsets.UTF_8));
102     }
103
104     @Override
105     public void writeTo(final DataOutput out) throws IOException {
106         final byte[] serialized = getSerialized();
107         out.writeInt(serialized.length);
108         out.write(serialized);
109     }
110
111     public String getName() {
112         return name;
113     }
114
115     @Override
116     public int hashCode() {
117         return name.hashCode();
118     }
119
120     @Override
121     public boolean equals(final Object o) {
122         return this == o || (o instanceof FrontendType && name.equals(((FrontendType)o).name));
123     }
124
125     @Override
126     public int compareTo(final FrontendType o) {
127         return this == o ? 0 : name.compareTo(o.name);
128     }
129
130     @Override
131     public String toString() {
132         return MoreObjects.toStringHelper(FrontendType.class).add("name", name).toString();
133     }
134
135     private byte[] getSerialized() {
136         byte[] local = serialized;
137         if (local == null) {
138             local = name.getBytes(StandardCharsets.UTF_8);
139             serialized = local;
140         }
141         return local;
142     }
143
144     Object writeReplace() {
145         return new Proxy(getSerialized());
146     }
147 }