blob: 95ddba826e6e2dc7fd19f77c30a52d5dd2add927 [file] [log] [blame]
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.lifecycle.Service;
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.net.region.Region;
23import org.onosproject.net.region.RegionService;
24import org.onosproject.ui.UiTopoLayoutService;
25import org.onosproject.ui.model.topo.UiTopoLayout;
26import org.onosproject.ui.model.topo.UiTopoLayoutId;
27
28import static org.onosproject.net.region.RegionId.regionId;
29import static org.onosproject.ui.model.topo.UiTopoLayoutId.layoutId;
30
31/**
Simon Huntbc30e682017-02-15 18:39:23 -080032 * Add a new UI layout.
Yuta HIGUCHId1ce4bc2017-06-03 01:05:33 -070033 *
Simon Hunted81ed62017-03-21 17:53:38 -070034 * <pre>
35 * layout-add {layout-id} {bg-ref} \
36 * [ {region-id} {parent-layout-id} {scale} {offset-x} {offset-y} ]
37 * </pre>
38 * Note that if you want to skip a parameter, but set later parameters,
39 * use dot (".") as a placeholder for null. For example, no associated region
40 * or parent layout, but setting the scale and offset for the root layout...
41 * <pre>
42 * layout-add root @bayareaGEO . . 1.2 0.0 -4.0
43 * </pre>
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070044 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070045@Service
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070046@Command(scope = "onos", name = "layout-add",
Simon Huntbc30e682017-02-15 18:39:23 -080047 description = "Adds a new UI layout.")
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070048public class LayoutAddCommand extends AbstractShellCommand {
49
Simon Huntbc30e682017-02-15 18:39:23 -080050 private static final char CODE_GEO = '@';
51 private static final char CODE_GRID = '+';
Simon Hunted81ed62017-03-21 17:53:38 -070052
53 private static final String NULL_TOKEN = ".";
Simon Hunte9717e62017-02-16 16:54:43 -080054 private static final String ROOT = "root";
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070055
Simon Hunted81ed62017-03-21 17:53:38 -070056 private static final double DEFAULT_SCALE = 1.0;
57 private static final double DEFAULT_OFFSET = 0.0;
58
59
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070060 @Argument(index = 0, name = "id", description = "Layout ID",
61 required = true, multiValued = false)
62 String id = null;
63
Simon Huntbc30e682017-02-15 18:39:23 -080064 @Argument(index = 1, name = "bgref", description = "Background Ref",
65 required = true, multiValued = false)
66 String backgroundRef = null;
67
68 @Argument(index = 2, name = "rid", description = "Region ID (optional)",
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070069 required = false, multiValued = false)
70 String regionId = null;
71
Simon Huntbc30e682017-02-15 18:39:23 -080072 @Argument(index = 3, name = "plid", description = "Parent layout ID (optional)",
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070073 required = false, multiValued = false)
74 String parentId = null;
75
Simon Hunted81ed62017-03-21 17:53:38 -070076 @Argument(index = 4, name = "scale", description = "Zoom scale (optional; default 1.0)",
77 required = false, multiValued = false)
78 String zoomScale = null;
79
80 @Argument(index = 5, name = "offx", description = "Zoom offset-X (optional; default 0.0)",
81 required = false, multiValued = false)
82 String zoomOffsetX = null;
83
84 @Argument(index = 6, name = "offy", description = "Zoom offset-Y (optional; default 0.0)",
85 required = false, multiValued = false)
86 String zoomOffsetY = null;
87
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070088 private RegionService regionService;
89
90 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070091 protected void doExecute() {
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070092 UiTopoLayoutService service = get(UiTopoLayoutService.class);
93 RegionService regionService = get(RegionService.class);
94
Simon Hunted81ed62017-03-21 17:53:38 -070095 UiTopoLayout layout;
96
Simon Hunte9717e62017-02-16 16:54:43 -080097 if (ROOT.equals(id)) {
Simon Hunted81ed62017-03-21 17:53:38 -070098 layout = service.getRootLayout();
99 setAppropriateBackground(layout);
100 setZoomParameters(layout);
Simon Hunte9717e62017-02-16 16:54:43 -0800101 return;
102 }
103
Simon Hunted81ed62017-03-21 17:53:38 -0700104 // Otherwise, it is a user-defined layout...
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -0700105
Simon Hunted81ed62017-03-21 17:53:38 -0700106 Region region = nullToken(regionId) ? null : regionService.getRegion(regionId(regionId));
107 UiTopoLayoutId pid = nullToken(parentId) ? UiTopoLayoutId.DEFAULT_ID : layoutId(parentId);
108
109 layout = new UiTopoLayout(layoutId(id)).region(region).parent(pid);
110
111 setAppropriateBackground(layout);
112 setZoomParameters(layout);
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -0700113 service.addLayout(layout);
114 }
Simon Huntbc30e682017-02-15 18:39:23 -0800115
Simon Hunted81ed62017-03-21 17:53:38 -0700116 private boolean nullToken(String token) {
117 return token == null || token.equals(NULL_TOKEN);
118 }
119
120 private void setAppropriateBackground(UiTopoLayout layout) {
Simon Huntbc30e682017-02-15 18:39:23 -0800121 /*
122 * A note about the format of bgref.. it should be one of:
123 * "." - signifies no background
124 * "@{map-id}" - signifies geo background (map)
125 * "+{sprite-id}" - signifies grid background (sprite)
126 *
Simon Hunted81ed62017-03-21 17:53:38 -0700127 * For example, ".", "@bayareaGEO", "+segmentRouting"
Simon Huntbc30e682017-02-15 18:39:23 -0800128 */
Simon Hunted81ed62017-03-21 17:53:38 -0700129 char type = backgroundRef.charAt(0);
Simon Huntbc30e682017-02-15 18:39:23 -0800130
131 if (type == CODE_GEO) {
132 // GEO (map) reference
Simon Hunted81ed62017-03-21 17:53:38 -0700133 layout.geomap(backgroundRef.substring(1));
Simon Huntbc30e682017-02-15 18:39:23 -0800134
135 } else if (type == CODE_GRID) {
136 // Grid (sprite) reference
Simon Hunted81ed62017-03-21 17:53:38 -0700137 layout.sprites(backgroundRef.substring(1));
Simon Huntbc30e682017-02-15 18:39:23 -0800138 }
Simon Hunted81ed62017-03-21 17:53:38 -0700139 // simply ignore null token (".")
140 }
141
142 private double parseDouble(String s, double def) {
143 if (nullToken(s)) {
144 return def;
145 }
146
147 double result;
148 try {
149 result = Double.parseDouble(s);
150 } catch (NumberFormatException e) {
151 result = def;
152 }
153
154 return result;
155 }
156
157 private void setZoomParameters(UiTopoLayout layout) {
158 double scale = parseDouble(zoomScale, DEFAULT_SCALE);
159 double offsetX = parseDouble(zoomOffsetX, DEFAULT_OFFSET);
160 double offsetY = parseDouble(zoomOffsetY, DEFAULT_OFFSET);
161
162 layout.scale(scale).offsetX(offsetX).offsetY(offsetY);
Simon Huntbc30e682017-02-15 18:39:23 -0800163 }
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -0700164}