blob: 442f1643d7e86087e8fa9f43f404f5557da84faf [file] [log] [blame]
Simon Huntf59d36b2016-10-04 19:05:53 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Simon Huntf59d36b2016-10-04 19:05:53 -07003 *
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
17package org.onosproject.net.config.basics;
18
Simon Hunt4f3a4072016-10-17 17:52:11 -070019import com.google.common.base.MoreObjects;
Simon Huntf59d36b2016-10-04 19:05:53 -070020import org.onosproject.net.config.Config;
Simon Hunt4f3a4072016-10-17 17:52:11 -070021import org.onosproject.net.config.InvalidFieldException;
Simon Huntf59d36b2016-10-04 19:05:53 -070022import org.onosproject.net.region.RegionId;
23import org.onosproject.ui.model.topo.UiTopoLayoutId;
24
25import static org.onosproject.net.region.RegionId.regionId;
26
27/**
28 * Basic configuration for UI topology layouts.
Simon Hunt4f3a4072016-10-17 17:52:11 -070029 * <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 Huntf59d36b2016-10-04 19:05:53 -070036 */
37public class BasicUiTopoLayoutConfig extends Config<UiTopoLayoutId> {
38
Simon Hunt4f3a4072016-10-17 17:52:11 -070039 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 Huntf59d36b2016-10-04 19:05:53 -070056
Jordan Halterman83949a12017-06-21 10:35:38 -070057 private static final int GEOMAP_MAX_LENGTH = 128;
58 private static final int SPRITES_MAX_LENGTH = 128;
59
Simon Huntf59d36b2016-10-04 19:05:53 -070060 @Override
61 public boolean isValid() {
Simon Hunt4f3a4072016-10-17 17:52:11 -070062 if (object.has(GEOMAP) && object.has(SPRITES)) {
63 throw new InvalidFieldException(GEOMAP, E_GEOMAP_SPRITE);
64 }
65
Jordan Halterman83949a12017-06-21 10:35:38 -070066 // Validate the region and parent
67 region();
68 parent();
69
Simon Hunt4f3a4072016-10-17 17:52:11 -070070 return hasOnlyFields(REGION, PARENT, GEOMAP, SPRITES, SCALE,
Jordan Halterman83949a12017-06-21 10:35:38 -070071 OFFSET_X, OFFSET_Y)
72 && isValidLength(GEOMAP, GEOMAP_MAX_LENGTH)
73 && isValidLength(SPRITES, SPRITES_MAX_LENGTH);
Simon Hunt4f3a4072016-10-17 17:52:11 -070074 }
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 Huntf59d36b2016-10-04 19:05:53 -070087 }
88
89 /**
90 * Returns the identifier of the backing region. This will be
91 * null if there is no backing region.
92 *
Simon Hunt4f3a4072016-10-17 17:52:11 -070093 * @return backing region identifier
Simon Huntf59d36b2016-10-04 19:05:53 -070094 */
Simon Hunt4f3a4072016-10-17 17:52:11 -070095 public RegionId region() {
Simon Huntf59d36b2016-10-04 19:05:53 -070096 String r = get(REGION, null);
97 return r == null ? null : regionId(r);
98 }
99
100 /**
Simon Hunt4f3a4072016-10-17 17:52:11 -0700101 * 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 Huntf59d36b2016-10-04 19:05:53 -0700112 * Returns the identifier of the parent layout.
113 *
114 * @return layout identifier of parent
115 */
Simon Hunt4f3a4072016-10-17 17:52:11 -0700116 public UiTopoLayoutId parent() {
Simon Huntf59d36b2016-10-04 19:05:53 -0700117 String p = get(PARENT, null);
118 return p == null ? UiTopoLayoutId.DEFAULT_ID : UiTopoLayoutId.layoutId(p);
119 }
120
Simon Hunt4f3a4072016-10-17 17:52:11 -0700121 /**
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 Huntd0fa2842016-10-24 18:04:05 -0700144 * @param geomap geomap name; null to clear
Simon Hunt4f3a4072016-10-17 17:52:11 -0700145 * @return config for UI topology layout
146 * @throws InvalidFieldException if the sprites field is already set
147 */
148 public BasicUiTopoLayoutConfig geomap(String geomap) {
Simon Huntd0fa2842016-10-24 18:04:05 -0700149 if (geomap != null && hasField(SPRITES)) {
Simon Hunt4f3a4072016-10-17 17:52:11 -0700150 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 Huntd0fa2842016-10-24 18:04:05 -0700168 * @param sprites sprites definition name; null to clear
Simon Hunt4f3a4072016-10-17 17:52:11 -0700169 * @return config for UI topology layout
170 * @throws InvalidFieldException if the geomap field is already set
171 */
172 public BasicUiTopoLayoutConfig sprites(String sprites) {
Simon Huntd0fa2842016-10-24 18:04:05 -0700173 if (sprites != null && hasField(GEOMAP)) {
Simon Hunt4f3a4072016-10-17 17:52:11 -0700174 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 Huntf59d36b2016-10-04 19:05:53 -0700240}