@Test public void codecEncodeTest() { GroupBucket bucket1 = DefaultGroupBucket .createSelectGroupBucket(DefaultTrafficTreatment.emptyTreatment()); GroupBucket bucket2 = DefaultGroupBucket .createIndirectGroupBucket(DefaultTrafficTreatment.emptyTreatment()); GroupBuckets buckets = new GroupBuckets(ImmutableList.of(bucket1, bucket2)); DefaultGroup group = new DefaultGroup( new DefaultGroupId(1), NetTestTools.did("d1"), GroupDescription.Type.INDIRECT, buckets); MockCodecContext context = new MockCodecContext(); GroupCodec codec = new GroupCodec(); ObjectNode groupJson = codec.encode(group, context); assertThat(groupJson, matchesGroup(group)); }
@Override public ObjectNode encode(Group group, CodecContext context) { checkNotNull(group, "Group cannot be null"); ObjectNode result = context.mapper().createObjectNode() .put(ID, group.id().toString()) .put(STATE, group.state().toString()) .put(LIFE, group.life()) .put(PACKETS, group.packets()) .put(BYTES, group.bytes()) .put(REFERENCE_COUNT, group.referenceCount()) .put(TYPE, group.type().toString()) .put(DEVICE_ID, group.deviceId().toString()); if (group.appId() != null) { result.put(APP_ID, group.appId().toString()); } if (group.appCookie() != null) { result.put(APP_COOKIE, group.appCookie().toString()); } if (group.givenGroupId() != null) { result.put(GIVEN_GROUP_ID, group.givenGroupId()); } ArrayNode buckets = context.mapper().createArrayNode(); group.buckets().buckets().forEach(bucket -> { ObjectNode bucketJson = context.codec(GroupBucket.class).encode(bucket, context); buckets.add(bucketJson); }); result.set(BUCKETS, buckets); return result; }
GroupCodec extends JsonCodec<Group> { @Override public ObjectNode encode(Group group, CodecContext context) { checkNotNull(group, "Group cannot be null"); ObjectNode result = context.mapper().createObjectNode() .put(ID, group.id().toString()) .put(STATE, group.state().toString()) .put(LIFE, group.life()) .put(PACKETS, group.packets()) .put(BYTES, group.bytes()) .put(REFERENCE_COUNT, group.referenceCount()) .put(TYPE, group.type().toString()) .put(DEVICE_ID, group.deviceId().toString()); if (group.appId() != null) { result.put(APP_ID, group.appId().toString()); } if (group.appCookie() != null) { result.put(APP_COOKIE, group.appCookie().toString()); } if (group.givenGroupId() != null) { result.put(GIVEN_GROUP_ID, group.givenGroupId()); } ArrayNode buckets = context.mapper().createArrayNode(); group.buckets().buckets().forEach(bucket -> { ObjectNode bucketJson = context.codec(GroupBucket.class).encode(bucket, context); buckets.add(bucketJson); }); result.set(BUCKETS, buckets); return result; } }
GroupCodec extends JsonCodec<Group> { @Override public ObjectNode encode(Group group, CodecContext context) { checkNotNull(group, "Group cannot be null"); ObjectNode result = context.mapper().createObjectNode() .put(ID, group.id().toString()) .put(STATE, group.state().toString()) .put(LIFE, group.life()) .put(PACKETS, group.packets()) .put(BYTES, group.bytes()) .put(REFERENCE_COUNT, group.referenceCount()) .put(TYPE, group.type().toString()) .put(DEVICE_ID, group.deviceId().toString()); if (group.appId() != null) { result.put(APP_ID, group.appId().toString()); } if (group.appCookie() != null) { result.put(APP_COOKIE, group.appCookie().toString()); } if (group.givenGroupId() != null) { result.put(GIVEN_GROUP_ID, group.givenGroupId()); } ArrayNode buckets = context.mapper().createArrayNode(); group.buckets().buckets().forEach(bucket -> { ObjectNode bucketJson = context.codec(GroupBucket.class).encode(bucket, context); buckets.add(bucketJson); }); result.set(BUCKETS, buckets); return result; }  }
GroupCodec extends JsonCodec<Group> { @Override public ObjectNode encode(Group group, CodecContext context) { checkNotNull(group, "Group cannot be null"); ObjectNode result = context.mapper().createObjectNode() .put(ID, group.id().toString()) .put(STATE, group.state().toString()) .put(LIFE, group.life()) .put(PACKETS, group.packets()) .put(BYTES, group.bytes()) .put(REFERENCE_COUNT, group.referenceCount()) .put(TYPE, group.type().toString()) .put(DEVICE_ID, group.deviceId().toString()); if (group.appId() != null) { result.put(APP_ID, group.appId().toString()); } if (group.appCookie() != null) { result.put(APP_COOKIE, group.appCookie().toString()); } if (group.givenGroupId() != null) { result.put(GIVEN_GROUP_ID, group.givenGroupId()); } ArrayNode buckets = context.mapper().createArrayNode(); group.buckets().buckets().forEach(bucket -> { ObjectNode bucketJson = context.codec(GroupBucket.class).encode(bucket, context); buckets.add(bucketJson); }); result.set(BUCKETS, buckets); return result; }  @Override ObjectNode encode(Group group, CodecContext context); @Override Group decode(ObjectNode json, CodecContext context); }
GroupCodec extends JsonCodec<Group> { @Override public ObjectNode encode(Group group, CodecContext context) { checkNotNull(group, "Group cannot be null"); ObjectNode result = context.mapper().createObjectNode() .put(ID, group.id().toString()) .put(STATE, group.state().toString()) .put(LIFE, group.life()) .put(PACKETS, group.packets()) .put(BYTES, group.bytes()) .put(REFERENCE_COUNT, group.referenceCount()) .put(TYPE, group.type().toString()) .put(DEVICE_ID, group.deviceId().toString()); if (group.appId() != null) { result.put(APP_ID, group.appId().toString()); } if (group.appCookie() != null) { result.put(APP_COOKIE, group.appCookie().toString()); } if (group.givenGroupId() != null) { result.put(GIVEN_GROUP_ID, group.givenGroupId()); } ArrayNode buckets = context.mapper().createArrayNode(); group.buckets().buckets().forEach(bucket -> { ObjectNode bucketJson = context.codec(GroupBucket.class).encode(bucket, context); buckets.add(bucketJson); }); result.set(BUCKETS, buckets); return result; }  @Override ObjectNode encode(Group group, CodecContext context); @Override Group decode(ObjectNode json, CodecContext context); static final String REST_APP_ID; }