@Override
    public ObjectNode encode(KubevirtSecurityGroup sg, CodecContext context) {
        checkNotNull(sg, "Kubevirt Security Group cannot be null");

        ObjectNode result = context.mapper().createObjectNode()
                .put(ID, sg.id())
                .put(NAME, sg.name());

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

        if (sg.rules() != null && !sg.rules().isEmpty()) {
            ArrayNode rules = context.mapper().createArrayNode();
            sg.rules().forEach(rule -> {
                ObjectNode ruleJson = context.codec(
                        KubevirtSecurityGroupRule.class).encode(rule, context);
                rules.add(ruleJson);
            });
            result.set(RULES, rules);
        }

        return result;
    }
@Test
    public void testKubevirtSecurityGroupEncode() {
        KubevirtSecurityGroupRule rule = DefaultKubevirtSecurityGroupRule.builder()
                .id("sgr-1")
                .securityGroupId("sg-1")
                .direction("ingress")
                .etherType("IPv4")
                .portRangeMin(0)
                .portRangeMax(80)
                .protocol("tcp")
                .remoteIpPrefix(IpPrefix.valueOf("0.0.0.0/0"))
                .remoteGroupId("g-1")
                .build();

        KubevirtSecurityGroup sg = DefaultKubevirtSecurityGroup.builder()
                .id("sg-1")
                .name("sg")
                .description("example-sg")
                .rules(ImmutableSet.of(rule))
                .build();

        ObjectNode sgJson = kubevirtSecurityGroupCodec.encode(sg, context);
        assertThat(sgJson, matchesKubevirtSecurityGroup(sg));
    }