use of org.onosproject.net.region.Region in project onos by opennetworkinglab.
the class DistributedRegionStoreTest method membership.
@Test
public void membership() {
Region r = store.createRegion(RID1, "R1", METRO, NO_ANNOTS, MASTERS);
assertTrue("no devices expected", store.getRegionDevices(RID1).isEmpty());
assertNull("no region expected", store.getRegionForDevice(DID1));
store.addDevices(RID1, ImmutableSet.of(DID1, DID2));
Set<DeviceId> deviceIds = store.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, store.getRegionForDevice(DID1));
store.addDevices(RID1, ImmutableSet.of(DID3));
deviceIds = store.getRegionDevices(RID1);
assertEquals("incorrect device count", 3, deviceIds.size());
assertTrue("missing d3", deviceIds.contains(DID3));
store.addDevices(RID1, ImmutableSet.of(DID3, DID1));
deviceIds = store.getRegionDevices(RID1);
assertEquals("incorrect device count", 3, deviceIds.size());
// Test adding DID3 to RID2 but it is already in RID1.
// DID3 will be removed from RID1 and added to RID2.
Region r2 = store.createRegion(RID2, "R2", CAMPUS, NO_ANNOTS, MASTERS);
store.addDevices(RID2, ImmutableSet.of(DID3));
deviceIds = store.getRegionDevices(RID1);
assertEquals("incorrect device count", 2, deviceIds.size());
deviceIds = store.getRegionDevices(RID2);
assertEquals("incorrect device count", 1, deviceIds.size());
store.removeDevices(RID1, ImmutableSet.of(DID2, DID3));
deviceIds = store.getRegionDevices(RID1);
assertEquals("incorrect device count", 1, deviceIds.size());
assertTrue("missing d1", deviceIds.contains(DID1));
store.removeDevices(RID1, ImmutableSet.of(DID1, DID3));
assertTrue("no devices expected", store.getRegionDevices(RID1).isEmpty());
store.removeDevices(RID1, ImmutableSet.of(DID2));
assertTrue("no devices expected", store.getRegionDevices(RID1).isEmpty());
}
use of org.onosproject.net.region.Region in project onos by opennetworkinglab.
the class RegionProtoTranslator method translate.
/**
* Translates gRPC RegionProto message to {@link org.onosproject.net.region.Region}.
*
* @param region gRPC message
* @return {@link org.onosproject.net.region.Region}
*/
public static Region translate(RegionProtoOuterClass.RegionProto region) {
RegionId id = RegionId.regionId(region.getRegionId());
Region.Type type = RegionEnumsProtoTranslator.translate(region.getType()).get();
String name = Strings.nullToEmpty(region.getName());
List<Set<NodeId>> masters = new ArrayList<>();
region.getMastersList().forEach(s -> {
Set<NodeId> nodeIdSet = new HashSet<NodeId>();
s.getNodeIdList().forEach(n -> {
nodeIdSet.add(new NodeId(n));
});
masters.add(nodeIdSet);
});
Annotations annots = AnnotationsTranslator.asAnnotations(region.getAnnotations());
return new DefaultRegion(id, name, type, annots, masters);
}
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();
}
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 removeDevices.
/**
* Removes the specified collection of devices from the region.
*
* @param regionId region identifier
* @param stream deviceIds JSON stream
* @return 204 NO CONTENT
* @onos.rsModel RegionDeviceIds
*/
@DELETE
@Path("{regionId}/devices")
@Consumes(MediaType.APPLICATION_JSON)
public Response removeDevices(@PathParam("regionId") String regionId, InputStream stream) {
RegionId rid = RegionId.regionId(regionId);
Region region = nullIsNotFound(regionService.getRegion(rid), REGION_NOT_FOUND + rid);
try {
regionAdminService.removeDevices(rid, extractDeviceIds(stream));
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
return Response.noContent().build();
}
Aggregations