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);
}
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());
}
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);
}
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;
}
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));
}
Aggregations