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