blob: 7ed8e81c4307af59de6a00612b9fd877b9cf4c17 [file] [log] [blame]
Madan Jampanie17d3282016-02-03 15:30:57 -08001/*
2 * Copyright 2016 Open Networking Laboratory
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.store.service.DistributedPrimitive;
20import org.onosproject.store.service.Serializer;
21
22/**
23 * Abstract builder for distributed primitives.
24 *
25 * @param <T> distributed primitive type
26 */
Madan Jampani538be742016-02-10 14:55:38 -080027public abstract class DistributedPrimitiveBuilder<B extends DistributedPrimitiveBuilder<B, T>,
28 T extends DistributedPrimitive> {
Madan Jampanie17d3282016-02-03 15:30:57 -080029
30 private DistributedPrimitive.Type type;
31 private String name;
32 private ApplicationId applicationId;
33 private Serializer serializer;
34 private boolean partitionsDisabled = false;
35 private boolean meteringDisabled = false;
Madan Jampani538be742016-02-10 14:55:38 -080036 private boolean readOnly = false;
37 private boolean relaxedReadConsistency = false;
Madan Jampanie17d3282016-02-03 15:30:57 -080038
39 public DistributedPrimitiveBuilder(DistributedPrimitive.Type type) {
40 this.type = type;
41 }
42
43 /**
44 * Sets the primitive name.
45 *
46 * @param name primitive name
47 * @return this builder
48 */
Madan Jampani538be742016-02-10 14:55:38 -080049 public B withName(String name) {
Madan Jampanie17d3282016-02-03 15:30:57 -080050 this.name = name;
Madan Jampani538be742016-02-10 14:55:38 -080051 return (B) this;
Madan Jampanie17d3282016-02-03 15:30:57 -080052 }
53
54 /**
55 * Sets the serializer to use for transcoding info held in the primitive.
56 *
57 * @param serializer serializer
58 * @return this builder
59 */
Madan Jampani538be742016-02-10 14:55:38 -080060 public B withSerializer(Serializer serializer) {
Madan Jampanie17d3282016-02-03 15:30:57 -080061 this.serializer = serializer;
Madan Jampani538be742016-02-10 14:55:38 -080062 return (B) this;
Madan Jampanie17d3282016-02-03 15:30:57 -080063 }
64
65 /**
66 * Sets the application id that owns this primitive.
67 *
68 * @param applicationId application identifier
69 * @return this builder
70 */
Madan Jampani538be742016-02-10 14:55:38 -080071 public B withApplicationId(ApplicationId applicationId) {
Madan Jampanie17d3282016-02-03 15:30:57 -080072 this.applicationId = applicationId;
Madan Jampani538be742016-02-10 14:55:38 -080073 return (B) this;
Madan Jampanie17d3282016-02-03 15:30:57 -080074 }
75
76 /**
77 * Creates this primitive on a special partition that comprises of all members in the cluster.
78 * @deprecated usage of this method is discouraged for most common scenarios. Eventually it will be replaced
79 * with a better alternative that does not exposes low level details. Until then avoid using this method.
80 * @return this builder
81 */
82 @Deprecated
Madan Jampani538be742016-02-10 14:55:38 -080083 public B withPartitionsDisabled() {
Madan Jampanie17d3282016-02-03 15:30:57 -080084 this.partitionsDisabled = true;
Madan Jampani538be742016-02-10 14:55:38 -080085 return (B) this;
Madan Jampanie17d3282016-02-03 15:30:57 -080086 }
87
88 /**
89 * Disables recording usage stats for this primitive.
90 * @deprecated usage of this method is discouraged for most common scenarios.
91 * @return this builder
92 */
93 @Deprecated
Madan Jampani538be742016-02-10 14:55:38 -080094 public B withMeteringDisabled() {
Madan Jampanie17d3282016-02-03 15:30:57 -080095 this.meteringDisabled = true;
Madan Jampani538be742016-02-10 14:55:38 -080096 return (B) this;
97 }
98
99 /**
100 * Disables state changing operations on the returned distributed primitive.
101 * @return this builder
102 */
103 public B withUpdatesDisabled() {
104 this.readOnly = true;
105 return (B) this;
106 }
107
108 /**
109 * Turns on relaxed consistency for read operations.
110 * @return this builder
111 */
112 public B withRelaxedReadConsistency() {
113 this.relaxedReadConsistency = true;
114 return (B) this;
Madan Jampanie17d3282016-02-03 15:30:57 -0800115 }
116
117 /**
118 * Returns if metering is enabled.
119 *
120 * @return {@code true} if yes; {@code false} otherwise
121 */
122 public final boolean meteringEnabled() {
123 return !meteringDisabled;
124 }
125
126 /**
127 * Returns if partitions are disabled.
128 *
129 * @return {@code true} if yes; {@code false} otherwise
130 */
131 public final boolean partitionsDisabled() {
132 return partitionsDisabled;
133 }
134
135 /**
Madan Jampani538be742016-02-10 14:55:38 -0800136 * Returns if updates are disabled.
137 *
138 * @return {@code true} if yes; {@code false} otherwise
139 */
140 public final boolean readOnly() {
141 return readOnly;
142 }
143
144 /**
145 * Returns if consistency is relaxed for read operations.
146 *
147 * @return {@code true} if yes; {@code false} otherwise
148 */
149 public final boolean relaxedReadConsistency() {
150 return relaxedReadConsistency;
151 }
152
153 /**
Madan Jampanie17d3282016-02-03 15:30:57 -0800154 * Returns the serializer.
155 *
156 * @return serializer
157 */
158 public final Serializer serializer() {
159 return serializer;
160 }
161
162 /**
163 * Returns the application identifier.
164 *
165 * @return application id
166 */
167 public final ApplicationId applicationId() {
168 return applicationId;
169 }
170
171 /**
172 * Returns the name of the primitive.
173 *
174 * @return primitive name
175 */
176 public final String name() {
177 return name;
178 }
179
180 /**
181 * Returns the primitive type.
182 *
183 * @return primitive type
184 */
185 public final DistributedPrimitive.Type type() {
186 return type;
187 }
188
189 /**
190 * Constructs an instance of the distributed primitive.
191 * @return distributed primitive
192 */
193 public abstract T build();
194}