@Test public void testMeterEncode() { Band band1 = DefaultBand.builder() .ofType(Band.Type.DROP) .burstSize(10) .withRate(10).build(); Band band2 = DefaultBand.builder() .ofType(Band.Type.REMARK) .burstSize(10) .withRate(10) .dropPrecedence((short) 10).build(); Meter meter = DefaultMeter.builder() .fromApp(APP_ID) .withId(MeterId.meterId(1L)) .forDevice(NetTestTools.did("d1")) .withBands(ImmutableList.of(band1, band2)) .withUnit(Meter.Unit.KB_PER_SEC).build(); ObjectNode meterJson = meterCodec.encode(meter, context); assertThat(meterJson, matchesMeter(meter)); }
@Override public ObjectNode encode(Meter meter, CodecContext context) { checkNotNull(meter, "Meter cannot be null"); ObjectNode result = context.mapper().createObjectNode() .put(ID, meter.id().toString()) .put(LIFE, meter.life()) .put(PACKETS, meter.packetsSeen()) .put(BYTES, meter.bytesSeen()) .put(REFERENCE_COUNT, meter.referenceCount()) .put(UNIT, meter.unit().toString()) .put(BURST, meter.isBurst()) .put(DEVICE_ID, meter.deviceId().toString()); if (meter.appId() != null) { result.put(APP_ID, meter.appId().toString()); } if (meter.state() != null) { result.put(STATE, meter.state().toString()); } ArrayNode bands = context.mapper().createArrayNode(); meter.bands().forEach(band -> { ObjectNode bandJson = context.codec(Band.class).encode(band, context); bands.add(bandJson); }); result.set(BANDS, bands); return result; }
MeterCodec extends JsonCodec<Meter> { @Override public ObjectNode encode(Meter meter, CodecContext context) { checkNotNull(meter, "Meter cannot be null"); ObjectNode result = context.mapper().createObjectNode() .put(ID, meter.id().toString()) .put(LIFE, meter.life()) .put(PACKETS, meter.packetsSeen()) .put(BYTES, meter.bytesSeen()) .put(REFERENCE_COUNT, meter.referenceCount()) .put(UNIT, meter.unit().toString()) .put(BURST, meter.isBurst()) .put(DEVICE_ID, meter.deviceId().toString()); if (meter.appId() != null) { result.put(APP_ID, meter.appId().toString()); } if (meter.state() != null) { result.put(STATE, meter.state().toString()); } ArrayNode bands = context.mapper().createArrayNode(); meter.bands().forEach(band -> { ObjectNode bandJson = context.codec(Band.class).encode(band, context); bands.add(bandJson); }); result.set(BANDS, bands); return result; } }
MeterCodec extends JsonCodec<Meter> { @Override public ObjectNode encode(Meter meter, CodecContext context) { checkNotNull(meter, "Meter cannot be null"); ObjectNode result = context.mapper().createObjectNode() .put(ID, meter.id().toString()) .put(LIFE, meter.life()) .put(PACKETS, meter.packetsSeen()) .put(BYTES, meter.bytesSeen()) .put(REFERENCE_COUNT, meter.referenceCount()) .put(UNIT, meter.unit().toString()) .put(BURST, meter.isBurst()) .put(DEVICE_ID, meter.deviceId().toString()); if (meter.appId() != null) { result.put(APP_ID, meter.appId().toString()); } if (meter.state() != null) { result.put(STATE, meter.state().toString()); } ArrayNode bands = context.mapper().createArrayNode(); meter.bands().forEach(band -> { ObjectNode bandJson = context.codec(Band.class).encode(band, context); bands.add(bandJson); }); result.set(BANDS, bands); return result; }  }
MeterCodec extends JsonCodec<Meter> { @Override public ObjectNode encode(Meter meter, CodecContext context) { checkNotNull(meter, "Meter cannot be null"); ObjectNode result = context.mapper().createObjectNode() .put(ID, meter.id().toString()) .put(LIFE, meter.life()) .put(PACKETS, meter.packetsSeen()) .put(BYTES, meter.bytesSeen()) .put(REFERENCE_COUNT, meter.referenceCount()) .put(UNIT, meter.unit().toString()) .put(BURST, meter.isBurst()) .put(DEVICE_ID, meter.deviceId().toString()); if (meter.appId() != null) { result.put(APP_ID, meter.appId().toString()); } if (meter.state() != null) { result.put(STATE, meter.state().toString()); } ArrayNode bands = context.mapper().createArrayNode(); meter.bands().forEach(band -> { ObjectNode bandJson = context.codec(Band.class).encode(band, context); bands.add(bandJson); }); result.set(BANDS, bands); return result; }  @Override ObjectNode encode(Meter meter, CodecContext context); }
MeterCodec extends JsonCodec<Meter> { @Override public ObjectNode encode(Meter meter, CodecContext context) { checkNotNull(meter, "Meter cannot be null"); ObjectNode result = context.mapper().createObjectNode() .put(ID, meter.id().toString()) .put(LIFE, meter.life()) .put(PACKETS, meter.packetsSeen()) .put(BYTES, meter.bytesSeen()) .put(REFERENCE_COUNT, meter.referenceCount()) .put(UNIT, meter.unit().toString()) .put(BURST, meter.isBurst()) .put(DEVICE_ID, meter.deviceId().toString()); if (meter.appId() != null) { result.put(APP_ID, meter.appId().toString()); } if (meter.state() != null) { result.put(STATE, meter.state().toString()); } ArrayNode bands = context.mapper().createArrayNode(); meter.bands().forEach(band -> { ObjectNode bandJson = context.codec(Band.class).encode(band, context); bands.add(bandJson); }); result.set(BANDS, bands); return result; }  @Override ObjectNode encode(Meter meter, CodecContext context); static final String REST_APP_ID; }