use of org.onosproject.net.region.Region in project onos by opennetworkinglab.
the class MastershipManager method balanceRolesInRegion.
/**
* Balances the nodes in specified region.
*
* @param region region in which nodes are to be balanced
* @param allControllerDevices controller nodes to devices map
* @return controller nodes that were balanced
*/
private Map<ControllerNode, Set<DeviceId>> balanceRolesInRegion(Region region, Map<ControllerNode, Set<DeviceId>> allControllerDevices) {
// Retrieve all devices associated with specified region
Set<DeviceId> devicesInRegion = regionService.getRegionDevices(region.id());
log.info("Region {} has {} devices.", region.id(), devicesInRegion.size());
if (devicesInRegion.isEmpty()) {
// no devices in this region, so nothing to balance.
return new HashMap<>();
}
List<Set<NodeId>> mastersList = region.masters();
log.info("Region {} has {} sets of masters.", region.id(), mastersList.size());
if (mastersList.isEmpty()) {
// for now just leave devices alone
return new HashMap<>();
}
// Get the region's preferred set of masters
Set<DeviceId> devicesInMasters = Sets.newHashSet();
Map<ControllerNode, Set<DeviceId>> regionalControllerDevices = getRegionsPreferredMasters(region, devicesInMasters, allControllerDevices);
// Now re-balance the buckets until they are roughly even.
List<CompletableFuture<Void>> balanceBucketsFutures = Lists.newArrayList();
balanceControllerNodes(regionalControllerDevices, devicesInMasters.size(), balanceBucketsFutures);
// Handle devices that are not currently mastered by the master node set
Set<DeviceId> devicesNotMasteredWithControllers = Sets.difference(devicesInRegion, devicesInMasters);
if (!devicesNotMasteredWithControllers.isEmpty()) {
// active controllers in master node set are already balanced, just
// assign device mastership in sequence
List<ControllerNode> sorted = new ArrayList<>(regionalControllerDevices.keySet());
Collections.sort(sorted, Comparator.comparingInt(o -> (regionalControllerDevices.get(o)).size()));
int deviceIndex = 0;
for (DeviceId deviceId : devicesNotMasteredWithControllers) {
ControllerNode cnode = sorted.get(deviceIndex % sorted.size());
balanceBucketsFutures.add(setRole(cnode.id(), deviceId, MASTER));
regionalControllerDevices.get(cnode).add(deviceId);
deviceIndex++;
}
}
CompletableFuture<Void> balanceRolesFuture = allOf(balanceBucketsFutures.toArray(new CompletableFuture[balanceBucketsFutures.size()]));
Futures.getUnchecked(balanceRolesFuture);
// Update the map before returning
regionalControllerDevices.forEach((controllerNode, deviceIds) -> {
regionalControllerDevices.put(controllerNode, new HashSet<>(getDevicesOf(controllerNode.id())));
});
return regionalControllerDevices;
}
use of org.onosproject.net.region.Region in project onos by opennetworkinglab.
the class RegionCodecTest method testRegionDecode.
/**
* Tests decoding of a json object.
*/
@Test
public void testRegionDecode() throws IOException {
Region region = getRegion("Region.json");
checkCommonData(region);
assertThat(region.masters().size(), is(2));
NodeId nodeId1 = NodeId.nodeId("1");
NodeId nodeId2 = NodeId.nodeId("2");
Set<NodeId> nodeIds1 = region.masters().get(0);
Set<NodeId> nodeIds2 = region.masters().get(1);
assertThat(nodeIds1.containsAll(ImmutableSet.of(nodeId1)), is(true));
assertThat(nodeIds2.containsAll(ImmutableSet.of(nodeId1, nodeId2)), is(true));
}
use of org.onosproject.net.region.Region in project onos by opennetworkinglab.
the class ModelCache method loadRegions.
private void loadRegions() {
for (Region r : services.region().getRegions()) {
UiRegion region = addNewRegion(r);
updateRegion(region);
}
}
use of org.onosproject.net.region.Region in project onos by opennetworkinglab.
the class RegionsWebResource method createRegion.
/**
* Creates a new region using the supplied JSON input stream.
*
* @param stream region JSON stream
* @return status of the request - CREATED if the JSON is correct,
* BAD_REQUEST if the JSON is invalid
* @onos.rsModel RegionPost
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createRegion(InputStream stream) {
URI location;
try {
ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
final Region region = codec(Region.class).decode(jsonTree, this);
final Region resultRegion = regionAdminService.createRegion(region.id(), region.name(), region.type(), region.masters());
location = new URI(resultRegion.id().id());
} catch (IOException | URISyntaxException e) {
throw new IllegalArgumentException(e);
}
return Response.created(location).build();
}
use of org.onosproject.net.region.Region in project onos by opennetworkinglab.
the class RegionsWebResource method addDevices.
/**
* Adds the specified collection of devices to the region.
*
* @param regionId region identifier
* @param stream deviceIds JSON stream
* @return status of the request - CREATED if the JSON is correct,
* BAD_REQUEST if the JSON is invalid
* @onos.rsModel RegionDeviceIds
*/
@POST
@Path("{regionId}/devices")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response addDevices(@PathParam("regionId") String regionId, InputStream stream) {
RegionId rid = RegionId.regionId(regionId);
Region region = nullIsNotFound(regionService.getRegion(rid), REGION_NOT_FOUND + rid);
URI location;
try {
regionAdminService.addDevices(region.id(), extractDeviceIds(stream));
location = new URI(rid.id());
} catch (IOException | URISyntaxException e) {
throw new IllegalArgumentException(e);
}
return Response.created(location).build();
}
Aggregations