Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.net.config.basics; |
| 18 | |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 20 | import org.onosproject.net.config.Config; |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 21 | import org.onosproject.net.config.InvalidFieldException; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 22 | import org.onosproject.net.region.RegionId; |
| 23 | import org.onosproject.ui.model.topo.UiTopoLayoutId; |
| 24 | |
| 25 | import static org.onosproject.net.region.RegionId.regionId; |
| 26 | |
| 27 | /** |
| 28 | * Basic configuration for UI topology layouts. |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 29 | * <p> |
| 30 | * Note that a layout configuration will include information about |
| 31 | * which background map (or sprites definition) to use, and at what |
| 32 | * relative scale and offset. |
| 33 | * <p> |
| 34 | * Note also that the {@code geomap} and {@code sprites} fields are |
| 35 | * mutually exclusive. |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 36 | */ |
| 37 | public class BasicUiTopoLayoutConfig extends Config<UiTopoLayoutId> { |
| 38 | |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 39 | static final String REGION = "region"; |
| 40 | static final String PARENT = "parent"; |
| 41 | static final String GEOMAP = "geomap"; |
| 42 | static final String SPRITES = "sprites"; |
| 43 | static final String SCALE = "scale"; |
| 44 | static final String OFFSET_X = "offsetX"; |
| 45 | static final String OFFSET_Y = "offsetY"; |
| 46 | |
| 47 | static final double DEFAULT_SCALE = 1.0; |
| 48 | static final double DEFAULT_OFFSET = 0.0; |
| 49 | |
| 50 | private static final String E_GEOMAP_SPRITE = |
| 51 | "Layout cannot have both geomap and sprites defined"; |
| 52 | private static final String E_SPRITES_ALREADY_SET = |
| 53 | "Can't set geomap when sprites is already set"; |
| 54 | private static final String E_GEOMAP_ALREADY_SET = |
| 55 | "Can't set sprites when geomap is already set"; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 56 | |
Jordan Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 57 | private static final int GEOMAP_MAX_LENGTH = 128; |
| 58 | private static final int SPRITES_MAX_LENGTH = 128; |
| 59 | |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 60 | @Override |
| 61 | public boolean isValid() { |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 62 | if (object.has(GEOMAP) && object.has(SPRITES)) { |
| 63 | throw new InvalidFieldException(GEOMAP, E_GEOMAP_SPRITE); |
| 64 | } |
| 65 | |
Jordan Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 66 | // Validate the region and parent |
| 67 | region(); |
| 68 | parent(); |
| 69 | |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 70 | return hasOnlyFields(REGION, PARENT, GEOMAP, SPRITES, SCALE, |
Jordan Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 71 | OFFSET_X, OFFSET_Y) |
| 72 | && isValidLength(GEOMAP, GEOMAP_MAX_LENGTH) |
| 73 | && isValidLength(SPRITES, SPRITES_MAX_LENGTH); |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public String toString() { |
| 78 | return MoreObjects.toStringHelper(this) |
| 79 | .add("region", region()) |
| 80 | .add("parent", parent()) |
| 81 | .add("geomap", geomap()) |
| 82 | .add("sprites", sprites()) |
| 83 | .add("scale", scale()) |
| 84 | .add("offX", offsetX()) |
| 85 | .add("offY", offsetY()) |
| 86 | .toString(); |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns the identifier of the backing region. This will be |
| 91 | * null if there is no backing region. |
| 92 | * |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 93 | * @return backing region identifier |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 94 | */ |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 95 | public RegionId region() { |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 96 | String r = get(REGION, null); |
| 97 | return r == null ? null : regionId(r); |
| 98 | } |
| 99 | |
| 100 | /** |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 101 | * Sets the identifier of the backing region. |
| 102 | * |
| 103 | * @param id backing region identifier, or null to unset |
| 104 | * @return config for UI topology layout |
| 105 | */ |
| 106 | public BasicUiTopoLayoutConfig region(RegionId id) { |
| 107 | setOrClear(REGION, id == null ? null : id.id()); |
| 108 | return this; |
| 109 | } |
| 110 | |
| 111 | /** |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 112 | * Returns the identifier of the parent layout. |
| 113 | * |
| 114 | * @return layout identifier of parent |
| 115 | */ |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 116 | public UiTopoLayoutId parent() { |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 117 | String p = get(PARENT, null); |
| 118 | return p == null ? UiTopoLayoutId.DEFAULT_ID : UiTopoLayoutId.layoutId(p); |
| 119 | } |
| 120 | |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 121 | /** |
| 122 | * Sets the identifier of the parent layout. |
| 123 | * |
| 124 | * @param id parent ui-topo-layout identifier, or null to unset |
| 125 | * @return config for UI topology layout |
| 126 | */ |
| 127 | public BasicUiTopoLayoutConfig parent(UiTopoLayoutId id) { |
| 128 | setOrClear(PARENT, id == null ? null : id.id()); |
| 129 | return this; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Returns the identifier for the background geo-map. |
| 134 | * |
| 135 | * @return geo-map identifier |
| 136 | */ |
| 137 | public String geomap() { |
| 138 | return get(GEOMAP, null); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Sets the name of the geomap (topojson file) to use for this layout. |
| 143 | * |
Simon Hunt | d0fa284 | 2016-10-24 18:04:05 -0700 | [diff] [blame] | 144 | * @param geomap geomap name; null to clear |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 145 | * @return config for UI topology layout |
| 146 | * @throws InvalidFieldException if the sprites field is already set |
| 147 | */ |
| 148 | public BasicUiTopoLayoutConfig geomap(String geomap) { |
Simon Hunt | d0fa284 | 2016-10-24 18:04:05 -0700 | [diff] [blame] | 149 | if (geomap != null && hasField(SPRITES)) { |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 150 | throw new InvalidFieldException(GEOMAP, E_SPRITES_ALREADY_SET); |
| 151 | } |
| 152 | setOrClear(GEOMAP, geomap); |
| 153 | return this; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Returns the identifier for the background sprites. |
| 158 | * |
| 159 | * @return sprites identifier |
| 160 | */ |
| 161 | public String sprites() { |
| 162 | return get(SPRITES, null); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Sets the name of the sprites definition to use for this layout. |
| 167 | * |
Simon Hunt | d0fa284 | 2016-10-24 18:04:05 -0700 | [diff] [blame] | 168 | * @param sprites sprites definition name; null to clear |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 169 | * @return config for UI topology layout |
| 170 | * @throws InvalidFieldException if the geomap field is already set |
| 171 | */ |
| 172 | public BasicUiTopoLayoutConfig sprites(String sprites) { |
Simon Hunt | d0fa284 | 2016-10-24 18:04:05 -0700 | [diff] [blame] | 173 | if (sprites != null && hasField(GEOMAP)) { |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 174 | throw new InvalidFieldException(GEOMAP, E_GEOMAP_ALREADY_SET); |
| 175 | } |
| 176 | setOrClear(SPRITES, sprites); |
| 177 | return this; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Returns the scale for the geomap / sprites background. |
| 182 | * |
| 183 | * @return scale of background map / diagram |
| 184 | */ |
| 185 | public double scale() { |
| 186 | return get(SCALE, DEFAULT_SCALE); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Sets the scale for the geomap / sprites background. |
| 191 | * |
| 192 | * @param scale the scale to set |
| 193 | * @return config for UI topology layout |
| 194 | */ |
| 195 | public BasicUiTopoLayoutConfig scale(Double scale) { |
| 196 | setOrClear(SCALE, scale); |
| 197 | return this; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Returns the x-offset for the geomap / sprites background. |
| 202 | * |
| 203 | * @return x-offset of background map / diagram |
| 204 | */ |
| 205 | public double offsetX() { |
| 206 | return get(OFFSET_X, DEFAULT_OFFSET); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Sets the x-offset for the geomap / sprites background. |
| 211 | * |
| 212 | * @param offsetX the x-offset to set |
| 213 | * @return config for UI topology layout |
| 214 | */ |
| 215 | public BasicUiTopoLayoutConfig offsetX(Double offsetX) { |
| 216 | setOrClear(OFFSET_X, offsetX); |
| 217 | return this; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Returns the y-offset for the geomap / sprites background. |
| 222 | * |
| 223 | * @return y-offset of background map / diagram |
| 224 | */ |
| 225 | public double offsetY() { |
| 226 | return get(OFFSET_Y, DEFAULT_OFFSET); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Sets the scale for the geomap / sprites background. |
| 231 | * |
| 232 | * @param offsetY the y-offset to set |
| 233 | * @return config for UI topology layout |
| 234 | */ |
| 235 | public BasicUiTopoLayoutConfig offsetY(Double offsetY) { |
| 236 | setOrClear(OFFSET_Y, offsetY); |
| 237 | return this; |
| 238 | } |
| 239 | |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 240 | } |