Search in sources :

Example 6 with Region

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;
}
Also used : CLUSTER_READ(org.onosproject.security.AppPermission.Type.CLUSTER_READ) RegionService(org.onosproject.net.region.RegionService) MASTER(org.onosproject.net.MastershipRole.MASTER) OsgiPropertyConstants(org.onosproject.net.OsgiPropertyConstants) Map(java.util.Map) MastershipService(org.onosproject.mastership.MastershipService) NodeId(org.onosproject.cluster.NodeId) Region(org.onosproject.net.region.Region) UpgradeService(org.onosproject.upgrade.UpgradeService) AbstractListenerManager(org.onosproject.event.AbstractListenerManager) Deactivate(org.osgi.service.component.annotations.Deactivate) Collection(java.util.Collection) MastershipTermService(org.onosproject.mastership.MastershipTermService) MastershipInfo(org.onosproject.mastership.MastershipInfo) Set(java.util.Set) Sets(com.google.common.collect.Sets) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) MastershipStore(org.onosproject.mastership.MastershipStore) Timer(com.codahale.metrics.Timer) ClusterService(org.onosproject.cluster.ClusterService) DeviceId(org.onosproject.net.DeviceId) MastershipStoreDelegate(org.onosproject.mastership.MastershipStoreDelegate) MetricsUtil.stopTimer(org.onlab.metrics.MetricsUtil.stopTimer) AppGuard.checkPermission(org.onosproject.security.AppGuard.checkPermission) MetricsService(org.onlab.metrics.MetricsService) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ControllerNode(org.onosproject.cluster.ControllerNode) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MetricsHelper(org.onosproject.core.MetricsHelper) MastershipEvent(org.onosproject.mastership.MastershipEvent) Component(org.osgi.service.component.annotations.Component) Lists(com.google.common.collect.Lists) CompletableFuture.allOf(java.util.concurrent.CompletableFuture.allOf) Context(com.codahale.metrics.Timer.Context) Activate(org.osgi.service.component.annotations.Activate) ComponentConfigService(org.onosproject.cfg.ComponentConfigService) UpgradeEventListener(org.onosproject.upgrade.UpgradeEventListener) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) MetricsUtil.startTimer(org.onlab.metrics.MetricsUtil.startTimer) MastershipRole(org.onosproject.net.MastershipRole) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) CLUSTER_WRITE(org.onosproject.security.AppPermission.Type.CLUSTER_WRITE) MastershipTerm(org.onosproject.mastership.MastershipTerm) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) UpgradeEvent(org.onosproject.upgrade.UpgradeEvent) Futures(com.google.common.util.concurrent.Futures) ConfigProperty(org.onosproject.cfg.ConfigProperty) Modified(org.osgi.service.component.annotations.Modified) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) RoleInfo(org.onosproject.cluster.RoleInfo) MastershipListener(org.onosproject.mastership.MastershipListener) Reference(org.osgi.service.component.annotations.Reference) Comparator(java.util.Comparator) MastershipAdminService(org.onosproject.mastership.MastershipAdminService) Collections(java.util.Collections) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) DeviceId(org.onosproject.net.DeviceId) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) ControllerNode(org.onosproject.cluster.ControllerNode) CompletableFuture(java.util.concurrent.CompletableFuture)

Example 7 with Region

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));
}
Also used : NodeId(org.onosproject.cluster.NodeId) Region(org.onosproject.net.region.Region) RegionJsonMatcher.matchesRegion(org.onosproject.codec.impl.RegionJsonMatcher.matchesRegion) DefaultRegion(org.onosproject.net.region.DefaultRegion) Test(org.junit.Test)

Example 8 with Region

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);
    }
}
Also used : UiRegion(org.onosproject.ui.model.topo.UiRegion) Region(org.onosproject.net.region.Region) UiRegion(org.onosproject.ui.model.topo.UiRegion)

Example 9 with 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();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Region(org.onosproject.net.region.Region) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 10 with Region

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();
}
Also used : Region(org.onosproject.net.region.Region) RegionId(org.onosproject.net.region.RegionId) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Aggregations

Region (org.onosproject.net.region.Region)24 Test (org.junit.Test)9 Set (java.util.Set)7 NodeId (org.onosproject.cluster.NodeId)7 RegionId (org.onosproject.net.region.RegionId)7 DeviceId (org.onosproject.net.DeviceId)6 DefaultRegion (org.onosproject.net.region.DefaultRegion)6 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 IOException (java.io.IOException)4 Consumes (javax.ws.rs.Consumes)4 Path (javax.ws.rs.Path)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Produces (javax.ws.rs.Produces)3 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 HashMap (java.util.HashMap)2 CompletableFuture (java.util.concurrent.CompletableFuture)2