Search in sources :

Example 1 with KubevirtRouter

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

the class KubevirtListRouterCommand method doExecute.

@Override
protected void doExecute() throws Exception {
    KubevirtRouterService service = get(KubevirtRouterService.class);
    List<KubevirtRouter> routers = Lists.newArrayList(service.routers());
    routers.sort(Comparator.comparing(KubevirtRouter::name));
    String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH, CLI_FLAG_LENGTH, CLI_IP_ADDRESSES_LENGTH, CLI_IP_ADDRESS_LENGTH, CLI_NAME_LENGTH));
    if (outputJson()) {
        print("%s", json(routers));
    } else {
        print(format, "Name", "SNAT", "Internal", "External", "GatewayNode");
        for (KubevirtRouter router : routers) {
            Set<String> internalNetworks = router.internal();
            String externalNetwork = router.external().values().stream().findAny().orElse(null);
            String internal = internalNetworks.size() == 0 ? "[]" : internalNetworks.toString();
            String external = externalNetwork == null ? "[]" : externalNetwork;
            String gwNode = router.electedGateway() == null ? "N/A" : router.electedGateway();
            print(format, StringUtils.substring(router.name(), 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH), StringUtils.substring(String.valueOf(router.enableSnat()), 0, CLI_FLAG_LENGTH - CLI_MARGIN_LENGTH), StringUtils.substring(internal, 0, CLI_IP_ADDRESSES_LENGTH - CLI_MARGIN_LENGTH), StringUtils.substring(external, 0, CLI_IP_ADDRESS_LENGTH - CLI_MARGIN_LENGTH), StringUtils.substring(gwNode, 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH));
        }
    }
}
Also used : KubevirtRouter(org.onosproject.kubevirtnetworking.api.KubevirtRouter) KubevirtRouterService(org.onosproject.kubevirtnetworking.api.KubevirtRouterService) KubevirtNetworkingUtil.genFormatString(org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.genFormatString)

Example 2 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 3 with KubevirtRouter

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

the class KubevirtNetworkHandler method setGatewayRuleToWorkerNodeWhenNodeCreated.

private void setGatewayRuleToWorkerNodeWhenNodeCreated(KubevirtNode node, KubevirtNetwork network) {
    KubevirtRouter router = getRouterForKubevirtNetwork(kubevirtRouterService, network);
    if (router == null) {
        return;
    }
    KubevirtNode electedGw = gatewayNodeForSpecifiedRouter(nodeService, router);
    if (electedGw == null) {
        return;
    }
    setDefaultGatewayRuleToWorkerNodeTunBridge(router, network, electedGw.intgBridge(), node, true);
}
Also used : KubevirtNode(org.onosproject.kubevirtnode.api.KubevirtNode) KubevirtRouter(org.onosproject.kubevirtnetworking.api.KubevirtRouter)

Example 4 with KubevirtRouter

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

the class KubevirtNetworkHandler method setGatewayArpRulesForTenantNetwork.

private void setGatewayArpRulesForTenantNetwork(KubevirtNode node, KubevirtNetwork network) {
    KubevirtRouter router = getRouterForKubevirtNetwork(kubevirtRouterService, network);
    if (router == null) {
        return;
    }
    KubevirtNode electedGw = gatewayNodeForSpecifiedRouter(nodeService, router);
    if (electedGw == null) {
        return;
    }
    setGatewayArpRuleForTenantInternalNetwork(router, network, TENANT_ARP_TABLE, electedGw.intgBridge(), network.tenantDeviceId(node.hostname()), true);
}
Also used : KubevirtNode(org.onosproject.kubevirtnode.api.KubevirtNode) KubevirtRouter(org.onosproject.kubevirtnetworking.api.KubevirtRouter)

Example 5 with KubevirtRouter

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

the class KubevirtRouterWatcher method parseKubevirtRouter.

private KubevirtRouter parseKubevirtRouter(String resource) {
    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode json = mapper.readTree(resource);
        ObjectNode spec = (ObjectNode) json.get("spec");
        KubevirtRouter router = codec(KubevirtRouter.class).decode(spec, this);
        KubevirtRouter existing = routerService.router(router.name());
        if (existing == null) {
            return router;
        } else {
            return router.updatedElectedGateway(existing.electedGateway());
        }
    } catch (IOException e) {
        log.error("Failed to parse kubevirt router object");
    }
    return null;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) KubevirtRouter(org.onosproject.kubevirtnetworking.api.KubevirtRouter) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

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