blob: f9939da108b33c0567b4b03e57d07113c4e5cb4f [file] [log] [blame]
Madan Jampanie17d3282016-02-03 15:30:57 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampanie17d3282016-02-03 15:30:57 -08003 *
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
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070018import java.util.concurrent.Executor;
19import java.util.function.Supplier;
20
Madan Jampanie17d3282016-02-03 15:30:57 -080021import org.onosproject.core.ApplicationId;
22import org.onosproject.store.service.DistributedPrimitive;
23import org.onosproject.store.service.Serializer;
24
25/**
26 * Abstract builder for distributed primitives.
27 *
28 * @param <T> distributed primitive type
29 */
Madan Jampani538be742016-02-10 14:55:38 -080030public abstract class DistributedPrimitiveBuilder<B extends DistributedPrimitiveBuilder<B, T>,
31 T extends DistributedPrimitive> {
Madan Jampanie17d3282016-02-03 15:30:57 -080032
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070033 private final DistributedPrimitive.Type type;
Madan Jampanie17d3282016-02-03 15:30:57 -080034 private String name;
35 private ApplicationId applicationId;
36 private Serializer serializer;
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070037 private Supplier<Executor> executorSupplier;
Madan Jampanie17d3282016-02-03 15:30:57 -080038 private boolean partitionsDisabled = false;
39 private boolean meteringDisabled = false;
Madan Jampani538be742016-02-10 14:55:38 -080040 private boolean readOnly = false;
41 private boolean relaxedReadConsistency = false;
Madan Jampanie17d3282016-02-03 15:30:57 -080042
43 public DistributedPrimitiveBuilder(DistributedPrimitive.Type type) {
44 this.type = type;
45 }
46
47 /**
48 * Sets the primitive name.
49 *
50 * @param name primitive name
51 * @return this builder
52 */
Madan Jampani538be742016-02-10 14:55:38 -080053 public B withName(String name) {
Madan Jampanie17d3282016-02-03 15:30:57 -080054 this.name = name;
Madan Jampani538be742016-02-10 14:55:38 -080055 return (B) this;
Madan Jampanie17d3282016-02-03 15:30:57 -080056 }
57
58 /**
59 * Sets the serializer to use for transcoding info held in the primitive.
60 *
61 * @param serializer serializer
62 * @return this builder
63 */
Madan Jampani538be742016-02-10 14:55:38 -080064 public B withSerializer(Serializer serializer) {
Madan Jampanie17d3282016-02-03 15:30:57 -080065 this.serializer = serializer;
Madan Jampani538be742016-02-10 14:55:38 -080066 return (B) this;
Madan Jampanie17d3282016-02-03 15:30:57 -080067 }
68
69 /**
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070070 * Sets the executor to use for asynchronous callbacks.
71 * <p>
72 * For partitioned primitives, the provided executor will be shared across all partitions.
73 *
74 * @param executor the executor to use for asynchronous callbacks
75 * @return this builder
76 */
77 public B withExecutor(Executor executor) {
78 return withExecutorSupplier(() -> executor);
79 }
80
81 /**
82 * Sets the supplier to be used to create executors.
83 * <p>
84 * When a factory is set, the supplier will be used to create a separate executor for each partition.
85 *
86 * @param executorSupplier the executor supplier
87 * @return this builder
88 */
89 public B withExecutorSupplier(Supplier<Executor> executorSupplier) {
90 this.executorSupplier = executorSupplier;
91 return (B) this;
92 }
93
94 /**
Madan Jampanie17d3282016-02-03 15:30:57 -080095 * Sets the application id that owns this primitive.
96 *
97 * @param applicationId application identifier
98 * @return this builder
99 */
Madan Jampani538be742016-02-10 14:55:38 -0800100 public B withApplicationId(ApplicationId applicationId) {
Madan Jampanie17d3282016-02-03 15:30:57 -0800101 this.applicationId = applicationId;
Madan Jampani538be742016-02-10 14:55:38 -0800102 return (B) this;
Madan Jampanie17d3282016-02-03 15:30:57 -0800103 }
104
105 /**
Madan Jampanie17d3282016-02-03 15:30:57 -0800106 * Disables recording usage stats for this primitive.
107 * @deprecated usage of this method is discouraged for most common scenarios.
108 * @return this builder
109 */
110 @Deprecated
Madan Jampani538be742016-02-10 14:55:38 -0800111 public B withMeteringDisabled() {
Madan Jampanie17d3282016-02-03 15:30:57 -0800112 this.meteringDisabled = true;
Madan Jampani538be742016-02-10 14:55:38 -0800113 return (B) this;
114 }
115
116 /**
117 * Disables state changing operations on the returned distributed primitive.
118 * @return this builder
119 */
120 public B withUpdatesDisabled() {
121 this.readOnly = true;
122 return (B) this;
123 }
124
125 /**
126 * Turns on relaxed consistency for read operations.
127 * @return this builder
128 */
129 public B withRelaxedReadConsistency() {
130 this.relaxedReadConsistency = true;
131 return (B) this;
Madan Jampanie17d3282016-02-03 15:30:57 -0800132 }
133
134 /**
135 * Returns if metering is enabled.
136 *
137 * @return {@code true} if yes; {@code false} otherwise
138 */
139 public final boolean meteringEnabled() {
140 return !meteringDisabled;
141 }
142
143 /**
144 * Returns if partitions are disabled.
145 *
146 * @return {@code true} if yes; {@code false} otherwise
147 */
148 public final boolean partitionsDisabled() {
149 return partitionsDisabled;
150 }
151
152 /**
Madan Jampani538be742016-02-10 14:55:38 -0800153 * Returns if updates are disabled.
154 *
155 * @return {@code true} if yes; {@code false} otherwise
156 */
157 public final boolean readOnly() {
158 return readOnly;
159 }
160
161 /**
162 * Returns if consistency is relaxed for read operations.
163 *
164 * @return {@code true} if yes; {@code false} otherwise
165 */
166 public final boolean relaxedReadConsistency() {
167 return relaxedReadConsistency;
168 }
169
170 /**
Madan Jampanie17d3282016-02-03 15:30:57 -0800171 * Returns the serializer.
172 *
173 * @return serializer
174 */
175 public final Serializer serializer() {
176 return serializer;
177 }
178
179 /**
Jordan Halterman9bdc24f2017-04-19 23:45:12 -0700180 * Returns the executor supplier.
181 *
182 * @return executor supplier
183 */
184 public final Supplier<Executor> executorSupplier() {
185 return executorSupplier;
186 }
187
188 /**
Madan Jampanie17d3282016-02-03 15:30:57 -0800189 * Returns the application identifier.
190 *
191 * @return application id
192 */
193 public final ApplicationId applicationId() {
194 return applicationId;
195 }
196
197 /**
198 * Returns the name of the primitive.
199 *
200 * @return primitive name
201 */
202 public final String name() {
203 return name;
204 }
205
206 /**
207 * Returns the primitive type.
208 *
209 * @return primitive type
210 */
211 public final DistributedPrimitive.Type type() {
212 return type;
213 }
214
215 /**
216 * Constructs an instance of the distributed primitive.
217 * @return distributed primitive
218 */
219 public abstract T build();
220}