Jian Li
Committed by Gerrit Code Review

[ONOS-4015] Implement Region administration CLI

- Implement region add/update/remove CLI
- Implement devices add/remove CLI

Change-Id: I38b40b24df7f864b0725104f63347081257743ac
...@@ -26,6 +26,7 @@ import org.onosproject.net.ElementId; ...@@ -26,6 +26,7 @@ import org.onosproject.net.ElementId;
26 import org.onosproject.net.Port; 26 import org.onosproject.net.Port;
27 import org.onosproject.net.flow.FlowRule; 27 import org.onosproject.net.flow.FlowRule;
28 import org.onosproject.net.group.Group; 28 import org.onosproject.net.group.Group;
29 +import org.onosproject.net.region.Region;
29 import org.onosproject.net.statistic.TypedFlowEntryWithLoad; 30 import org.onosproject.net.statistic.TypedFlowEntryWithLoad;
30 import org.onosproject.net.topology.TopologyCluster; 31 import org.onosproject.net.topology.TopologyCluster;
31 32
...@@ -141,4 +142,11 @@ public final class Comparators { ...@@ -141,4 +142,11 @@ public final class Comparators {
141 return deviceKey1.deviceKeyId().id().toString().compareTo(deviceKey2.deviceKeyId().id().toString()); 142 return deviceKey1.deviceKeyId().id().toString().compareTo(deviceKey2.deviceKeyId().id().toString());
142 } 143 }
143 }; 144 };
145 +
146 + public static final Comparator<Region> REGION_COMPARATOR = new Comparator<Region>() {
147 + @Override
148 + public int compare(Region region1, Region region2) {
149 + return region1.id().toString().compareTo(region2.id().toString());
150 + }
151 + };
144 } 152 }
......
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
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;
21 +import org.apache.karaf.shell.commands.Argument;
22 +import org.apache.karaf.shell.commands.Command;
23 +import org.onosproject.cli.AbstractShellCommand;
24 +import org.onosproject.cluster.NodeId;
25 +import org.onosproject.net.region.Region;
26 +import org.onosproject.net.region.RegionAdminService;
27 +import org.onosproject.net.region.RegionId;
28 +
29 +import java.util.List;
30 +import java.util.Set;
31 +import java.util.stream.Collectors;
32 +
33 +/**
34 + * Add a new region.
35 + */
36 +@Command(scope = "onos", name = "region-add",
37 + description = "Adds a new region.")
38 +public class RegionAddCommand extends AbstractShellCommand {
39 +
40 + private static final BiMap<String, Region.Type> REGION_TYPE_MAP = HashBiMap.create();
41 +
42 + static {
43 + for (Region.Type t : Region.Type.values()) {
44 + REGION_TYPE_MAP.put(t.name(), t);
45 + }
46 + }
47 +
48 + @Argument(index = 0, name = "id", description = "Region ID",
49 + required = true, multiValued = false)
50 + String id = null;
51 +
52 + @Argument(index = 1, name = "name", description = "Region Name",
53 + required = true, multiValued = false)
54 + String name = null;
55 +
56 + @Argument(index = 2, name = "type", description = "Region Type",
57 + required = true, multiValued = false)
58 + String type = null;
59 +
60 + @Argument(index = 3, name = "masters", description = "Region Master",
61 + required = true, multiValued = true)
62 + List<String> masters = null;
63 +
64 + @Override
65 + protected void execute() {
66 + RegionAdminService service = get(RegionAdminService.class);
67 + RegionId regionId = RegionId.regionId(id);
68 + Set<NodeId> nodeIds =
69 + masters.stream().map(s -> NodeId.nodeId(s)).collect(Collectors.toSet());
70 + List<Set<NodeId>> masters = Lists.newArrayList();
71 + masters.add(nodeIds);
72 + service.createRegion(regionId, name, REGION_TYPE_MAP.get(type), masters);
73 + print("Region successfully added.");
74 + }
75 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
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 org.apache.karaf.shell.commands.Argument;
19 +import org.apache.karaf.shell.commands.Command;
20 +import org.onosproject.cli.AbstractShellCommand;
21 +import org.onosproject.net.DeviceId;
22 +import org.onosproject.net.region.RegionAdminService;
23 +import org.onosproject.net.region.RegionId;
24 +
25 +import java.util.List;
26 +import java.util.stream.Collectors;
27 +
28 +/**
29 + * Add a set of devices into existing region.
30 + */
31 +@Command(scope = "onos", name = "region-add-devices",
32 + description = "Adds a set of devices into the region.")
33 +public class RegionAddDevicesCommand extends AbstractShellCommand {
34 +
35 + @Argument(index = 0, name = "id", description = "Region ID",
36 + required = true, multiValued = false)
37 + String id = null;
38 +
39 + @Argument(index = 1, name = "devIds", description = "Device IDs",
40 + required = true, multiValued = true)
41 + List<String> devIds = null;
42 +
43 + @Override
44 + protected void execute() {
45 + RegionAdminService service = get(RegionAdminService.class);
46 + RegionId regionId = RegionId.regionId(id);
47 +
48 + List<DeviceId> dids = devIds.stream().map(s ->
49 + DeviceId.deviceId(s)).collect(Collectors.toList());
50 +
51 + service.addDevices(regionId, dids);
52 + }
53 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
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 org.onosproject.cli.AbstractChoicesCompleter;
19 +import org.onosproject.cli.AbstractShellCommand;
20 +import org.onosproject.net.region.RegionService;
21 +
22 +import java.util.List;
23 +import java.util.stream.Collectors;
24 +
25 +/**
26 + * Region ID completer.
27 + */
28 +public class RegionIdCompleter extends AbstractChoicesCompleter {
29 + @Override
30 + protected List<String> choices() {
31 + RegionService service = AbstractShellCommand.get(RegionService.class);
32 + return service.getRegions()
33 + .stream()
34 + .map(r -> r.id().toString())
35 + .collect(Collectors.toList());
36 + }
37 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
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 org.apache.karaf.shell.commands.Argument;
19 +import org.apache.karaf.shell.commands.Command;
20 +import org.onosproject.cli.AbstractShellCommand;
21 +import org.onosproject.cli.Comparators;
22 +import org.onosproject.net.region.Region;
23 +import org.onosproject.net.region.RegionId;
24 +import org.onosproject.net.region.RegionService;
25 +
26 +import java.util.Collections;
27 +import java.util.List;
28 +
29 +import static com.google.common.collect.Lists.newArrayList;
30 +
31 +/**
32 + * List Region details including membership.
33 + */
34 +@Command(scope = "onos", name = "regions",
35 + description = "List Region details including membership")
36 +public class RegionListCommand extends AbstractShellCommand {
37 +
38 + private static final String FMT = "id=%s, name=%s, type=%s";
39 + private static final String FMT_MASTER = " master=%s";
40 +
41 + @Argument(index = 0, name = "id", description = "Region ID",
42 + required = false, multiValued = false)
43 + String id = null;
44 +
45 + @Override
46 + protected void execute() {
47 + RegionService regionService = get(RegionService.class);
48 +
49 + if (id == null) {
50 + for (Region region : getSortedRegions(regionService)) {
51 + printRegion(region);
52 + }
53 + } else {
54 + Region region = regionService.getRegion(RegionId.regionId(id));
55 +
56 + if (region == null) {
57 + error("No such region %s", id);
58 + } else {
59 + printRegion(region);
60 + }
61 + }
62 + }
63 +
64 + /**
65 + * Returns the list of regions sorted using the region identifier.
66 + *
67 + * @param service region service
68 + * @return sorted region list
69 + */
70 + protected List<Region> getSortedRegions(RegionService service) {
71 + List<Region> regions = newArrayList(service.getRegions());
72 + Collections.sort(regions, Comparators.REGION_COMPARATOR);
73 + return regions;
74 + }
75 +
76 + private void printRegion(Region region) {
77 + print(FMT, region.id(), region.name(), region.type());
78 + region.masters().forEach(m -> print(FMT_MASTER, m));
79 + }
80 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
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 org.apache.karaf.shell.commands.Argument;
19 +import org.apache.karaf.shell.commands.Command;
20 +import org.onosproject.cli.AbstractShellCommand;
21 +import org.onosproject.net.region.RegionAdminService;
22 +import org.onosproject.net.region.RegionId;
23 +
24 +/**
25 + * Removes a region from the existing region list.
26 + */
27 +@Command(scope = "onos", name = "region-remove",
28 + description = "Removes an existing region.")
29 +public class RegionRemoveCommand extends AbstractShellCommand {
30 +
31 + @Argument(index = 0, name = "id", description = "Region ID",
32 + required = true, multiValued = false)
33 + String id = null;
34 +
35 + @Override
36 + protected void execute() {
37 + RegionAdminService service = get(RegionAdminService.class);
38 + RegionId regionId = RegionId.regionId(id);
39 +
40 + service.removeRegion(regionId);
41 + print("Region with id %s is successfully removed.", regionId);
42 + }
43 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
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 org.apache.karaf.shell.commands.Argument;
19 +import org.apache.karaf.shell.commands.Command;
20 +import org.onosproject.cli.AbstractShellCommand;
21 +import org.onosproject.net.DeviceId;
22 +import org.onosproject.net.region.RegionAdminService;
23 +import org.onosproject.net.region.RegionId;
24 +
25 +import java.util.List;
26 +import java.util.stream.Collectors;
27 +
28 +/**
29 + * Remove a set of devices from existing region.
30 + */
31 +@Command(scope = "onos", name = "region-remove-devices",
32 + description = "Removes a set of devices from the region.")
33 +public class RegionRemoveDevicesCommand extends AbstractShellCommand {
34 +
35 + @Argument(index = 0, name = "id", description = "Region ID",
36 + required = true, multiValued = false)
37 + String id = null;
38 +
39 + @Argument(index = 1, name = "devIds", description = "Device IDs",
40 + required = true, multiValued = true)
41 + List<String> devIds = null;
42 +
43 + @Override
44 + protected void execute() {
45 + RegionAdminService service = get(RegionAdminService.class);
46 + RegionId regionId = RegionId.regionId(id);
47 +
48 + List<DeviceId> dids = devIds.stream().map(s ->
49 + DeviceId.deviceId(s)).collect(Collectors.toList());
50 +
51 + service.removeDevices(regionId, dids);
52 + }
53 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
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.Lists;
19 +import org.onosproject.cli.AbstractChoicesCompleter;
20 +import org.onosproject.net.region.Region;
21 +
22 +import java.util.List;
23 +
24 +/**
25 + * Region type completer.
26 + */
27 +public class RegionTypeCompleter extends AbstractChoicesCompleter {
28 + @Override
29 + protected List<String> choices() {
30 + List<String> types = Lists.newArrayList();
31 + for (Region.Type type : Region.Type.values()) {
32 + types.add(type.toString());
33 + }
34 +
35 + return types;
36 + }
37 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
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;
21 +import org.apache.karaf.shell.commands.Argument;
22 +import org.apache.karaf.shell.commands.Command;
23 +import org.onosproject.cli.AbstractShellCommand;
24 +import org.onosproject.cluster.NodeId;
25 +import org.onosproject.net.region.Region;
26 +import org.onosproject.net.region.RegionAdminService;
27 +import org.onosproject.net.region.RegionId;
28 +import org.onosproject.net.region.RegionService;
29 +
30 +import java.util.List;
31 +import java.util.Set;
32 +import java.util.stream.Collectors;
33 +
34 +/**
35 + * Update an existing region.
36 + */
37 +@Command(scope = "onos", name = "region-update",
38 + description = "Updates an existing region.")
39 +public class RegionUpdateCommand extends AbstractShellCommand {
40 +
41 + private static final BiMap<String, Region.Type> REGION_TYPE_MAP = HashBiMap.create();
42 +
43 + static {
44 + for (Region.Type t : Region.Type.values()) {
45 + REGION_TYPE_MAP.put(t.name(), t);
46 + }
47 + }
48 +
49 + @Argument(index = 0, name = "id", description = "Region ID",
50 + required = true, multiValued = false)
51 + String id = null;
52 +
53 + @Argument(index = 1, name = "name", description = "Region Name",
54 + required = true, multiValued = false)
55 + String name = null;
56 +
57 + @Argument(index = 2, name = "type", description = "Region Type",
58 + required = true, multiValued = false)
59 + String type = null;
60 +
61 + @Argument(index = 3, name = "masters", description = "Region Master",
62 + required = true, multiValued = true)
63 + List<String> masters = null;
64 +
65 + @Override
66 + protected void execute() {
67 + RegionService regionService = get(RegionService.class);
68 + RegionAdminService regionAdminService = get(RegionAdminService.class);
69 + RegionId regionId = RegionId.regionId(id);
70 +
71 + if (regionService.getRegion(regionId) == null) {
72 + print("The region with id %s does not exist.", regionId);
73 + return;
74 + }
75 +
76 + Set<NodeId> nodeIds =
77 + masters.stream().map(s -> NodeId.nodeId(s)).collect(Collectors.toSet());
78 + List<Set<NodeId>> masters = Lists.newArrayList();
79 + masters.add(nodeIds);
80 + regionAdminService.updateRegion(regionId, name, REGION_TYPE_MAP.get(type), masters);
81 + print("Region with id %s is successfully updated.", regionId);
82 + }
83 +}
...@@ -555,6 +555,49 @@ ...@@ -555,6 +555,49 @@
555 <action class="org.onosproject.cli.net.DeviceKeyRemoveCommand"/> 555 <action class="org.onosproject.cli.net.DeviceKeyRemoveCommand"/>
556 </command> 556 </command>
557 557
558 + <!--region commands -->
559 + <command>
560 + <action class="org.onosproject.cli.net.RegionListCommand"/>
561 + <completers>
562 + <ref component-id="regionIdCompleter"/>
563 + </completers>
564 + </command>
565 + <command>
566 + <action class="org.onosproject.cli.net.RegionAddCommand"/>
567 + <completers>
568 + <null/>
569 + <null/>
570 + <ref component-id="regionTypeCompleter"/>
571 + </completers>
572 + </command>
573 + <command>
574 + <action class="org.onosproject.cli.net.RegionUpdateCommand"/>
575 + <completers>
576 + <ref component-id="regionIdCompleter"/>
577 + <null/>
578 + <ref component-id="regionTypeCompleter"/>
579 + </completers>
580 + </command>
581 + <command>
582 + <action class="org.onosproject.cli.net.RegionRemoveCommand"/>
583 + <completers>
584 + <ref component-id="regionIdCompleter"/>
585 + </completers>
586 + </command>
587 + <command>
588 + <action class="org.onosproject.cli.net.RegionAddDevicesCommand"/>
589 + <completers>
590 + <ref component-id="regionIdCompleter"/>
591 + <ref component-id="deviceIdCompleter"/>
592 + </completers>
593 + </command>
594 + <command>
595 + <action class="org.onosproject.cli.net.RegionRemoveDevicesCommand"/>
596 + <completers>
597 + <ref component-id="regionIdCompleter"/>
598 + <ref component-id="deviceIdCompleter"/>
599 + </completers>
600 + </command>
558 </command-bundle> 601 </command-bundle>
559 602
560 <bean id="reviewAppNameCompleter" class="org.onosproject.cli.security.ReviewApplicationNameCompleter"/> 603 <bean id="reviewAppNameCompleter" class="org.onosproject.cli.security.ReviewApplicationNameCompleter"/>
...@@ -594,4 +637,6 @@ ...@@ -594,4 +637,6 @@
594 <bean id="subjectKeyCompleter" class="org.onosproject.cli.cfg.SubjectKeyCompleter"/> 637 <bean id="subjectKeyCompleter" class="org.onosproject.cli.cfg.SubjectKeyCompleter"/>
595 <bean id="configKeyCompleter" class="org.onosproject.cli.cfg.ConfigKeyCompleter"/> 638 <bean id="configKeyCompleter" class="org.onosproject.cli.cfg.ConfigKeyCompleter"/>
596 639
640 + <bean id="regionIdCompleter" class="org.onosproject.cli.net.RegionIdCompleter"/>
641 + <bean id="regionTypeCompleter" class="org.onosproject.cli.net.RegionTypeCompleter"/>
597 </blueprint> 642 </blueprint>
......