blob: 5901241883b2368c031b7f55e955c450efbc9b5f [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;
24import org.apache.karaf.shell.api.action.lifecycle.Service;
Jian Licf744442016-02-25 17:32:42 +090025import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.cluster.NodeId;
Simon Hunt53612212016-12-04 17:19:52 -080027import org.onosproject.net.config.NetworkConfigService;
28import org.onosproject.net.config.basics.BasicRegionConfig;
Jian Licf744442016-02-25 17:32:42 +090029import org.onosproject.net.region.Region;
30import org.onosproject.net.region.RegionAdminService;
31import org.onosproject.net.region.RegionId;
32
33import java.util.List;
34import java.util.Set;
Jian Licf744442016-02-25 17:32:42 +090035
36/**
37 * Add a new region.
38 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070039@Service
Jian Licf744442016-02-25 17:32:42 +090040@Command(scope = "onos", name = "region-add",
41 description = "Adds a new region.")
42public class RegionAddCommand extends AbstractShellCommand {
43
Simon Hunt0ee20bf2017-05-10 19:59:17 -070044 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 Hunt53612212016-12-04 17:19:52 -080048 private static final String SLASH = "/";
49
Simon Hunt0ee20bf2017-05-10 19:59:17 -070050 private static final BiMap<String, Region.Type> REGION_TYPE_MAP = HashBiMap.create();
Jian Licf744442016-02-25 17:32:42 +090051
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 Li43a0b2f2016-02-29 10:01:54 -080066 @Argument(index = 2, name = "type", description = "Region Type (CONTINENT|" +
Simon Hunt53612212016-12-04 17:19:52 -080067 "COUNTRY|METRO|CAMPUS|BUILDING|DATA_CENTER|FLOOR|ROOM|RACK|LOGICAL_GROUP)",
Jian Licf744442016-02-25 17:32:42 +090068 required = true, multiValued = false)
69 String type = null;
70
Simon Hunt0ee20bf2017-05-10 19:59:17 -070071 @Argument(index = 3, name = "latOrY",
72 description = "Geo latitude / Grid y-coord",
Simon Hunt53612212016-12-04 17:19:52 -080073 required = true, multiValued = false)
Simon Hunt0ee20bf2017-05-10 19:59:17 -070074 Double latOrY = null;
Simon Hunt53612212016-12-04 17:19:52 -080075
Simon Hunt0ee20bf2017-05-10 19:59:17 -070076 @Argument(index = 4, name = "longOrX",
77 description = "Geo longitude / Grid x-coord",
Simon Hunt53612212016-12-04 17:19:52 -080078 required = true, multiValued = false)
Simon Hunt0ee20bf2017-05-10 19:59:17 -070079 Double longOrX = null;
Simon Hunt53612212016-12-04 17:19:52 -080080
Simon Hunt0ee20bf2017-05-10 19:59:17 -070081 @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 Li43a0b2f2016-02-29 10:01:54 -080086 "of nodeIds should be split with '/' delimiter (e.g., 1 2 3 / 4 5 6)",
Jian Licf744442016-02-25 17:32:42 +090087 required = true, multiValued = true)
Jian Li43a0b2f2016-02-29 10:01:54 -080088 List<String> masterArgs = null;
Jian Licf744442016-02-25 17:32:42 +090089
90 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070091 protected void doExecute() {
Jian Licf744442016-02-25 17:32:42 +090092 RegionAdminService service = get(RegionAdminService.class);
93 RegionId regionId = RegionId.regionId(id);
Jian Li43a0b2f2016-02-29 10:01:54 -080094
Simon Hunt0ee20bf2017-05-10 19:59:17 -070095 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 Licf744442016-02-25 17:32:42 +0900122 List<Set<NodeId>> masters = Lists.newArrayList();
Jian Li43a0b2f2016-02-29 10:01:54 -0800123 Set<NodeId> nodeIds = Sets.newHashSet();
124 for (String masterArg : masterArgs) {
Simon Hunt53612212016-12-04 17:19:52 -0800125 if (masterArg.equals(SLASH)) {
Jian Li43a0b2f2016-02-29 10:01:54 -0800126 masters.add(nodeIds);
127 nodeIds = Sets.newHashSet();
128 } else {
129 nodeIds.add(NodeId.nodeId(masterArg));
130 }
131 }
Jian Licf744442016-02-25 17:32:42 +0900132 masters.add(nodeIds);
Simon Hunt0ee20bf2017-05-10 19:59:17 -0700133 return masters;
Jian Licf744442016-02-25 17:32:42 +0900134 }
135}