blob: 9a1047352e900c9564d4631f084ef9a95c375293 [file] [log] [blame]
Jian Licf744442016-02-25 17:32:42 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jian Licf744442016-02-25 17:32:42 +09003 *
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 com.google.common.collect.BiMap;
19import com.google.common.collect.HashBiMap;
20import com.google.common.collect.Lists;
Jian Li43a0b2f2016-02-29 10:01:54 -080021import com.google.common.collect.Sets;
Jian Licf744442016-02-25 17:32:42 +090022import org.apache.karaf.shell.commands.Argument;
23import org.apache.karaf.shell.commands.Command;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.cluster.NodeId;
Simon Hunt53612212016-12-04 17:19:52 -080026import org.onosproject.net.config.NetworkConfigService;
27import org.onosproject.net.config.basics.BasicRegionConfig;
Jian Licf744442016-02-25 17:32:42 +090028import org.onosproject.net.region.Region;
29import org.onosproject.net.region.RegionAdminService;
30import org.onosproject.net.region.RegionId;
31
32import java.util.List;
33import java.util.Set;
Jian Licf744442016-02-25 17:32:42 +090034
35/**
36 * Add a new region.
37 */
38@Command(scope = "onos", name = "region-add",
39 description = "Adds a new region.")
40public class RegionAddCommand extends AbstractShellCommand {
41
Simon Hunt0ee20bf2017-05-10 19:59:17 -070042 private static final String E_BAD_LOC_TYPE = "locType must be {geo|grid}";
43
44 private static final String GEO = "geo";
45 private static final String GRID = "grid";
Simon Hunt53612212016-12-04 17:19:52 -080046 private static final String SLASH = "/";
47
Simon Hunt0ee20bf2017-05-10 19:59:17 -070048 private static final BiMap<String, Region.Type> REGION_TYPE_MAP = HashBiMap.create();
Jian Licf744442016-02-25 17:32:42 +090049
50 static {
51 for (Region.Type t : Region.Type.values()) {
52 REGION_TYPE_MAP.put(t.name(), t);
53 }
54 }
55
56 @Argument(index = 0, name = "id", description = "Region ID",
57 required = true, multiValued = false)
58 String id = null;
59
60 @Argument(index = 1, name = "name", description = "Region Name",
61 required = true, multiValued = false)
62 String name = null;
63
Jian Li43a0b2f2016-02-29 10:01:54 -080064 @Argument(index = 2, name = "type", description = "Region Type (CONTINENT|" +
Simon Hunt53612212016-12-04 17:19:52 -080065 "COUNTRY|METRO|CAMPUS|BUILDING|DATA_CENTER|FLOOR|ROOM|RACK|LOGICAL_GROUP)",
Jian Licf744442016-02-25 17:32:42 +090066 required = true, multiValued = false)
67 String type = null;
68
Simon Hunt0ee20bf2017-05-10 19:59:17 -070069 @Argument(index = 3, name = "latOrY",
70 description = "Geo latitude / Grid y-coord",
Simon Hunt53612212016-12-04 17:19:52 -080071 required = true, multiValued = false)
Simon Hunt0ee20bf2017-05-10 19:59:17 -070072 Double latOrY = null;
Simon Hunt53612212016-12-04 17:19:52 -080073
Simon Hunt0ee20bf2017-05-10 19:59:17 -070074 @Argument(index = 4, name = "longOrX",
75 description = "Geo longitude / Grid x-coord",
Simon Hunt53612212016-12-04 17:19:52 -080076 required = true, multiValued = false)
Simon Hunt0ee20bf2017-05-10 19:59:17 -070077 Double longOrX = null;
Simon Hunt53612212016-12-04 17:19:52 -080078
Simon Hunt0ee20bf2017-05-10 19:59:17 -070079 @Argument(index = 5, name = "locType", description = "Location type {geo|grid}",
80 required = false, multiValued = false)
81 String locType = GEO;
82
83 @Argument(index = 6, name = "masters", description = "Region Master, a set " +
Jian Li43a0b2f2016-02-29 10:01:54 -080084 "of nodeIds should be split with '/' delimiter (e.g., 1 2 3 / 4 5 6)",
Jian Licf744442016-02-25 17:32:42 +090085 required = true, multiValued = true)
Jian Li43a0b2f2016-02-29 10:01:54 -080086 List<String> masterArgs = null;
Jian Licf744442016-02-25 17:32:42 +090087
88 @Override
89 protected void execute() {
90 RegionAdminService service = get(RegionAdminService.class);
91 RegionId regionId = RegionId.regionId(id);
Jian Li43a0b2f2016-02-29 10:01:54 -080092
Simon Hunt0ee20bf2017-05-10 19:59:17 -070093 NetworkConfigService cfgService = get(NetworkConfigService.class);
94 BasicRegionConfig cfg = cfgService.addConfig(regionId, BasicRegionConfig.class);
95 setConfigurationData(cfg);
96
97 List<Set<NodeId>> masters = parseMasterArgs();
98 service.createRegion(regionId, name, REGION_TYPE_MAP.get(type), masters);
99 print("Region successfully added.");
100 }
101
102 private void setConfigurationData(BasicRegionConfig cfg) {
103 cfg.name(name).locType(locType);
104
105 if (GEO.equals(locType)) {
106 cfg.latitude(latOrY).longitude(longOrX);
107
108 } else if (GRID.equals(locType)) {
109 cfg.gridY(latOrY).gridX(longOrX);
110
111 } else {
112 throw new IllegalArgumentException(E_BAD_LOC_TYPE);
113
114 }
115
116 cfg.apply();
117 }
118
119 private List<Set<NodeId>> parseMasterArgs() {
Jian Licf744442016-02-25 17:32:42 +0900120 List<Set<NodeId>> masters = Lists.newArrayList();
Jian Li43a0b2f2016-02-29 10:01:54 -0800121 Set<NodeId> nodeIds = Sets.newHashSet();
122 for (String masterArg : masterArgs) {
Simon Hunt53612212016-12-04 17:19:52 -0800123 if (masterArg.equals(SLASH)) {
Jian Li43a0b2f2016-02-29 10:01:54 -0800124 masters.add(nodeIds);
125 nodeIds = Sets.newHashSet();
126 } else {
127 nodeIds.add(NodeId.nodeId(masterArg));
128 }
129 }
Jian Licf744442016-02-25 17:32:42 +0900130 masters.add(nodeIds);
Simon Hunt0ee20bf2017-05-10 19:59:17 -0700131 return masters;
Jian Licf744442016-02-25 17:32:42 +0900132 }
133}