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; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 26 | import org.onosproject.net.config.NetworkConfigService; |
| 27 | import org.onosproject.net.config.basics.BasicRegionConfig; |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 28 | import org.onosproject.net.region.Region; |
| 29 | import org.onosproject.net.region.RegionAdminService; |
| 30 | import org.onosproject.net.region.RegionId; |
| 31 | |
| 32 | import java.util.List; |
| 33 | import java.util.Set; |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Add a new region. |
| 37 | */ |
| 38 | @Command(scope = "onos", name = "region-add", |
| 39 | description = "Adds a new region.") |
| 40 | public class RegionAddCommand extends AbstractShellCommand { |
| 41 | |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 42 | private static final String SLASH = "/"; |
| 43 | |
| 44 | private static final BiMap<String, Region.Type> REGION_TYPE_MAP |
| 45 | = HashBiMap.create(); |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 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) |
| 55 | String id = null; |
| 56 | |
| 57 | @Argument(index = 1, name = "name", description = "Region Name", |
| 58 | required = true, multiValued = false) |
| 59 | String name = null; |
| 60 | |
Jian Li | 43a0b2f | 2016-02-29 10:01:54 -0800 | [diff] [blame] | 61 | @Argument(index = 2, name = "type", description = "Region Type (CONTINENT|" + |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 62 | "COUNTRY|METRO|CAMPUS|BUILDING|DATA_CENTER|FLOOR|ROOM|RACK|LOGICAL_GROUP)", |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 63 | required = true, multiValued = false) |
| 64 | String type = null; |
| 65 | |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 66 | @Argument(index = 3, name = "latitude", description = "Geo latitude", |
| 67 | required = true, multiValued = false) |
| 68 | Double latitude = null; |
| 69 | |
| 70 | @Argument(index = 4, name = "longitude", description = "Geo longitude", |
| 71 | required = true, multiValued = false) |
| 72 | Double longitude = null; |
| 73 | |
| 74 | @Argument(index = 5, name = "masters", description = "Region Master, a set " + |
Jian Li | 43a0b2f | 2016-02-29 10:01:54 -0800 | [diff] [blame] | 75 | "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] | 76 | required = true, multiValued = true) |
Jian Li | 43a0b2f | 2016-02-29 10:01:54 -0800 | [diff] [blame] | 77 | List<String> masterArgs = null; |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 78 | |
| 79 | @Override |
| 80 | protected void execute() { |
| 81 | RegionAdminService service = get(RegionAdminService.class); |
| 82 | RegionId regionId = RegionId.regionId(id); |
Jian Li | 43a0b2f | 2016-02-29 10:01:54 -0800 | [diff] [blame] | 83 | |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 84 | List<Set<NodeId>> masters = Lists.newArrayList(); |
Jian Li | 43a0b2f | 2016-02-29 10:01:54 -0800 | [diff] [blame] | 85 | Set<NodeId> nodeIds = Sets.newHashSet(); |
| 86 | for (String masterArg : masterArgs) { |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 87 | if (masterArg.equals(SLASH)) { |
Jian Li | 43a0b2f | 2016-02-29 10:01:54 -0800 | [diff] [blame] | 88 | masters.add(nodeIds); |
| 89 | nodeIds = Sets.newHashSet(); |
| 90 | } else { |
| 91 | nodeIds.add(NodeId.nodeId(masterArg)); |
| 92 | } |
| 93 | } |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 94 | masters.add(nodeIds); |
Jian Li | 43a0b2f | 2016-02-29 10:01:54 -0800 | [diff] [blame] | 95 | |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 96 | NetworkConfigService cfgService = get(NetworkConfigService.class); |
| 97 | BasicRegionConfig cfg = cfgService.addConfig(regionId, BasicRegionConfig.class); |
| 98 | cfg.name(name) |
| 99 | .latitude(latitude) |
| 100 | .longitude(longitude) |
| 101 | .apply(); |
| 102 | |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 103 | service.createRegion(regionId, name, REGION_TYPE_MAP.get(type), masters); |
| 104 | print("Region successfully added."); |
| 105 | } |
| 106 | } |