@Override
    public ObjectNode encode(KubevirtRouter router, CodecContext context) {
        checkNotNull(router, "Kubevirt router cannot be null");

        ObjectNode result = context.mapper().createObjectNode()
                .put(NAME, router.name())
                .put(ENABLE_SNAT, router.enableSnat())
                .put(MAC_ADDRESS, router.mac().toString());

        if (router.description() != null) {
            result.put(DESCRIPTION, router.description());
        }

        if (router.internal() != null && !router.internal().isEmpty()) {
            ArrayNode internal = context.mapper().createArrayNode();
            router.internal().forEach(internal::add);

            result.set(INTERNAL, internal);
        }

        if (router.external() != null && !router.external().isEmpty()) {
            ArrayNode external = context.mapper().createArrayNode();
            router.external().forEach((k, v) -> {
                ObjectNode item = context.mapper().createObjectNode();
                item.put(IP_ADDRESS, k);
                item.put(NETWORK, v);
                external.add(item);
            });
            result.set(EXTERNAL, external);
        }

        if (router.peerRouter() != null) {
            ObjectNode peerRouter = context.mapper().createObjectNode();
            peerRouter.put(IP_ADDRESS, router.peerRouter().ipAddress().toString());

            if (router.peerRouter().macAddress() != null) {
                peerRouter.put(MAC_ADDRESS, router.peerRouter().macAddress().toString());
            }

            result.set(PEER_ROUTER, peerRouter);
        }

        if (router.electedGateway() != null) {
            result.put(GATEWAY, router.electedGateway());
        }

        return result;
    }
@Test
    public void testKubevirtRouterEncode() {
        KubevirtPeerRouter peerRouter = new KubevirtPeerRouter(IpAddress.valueOf("10.10.10.10"),
                MacAddress.valueOf("11:22:33:44:55:66"));

        KubevirtRouter router = DefaultKubevirtRouter.builder()
                .name("router-1")
                .enableSnat(true)
                .mac(MacAddress.valueOf("11:22:33:44:55:66"))
                .description("router-1")
                .internal(ImmutableSet.of("vlan-1"))
                .external(ImmutableMap.of("10.10.10.20", "flat-1"))
                .peerRouter(peerRouter)
                .electedGateway("gatewayNode")
                .build();

        ObjectNode routerJson = kubevirtRouterCodec.encode(router, context);
        assertThat(routerJson, matchesKubevirtRouter(router));
    }