blob: 4091f4e7e5d249ad318860496f5bfaf0b38d9d88 [file] [log] [blame]
Simon Huntf59d36b2016-10-04 19:05:53 -07001/*
2 * Copyright 2016-present 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
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
57 @Override
58 public boolean isValid() {
Simon Hunt4f3a4072016-10-17 17:52:11 -070059 if (object.has(GEOMAP) && object.has(SPRITES)) {
60 throw new InvalidFieldException(GEOMAP, E_GEOMAP_SPRITE);
61 }
62
63 return hasOnlyFields(REGION, PARENT, GEOMAP, SPRITES, SCALE,
64 OFFSET_X, OFFSET_Y);
65 }
66
67 @Override
68 public String toString() {
69 return MoreObjects.toStringHelper(this)
70 .add("region", region())
71 .add("parent", parent())
72 .add("geomap", geomap())
73 .add("sprites", sprites())
74 .add("scale", scale())
75 .add("offX", offsetX())
76 .add("offY", offsetY())
77 .toString();
Simon Huntf59d36b2016-10-04 19:05:53 -070078 }
79
80 /**
81 * Returns the identifier of the backing region. This will be
82 * null if there is no backing region.
83 *
Simon Hunt4f3a4072016-10-17 17:52:11 -070084 * @return backing region identifier
Simon Huntf59d36b2016-10-04 19:05:53 -070085 */
Simon Hunt4f3a4072016-10-17 17:52:11 -070086 public RegionId region() {
Simon Huntf59d36b2016-10-04 19:05:53 -070087 String r = get(REGION, null);
88 return r == null ? null : regionId(r);
89 }
90
91 /**
Simon Hunt4f3a4072016-10-17 17:52:11 -070092 * Sets the identifier of the backing region.
93 *
94 * @param id backing region identifier, or null to unset
95 * @return config for UI topology layout
96 */
97 public BasicUiTopoLayoutConfig region(RegionId id) {
98 setOrClear(REGION, id == null ? null : id.id());
99 return this;
100 }
101
102 /**
Simon Huntf59d36b2016-10-04 19:05:53 -0700103 * Returns the identifier of the parent layout.
104 *
105 * @return layout identifier of parent
106 */
Simon Hunt4f3a4072016-10-17 17:52:11 -0700107 public UiTopoLayoutId parent() {
Simon Huntf59d36b2016-10-04 19:05:53 -0700108 String p = get(PARENT, null);
109 return p == null ? UiTopoLayoutId.DEFAULT_ID : UiTopoLayoutId.layoutId(p);
110 }
111
Simon Hunt4f3a4072016-10-17 17:52:11 -0700112 /**
113 * Sets the identifier of the parent layout.
114 *
115 * @param id parent ui-topo-layout identifier, or null to unset
116 * @return config for UI topology layout
117 */
118 public BasicUiTopoLayoutConfig parent(UiTopoLayoutId id) {
119 setOrClear(PARENT, id == null ? null : id.id());
120 return this;
121 }
122
123 /**
124 * Returns the identifier for the background geo-map.
125 *
126 * @return geo-map identifier
127 */
128 public String geomap() {
129 return get(GEOMAP, null);
130 }
131
132 /**
133 * Sets the name of the geomap (topojson file) to use for this layout.
134 *
Simon Huntd0fa2842016-10-24 18:04:05 -0700135 * @param geomap geomap name; null to clear
Simon Hunt4f3a4072016-10-17 17:52:11 -0700136 * @return config for UI topology layout
137 * @throws InvalidFieldException if the sprites field is already set
138 */
139 public BasicUiTopoLayoutConfig geomap(String geomap) {
Simon Huntd0fa2842016-10-24 18:04:05 -0700140 if (geomap != null && hasField(SPRITES)) {
Simon Hunt4f3a4072016-10-17 17:52:11 -0700141 throw new InvalidFieldException(GEOMAP, E_SPRITES_ALREADY_SET);
142 }
143 setOrClear(GEOMAP, geomap);
144 return this;
145 }
146
147 /**
148 * Returns the identifier for the background sprites.
149 *
150 * @return sprites identifier
151 */
152 public String sprites() {
153 return get(SPRITES, null);
154 }
155
156 /**
157 * Sets the name of the sprites definition to use for this layout.
158 *
Simon Huntd0fa2842016-10-24 18:04:05 -0700159 * @param sprites sprites definition name; null to clear
Simon Hunt4f3a4072016-10-17 17:52:11 -0700160 * @return config for UI topology layout
161 * @throws InvalidFieldException if the geomap field is already set
162 */
163 public BasicUiTopoLayoutConfig sprites(String sprites) {
Simon Huntd0fa2842016-10-24 18:04:05 -0700164 if (sprites != null && hasField(GEOMAP)) {
Simon Hunt4f3a4072016-10-17 17:52:11 -0700165 throw new InvalidFieldException(GEOMAP, E_GEOMAP_ALREADY_SET);
166 }
167 setOrClear(SPRITES, sprites);
168 return this;
169 }
170
171 /**
172 * Returns the scale for the geomap / sprites background.
173 *
174 * @return scale of background map / diagram
175 */
176 public double scale() {
177 return get(SCALE, DEFAULT_SCALE);
178 }
179
180 /**
181 * Sets the scale for the geomap / sprites background.
182 *
183 * @param scale the scale to set
184 * @return config for UI topology layout
185 */
186 public BasicUiTopoLayoutConfig scale(Double scale) {
187 setOrClear(SCALE, scale);
188 return this;
189 }
190
191 /**
192 * Returns the x-offset for the geomap / sprites background.
193 *
194 * @return x-offset of background map / diagram
195 */
196 public double offsetX() {
197 return get(OFFSET_X, DEFAULT_OFFSET);
198 }
199
200 /**
201 * Sets the x-offset for the geomap / sprites background.
202 *
203 * @param offsetX the x-offset to set
204 * @return config for UI topology layout
205 */
206 public BasicUiTopoLayoutConfig offsetX(Double offsetX) {
207 setOrClear(OFFSET_X, offsetX);
208 return this;
209 }
210
211 /**
212 * Returns the y-offset for the geomap / sprites background.
213 *
214 * @return y-offset of background map / diagram
215 */
216 public double offsetY() {
217 return get(OFFSET_Y, DEFAULT_OFFSET);
218 }
219
220 /**
221 * Sets the scale for the geomap / sprites background.
222 *
223 * @param offsetY the y-offset to set
224 * @return config for UI topology layout
225 */
226 public BasicUiTopoLayoutConfig offsetY(Double offsetY) {
227 setOrClear(OFFSET_Y, offsetY);
228 return this;
229 }
230
Simon Huntf59d36b2016-10-04 19:05:53 -0700231}