Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -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 | package org.onosproject.cli.net; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.net.region.Region; |
| 22 | import org.onosproject.net.region.RegionService; |
| 23 | import org.onosproject.ui.UiTopoLayoutService; |
| 24 | import org.onosproject.ui.model.topo.UiTopoLayout; |
| 25 | import org.onosproject.ui.model.topo.UiTopoLayoutId; |
| 26 | |
| 27 | import static org.onosproject.net.region.RegionId.regionId; |
| 28 | import static org.onosproject.ui.model.topo.UiTopoLayoutId.layoutId; |
| 29 | |
| 30 | /** |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 31 | * Add a new UI layout. |
Yuta HIGUCHI | d1ce4bc | 2017-06-03 01:05:33 -0700 | [diff] [blame] | 32 | * |
Simon Hunt | ed81ed6 | 2017-03-21 17:53:38 -0700 | [diff] [blame] | 33 | * <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 Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 43 | */ |
| 44 | @Command(scope = "onos", name = "layout-add", |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 45 | description = "Adds a new UI layout.") |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 46 | public class LayoutAddCommand extends AbstractShellCommand { |
| 47 | |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 48 | private static final char CODE_GEO = '@'; |
| 49 | private static final char CODE_GRID = '+'; |
Simon Hunt | ed81ed6 | 2017-03-21 17:53:38 -0700 | [diff] [blame] | 50 | |
| 51 | private static final String NULL_TOKEN = "."; |
Simon Hunt | e9717e6 | 2017-02-16 16:54:43 -0800 | [diff] [blame] | 52 | private static final String ROOT = "root"; |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 53 | |
Simon Hunt | ed81ed6 | 2017-03-21 17:53:38 -0700 | [diff] [blame] | 54 | private static final double DEFAULT_SCALE = 1.0; |
| 55 | private static final double DEFAULT_OFFSET = 0.0; |
| 56 | |
| 57 | |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 58 | @Argument(index = 0, name = "id", description = "Layout ID", |
| 59 | required = true, multiValued = false) |
| 60 | String id = null; |
| 61 | |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 62 | @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 Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 67 | required = false, multiValued = false) |
| 68 | String regionId = null; |
| 69 | |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 70 | @Argument(index = 3, name = "plid", description = "Parent layout ID (optional)", |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 71 | required = false, multiValued = false) |
| 72 | String parentId = null; |
| 73 | |
Simon Hunt | ed81ed6 | 2017-03-21 17:53:38 -0700 | [diff] [blame] | 74 | @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 Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 86 | private RegionService regionService; |
| 87 | |
| 88 | @Override |
| 89 | protected void execute() { |
| 90 | UiTopoLayoutService service = get(UiTopoLayoutService.class); |
| 91 | RegionService regionService = get(RegionService.class); |
| 92 | |
Simon Hunt | ed81ed6 | 2017-03-21 17:53:38 -0700 | [diff] [blame] | 93 | UiTopoLayout layout; |
| 94 | |
Simon Hunt | e9717e6 | 2017-02-16 16:54:43 -0800 | [diff] [blame] | 95 | if (ROOT.equals(id)) { |
Simon Hunt | ed81ed6 | 2017-03-21 17:53:38 -0700 | [diff] [blame] | 96 | layout = service.getRootLayout(); |
| 97 | setAppropriateBackground(layout); |
| 98 | setZoomParameters(layout); |
Simon Hunt | e9717e6 | 2017-02-16 16:54:43 -0800 | [diff] [blame] | 99 | return; |
| 100 | } |
| 101 | |
Simon Hunt | ed81ed6 | 2017-03-21 17:53:38 -0700 | [diff] [blame] | 102 | // Otherwise, it is a user-defined layout... |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 103 | |
Simon Hunt | ed81ed6 | 2017-03-21 17:53:38 -0700 | [diff] [blame] | 104 | 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 Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 111 | service.addLayout(layout); |
| 112 | } |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 113 | |
Simon Hunt | ed81ed6 | 2017-03-21 17:53:38 -0700 | [diff] [blame] | 114 | private boolean nullToken(String token) { |
| 115 | return token == null || token.equals(NULL_TOKEN); |
| 116 | } |
| 117 | |
| 118 | private void setAppropriateBackground(UiTopoLayout layout) { |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 119 | /* |
| 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 Hunt | ed81ed6 | 2017-03-21 17:53:38 -0700 | [diff] [blame] | 125 | * For example, ".", "@bayareaGEO", "+segmentRouting" |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 126 | */ |
Simon Hunt | ed81ed6 | 2017-03-21 17:53:38 -0700 | [diff] [blame] | 127 | char type = backgroundRef.charAt(0); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 128 | |
| 129 | if (type == CODE_GEO) { |
| 130 | // GEO (map) reference |
Simon Hunt | ed81ed6 | 2017-03-21 17:53:38 -0700 | [diff] [blame] | 131 | layout.geomap(backgroundRef.substring(1)); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 132 | |
| 133 | } else if (type == CODE_GRID) { |
| 134 | // Grid (sprite) reference |
Simon Hunt | ed81ed6 | 2017-03-21 17:53:38 -0700 | [diff] [blame] | 135 | layout.sprites(backgroundRef.substring(1)); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 136 | } |
Simon Hunt | ed81ed6 | 2017-03-21 17:53:38 -0700 | [diff] [blame] | 137 | // 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 Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 161 | } |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 162 | } |