Search in sources :

Example 1 with Region

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());
}
Also used : DeviceId(org.onosproject.net.DeviceId) Region(org.onosproject.net.region.Region) Test(org.junit.Test)

Example 2 with Region

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);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) 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) RegionId(org.onosproject.net.region.RegionId) HashSet(java.util.HashSet)

Example 3 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)

Example 4 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 5 with Region

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

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