blob: a31bbbbcd9f97fa260c9c215007e5e35867f24c5 [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;
26import org.onosproject.net.region.Region;
27import org.onosproject.net.region.RegionAdminService;
28import org.onosproject.net.region.RegionId;
29import org.onosproject.net.region.RegionService;
30
31import java.util.List;
32import java.util.Set;
Jian Licf744442016-02-25 17:32:42 +090033
34/**
35 * Update an existing region.
36 */
37@Command(scope = "onos", name = "region-update",
38 description = "Updates an existing region.")
39public class RegionUpdateCommand extends AbstractShellCommand {
40
41 private static final BiMap<String, Region.Type> REGION_TYPE_MAP = HashBiMap.create();
42
43 static {
44 for (Region.Type t : Region.Type.values()) {
45 REGION_TYPE_MAP.put(t.name(), t);
46 }
47 }
48
49 @Argument(index = 0, name = "id", description = "Region ID",
50 required = true, multiValued = false)
51 String id = null;
52
53 @Argument(index = 1, name = "name", description = "Region Name",
54 required = true, multiValued = false)
55 String name = null;
56
Jian Li43a0b2f2016-02-29 10:01:54 -080057 @Argument(index = 2, name = "type", description = "Region Type (CONTINENT|" +
58 "COUNTRY|METRO|CAMPUS|BUILDING|FLOOR|ROOM|RACK|LOGICAL_GROUP)",
Jian Licf744442016-02-25 17:32:42 +090059 required = true, multiValued = false)
60 String type = null;
61
Jian Li43a0b2f2016-02-29 10:01:54 -080062 @Argument(index = 3, name = "masters", description = "Region Master, a set " +
63 "of nodeIds should be split with '/' delimiter (e.g., 1 2 3 / 4 5 6)",
Jian Licf744442016-02-25 17:32:42 +090064 required = true, multiValued = true)
Jian Li43a0b2f2016-02-29 10:01:54 -080065 List<String> masterArgs = null;
Jian Licf744442016-02-25 17:32:42 +090066
67 @Override
68 protected void execute() {
69 RegionService regionService = get(RegionService.class);
70 RegionAdminService regionAdminService = get(RegionAdminService.class);
71 RegionId regionId = RegionId.regionId(id);
72
73 if (regionService.getRegion(regionId) == null) {
74 print("The region with id %s does not exist.", regionId);
75 return;
76 }
77
Jian Licf744442016-02-25 17:32:42 +090078 List<Set<NodeId>> masters = Lists.newArrayList();
Jian Li43a0b2f2016-02-29 10:01:54 -080079 Set<NodeId> nodeIds = Sets.newHashSet();
80 for (String masterArg : masterArgs) {
Jon Halla3fcf672017-03-28 16:53:22 -070081 if ("/".equals(masterArg)) {
Jian Li43a0b2f2016-02-29 10:01:54 -080082 masters.add(nodeIds);
83 nodeIds = Sets.newHashSet();
84 } else {
85 nodeIds.add(NodeId.nodeId(masterArg));
86 }
87 }
Jian Licf744442016-02-25 17:32:42 +090088 masters.add(nodeIds);
Jian Li43a0b2f2016-02-29 10:01:54 -080089
Jian Licf744442016-02-25 17:32:42 +090090 regionAdminService.updateRegion(regionId, name, REGION_TYPE_MAP.get(type), masters);
91 print("Region with id %s is successfully updated.", regionId);
92 }
93}