Search in sources :

Example 1 with DefaultRegion

use of org.onosproject.net.region.DefaultRegion 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 2 with DefaultRegion

use of org.onosproject.net.region.DefaultRegion 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 3 with DefaultRegion

use of org.onosproject.net.region.DefaultRegion 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

Set (java.util.Set)3 NodeId (org.onosproject.cluster.NodeId)3 Annotations (org.onosproject.net.Annotations)3 DefaultRegion (org.onosproject.net.region.DefaultRegion)3 Region (org.onosproject.net.region.Region)3 RegionId (org.onosproject.net.region.RegionId)3 ArrayList (java.util.ArrayList)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 RegionJsonMatcher.matchesRegion (org.onosproject.codec.impl.RegionJsonMatcher.matchesRegion)1 DefaultAnnotations (org.onosproject.net.DefaultAnnotations)1