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;
import org.onosproject.net.Port;
import org.onosproject.net.flow.FlowRule;
import org.onosproject.net.group.Group;
import org.onosproject.net.region.Region;
import org.onosproject.net.statistic.TypedFlowEntryWithLoad;
import org.onosproject.net.topology.TopologyCluster;
......@@ -141,4 +142,11 @@ public final class Comparators {
return deviceKey1.deviceKeyId().id().toString().compareTo(deviceKey2.deviceKeyId().id().toString());
}
};
public static final Comparator<Region> REGION_COMPARATOR = new Comparator<Region>() {
@Override
public int compare(Region region1, Region region2) {
return region1.id().toString().compareTo(region2.id().toString());
}
};
}
......
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.cli.net;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Lists;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.cluster.NodeId;
import org.onosproject.net.region.Region;
import org.onosproject.net.region.RegionAdminService;
import org.onosproject.net.region.RegionId;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Add a new region.
*/
@Command(scope = "onos", name = "region-add",
description = "Adds a new region.")
public class RegionAddCommand extends AbstractShellCommand {
private static final BiMap<String, Region.Type> REGION_TYPE_MAP = HashBiMap.create();
static {
for (Region.Type t : Region.Type.values()) {
REGION_TYPE_MAP.put(t.name(), t);
}
}
@Argument(index = 0, name = "id", description = "Region ID",
required = true, multiValued = false)
String id = null;
@Argument(index = 1, name = "name", description = "Region Name",
required = true, multiValued = false)
String name = null;
@Argument(index = 2, name = "type", description = "Region Type",
required = true, multiValued = false)
String type = null;
@Argument(index = 3, name = "masters", description = "Region Master",
required = true, multiValued = true)
List<String> masters = null;
@Override
protected void execute() {
RegionAdminService service = get(RegionAdminService.class);
RegionId regionId = RegionId.regionId(id);
Set<NodeId> nodeIds =
masters.stream().map(s -> NodeId.nodeId(s)).collect(Collectors.toSet());
List<Set<NodeId>> masters = Lists.newArrayList();
masters.add(nodeIds);
service.createRegion(regionId, name, REGION_TYPE_MAP.get(type), masters);
print("Region successfully added.");
}
}
\ No newline at end of file
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.cli.net;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.net.DeviceId;
import org.onosproject.net.region.RegionAdminService;
import org.onosproject.net.region.RegionId;
import java.util.List;
import java.util.stream.Collectors;
/**
* Add a set of devices into existing region.
*/
@Command(scope = "onos", name = "region-add-devices",
description = "Adds a set of devices into the region.")
public class RegionAddDevicesCommand extends AbstractShellCommand {
@Argument(index = 0, name = "id", description = "Region ID",
required = true, multiValued = false)
String id = null;
@Argument(index = 1, name = "devIds", description = "Device IDs",
required = true, multiValued = true)
List<String> devIds = null;
@Override
protected void execute() {
RegionAdminService service = get(RegionAdminService.class);
RegionId regionId = RegionId.regionId(id);
List<DeviceId> dids = devIds.stream().map(s ->
DeviceId.deviceId(s)).collect(Collectors.toList());
service.addDevices(regionId, dids);
}
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.cli.net;
import org.onosproject.cli.AbstractChoicesCompleter;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.net.region.RegionService;
import java.util.List;
import java.util.stream.Collectors;
/**
* Region ID completer.
*/
public class RegionIdCompleter extends AbstractChoicesCompleter {
@Override
protected List<String> choices() {
RegionService service = AbstractShellCommand.get(RegionService.class);
return service.getRegions()
.stream()
.map(r -> r.id().toString())
.collect(Collectors.toList());
}
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.cli.net;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.cli.Comparators;
import org.onosproject.net.region.Region;
import org.onosproject.net.region.RegionId;
import org.onosproject.net.region.RegionService;
import java.util.Collections;
import java.util.List;
import static com.google.common.collect.Lists.newArrayList;
/**
* List Region details including membership.
*/
@Command(scope = "onos", name = "regions",
description = "List Region details including membership")
public class RegionListCommand extends AbstractShellCommand {
private static final String FMT = "id=%s, name=%s, type=%s";
private static final String FMT_MASTER = " master=%s";
@Argument(index = 0, name = "id", description = "Region ID",
required = false, multiValued = false)
String id = null;
@Override
protected void execute() {
RegionService regionService = get(RegionService.class);
if (id == null) {
for (Region region : getSortedRegions(regionService)) {
printRegion(region);
}
} else {
Region region = regionService.getRegion(RegionId.regionId(id));
if (region == null) {
error("No such region %s", id);
} else {
printRegion(region);
}
}
}
/**
* Returns the list of regions sorted using the region identifier.
*
* @param service region service
* @return sorted region list
*/
protected List<Region> getSortedRegions(RegionService service) {
List<Region> regions = newArrayList(service.getRegions());
Collections.sort(regions, Comparators.REGION_COMPARATOR);
return regions;
}
private void printRegion(Region region) {
print(FMT, region.id(), region.name(), region.type());
region.masters().forEach(m -> print(FMT_MASTER, m));
}
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.cli.net;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.net.region.RegionAdminService;
import org.onosproject.net.region.RegionId;
/**
* Removes a region from the existing region list.
*/
@Command(scope = "onos", name = "region-remove",
description = "Removes an existing region.")
public class RegionRemoveCommand extends AbstractShellCommand {
@Argument(index = 0, name = "id", description = "Region ID",
required = true, multiValued = false)
String id = null;
@Override
protected void execute() {
RegionAdminService service = get(RegionAdminService.class);
RegionId regionId = RegionId.regionId(id);
service.removeRegion(regionId);
print("Region with id %s is successfully removed.", regionId);
}
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.cli.net;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.net.DeviceId;
import org.onosproject.net.region.RegionAdminService;
import org.onosproject.net.region.RegionId;
import java.util.List;
import java.util.stream.Collectors;
/**
* Remove a set of devices from existing region.
*/
@Command(scope = "onos", name = "region-remove-devices",
description = "Removes a set of devices from the region.")
public class RegionRemoveDevicesCommand extends AbstractShellCommand {
@Argument(index = 0, name = "id", description = "Region ID",
required = true, multiValued = false)
String id = null;
@Argument(index = 1, name = "devIds", description = "Device IDs",
required = true, multiValued = true)
List<String> devIds = null;
@Override
protected void execute() {
RegionAdminService service = get(RegionAdminService.class);
RegionId regionId = RegionId.regionId(id);
List<DeviceId> dids = devIds.stream().map(s ->
DeviceId.deviceId(s)).collect(Collectors.toList());
service.removeDevices(regionId, dids);
}
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.cli.net;
import com.google.common.collect.Lists;
import org.onosproject.cli.AbstractChoicesCompleter;
import org.onosproject.net.region.Region;
import java.util.List;
/**
* Region type completer.
*/
public class RegionTypeCompleter extends AbstractChoicesCompleter {
@Override
protected List<String> choices() {
List<String> types = Lists.newArrayList();
for (Region.Type type : Region.Type.values()) {
types.add(type.toString());
}
return types;
}
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.cli.net;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Lists;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.cluster.NodeId;
import org.onosproject.net.region.Region;
import org.onosproject.net.region.RegionAdminService;
import org.onosproject.net.region.RegionId;
import org.onosproject.net.region.RegionService;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Update an existing region.
*/
@Command(scope = "onos", name = "region-update",
description = "Updates an existing region.")
public class RegionUpdateCommand extends AbstractShellCommand {
private static final BiMap<String, Region.Type> REGION_TYPE_MAP = HashBiMap.create();
static {
for (Region.Type t : Region.Type.values()) {
REGION_TYPE_MAP.put(t.name(), t);
}
}
@Argument(index = 0, name = "id", description = "Region ID",
required = true, multiValued = false)
String id = null;
@Argument(index = 1, name = "name", description = "Region Name",
required = true, multiValued = false)
String name = null;
@Argument(index = 2, name = "type", description = "Region Type",
required = true, multiValued = false)
String type = null;
@Argument(index = 3, name = "masters", description = "Region Master",
required = true, multiValued = true)
List<String> masters = null;
@Override
protected void execute() {
RegionService regionService = get(RegionService.class);
RegionAdminService regionAdminService = get(RegionAdminService.class);
RegionId regionId = RegionId.regionId(id);
if (regionService.getRegion(regionId) == null) {
print("The region with id %s does not exist.", regionId);
return;
}
Set<NodeId> nodeIds =
masters.stream().map(s -> NodeId.nodeId(s)).collect(Collectors.toSet());
List<Set<NodeId>> masters = Lists.newArrayList();
masters.add(nodeIds);
regionAdminService.updateRegion(regionId, name, REGION_TYPE_MAP.get(type), masters);
print("Region with id %s is successfully updated.", regionId);
}
}
......@@ -555,6 +555,49 @@
<action class="org.onosproject.cli.net.DeviceKeyRemoveCommand"/>
</command>
<!--region commands -->
<command>
<action class="org.onosproject.cli.net.RegionListCommand"/>
<completers>
<ref component-id="regionIdCompleter"/>
</completers>
</command>
<command>
<action class="org.onosproject.cli.net.RegionAddCommand"/>
<completers>
<null/>
<null/>
<ref component-id="regionTypeCompleter"/>
</completers>
</command>
<command>
<action class="org.onosproject.cli.net.RegionUpdateCommand"/>
<completers>
<ref component-id="regionIdCompleter"/>
<null/>
<ref component-id="regionTypeCompleter"/>
</completers>
</command>
<command>
<action class="org.onosproject.cli.net.RegionRemoveCommand"/>
<completers>
<ref component-id="regionIdCompleter"/>
</completers>
</command>
<command>
<action class="org.onosproject.cli.net.RegionAddDevicesCommand"/>
<completers>
<ref component-id="regionIdCompleter"/>
<ref component-id="deviceIdCompleter"/>
</completers>
</command>
<command>
<action class="org.onosproject.cli.net.RegionRemoveDevicesCommand"/>
<completers>
<ref component-id="regionIdCompleter"/>
<ref component-id="deviceIdCompleter"/>
</completers>
</command>
</command-bundle>
<bean id="reviewAppNameCompleter" class="org.onosproject.cli.security.ReviewApplicationNameCompleter"/>
......@@ -594,4 +637,6 @@
<bean id="subjectKeyCompleter" class="org.onosproject.cli.cfg.SubjectKeyCompleter"/>
<bean id="configKeyCompleter" class="org.onosproject.cli.cfg.ConfigKeyCompleter"/>
<bean id="regionIdCompleter" class="org.onosproject.cli.net.RegionIdCompleter"/>
<bean id="regionTypeCompleter" class="org.onosproject.cli.net.RegionTypeCompleter"/>
</blueprint>
......