blob: 678ae10d91e4bcac4c7460ae1ac7990a2ac40df8 [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;
Ray Milkey0068fd02018-10-11 15:45:39 -070024import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070025import org.apache.karaf.shell.api.action.lifecycle.Service;
Jian Licf744442016-02-25 17:32:42 +090026import org.onosproject.cli.AbstractShellCommand;
Ray Milkey0068fd02018-10-11 15:45:39 -070027import org.onosproject.cli.NodeIdCompleter;
Jian Licf744442016-02-25 17:32:42 +090028import org.onosproject.cluster.NodeId;
29import org.onosproject.net.region.Region;
30import org.onosproject.net.region.RegionAdminService;
31import org.onosproject.net.region.RegionId;
32import org.onosproject.net.region.RegionService;
33
34import java.util.List;
35import java.util.Set;
Jian Licf744442016-02-25 17:32:42 +090036
37/**
38 * Update an existing region.
39 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070040@Service
Jian Licf744442016-02-25 17:32:42 +090041@Command(scope = "onos", name = "region-update",
42 description = "Updates an existing region.")
43public class RegionUpdateCommand extends AbstractShellCommand {
44
45 private static final BiMap<String, Region.Type> REGION_TYPE_MAP = HashBiMap.create();
46
47 static {
48 for (Region.Type t : Region.Type.values()) {
49 REGION_TYPE_MAP.put(t.name(), t);
50 }
51 }
52
53 @Argument(index = 0, name = "id", description = "Region ID",
54 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070055 @Completion(RegionIdCompleter.class)
Jian Licf744442016-02-25 17:32:42 +090056 String id = null;
57
58 @Argument(index = 1, name = "name", description = "Region Name",
59 required = true, multiValued = false)
60 String name = null;
61
Jian Li43a0b2f2016-02-29 10:01:54 -080062 @Argument(index = 2, name = "type", description = "Region Type (CONTINENT|" +
63 "COUNTRY|METRO|CAMPUS|BUILDING|FLOOR|ROOM|RACK|LOGICAL_GROUP)",
Jian Licf744442016-02-25 17:32:42 +090064 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070065 @Completion(RegionTypeCompleter.class)
Jian Licf744442016-02-25 17:32:42 +090066 String type = null;
67
Jian Li43a0b2f2016-02-29 10:01:54 -080068 @Argument(index = 3, name = "masters", description = "Region Master, a set " +
69 "of nodeIds should be split with '/' delimiter (e.g., 1 2 3 / 4 5 6)",
Jian Licf744442016-02-25 17:32:42 +090070 required = true, multiValued = true)
Ray Milkey0068fd02018-10-11 15:45:39 -070071 @Completion(NodeIdCompleter.class)
Jian Li43a0b2f2016-02-29 10:01:54 -080072 List<String> masterArgs = null;
Jian Licf744442016-02-25 17:32:42 +090073
74 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070075 protected void doExecute() {
Jian Licf744442016-02-25 17:32:42 +090076 RegionService regionService = get(RegionService.class);
77 RegionAdminService regionAdminService = get(RegionAdminService.class);
78 RegionId regionId = RegionId.regionId(id);
79
80 if (regionService.getRegion(regionId) == null) {
81 print("The region with id %s does not exist.", regionId);
82 return;
83 }
84
Jian Licf744442016-02-25 17:32:42 +090085 List<Set<NodeId>> masters = Lists.newArrayList();
Jian Li43a0b2f2016-02-29 10:01:54 -080086 Set<NodeId> nodeIds = Sets.newHashSet();
87 for (String masterArg : masterArgs) {
Jon Halla3fcf672017-03-28 16:53:22 -070088 if ("/".equals(masterArg)) {
Jian Li43a0b2f2016-02-29 10:01:54 -080089 masters.add(nodeIds);
90 nodeIds = Sets.newHashSet();
91 } else {
92 nodeIds.add(NodeId.nodeId(masterArg));
93 }
94 }
Jian Licf744442016-02-25 17:32:42 +090095 masters.add(nodeIds);
Jian Li43a0b2f2016-02-29 10:01:54 -080096
Jian Licf744442016-02-25 17:32:42 +090097 regionAdminService.updateRegion(regionId, name, REGION_TYPE_MAP.get(type), masters);
98 print("Region with id %s is successfully updated.", regionId);
99 }
100}