blob: f16c695eee737133197f0af7603ef43157e6c682 [file] [log] [blame]
Jian Licf744442016-02-25 17:32:42 +09001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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;
26import org.onosproject.net.region.Region;
27import org.onosproject.net.region.RegionAdminService;
28import org.onosproject.net.region.RegionId;
29
30import java.util.List;
31import java.util.Set;
Jian Licf744442016-02-25 17:32:42 +090032
33/**
34 * Add a new region.
35 */
36@Command(scope = "onos", name = "region-add",
37 description = "Adds a new region.")
38public class RegionAddCommand extends AbstractShellCommand {
39
40 private static final BiMap<String, Region.Type> REGION_TYPE_MAP = HashBiMap.create();
41
42 static {
43 for (Region.Type t : Region.Type.values()) {
44 REGION_TYPE_MAP.put(t.name(), t);
45 }
46 }
47
48 @Argument(index = 0, name = "id", description = "Region ID",
49 required = true, multiValued = false)
50 String id = null;
51
52 @Argument(index = 1, name = "name", description = "Region Name",
53 required = true, multiValued = false)
54 String name = null;
55
Jian Li43a0b2f2016-02-29 10:01:54 -080056 @Argument(index = 2, name = "type", description = "Region Type (CONTINENT|" +
57 "COUNTRY|METRO|CAMPUS|BUILDING|FLOOR|ROOM|RACK|LOGICAL_GROUP)",
Jian Licf744442016-02-25 17:32:42 +090058 required = true, multiValued = false)
59 String type = null;
60
Jian Li43a0b2f2016-02-29 10:01:54 -080061 @Argument(index = 3, name = "masters", description = "Region Master, a set " +
62 "of nodeIds should be split with '/' delimiter (e.g., 1 2 3 / 4 5 6)",
Jian Licf744442016-02-25 17:32:42 +090063 required = true, multiValued = true)
Jian Li43a0b2f2016-02-29 10:01:54 -080064 List<String> masterArgs = null;
Jian Licf744442016-02-25 17:32:42 +090065
66 @Override
67 protected void execute() {
68 RegionAdminService service = get(RegionAdminService.class);
69 RegionId regionId = RegionId.regionId(id);
Jian Li43a0b2f2016-02-29 10:01:54 -080070
Jian Licf744442016-02-25 17:32:42 +090071 List<Set<NodeId>> masters = Lists.newArrayList();
Jian Li43a0b2f2016-02-29 10:01:54 -080072 Set<NodeId> nodeIds = Sets.newHashSet();
73 for (String masterArg : masterArgs) {
74 if (masterArg.equals("/")) {
75 masters.add(nodeIds);
76 nodeIds = Sets.newHashSet();
77 } else {
78 nodeIds.add(NodeId.nodeId(masterArg));
79 }
80 }
Jian Licf744442016-02-25 17:32:42 +090081 masters.add(nodeIds);
Jian Li43a0b2f2016-02-29 10:01:54 -080082
Jian Licf744442016-02-25 17:32:42 +090083 service.createRegion(regionId, name, REGION_TYPE_MAP.get(type), masters);
84 print("Region successfully added.");
85 }
86}