Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.cli.net; |
| 17 | |
| 18 | import com.google.common.collect.BiMap; |
| 19 | import com.google.common.collect.HashBiMap; |
| 20 | import com.google.common.collect.Lists; |
Jian Li | 43a0b2f | 2016-02-29 10:01:54 -0800 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Argument; |
| 23 | import org.apache.karaf.shell.commands.Command; |
| 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.cluster.NodeId; |
| 26 | import org.onosproject.net.region.Region; |
| 27 | import org.onosproject.net.region.RegionAdminService; |
| 28 | import org.onosproject.net.region.RegionId; |
| 29 | import org.onosproject.net.region.RegionService; |
| 30 | |
| 31 | import java.util.List; |
| 32 | import java.util.Set; |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Update an existing region. |
| 36 | */ |
| 37 | @Command(scope = "onos", name = "region-update", |
| 38 | description = "Updates an existing region.") |
| 39 | public 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 Li | 43a0b2f | 2016-02-29 10:01:54 -0800 | [diff] [blame] | 57 | @Argument(index = 2, name = "type", description = "Region Type (CONTINENT|" + |
| 58 | "COUNTRY|METRO|CAMPUS|BUILDING|FLOOR|ROOM|RACK|LOGICAL_GROUP)", |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 59 | required = true, multiValued = false) |
| 60 | String type = null; |
| 61 | |
Jian Li | 43a0b2f | 2016-02-29 10:01:54 -0800 | [diff] [blame] | 62 | @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 Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 64 | required = true, multiValued = true) |
Jian Li | 43a0b2f | 2016-02-29 10:01:54 -0800 | [diff] [blame] | 65 | List<String> masterArgs = null; |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 66 | |
| 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 Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 78 | List<Set<NodeId>> masters = Lists.newArrayList(); |
Jian Li | 43a0b2f | 2016-02-29 10:01:54 -0800 | [diff] [blame] | 79 | Set<NodeId> nodeIds = Sets.newHashSet(); |
| 80 | for (String masterArg : masterArgs) { |
| 81 | if (masterArg.equals("/")) { |
| 82 | masters.add(nodeIds); |
| 83 | nodeIds = Sets.newHashSet(); |
| 84 | } else { |
| 85 | nodeIds.add(NodeId.nodeId(masterArg)); |
| 86 | } |
| 87 | } |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 88 | masters.add(nodeIds); |
Jian Li | 43a0b2f | 2016-02-29 10:01:54 -0800 | [diff] [blame] | 89 | |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 90 | regionAdminService.updateRegion(regionId, name, REGION_TYPE_MAP.get(type), masters); |
| 91 | print("Region with id %s is successfully updated.", regionId); |
| 92 | } |
| 93 | } |