blob: 942fe39ea5b78d6f9a8d76ac1a6750df77bb9f64 [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.
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070032 */
33@Command(scope = "onos", name = "layout-add",
Simon Huntbc30e682017-02-15 18:39:23 -080034 description = "Adds a new UI layout.")
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070035public class LayoutAddCommand extends AbstractShellCommand {
36
Simon Huntbc30e682017-02-15 18:39:23 -080037 private static final char CODE_GEO = '@';
38 private static final char CODE_GRID = '+';
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070039
40 @Argument(index = 0, name = "id", description = "Layout ID",
41 required = true, multiValued = false)
42 String id = null;
43
Simon Huntbc30e682017-02-15 18:39:23 -080044 @Argument(index = 1, name = "bgref", description = "Background Ref",
45 required = true, multiValued = false)
46 String backgroundRef = null;
47
48 @Argument(index = 2, name = "rid", description = "Region ID (optional)",
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070049 required = false, multiValued = false)
50 String regionId = null;
51
Simon Huntbc30e682017-02-15 18:39:23 -080052 @Argument(index = 3, name = "plid", description = "Parent layout ID (optional)",
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070053 required = false, multiValued = false)
54 String parentId = null;
55
56 private RegionService regionService;
57
58 @Override
59 protected void execute() {
60 UiTopoLayoutService service = get(UiTopoLayoutService.class);
61 RegionService regionService = get(RegionService.class);
62
63 Region region = regionId == null ? null : regionService.getRegion(regionId(regionId));
64 UiTopoLayoutId pid = parentId == null ? UiTopoLayoutId.DEFAULT_ID : layoutId(parentId);
65
Simon Huntd0fa2842016-10-24 18:04:05 -070066 UiTopoLayout layout = new UiTopoLayout(layoutId(id)).region(region).parent(pid);
Simon Huntbc30e682017-02-15 18:39:23 -080067 setAppropriateBackground(layout, backgroundRef);
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070068 service.addLayout(layout);
69 }
Simon Huntbc30e682017-02-15 18:39:23 -080070
71 private void setAppropriateBackground(UiTopoLayout layout, String bgRef) {
72 /*
73 * A note about the format of bgref.. it should be one of:
74 * "." - signifies no background
75 * "@{map-id}" - signifies geo background (map)
76 * "+{sprite-id}" - signifies grid background (sprite)
77 *
78 * For example, "!", "@bayareaGEO", "+segmentRouting"
79 */
80 char type = bgRef.charAt(0);
81
82 if (type == CODE_GEO) {
83 // GEO (map) reference
84 layout.geomap(bgRef.substring(1));
85
86 } else if (type == CODE_GRID) {
87 // Grid (sprite) reference
88 layout.sprites(bgRef.substring(1));
89 }
90 }
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070091}