blob: eff8fb5226b8f2cb052635ffcdcc7cdcbfc4f86b [file] [log] [blame]
Jian Licf744442016-02-25 17:32:42 +09001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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;
Jian Licf744442016-02-25 17:32:42 +090022import org.apache.karaf.shell.commands.Argument;
23import org.apache.karaf.shell.commands.Command;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.cluster.NodeId;
Simon Hunt53612212016-12-04 17:19:52 -080026import org.onosproject.net.config.NetworkConfigService;
27import org.onosproject.net.config.basics.BasicRegionConfig;
Jian Licf744442016-02-25 17:32:42 +090028import org.onosproject.net.region.Region;
29import org.onosproject.net.region.RegionAdminService;
30import org.onosproject.net.region.RegionId;
31
32import java.util.List;
33import java.util.Set;
Jian Licf744442016-02-25 17:32:42 +090034
35/**
36 * Add a new region.
37 */
38@Command(scope = "onos", name = "region-add",
39 description = "Adds a new region.")
40public class RegionAddCommand extends AbstractShellCommand {
41
Simon Hunt53612212016-12-04 17:19:52 -080042 private static final String SLASH = "/";
43
44 private static final BiMap<String, Region.Type> REGION_TYPE_MAP
45 = HashBiMap.create();
Jian Licf744442016-02-25 17:32:42 +090046
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 Li43a0b2f2016-02-29 10:01:54 -080061 @Argument(index = 2, name = "type", description = "Region Type (CONTINENT|" +
Simon Hunt53612212016-12-04 17:19:52 -080062 "COUNTRY|METRO|CAMPUS|BUILDING|DATA_CENTER|FLOOR|ROOM|RACK|LOGICAL_GROUP)",
Jian Licf744442016-02-25 17:32:42 +090063 required = true, multiValued = false)
64 String type = null;
65
Simon Hunt53612212016-12-04 17:19:52 -080066 @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 Li43a0b2f2016-02-29 10:01:54 -080075 "of nodeIds should be split with '/' delimiter (e.g., 1 2 3 / 4 5 6)",
Jian Licf744442016-02-25 17:32:42 +090076 required = true, multiValued = true)
Jian Li43a0b2f2016-02-29 10:01:54 -080077 List<String> masterArgs = null;
Jian Licf744442016-02-25 17:32:42 +090078
79 @Override
80 protected void execute() {
81 RegionAdminService service = get(RegionAdminService.class);
82 RegionId regionId = RegionId.regionId(id);
Jian Li43a0b2f2016-02-29 10:01:54 -080083
Jian Licf744442016-02-25 17:32:42 +090084 List<Set<NodeId>> masters = Lists.newArrayList();
Jian Li43a0b2f2016-02-29 10:01:54 -080085 Set<NodeId> nodeIds = Sets.newHashSet();
86 for (String masterArg : masterArgs) {
Simon Hunt53612212016-12-04 17:19:52 -080087 if (masterArg.equals(SLASH)) {
Jian Li43a0b2f2016-02-29 10:01:54 -080088 masters.add(nodeIds);
89 nodeIds = Sets.newHashSet();
90 } else {
91 nodeIds.add(NodeId.nodeId(masterArg));
92 }
93 }
Jian Licf744442016-02-25 17:32:42 +090094 masters.add(nodeIds);
Jian Li43a0b2f2016-02-29 10:01:54 -080095
Simon Hunt53612212016-12-04 17:19:52 -080096 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 Licf744442016-02-25 17:32:42 +0900103 service.createRegion(regionId, name, REGION_TYPE_MAP.get(type), masters);
104 print("Region successfully added.");
105 }
106}