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