blob: c5763758b84184ce0cff8cab6732b97dc682f41e [file] [log] [blame]
Jordan Halterman2c045992018-03-20 21:33:00 -07001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.store.primitives;
17
18import org.onosproject.core.ApplicationId;
19import org.onosproject.core.Version;
20import org.onosproject.store.service.DistributedPrimitive;
21import org.onosproject.store.service.Serializer;
22import org.onosproject.store.service.RevisionType;
23
24import static com.google.common.base.Preconditions.checkNotNull;
25
26/**
27 * Abstract builder for distributed primitives.
28 *
29 * @param <O> distributed primitive options type
30 */
31public abstract class DistributedPrimitiveOptions<O extends DistributedPrimitiveOptions<O>> {
32
33 private final DistributedPrimitive.Type type;
34 private String name;
35 private ApplicationId applicationId;
36 private Serializer serializer;
37 private boolean partitionsDisabled = false;
38 private boolean meteringDisabled = false;
39 private boolean readOnly = false;
40 private boolean relaxedReadConsistency = false;
41 private int revisionNumber = 1;
42 private RevisionType revisionType = RevisionType.NONE;
43
44 public DistributedPrimitiveOptions(DistributedPrimitive.Type type) {
45 this.type = type;
46 }
47
48 /**
49 * Sets the primitive name.
50 *
51 * @param name primitive name
52 * @return this builder
53 */
54 public O withName(String name) {
55 this.name = name;
56 return (O) this;
57 }
58
59 /**
60 * Sets the serializer to use for transcoding info held in the primitive.
61 *
62 * @param serializer serializer
63 * @return this builder
64 */
65 public O withSerializer(Serializer serializer) {
66 this.serializer = serializer;
67 return (O) this;
68 }
69
70 /**
71 * Sets the application id that owns this primitive.
72 *
73 * @param applicationId application identifier
74 * @return this builder
75 */
76 public O withApplicationId(ApplicationId applicationId) {
77 this.applicationId = applicationId;
78 return (O) this;
79 }
80
81 /**
82 * Sets the primitive version.
83 *
84 * @param version the primitive version
85 * @return this builder
86 */
87 public O withVersion(Version version) {
88 return withRevisionNumber(version.toInt());
89 }
90
91 /**
92 * Sets the primitive revision.
93 *
94 * @param revision the primitive revision
95 * @return this builder
96 */
97 public O withRevisionNumber(int revision) {
98 this.revisionNumber = revision;
99 return (O) this;
100 }
101
102 /**
103 * Sets the primitive revision type.
104 *
105 * @param revisionType the revision type
106 * @return this builder
107 */
108 public O withRevisionType(RevisionType revisionType) {
109 this.revisionType = checkNotNull(revisionType);
110 return (O) this;
111 }
112
113 /**
114 * Disables recording usage stats for this primitive.
115 * @deprecated usage of this method is discouraged for most common scenarios.
116 * @return this builder
117 */
118 @Deprecated
119 public O withMeteringDisabled() {
120 this.meteringDisabled = true;
121 return (O) this;
122 }
123
124 /**
125 * Disables state changing operations on the returned distributed primitive.
126 * @return this builder
127 */
128 public O withUpdatesDisabled() {
129 this.readOnly = true;
130 return (O) this;
131 }
132
133 /**
134 * Turns on relaxed consistency for read operations.
135 * @return this builder
136 */
137 public O withRelaxedReadConsistency() {
138 this.relaxedReadConsistency = true;
139 return (O) this;
140 }
141
142 /**
143 * Returns if metering is enabled.
144 *
145 * @return {@code true} if yes; {@code false} otherwise
146 */
147 public final boolean meteringEnabled() {
148 return !meteringDisabled;
149 }
150
151 /**
152 * Returns if partitions are disabled.
153 *
154 * @return {@code true} if yes; {@code false} otherwise
155 */
156 public final boolean partitionsDisabled() {
157 return partitionsDisabled;
158 }
159
160 /**
161 * Returns if updates are disabled.
162 *
163 * @return {@code true} if yes; {@code false} otherwise
164 */
165 public final boolean readOnly() {
166 return readOnly;
167 }
168
169 /**
170 * Returns if consistency is relaxed for read operations.
171 *
172 * @return {@code true} if yes; {@code false} otherwise
173 */
174 public final boolean relaxedReadConsistency() {
175 return relaxedReadConsistency;
176 }
177
178 /**
179 * Returns the serializer.
180 *
181 * @return serializer
182 */
183 public final Serializer serializer() {
184 return serializer;
185 }
186
187 /**
188 * Returns the application identifier.
189 *
190 * @return application id
191 */
192 public final ApplicationId applicationId() {
193 return applicationId;
194 }
195
196 /**
197 * Returns the name of the primitive.
198 *
199 * @return primitive name
200 */
201 public final String name() {
202 return name;
203 }
204
205 /**
206 * Returns the primitive type.
207 *
208 * @return primitive type
209 */
210 public final DistributedPrimitive.Type type() {
211 return type;
212 }
213
214 /**
215 * Returns the primitive revision number.
216 *
217 * @return the primitive revision number
218 */
219 public final int revision() {
220 return revisionNumber;
221 }
222
223 /**
224 * Returns the primitive revision type.
225 *
226 * @return the primitive revision type
227 */
228 public RevisionType revisionType() {
229 return revisionType;
230 }
231}