blob: 1691086e203603348b100dc378817e506596c397 [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;
Jordan Halterman45008172018-03-19 16:40:31 -070041 private Version version;
42 private RevisionType revisionType;
Jordan Halterman2c045992018-03-20 21:33:00 -070043
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) {
Jordan Halterman45008172018-03-19 16:40:31 -070088 this.version = version;
Jordan Halterman2c045992018-03-20 21:33:00 -070089 return (O) this;
90 }
91
92 /**
93 * Sets the primitive revision type.
94 *
95 * @param revisionType the revision type
96 * @return this builder
97 */
98 public O withRevisionType(RevisionType revisionType) {
99 this.revisionType = checkNotNull(revisionType);
100 return (O) this;
101 }
102
103 /**
104 * Disables recording usage stats for this primitive.
105 * @deprecated usage of this method is discouraged for most common scenarios.
106 * @return this builder
107 */
108 @Deprecated
109 public O withMeteringDisabled() {
110 this.meteringDisabled = true;
111 return (O) this;
112 }
113
114 /**
115 * Disables state changing operations on the returned distributed primitive.
116 * @return this builder
117 */
118 public O withUpdatesDisabled() {
119 this.readOnly = true;
120 return (O) this;
121 }
122
123 /**
124 * Turns on relaxed consistency for read operations.
125 * @return this builder
126 */
127 public O withRelaxedReadConsistency() {
128 this.relaxedReadConsistency = true;
129 return (O) this;
130 }
131
132 /**
133 * Returns if metering is enabled.
134 *
135 * @return {@code true} if yes; {@code false} otherwise
136 */
137 public final boolean meteringEnabled() {
138 return !meteringDisabled;
139 }
140
141 /**
142 * Returns if partitions are disabled.
143 *
144 * @return {@code true} if yes; {@code false} otherwise
145 */
146 public final boolean partitionsDisabled() {
147 return partitionsDisabled;
148 }
149
150 /**
151 * Returns if updates are disabled.
152 *
153 * @return {@code true} if yes; {@code false} otherwise
154 */
155 public final boolean readOnly() {
156 return readOnly;
157 }
158
159 /**
160 * Returns if consistency is relaxed for read operations.
161 *
162 * @return {@code true} if yes; {@code false} otherwise
163 */
164 public final boolean relaxedReadConsistency() {
165 return relaxedReadConsistency;
166 }
167
168 /**
169 * Returns the serializer.
170 *
171 * @return serializer
172 */
173 public final Serializer serializer() {
174 return serializer;
175 }
176
177 /**
178 * Returns the application identifier.
179 *
180 * @return application id
181 */
182 public final ApplicationId applicationId() {
183 return applicationId;
184 }
185
186 /**
187 * Returns the name of the primitive.
188 *
189 * @return primitive name
190 */
191 public final String name() {
192 return name;
193 }
194
195 /**
196 * Returns the primitive type.
197 *
198 * @return primitive type
199 */
200 public final DistributedPrimitive.Type type() {
201 return type;
202 }
203
204 /**
Jordan Halterman45008172018-03-19 16:40:31 -0700205 * Returns the primitive version.
Jordan Halterman2c045992018-03-20 21:33:00 -0700206 *
Jordan Halterman45008172018-03-19 16:40:31 -0700207 * @return the primitive version
Jordan Halterman2c045992018-03-20 21:33:00 -0700208 */
Jordan Halterman45008172018-03-19 16:40:31 -0700209 public final Version version() {
210 return version;
Jordan Halterman2c045992018-03-20 21:33:00 -0700211 }
212
213 /**
214 * Returns the primitive revision type.
215 *
216 * @return the primitive revision type
217 */
218 public RevisionType revisionType() {
219 return revisionType;
220 }
221}