blob: 646e4ab4f2c767b2806db17a806893db4c01b565 [file] [log] [blame]
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -07001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -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 */
16package org.onosproject.cli.net;
17
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.net.region.Region;
22import org.onosproject.net.region.RegionService;
23import org.onosproject.ui.UiTopoLayoutService;
24import org.onosproject.ui.model.topo.UiTopoLayout;
25import org.onosproject.ui.model.topo.UiTopoLayoutId;
26
27import static org.onosproject.net.region.RegionId.regionId;
28import static org.onosproject.ui.model.topo.UiTopoLayoutId.layoutId;
29
30/**
Simon Huntbc30e682017-02-15 18:39:23 -080031 * Add a new UI layout.
Yuta HIGUCHId1ce4bc2017-06-03 01:05:33 -070032 *
Simon Hunted81ed62017-03-21 17:53:38 -070033 * <pre>
34 * layout-add {layout-id} {bg-ref} \
35 * [ {region-id} {parent-layout-id} {scale} {offset-x} {offset-y} ]
36 * </pre>
37 * Note that if you want to skip a parameter, but set later parameters,
38 * use dot (".") as a placeholder for null. For example, no associated region
39 * or parent layout, but setting the scale and offset for the root layout...
40 * <pre>
41 * layout-add root @bayareaGEO . . 1.2 0.0 -4.0
42 * </pre>
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070043 */
44@Command(scope = "onos", name = "layout-add",
Simon Huntbc30e682017-02-15 18:39:23 -080045 description = "Adds a new UI layout.")
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070046public class LayoutAddCommand extends AbstractShellCommand {
47
Simon Huntbc30e682017-02-15 18:39:23 -080048 private static final char CODE_GEO = '@';
49 private static final char CODE_GRID = '+';
Simon Hunted81ed62017-03-21 17:53:38 -070050
51 private static final String NULL_TOKEN = ".";
Simon Hunte9717e62017-02-16 16:54:43 -080052 private static final String ROOT = "root";
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070053
Simon Hunted81ed62017-03-21 17:53:38 -070054 private static final double DEFAULT_SCALE = 1.0;
55 private static final double DEFAULT_OFFSET = 0.0;
56
57
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070058 @Argument(index = 0, name = "id", description = "Layout ID",
59 required = true, multiValued = false)
60 String id = null;
61
Simon Huntbc30e682017-02-15 18:39:23 -080062 @Argument(index = 1, name = "bgref", description = "Background Ref",
63 required = true, multiValued = false)
64 String backgroundRef = null;
65
66 @Argument(index = 2, name = "rid", description = "Region ID (optional)",
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070067 required = false, multiValued = false)
68 String regionId = null;
69
Simon Huntbc30e682017-02-15 18:39:23 -080070 @Argument(index = 3, name = "plid", description = "Parent layout ID (optional)",
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070071 required = false, multiValued = false)
72 String parentId = null;
73
Simon Hunted81ed62017-03-21 17:53:38 -070074 @Argument(index = 4, name = "scale", description = "Zoom scale (optional; default 1.0)",
75 required = false, multiValued = false)
76 String zoomScale = null;
77
78 @Argument(index = 5, name = "offx", description = "Zoom offset-X (optional; default 0.0)",
79 required = false, multiValued = false)
80 String zoomOffsetX = null;
81
82 @Argument(index = 6, name = "offy", description = "Zoom offset-Y (optional; default 0.0)",
83 required = false, multiValued = false)
84 String zoomOffsetY = null;
85
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070086 private RegionService regionService;
87
88 @Override
89 protected void execute() {
90 UiTopoLayoutService service = get(UiTopoLayoutService.class);
91 RegionService regionService = get(RegionService.class);
92
Simon Hunted81ed62017-03-21 17:53:38 -070093 UiTopoLayout layout;
94
Simon Hunte9717e62017-02-16 16:54:43 -080095 if (ROOT.equals(id)) {
Simon Hunted81ed62017-03-21 17:53:38 -070096 layout = service.getRootLayout();
97 setAppropriateBackground(layout);
98 setZoomParameters(layout);
Simon Hunte9717e62017-02-16 16:54:43 -080099 return;
100 }
101
Simon Hunted81ed62017-03-21 17:53:38 -0700102 // Otherwise, it is a user-defined layout...
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -0700103
Simon Hunted81ed62017-03-21 17:53:38 -0700104 Region region = nullToken(regionId) ? null : regionService.getRegion(regionId(regionId));
105 UiTopoLayoutId pid = nullToken(parentId) ? UiTopoLayoutId.DEFAULT_ID : layoutId(parentId);
106
107 layout = new UiTopoLayout(layoutId(id)).region(region).parent(pid);
108
109 setAppropriateBackground(layout);
110 setZoomParameters(layout);
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -0700111 service.addLayout(layout);
112 }
Simon Huntbc30e682017-02-15 18:39:23 -0800113
Simon Hunted81ed62017-03-21 17:53:38 -0700114 private boolean nullToken(String token) {
115 return token == null || token.equals(NULL_TOKEN);
116 }
117
118 private void setAppropriateBackground(UiTopoLayout layout) {
Simon Huntbc30e682017-02-15 18:39:23 -0800119 /*
120 * A note about the format of bgref.. it should be one of:
121 * "." - signifies no background
122 * "@{map-id}" - signifies geo background (map)
123 * "+{sprite-id}" - signifies grid background (sprite)
124 *
Simon Hunted81ed62017-03-21 17:53:38 -0700125 * For example, ".", "@bayareaGEO", "+segmentRouting"
Simon Huntbc30e682017-02-15 18:39:23 -0800126 */
Simon Hunted81ed62017-03-21 17:53:38 -0700127 char type = backgroundRef.charAt(0);
Simon Huntbc30e682017-02-15 18:39:23 -0800128
129 if (type == CODE_GEO) {
130 // GEO (map) reference
Simon Hunted81ed62017-03-21 17:53:38 -0700131 layout.geomap(backgroundRef.substring(1));
Simon Huntbc30e682017-02-15 18:39:23 -0800132
133 } else if (type == CODE_GRID) {
134 // Grid (sprite) reference
Simon Hunted81ed62017-03-21 17:53:38 -0700135 layout.sprites(backgroundRef.substring(1));
Simon Huntbc30e682017-02-15 18:39:23 -0800136 }
Simon Hunted81ed62017-03-21 17:53:38 -0700137 // simply ignore null token (".")
138 }
139
140 private double parseDouble(String s, double def) {
141 if (nullToken(s)) {
142 return def;
143 }
144
145 double result;
146 try {
147 result = Double.parseDouble(s);
148 } catch (NumberFormatException e) {
149 result = def;
150 }
151
152 return result;
153 }
154
155 private void setZoomParameters(UiTopoLayout layout) {
156 double scale = parseDouble(zoomScale, DEFAULT_SCALE);
157 double offsetX = parseDouble(zoomOffsetX, DEFAULT_OFFSET);
158 double offsetY = parseDouble(zoomOffsetY, DEFAULT_OFFSET);
159
160 layout.scale(scale).offsetX(offsetX).offsetY(offsetY);
Simon Huntbc30e682017-02-15 18:39:23 -0800161 }
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -0700162}