Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.Argument; |
| 23 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 24 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 25 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 26 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 27 | import org.onosproject.cli.NodeIdCompleter; |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 28 | import org.onosproject.cluster.NodeId; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 29 | import org.onosproject.net.config.NetworkConfigService; |
| 30 | import org.onosproject.net.config.basics.BasicRegionConfig; |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 31 | import org.onosproject.net.region.Region; |
| 32 | import org.onosproject.net.region.RegionAdminService; |
| 33 | import org.onosproject.net.region.RegionId; |
| 34 | |
| 35 | import java.util.List; |
| 36 | import java.util.Set; |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Add a new region. |
| 40 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 41 | @Service |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 42 | @Command(scope = "onos", name = "region-add", |
| 43 | description = "Adds a new region.") |
| 44 | public class RegionAddCommand extends AbstractShellCommand { |
| 45 | |
Simon Hunt | 0ee20bf | 2017-05-10 19:59:17 -0700 | [diff] [blame] | 46 | private static final String E_BAD_LOC_TYPE = "locType must be {geo|grid}"; |
| 47 | |
| 48 | private static final String GEO = "geo"; |
| 49 | private static final String GRID = "grid"; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 50 | private static final String SLASH = "/"; |
| 51 | |
Simon Hunt | 0ee20bf | 2017-05-10 19:59:17 -0700 | [diff] [blame] | 52 | private static final BiMap<String, Region.Type> REGION_TYPE_MAP = HashBiMap.create(); |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 53 | |
| 54 | static { |
| 55 | for (Region.Type t : Region.Type.values()) { |
| 56 | REGION_TYPE_MAP.put(t.name(), t); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | @Argument(index = 0, name = "id", description = "Region ID", |
| 61 | required = true, multiValued = false) |
| 62 | String id = null; |
| 63 | |
| 64 | @Argument(index = 1, name = "name", description = "Region Name", |
| 65 | required = true, multiValued = false) |
| 66 | String name = null; |
| 67 | |
Jian Li | 43a0b2f | 2016-02-29 10:01:54 -0800 | [diff] [blame] | 68 | @Argument(index = 2, name = "type", description = "Region Type (CONTINENT|" + |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 69 | "COUNTRY|METRO|CAMPUS|BUILDING|DATA_CENTER|FLOOR|ROOM|RACK|LOGICAL_GROUP)", |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 70 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 71 | @Completion(RegionTypeCompleter.class) |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 72 | String type = null; |
| 73 | |
Simon Hunt | 0ee20bf | 2017-05-10 19:59:17 -0700 | [diff] [blame] | 74 | @Argument(index = 3, name = "latOrY", |
| 75 | description = "Geo latitude / Grid y-coord", |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 76 | required = true, multiValued = false) |
Simon Hunt | 0ee20bf | 2017-05-10 19:59:17 -0700 | [diff] [blame] | 77 | Double latOrY = null; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 78 | |
Simon Hunt | 0ee20bf | 2017-05-10 19:59:17 -0700 | [diff] [blame] | 79 | @Argument(index = 4, name = "longOrX", |
| 80 | description = "Geo longitude / Grid x-coord", |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 81 | required = true, multiValued = false) |
Simon Hunt | 0ee20bf | 2017-05-10 19:59:17 -0700 | [diff] [blame] | 82 | Double longOrX = null; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 83 | |
Simon Hunt | 0ee20bf | 2017-05-10 19:59:17 -0700 | [diff] [blame] | 84 | @Argument(index = 5, name = "locType", description = "Location type {geo|grid}", |
| 85 | required = false, multiValued = false) |
| 86 | String locType = GEO; |
| 87 | |
| 88 | @Argument(index = 6, name = "masters", description = "Region Master, a set " + |
Jian Li | 43a0b2f | 2016-02-29 10:01:54 -0800 | [diff] [blame] | 89 | "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] | 90 | required = true, multiValued = true) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 91 | @Completion(NodeIdCompleter.class) |
Jian Li | 43a0b2f | 2016-02-29 10:01:54 -0800 | [diff] [blame] | 92 | List<String> masterArgs = null; |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 93 | |
| 94 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 95 | protected void doExecute() { |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 96 | RegionAdminService service = get(RegionAdminService.class); |
| 97 | RegionId regionId = RegionId.regionId(id); |
Jian Li | 43a0b2f | 2016-02-29 10:01:54 -0800 | [diff] [blame] | 98 | |
Simon Hunt | 0ee20bf | 2017-05-10 19:59:17 -0700 | [diff] [blame] | 99 | NetworkConfigService cfgService = get(NetworkConfigService.class); |
| 100 | BasicRegionConfig cfg = cfgService.addConfig(regionId, BasicRegionConfig.class); |
| 101 | setConfigurationData(cfg); |
| 102 | |
| 103 | List<Set<NodeId>> masters = parseMasterArgs(); |
| 104 | service.createRegion(regionId, name, REGION_TYPE_MAP.get(type), masters); |
| 105 | print("Region successfully added."); |
| 106 | } |
| 107 | |
| 108 | private void setConfigurationData(BasicRegionConfig cfg) { |
| 109 | cfg.name(name).locType(locType); |
| 110 | |
| 111 | if (GEO.equals(locType)) { |
| 112 | cfg.latitude(latOrY).longitude(longOrX); |
| 113 | |
| 114 | } else if (GRID.equals(locType)) { |
| 115 | cfg.gridY(latOrY).gridX(longOrX); |
| 116 | |
| 117 | } else { |
| 118 | throw new IllegalArgumentException(E_BAD_LOC_TYPE); |
| 119 | |
| 120 | } |
| 121 | |
| 122 | cfg.apply(); |
| 123 | } |
| 124 | |
| 125 | private List<Set<NodeId>> parseMasterArgs() { |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 126 | List<Set<NodeId>> masters = Lists.newArrayList(); |
Jian Li | 43a0b2f | 2016-02-29 10:01:54 -0800 | [diff] [blame] | 127 | Set<NodeId> nodeIds = Sets.newHashSet(); |
| 128 | for (String masterArg : masterArgs) { |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 129 | if (masterArg.equals(SLASH)) { |
Jian Li | 43a0b2f | 2016-02-29 10:01:54 -0800 | [diff] [blame] | 130 | masters.add(nodeIds); |
| 131 | nodeIds = Sets.newHashSet(); |
| 132 | } else { |
| 133 | nodeIds.add(NodeId.nodeId(masterArg)); |
| 134 | } |
| 135 | } |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 136 | masters.add(nodeIds); |
Simon Hunt | 0ee20bf | 2017-05-10 19:59:17 -0700 | [diff] [blame] | 137 | return masters; |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 138 | } |
| 139 | } |