blob: 392bdba8444e7d789c6f1133566067190501fb2c [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;
Simon Hunt53612212016-12-04 17:19:52 -080029import org.onosproject.net.config.NetworkConfigService;
30import org.onosproject.net.config.basics.BasicRegionConfig;
Jian Licf744442016-02-25 17:32:42 +090031import org.onosproject.net.region.Region;
32import org.onosproject.net.region.RegionAdminService;
33import org.onosproject.net.region.RegionId;
34
35import java.util.List;
36import java.util.Set;
Jian Licf744442016-02-25 17:32:42 +090037
38/**
39 * Add a new region.
40 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041@Service
Jian Licf744442016-02-25 17:32:42 +090042@Command(scope = "onos", name = "region-add",
43 description = "Adds a new region.")
44public class RegionAddCommand extends AbstractShellCommand {
45
Simon Hunt0ee20bf2017-05-10 19:59:17 -070046 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 Hunt53612212016-12-04 17:19:52 -080050 private static final String SLASH = "/";
51
Simon Hunt0ee20bf2017-05-10 19:59:17 -070052 private static final BiMap<String, Region.Type> REGION_TYPE_MAP = HashBiMap.create();
Jian Licf744442016-02-25 17:32:42 +090053
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 Li43a0b2f2016-02-29 10:01:54 -080068 @Argument(index = 2, name = "type", description = "Region Type (CONTINENT|" +
Simon Hunt53612212016-12-04 17:19:52 -080069 "COUNTRY|METRO|CAMPUS|BUILDING|DATA_CENTER|FLOOR|ROOM|RACK|LOGICAL_GROUP)",
Jian Licf744442016-02-25 17:32:42 +090070 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070071 @Completion(RegionTypeCompleter.class)
Jian Licf744442016-02-25 17:32:42 +090072 String type = null;
73
Simon Hunt0ee20bf2017-05-10 19:59:17 -070074 @Argument(index = 3, name = "latOrY",
75 description = "Geo latitude / Grid y-coord",
Simon Hunt53612212016-12-04 17:19:52 -080076 required = true, multiValued = false)
Simon Hunt0ee20bf2017-05-10 19:59:17 -070077 Double latOrY = null;
Simon Hunt53612212016-12-04 17:19:52 -080078
Simon Hunt0ee20bf2017-05-10 19:59:17 -070079 @Argument(index = 4, name = "longOrX",
80 description = "Geo longitude / Grid x-coord",
Simon Hunt53612212016-12-04 17:19:52 -080081 required = true, multiValued = false)
Simon Hunt0ee20bf2017-05-10 19:59:17 -070082 Double longOrX = null;
Simon Hunt53612212016-12-04 17:19:52 -080083
Simon Hunt0ee20bf2017-05-10 19:59:17 -070084 @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 Li43a0b2f2016-02-29 10:01:54 -080089 "of nodeIds should be split with '/' delimiter (e.g., 1 2 3 / 4 5 6)",
Jian Licf744442016-02-25 17:32:42 +090090 required = true, multiValued = true)
Ray Milkey0068fd02018-10-11 15:45:39 -070091 @Completion(NodeIdCompleter.class)
Jian Li43a0b2f2016-02-29 10:01:54 -080092 List<String> masterArgs = null;
Jian Licf744442016-02-25 17:32:42 +090093
94 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070095 protected void doExecute() {
Jian Licf744442016-02-25 17:32:42 +090096 RegionAdminService service = get(RegionAdminService.class);
97 RegionId regionId = RegionId.regionId(id);
Jian Li43a0b2f2016-02-29 10:01:54 -080098
Simon Hunt0ee20bf2017-05-10 19:59:17 -070099 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 Licf744442016-02-25 17:32:42 +0900126 List<Set<NodeId>> masters = Lists.newArrayList();
Jian Li43a0b2f2016-02-29 10:01:54 -0800127 Set<NodeId> nodeIds = Sets.newHashSet();
128 for (String masterArg : masterArgs) {
Simon Hunt53612212016-12-04 17:19:52 -0800129 if (masterArg.equals(SLASH)) {
Jian Li43a0b2f2016-02-29 10:01:54 -0800130 masters.add(nodeIds);
131 nodeIds = Sets.newHashSet();
132 } else {
133 nodeIds.add(NodeId.nodeId(masterArg));
134 }
135 }
Jian Licf744442016-02-25 17:32:42 +0900136 masters.add(nodeIds);
Simon Hunt0ee20bf2017-05-10 19:59:17 -0700137 return masters;
Jian Licf744442016-02-25 17:32:42 +0900138 }
139}