Search in sources :

Example 6 with KubevirtRouter

use of org.onosproject.kubevirtnetworking.api.KubevirtRouter in project onos by opennetworkinglab.

the class KubevirtNetworkingUtil method getRouterSnatIpAddress.

/**
 * Returns the snat ip address with specified router.
 *
 * @param routerService     router service
 * @param internalNetworkId internal network id which is associated with the router
 * @return snat ip address if exist, null otherwise
 */
public static IpAddress getRouterSnatIpAddress(KubevirtRouterService routerService, String internalNetworkId) {
    KubevirtRouter router = routerService.routers().stream().filter(r -> r.internal().contains(internalNetworkId)).findAny().orElse(null);
    if (router == null) {
        return null;
    }
    String routerSnatIp = router.external().keySet().stream().findAny().orElse(null);
    if (routerSnatIp == null) {
        return null;
    }
    return Ip4Address.valueOf(routerSnatIp);
}
Also used : KubevirtRouter(org.onosproject.kubevirtnetworking.api.KubevirtRouter)

Example 7 with KubevirtRouter

use of org.onosproject.kubevirtnetworking.api.KubevirtRouter in project onos by opennetworkinglab.

the class KubevirtRouterCodec method decode.

@Override
public KubevirtRouter decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }
    String name = nullIsIllegal(json.get(NAME).asText(), NAME + MISSING_MESSAGE);
    String vrouterMac = nullIsIllegal(json.get(MAC_ADDRESS).asText(), MAC_ADDRESS + MISSING_MESSAGE);
    KubevirtRouter.Builder builder = DefaultKubevirtRouter.builder().name(name).mac(MacAddress.valueOf(vrouterMac));
    JsonNode descriptionJson = json.get(DESCRIPTION);
    if (descriptionJson != null) {
        builder.description(descriptionJson.asText());
    }
    JsonNode enableSnatJson = json.get(ENABLE_SNAT);
    if (enableSnatJson != null) {
        builder.enableSnat(enableSnatJson.asBoolean());
    }
    JsonNode electedGwJson = json.get(GATEWAY);
    if (electedGwJson != null) {
        builder.electedGateway(electedGwJson.asText());
    }
    ArrayNode internalJson = (ArrayNode) json.get(INTERNAL);
    Set<String> internal = new HashSet<>();
    if (internalJson != null) {
        for (int i = 0; i < internalJson.size(); i++) {
            internal.add(internalJson.get(i).asText());
        }
        builder.internal(internal);
    }
    ObjectNode externalJson = (ObjectNode) json.get(EXTERNAL);
    if (externalJson != null) {
        Map<String, String> external = ImmutableMap.of(externalJson.get(IP_ADDRESS).asText(), externalJson.get(NETWORK).asText());
        builder.external(external);
    }
    ObjectNode peerRouterJson = (ObjectNode) json.get(PEER_ROUTER);
    if (peerRouterJson != null) {
        JsonNode ipJson = peerRouterJson.get(IP_ADDRESS);
        JsonNode macJson = peerRouterJson.get(MAC_ADDRESS);
        if (ipJson != null && macJson != null) {
            IpAddress ip = IpAddress.valueOf(ipJson.asText());
            MacAddress mac = MacAddress.valueOf(macJson.asText());
            KubevirtPeerRouter peer = new KubevirtPeerRouter(ip, mac);
            builder.peerRouter(peer);
        }
        // if mac address is not specified, we will not add mac address to peer router
        if (ipJson != null && macJson == null) {
            IpAddress ip = IpAddress.valueOf(ipJson.asText());
            KubevirtPeerRouter peer = new KubevirtPeerRouter(ip, null);
            builder.peerRouter(peer);
        }
    }
    return builder.build();
}
Also used : KubevirtPeerRouter(org.onosproject.kubevirtnetworking.api.KubevirtPeerRouter) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) MacAddress(org.onlab.packet.MacAddress) KubevirtRouter(org.onosproject.kubevirtnetworking.api.KubevirtRouter) DefaultKubevirtRouter(org.onosproject.kubevirtnetworking.api.DefaultKubevirtRouter) IpAddress(org.onlab.packet.IpAddress) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) HashSet(java.util.HashSet)

Example 8 with KubevirtRouter

use of org.onosproject.kubevirtnetworking.api.KubevirtRouter in project onos by opennetworkinglab.

the class KubevirtRouterCodecTest method testKubevirtRouterEncode.

/**
 * Tests the kubevirt router encoding.
 */
@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));
}
Also used : KubevirtPeerRouter(org.onosproject.kubevirtnetworking.api.KubevirtPeerRouter) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) KubevirtRouter(org.onosproject.kubevirtnetworking.api.KubevirtRouter) DefaultKubevirtRouter(org.onosproject.kubevirtnetworking.api.DefaultKubevirtRouter) KubevirtRouterJsonMatcher.matchesKubevirtRouter(org.onosproject.kubevirtnetworking.codec.KubevirtRouterJsonMatcher.matchesKubevirtRouter) Test(org.junit.Test)

Example 9 with KubevirtRouter

use of org.onosproject.kubevirtnetworking.api.KubevirtRouter in project onos by opennetworkinglab.

the class KubevirtRouterCodecTest method getKubevirtRouter.

private KubevirtRouter getKubevirtRouter(String resourceName) throws IOException {
    InputStream jsonStream = KubevirtRouterCodecTest.class.getResourceAsStream(resourceName);
    JsonNode json = context.mapper().readTree(jsonStream);
    MatcherAssert.assertThat(json, notNullValue());
    KubevirtRouter router = kubevirtRouterCodec.decode((ObjectNode) json, context);
    assertThat(router, notNullValue());
    return router;
}
Also used : InputStream(java.io.InputStream) KubevirtRouter(org.onosproject.kubevirtnetworking.api.KubevirtRouter) DefaultKubevirtRouter(org.onosproject.kubevirtnetworking.api.DefaultKubevirtRouter) KubevirtRouterJsonMatcher.matchesKubevirtRouter(org.onosproject.kubevirtnetworking.codec.KubevirtRouterJsonMatcher.matchesKubevirtRouter) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 10 with KubevirtRouter

use of org.onosproject.kubevirtnetworking.api.KubevirtRouter in project onos by opennetworkinglab.

the class KubevirtRouterCodecTest method testKubevirtRouterDecode.

@Test
public void testKubevirtRouterDecode() throws IOException {
    KubevirtRouter router = getKubevirtRouter("KubevirtRouter.json");
    assertEquals("router-1", router.name());
    assertEquals("Example Virtual Router", router.description());
    assertTrue(router.enableSnat());
    assertEquals("11:22:33:44:55:66", router.mac().toString());
    assertEquals("192.168.10.5", router.external().keySet().stream().findAny().orElse(null));
    assertEquals("external-network", router.external().get("192.168.10.5"));
    assertTrue(router.internal().contains("vxlan-network-1"));
    assertTrue(router.internal().contains("vxlan-network-2"));
    assertEquals("192.168.10.1", router.peerRouter().ipAddress().toString());
    assertEquals("gatewayNode", router.electedGateway());
}
Also used : KubevirtRouter(org.onosproject.kubevirtnetworking.api.KubevirtRouter) DefaultKubevirtRouter(org.onosproject.kubevirtnetworking.api.DefaultKubevirtRouter) KubevirtRouterJsonMatcher.matchesKubevirtRouter(org.onosproject.kubevirtnetworking.codec.KubevirtRouterJsonMatcher.matchesKubevirtRouter) Test(org.junit.Test)

Aggregations

KubevirtRouter (org.onosproject.kubevirtnetworking.api.KubevirtRouter)13 DefaultKubevirtRouter (org.onosproject.kubevirtnetworking.api.DefaultKubevirtRouter)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 KubevirtPeerRouter (org.onosproject.kubevirtnetworking.api.KubevirtPeerRouter)3 KubevirtRouterJsonMatcher.matchesKubevirtRouter (org.onosproject.kubevirtnetworking.codec.KubevirtRouterJsonMatcher.matchesKubevirtRouter)3 KubevirtNode (org.onosproject.kubevirtnode.api.KubevirtNode)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 Test (org.junit.Test)2 KubevirtRouterService (org.onosproject.kubevirtnetworking.api.KubevirtRouterService)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashSet (java.util.HashSet)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 IpAddress (org.onlab.packet.IpAddress)1 MacAddress (org.onlab.packet.MacAddress)1 KubevirtNetworkingUtil.genFormatString (org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.genFormatString)1