Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.store.primitives; |
| 17 | |
| 18 | import org.onosproject.core.ApplicationId; |
| 19 | import org.onosproject.store.service.DistributedPrimitive; |
| 20 | import org.onosproject.store.service.Serializer; |
| 21 | |
| 22 | /** |
| 23 | * Abstract builder for distributed primitives. |
| 24 | * |
| 25 | * @param <T> distributed primitive type |
| 26 | */ |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 27 | public abstract class DistributedPrimitiveBuilder<B extends DistributedPrimitiveBuilder<B, T>, |
| 28 | T extends DistributedPrimitive> { |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 29 | |
| 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 Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 36 | private boolean readOnly = false; |
| 37 | private boolean relaxedReadConsistency = false; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 38 | |
| 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 Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 49 | public B withName(String name) { |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 50 | this.name = name; |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 51 | return (B) this; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 52 | } |
| 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 Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 60 | public B withSerializer(Serializer serializer) { |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 61 | this.serializer = serializer; |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 62 | return (B) this; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Sets the application id that owns this primitive. |
| 67 | * |
| 68 | * @param applicationId application identifier |
| 69 | * @return this builder |
| 70 | */ |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 71 | public B withApplicationId(ApplicationId applicationId) { |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 72 | this.applicationId = applicationId; |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 73 | return (B) this; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 74 | } |
| 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 Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 83 | public B withPartitionsDisabled() { |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 84 | this.partitionsDisabled = true; |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 85 | return (B) this; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 86 | } |
| 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 Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 94 | public B withMeteringDisabled() { |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 95 | this.meteringDisabled = true; |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 96 | 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 Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 115 | } |
| 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 Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 136 | * 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 Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 154 | * 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 | } |