Search in sources :

Example 16 with Region

use of org.onosproject.net.region.Region in project onos by opennetworkinglab.

the class ModelCache method updateDevice.

// make sure the UiDevice is tagged with the region it belongs to
private void updateDevice(UiDevice device) {
    Region r = services.region().getRegionForDevice(device.id());
    RegionId rid = r == null ? UiRegion.NULL_ID : r.id();
    device.setRegionId(rid);
}
Also used : Region(org.onosproject.net.region.Region) UiRegion(org.onosproject.ui.model.topo.UiRegion) RegionId(org.onosproject.net.region.RegionId)

Example 17 with Region

use of org.onosproject.net.region.Region in project onos by opennetworkinglab.

the class RegionManagerTest method membership.

@Test
public void membership() {
    Region r = service.createRegion(RID1, "R1", METRO, MASTERS);
    assertTrue("no devices expected", service.getRegionDevices(RID1).isEmpty());
    assertNull("no region expected", service.getRegionForDevice(DID1));
    service.addDevices(RID1, ImmutableSet.of(DID1, DID2));
    Set<DeviceId> deviceIds = service.getRegionDevices(RID1);
    assertEquals("incorrect device count", 2, deviceIds.size());
    assertTrue("missing d1", deviceIds.contains(DID1));
    assertTrue("missing d2", deviceIds.contains(DID2));
    assertEquals("wrong region", r, service.getRegionForDevice(DID1));
    assertEquals("incorrect event", REGION_MEMBERSHIP_CHANGED, listener.event.type());
    service.addDevices(RID1, ImmutableSet.of(DID3));
    deviceIds = service.getRegionDevices(RID1);
    assertEquals("incorrect device count", 3, deviceIds.size());
    assertTrue("missing d3", deviceIds.contains(DID3));
    assertEquals("incorrect event", REGION_MEMBERSHIP_CHANGED, listener.event.type());
    service.addDevices(RID1, ImmutableSet.of(DID3, DID1));
    deviceIds = service.getRegionDevices(RID1);
    assertEquals("incorrect device count", 3, deviceIds.size());
    service.removeDevices(RID1, ImmutableSet.of(DID2, DID3));
    deviceIds = service.getRegionDevices(RID1);
    assertEquals("incorrect device count", 1, deviceIds.size());
    assertTrue("missing d1", deviceIds.contains(DID1));
    service.removeDevices(RID1, ImmutableSet.of(DID1, DID3));
    assertTrue("no devices expected", service.getRegionDevices(RID1).isEmpty());
    service.removeDevices(RID1, ImmutableSet.of(DID2));
    assertTrue("no devices expected", service.getRegionDevices(RID1).isEmpty());
}
Also used : DeviceId(org.onosproject.net.DeviceId) Region(org.onosproject.net.region.Region) Test(org.junit.Test)

Example 18 with Region

use of org.onosproject.net.region.Region in project onos by opennetworkinglab.

the class RegionCodec method decode.

@Override
public Region decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }
    // parse masters
    List<Set<NodeId>> masters = new ArrayList<>();
    JsonNode mastersJson = json.get(MASTERS);
    checkNotNull(mastersJson);
    IntStream.range(0, mastersJson.size()).forEach(i -> {
        JsonNode setsJson = mastersJson.get(i);
        final Set<NodeId> nodeIds = Sets.newHashSet();
        if (setsJson != null && setsJson.isArray()) {
            Set<NodeId> localNodeIds = Sets.newHashSet();
            IntStream.range(0, setsJson.size()).forEach(j -> {
                JsonNode nodeIdJson = setsJson.get(j);
                localNodeIds.add(decodeNodeId(nodeIdJson));
            });
            nodeIds.addAll(localNodeIds);
        }
        masters.add(nodeIds);
    });
    RegionId regionId = RegionId.regionId(extractMember(REGION_ID, json));
    String name = extractMember(NAME, json);
    Region.Type type = REGION_TYPE_MAP.get(extractMember(TYPE, json));
    Annotations annots = extractAnnotations(json, context);
    return new DefaultRegion(regionId, name, type, annots, masters);
}
Also used : Set(java.util.Set) Annotations(org.onosproject.net.Annotations) ArrayList(java.util.ArrayList) NodeId(org.onosproject.cluster.NodeId) Region(org.onosproject.net.region.Region) DefaultRegion(org.onosproject.net.region.DefaultRegion) DefaultRegion(org.onosproject.net.region.DefaultRegion) JsonNode(com.fasterxml.jackson.databind.JsonNode) RegionId(org.onosproject.net.region.RegionId)

Example 19 with Region

use of org.onosproject.net.region.Region in project onos by opennetworkinglab.

the class MastershipManager method balanceRolesUsingRegions.

/**
 * Balances the nodes considering Region information.
 *
 * @param allControllerDevices controller nodes to devices map
 * @return true: nodes balanced; false: nodes not balanced
 */
private boolean balanceRolesUsingRegions(Map<ControllerNode, Set<DeviceId>> allControllerDevices) {
    Set<Region> regions = regionService.getRegions();
    if (regions.isEmpty()) {
        // no balancing was done using regions.
        return false;
    }
    // Handle nodes belonging to regions
    Set<ControllerNode> nodesInRegions = Sets.newHashSet();
    for (Region region : regions) {
        Map<ControllerNode, Set<DeviceId>> activeRegionControllers = balanceRolesInRegion(region, allControllerDevices);
        nodesInRegions.addAll(activeRegionControllers.keySet());
    }
    // Handle nodes not belonging to any region
    Set<ControllerNode> nodesNotInRegions = Sets.difference(allControllerDevices.keySet(), nodesInRegions);
    if (!nodesNotInRegions.isEmpty()) {
        int deviceCount = 0;
        Map<ControllerNode, Set<DeviceId>> controllerDevicesNotInRegions = new HashMap<>();
        for (ControllerNode controllerNode : nodesNotInRegions) {
            controllerDevicesNotInRegions.put(controllerNode, allControllerDevices.get(controllerNode));
            deviceCount += allControllerDevices.get(controllerNode).size();
        }
        // Now re-balance the buckets until they are roughly even.
        List<CompletableFuture<Void>> balanceBucketsFutures = Lists.newArrayList();
        balanceControllerNodes(controllerDevicesNotInRegions, deviceCount, balanceBucketsFutures);
        CompletableFuture<Void> balanceRolesFuture = allOf(balanceBucketsFutures.toArray(new CompletableFuture[balanceBucketsFutures.size()]));
        Futures.getUnchecked(balanceRolesFuture);
    }
    // balancing was done using regions.
    return true;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Region(org.onosproject.net.region.Region) ControllerNode(org.onosproject.cluster.ControllerNode)

Example 20 with Region

use of org.onosproject.net.region.Region in project onos by opennetworkinglab.

the class RegionCodecTest method testRegionEncode.

/**
 * Tests encoding of a Region object.
 */
@Test
public void testRegionEncode() {
    NodeId nodeId1 = NodeId.nodeId("1");
    NodeId nodeId2 = NodeId.nodeId("2");
    NodeId nodeId3 = NodeId.nodeId("3");
    NodeId nodeId4 = NodeId.nodeId("4");
    Set<NodeId> set1 = ImmutableSet.of(nodeId1);
    Set<NodeId> set2 = ImmutableSet.of(nodeId1, nodeId2);
    Set<NodeId> set3 = ImmutableSet.of(nodeId1, nodeId2, nodeId3);
    Set<NodeId> set4 = ImmutableSet.of(nodeId1, nodeId2, nodeId3, nodeId4);
    List<Set<NodeId>> masters = ImmutableList.of(set1, set2, set3, set4);
    RegionId regionId = RegionId.regionId("1");
    String name = "foo";
    Region.Type type = Region.Type.ROOM;
    Annotations noAnnots = DefaultAnnotations.EMPTY;
    Region region = new DefaultRegion(regionId, name, type, noAnnots, masters);
    ObjectNode regionJson = regionCodec.encode(region, context);
    assertThat(regionJson, matchesRegion(region));
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) DefaultAnnotations(org.onosproject.net.DefaultAnnotations) Annotations(org.onosproject.net.Annotations) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) 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) DefaultRegion(org.onosproject.net.region.DefaultRegion) RegionId(org.onosproject.net.region.RegionId) Test(org.junit.Test)

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