Search in sources :

Example 11 with KubevirtNetwork

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

the class KubevirtNetworkingUtil method getPorts.

/**
 * Obtains the kubevirt port from kubevirt VMI.
 *
 * @param nodeService kubevirt node service
 * @param networks set of existing kubevirt networks
 * @param resource  VMI definition
 * @return kubevirt ports attached to the VMI
 */
public static Set<KubevirtPort> getPorts(KubevirtNodeService nodeService, Set<KubevirtNetwork> networks, String resource) {
    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode json = mapper.readTree(resource);
        JsonNode statusJson = json.get(STATUS);
        ArrayNode interfacesJson = (ArrayNode) statusJson.get(INTERFACES);
        String vmName = parseResourceName(resource);
        KubevirtPort.Builder builder = DefaultKubevirtPort.builder();
        String nodeName = parseVmiNodeName(resource);
        if (nodeName != null && nodeService.node(nodeName) != null) {
            builder.deviceId(nodeService.node(nodeName).intgBridge());
        }
        if (interfacesJson == null) {
            return ImmutableSet.of();
        }
        Set<KubevirtPort> ports = new HashSet<>();
        for (JsonNode interfaceJson : interfacesJson) {
            JsonNode jsonName = interfaceJson.get(NAME);
            // interface, we skip inspect this interface
            if (jsonName == null) {
                continue;
            }
            String name = jsonName.asText();
            KubevirtNetwork network = networks.stream().filter(n -> (NETWORK_PREFIX + n.name()).equals(name) || (n.name() + "-net").equals(name)).findAny().orElse(null);
            if (network != null) {
                // FIXME: we do not update IP address, as learning IP address
                // requires much more time due to the lag from VM agent
                String mac = interfaceJson.get(MAC).asText();
                builder.vmName(vmName).macAddress(MacAddress.valueOf(mac)).networkId(network.networkId());
                ports.add(builder.build());
            }
        }
        return ports;
    } catch (IOException e) {
        log.error("Failed to parse port info from VMI object", e);
    }
    return ImmutableSet.of();
}
Also used : KubevirtNetwork(org.onosproject.kubevirtnetworking.api.KubevirtNetwork) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) KubevirtPort(org.onosproject.kubevirtnetworking.api.KubevirtPort) DefaultKubevirtPort(org.onosproject.kubevirtnetworking.api.DefaultKubevirtPort) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet)

Example 12 with KubevirtNetwork

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

the class KubevirtNetworkWebResource method hasNetwork.

/**
 * Checks whether the network exists with given network id.
 *
 * @param id network identifier
 * @return 200 OK with true/false result
 */
@GET
@Path("exist/{id}")
public Response hasNetwork(@PathParam("id") String id) {
    log.trace(String.format(MESSAGE, "QUERY " + id));
    KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class);
    ObjectNode root = mapper().createObjectNode();
    KubevirtNetwork network = service.network(id);
    if (network == null) {
        root.put(RESULT, false);
    } else {
        root.put(RESULT, true);
    }
    return Response.ok(root).build();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) KubevirtNetwork(org.onosproject.kubevirtnetworking.api.KubevirtNetwork) KubevirtNetworkAdminService(org.onosproject.kubevirtnetworking.api.KubevirtNetworkAdminService) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 13 with KubevirtNetwork

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

the class KubevirtNetworkWebResource method updateNetwork.

/**
 * Updates the network with the specified identifier.
 *
 * @param id    network identifier
 * @param input network JSON input stream
 * @return 200 OK with the updated network, 400 BAD_REQUEST if the requested
 * network does not exist
 * @onos.rsModel KubevirtNetwork
 */
@PUT
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response updateNetwork(@PathParam("id") String id, InputStream input) {
    log.trace(String.format(MESSAGE, "UPDATED"));
    KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class);
    try {
        ObjectNode jsonTree = readTreeFromStream(mapper(), input);
        JsonNode specifiedNetworkId = jsonTree.get("networkId");
        if (specifiedNetworkId != null && !specifiedNetworkId.asText().equals(id)) {
            throw new IllegalArgumentException(NETWORK_INVALID);
        }
        final KubevirtNetwork network = codec(KubevirtNetwork.class).decode(jsonTree, this);
        service.updateNetwork(network);
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
    return Response.ok().build();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) KubevirtNetwork(org.onosproject.kubevirtnetworking.api.KubevirtNetwork) KubevirtNetworkAdminService(org.onosproject.kubevirtnetworking.api.KubevirtNetworkAdminService) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 14 with KubevirtNetwork

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

the class KubevirtNetworkWebResource method getNetworkById.

/**
 * Returns the network with the specified identifier.
 *
 * @param id network identifier
 * @return 200 OK with a network, 404 not found
 * @onos.rsModel KubevirtNetwork
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{id}")
public Response getNetworkById(@PathParam("id") String id) {
    KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class);
    final KubevirtNetwork network = nullIsNotFound(service.network(id), NETWORK_NOT_FOUND + id);
    return ok(codec(KubevirtNetwork.class).encode(network, this)).build();
}
Also used : KubevirtNetwork(org.onosproject.kubevirtnetworking.api.KubevirtNetwork) KubevirtNetworkAdminService(org.onosproject.kubevirtnetworking.api.KubevirtNetworkAdminService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 15 with KubevirtNetwork

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

the class KubevirtNetworkCodecTest method testKubevirtNetworkDecode.

/**
 * Tests the kubevirt network decoding.
 *
 * @throws IOException io exception
 */
@Test
public void testKubevirtNetworkDecode() throws IOException {
    KubevirtNetwork network = getKubevirtNetwork("KubevirtNetwork.json");
    assertThat(network.networkId(), is("network-1"));
    assertThat(network.name(), is("network-1"));
    assertThat(network.type().name(), is("FLAT"));
    assertThat(network.cidr(), is("10.10.0.0/24"));
    assertThat(network.gatewayIp().toString(), is("10.10.0.1"));
    assertThat(network.defaultRoute(), is(true));
    assertThat(network.ipPool().start().toString(), is("10.10.10.100"));
    assertThat(network.ipPool().end().toString(), is("10.10.10.200"));
    assertThat(network.dnses().size(), is(1));
    KubevirtHostRoute route = network.hostRoutes().stream().findFirst().orElse(null);
    assertThat(route, is(new KubevirtHostRoute(IpPrefix.valueOf("10.10.10.0/24"), IpAddress.valueOf("10.10.10.1"))));
}
Also used : KubevirtNetwork(org.onosproject.kubevirtnetworking.api.KubevirtNetwork) DefaultKubevirtNetwork(org.onosproject.kubevirtnetworking.api.DefaultKubevirtNetwork) KubevirtNetworkJsonMatcher.matchesKubevirtNetwork(org.onosproject.kubevirtnetworking.codec.KubevirtNetworkJsonMatcher.matchesKubevirtNetwork) KubevirtHostRoute(org.onosproject.kubevirtnetworking.api.KubevirtHostRoute) Test(org.junit.Test)

Aggregations

KubevirtNetwork (org.onosproject.kubevirtnetworking.api.KubevirtNetwork)25 DefaultTrafficSelector (org.onosproject.net.flow.DefaultTrafficSelector)6 DefaultTrafficTreatment (org.onosproject.net.flow.DefaultTrafficTreatment)6 TrafficSelector (org.onosproject.net.flow.TrafficSelector)6 TrafficTreatment (org.onosproject.net.flow.TrafficTreatment)6 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)5 KubevirtNetworkAdminService (org.onosproject.kubevirtnetworking.api.KubevirtNetworkAdminService)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 Produces (javax.ws.rs.Produces)4 DefaultKubevirtNetwork (org.onosproject.kubevirtnetworking.api.DefaultKubevirtNetwork)4 KubevirtPort (org.onosproject.kubevirtnetworking.api.KubevirtPort)4 KubevirtNode (org.onosproject.kubevirtnode.api.KubevirtNode)4 IOException (java.io.IOException)3 GET (javax.ws.rs.GET)3 Path (javax.ws.rs.Path)3 IpAddress (org.onlab.packet.IpAddress)3 KubevirtHostRoute (org.onosproject.kubevirtnetworking.api.KubevirtHostRoute)3 KubevirtIpPool (org.onosproject.kubevirtnetworking.api.KubevirtIpPool)3 KubevirtNetworkJsonMatcher.matchesKubevirtNetwork (org.onosproject.kubevirtnetworking.codec.KubevirtNetworkJsonMatcher.matchesKubevirtNetwork)3 KubevirtNetworkingUtil.getRouterForKubevirtNetwork (org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.getRouterForKubevirtNetwork)3