blob: 3b164d40d0d0c69a69e9fa7d6186c78ddecea4e0 [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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.apache.karaf.shell.api.action.Argument;
23import org.apache.karaf.shell.api.action.Command;
24import org.apache.karaf.shell.api.action.lifecycle.Service;
Jian Licf744442016-02-25 17:32:42 +090025import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.cluster.NodeId;
27import org.onosproject.net.region.Region;
28import org.onosproject.net.region.RegionAdminService;
29import org.onosproject.net.region.RegionId;
30import org.onosproject.net.region.RegionService;
31
32import java.util.List;
33import java.util.Set;
Jian Licf744442016-02-25 17:32:42 +090034
35/**
36 * Update an existing region.
37 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070038@Service
Jian Licf744442016-02-25 17:32:42 +090039@Command(scope = "onos", name = "region-update",
40 description = "Updates an existing region.")
41public class RegionUpdateCommand extends AbstractShellCommand {
42
43 private static final BiMap<String, Region.Type> REGION_TYPE_MAP = HashBiMap.create();
44
45 static {
46 for (Region.Type t : Region.Type.values()) {
47 REGION_TYPE_MAP.put(t.name(), t);
48 }
49 }
50
51 @Argument(index = 0, name = "id", description = "Region ID",
52 required = true, multiValued = false)
53 String id = null;
54
55 @Argument(index = 1, name = "name", description = "Region Name",
56 required = true, multiValued = false)
57 String name = null;
58
Jian Li43a0b2f2016-02-29 10:01:54 -080059 @Argument(index = 2, name = "type", description = "Region Type (CONTINENT|" +
60 "COUNTRY|METRO|CAMPUS|BUILDING|FLOOR|ROOM|RACK|LOGICAL_GROUP)",
Jian Licf744442016-02-25 17:32:42 +090061 required = true, multiValued = false)
62 String type = null;
63
Jian Li43a0b2f2016-02-29 10:01:54 -080064 @Argument(index = 3, name = "masters", description = "Region Master, a set " +
65 "of nodeIds should be split with '/' delimiter (e.g., 1 2 3 / 4 5 6)",
Jian Licf744442016-02-25 17:32:42 +090066 required = true, multiValued = true)
Jian Li43a0b2f2016-02-29 10:01:54 -080067 List<String> masterArgs = null;
Jian Licf744442016-02-25 17:32:42 +090068
69 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070070 protected void doExecute() {
Jian Licf744442016-02-25 17:32:42 +090071 RegionService regionService = get(RegionService.class);
72 RegionAdminService regionAdminService = get(RegionAdminService.class);
73 RegionId regionId = RegionId.regionId(id);
74
75 if (regionService.getRegion(regionId) == null) {
76 print("The region with id %s does not exist.", regionId);
77 return;
78 }
79
Jian Licf744442016-02-25 17:32:42 +090080 List<Set<NodeId>> masters = Lists.newArrayList();
Jian Li43a0b2f2016-02-29 10:01:54 -080081 Set<NodeId> nodeIds = Sets.newHashSet();
82 for (String masterArg : masterArgs) {
Jon Halla3fcf672017-03-28 16:53:22 -070083 if ("/".equals(masterArg)) {
Jian Li43a0b2f2016-02-29 10:01:54 -080084 masters.add(nodeIds);
85 nodeIds = Sets.newHashSet();
86 } else {
87 nodeIds.add(NodeId.nodeId(masterArg));
88 }
89 }
Jian Licf744442016-02-25 17:32:42 +090090 masters.add(nodeIds);
Jian Li43a0b2f2016-02-29 10:01:54 -080091
Jian Licf744442016-02-25 17:32:42 +090092 regionAdminService.updateRegion(regionId, name, REGION_TYPE_MAP.get(type), masters);
93 print("Region with id %s is successfully updated.", regionId);
94 }
95}