Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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. |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 32 | */ |
| 33 | @Command(scope = "onos", name = "layout-add", |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 34 | description = "Adds a new UI layout.") |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 35 | public class LayoutAddCommand extends AbstractShellCommand { |
| 36 | |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 37 | private static final char CODE_GEO = '@'; |
| 38 | private static final char CODE_GRID = '+'; |
Simon Hunt | e9717e6 | 2017-02-16 16:54:43 -0800 | [diff] [blame^] | 39 | private static final String ROOT = "root"; |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 40 | |
| 41 | @Argument(index = 0, name = "id", description = "Layout ID", |
| 42 | required = true, multiValued = false) |
| 43 | String id = null; |
| 44 | |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 45 | @Argument(index = 1, name = "bgref", description = "Background Ref", |
| 46 | required = true, multiValued = false) |
| 47 | String backgroundRef = null; |
| 48 | |
| 49 | @Argument(index = 2, name = "rid", description = "Region ID (optional)", |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 50 | required = false, multiValued = false) |
| 51 | String regionId = null; |
| 52 | |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 53 | @Argument(index = 3, name = "plid", description = "Parent layout ID (optional)", |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 54 | required = false, multiValued = false) |
| 55 | String parentId = null; |
| 56 | |
| 57 | private RegionService regionService; |
| 58 | |
| 59 | @Override |
| 60 | protected void execute() { |
| 61 | UiTopoLayoutService service = get(UiTopoLayoutService.class); |
| 62 | RegionService regionService = get(RegionService.class); |
| 63 | |
Simon Hunt | e9717e6 | 2017-02-16 16:54:43 -0800 | [diff] [blame^] | 64 | if (ROOT.equals(id)) { |
| 65 | // set the background for the root layout |
| 66 | setAppropriateBackground(service.getRootLayout(), backgroundRef); |
| 67 | return; |
| 68 | } |
| 69 | |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 70 | Region region = regionId == null ? null : regionService.getRegion(regionId(regionId)); |
| 71 | UiTopoLayoutId pid = parentId == null ? UiTopoLayoutId.DEFAULT_ID : layoutId(parentId); |
| 72 | |
Simon Hunt | d0fa284 | 2016-10-24 18:04:05 -0700 | [diff] [blame] | 73 | UiTopoLayout layout = new UiTopoLayout(layoutId(id)).region(region).parent(pid); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 74 | setAppropriateBackground(layout, backgroundRef); |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 75 | service.addLayout(layout); |
| 76 | } |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 77 | |
| 78 | private void setAppropriateBackground(UiTopoLayout layout, String bgRef) { |
| 79 | /* |
| 80 | * A note about the format of bgref.. it should be one of: |
| 81 | * "." - signifies no background |
| 82 | * "@{map-id}" - signifies geo background (map) |
| 83 | * "+{sprite-id}" - signifies grid background (sprite) |
| 84 | * |
| 85 | * For example, "!", "@bayareaGEO", "+segmentRouting" |
| 86 | */ |
| 87 | char type = bgRef.charAt(0); |
| 88 | |
| 89 | if (type == CODE_GEO) { |
| 90 | // GEO (map) reference |
| 91 | layout.geomap(bgRef.substring(1)); |
| 92 | |
| 93 | } else if (type == CODE_GRID) { |
| 94 | // Grid (sprite) reference |
| 95 | layout.sprites(bgRef.substring(1)); |
| 96 | } |
| 97 | } |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 98 | } |