@Override
public ObjectNode encode(FlowRule flowRule, CodecContext context) {
checkNotNull(flowRule, "Flow rule cannot be null");
CoreService service = context.getService(CoreService.class);
ApplicationId appId = service.getAppId(flowRule.appId());
String strAppId = (appId == null) ? "<none>" : appId.name();
final ObjectNode result = context.mapper().createObjectNode()
.put(ID, Long.toString(flowRule.id().value()))
.put(APP_ID, strAppId)
.put(PRIORITY, flowRule.priority())
.put(TIMEOUT, flowRule.timeout())
.put(IS_PERMANENT, flowRule.isPermanent())
.put(DEVICE_ID, flowRule.deviceId().toString())
.put(TABLE_ID, flowRule.tableId())
.put(TABLE_NAME, flowRule.table().toString());
if (flowRule.treatment() != null) {
final JsonCodec<TrafficTreatment> treatmentCodec =
context.codec(TrafficTreatment.class);
result.set(TREATMENT, treatmentCodec.encode(flowRule.treatment(), context));
}
if (flowRule.selector() != null) {
final JsonCodec<TrafficSelector> selectorCodec =
context.codec(TrafficSelector.class);
result.set(SELECTOR, selectorCodec.encode(flowRule.selector(), context));
}
return result;
}