Search in sources :

Example 16 with KubevirtNetwork

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

the class KubevirtNetworkCodecTest method testKubevirtNetworkEncode.

/**
 * Tests the kubevirt network encoding.
 */
@Test
public void testKubevirtNetworkEncode() {
    KubevirtHostRoute hostRoute1 = new KubevirtHostRoute(IpPrefix.valueOf("10.10.10.0/24"), IpAddress.valueOf("20.20.20.1"));
    KubevirtHostRoute hostRoute2 = new KubevirtHostRoute(IpPrefix.valueOf("20.20.20.0/24"), IpAddress.valueOf("10.10.10.1"));
    KubevirtIpPool ipPool = new KubevirtIpPool(IpAddress.valueOf("10.10.10.100"), IpAddress.valueOf("10.10.10.200"));
    KubevirtNetwork network = DefaultKubevirtNetwork.builder().networkId("net-1").name("net-1").type(KubevirtNetwork.Type.FLAT).gatewayIp(IpAddress.valueOf("10.10.10.1")).defaultRoute(true).mtu(1500).cidr("10.10.10.0/24").hostRoutes(ImmutableSet.of(hostRoute1, hostRoute2)).ipPool(ipPool).dnses(ImmutableSet.of(IpAddress.valueOf("8.8.8.8"))).build();
    ObjectNode networkJson = kubevirtNetworkCodec.encode(network, context);
    assertThat(networkJson, matchesKubevirtNetwork(network));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) 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) KubevirtIpPool(org.onosproject.kubevirtnetworking.api.KubevirtIpPool) Test(org.junit.Test)

Example 17 with KubevirtNetwork

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

the class KubevirtNetworkCodecTest method getKubevirtNetwork.

private KubevirtNetwork getKubevirtNetwork(String resourceName) throws IOException {
    InputStream jsonStream = KubevirtNetworkCodecTest.class.getResourceAsStream(resourceName);
    JsonNode json = context.mapper().readTree(jsonStream);
    MatcherAssert.assertThat(json, notNullValue());
    KubevirtNetwork network = kubevirtNetworkCodec.decode((ObjectNode) json, context);
    assertThat(network, notNullValue());
    return network;
}
Also used : KubevirtNetwork(org.onosproject.kubevirtnetworking.api.KubevirtNetwork) DefaultKubevirtNetwork(org.onosproject.kubevirtnetworking.api.DefaultKubevirtNetwork) KubevirtNetworkJsonMatcher.matchesKubevirtNetwork(org.onosproject.kubevirtnetworking.codec.KubevirtNetworkJsonMatcher.matchesKubevirtNetwork) InputStream(java.io.InputStream) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 18 with KubevirtNetwork

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

the class KubevirtNetworkWebResource method createNetwork.

/**
 * Creates a network from the JSON input stream.
 *
 * @param input network JSON input stream
 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
 * is invalid or duplicated network already exists
 * @onos.rsModel KubevirtNetwork
 */
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createNetwork(InputStream input) {
    log.trace(String.format(MESSAGE, "CREATE"));
    KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class);
    URI location;
    try {
        ObjectNode jsonTree = readTreeFromStream(mapper(), input);
        final KubevirtNetwork network = codec(KubevirtNetwork.class).decode(jsonTree, this);
        service.createNetwork(network);
        location = new URI(network.networkId());
    } catch (IOException | URISyntaxException e) {
        throw new IllegalArgumentException(e);
    }
    return Response.created(location).build();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) KubevirtNetwork(org.onosproject.kubevirtnetworking.api.KubevirtNetwork) KubevirtNetworkAdminService(org.onosproject.kubevirtnetworking.api.KubevirtNetworkAdminService) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 19 with KubevirtNetwork

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

the class KubevirtNetworkWebResource method getNetworks.

/**
 * Returns set of all networks.
 *
 * @return 200 OK with set of all networks
 * @onos.rsModel KubevirtNetworks
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getNetworks() {
    KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class);
    final Iterable<KubevirtNetwork> networks = service.networks();
    return ok(encodeArray(KubevirtNetwork.class, "networks", networks)).build();
}
Also used : KubevirtNetwork(org.onosproject.kubevirtnetworking.api.KubevirtNetwork) KubevirtNetworkAdminService(org.onosproject.kubevirtnetworking.api.KubevirtNetworkAdminService) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 20 with KubevirtNetwork

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

the class KubevirtNetworkCodec method decode.

@Override
public KubevirtNetwork decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }
    String networkId = nullIsIllegal(json.get(NETWORK_ID).asText(), NETWORK_ID + MISSING_MESSAGE);
    String type = nullIsIllegal(json.get(TYPE).asText(), TYPE + MISSING_MESSAGE);
    String name = nullIsIllegal(json.get(NAME).asText(), NAME + MISSING_MESSAGE);
    Integer mtu = nullIsIllegal(json.get(MTU).asInt(), MTU + MISSING_MESSAGE);
    String gatewayIp = nullIsIllegal(json.get(GATEWAY_IP).asText(), GATEWAY_IP + MISSING_MESSAGE);
    boolean defaultRoute = nullIsIllegal(json.get(DEFAULT_ROUTE).asBoolean(), DEFAULT_ROUTE + MISSING_MESSAGE);
    String cidr = nullIsIllegal(json.get(CIDR).asText(), CIDR + MISSING_MESSAGE);
    KubevirtNetwork.Builder networkBuilder = DefaultKubevirtNetwork.builder().networkId(networkId).type(KubevirtNetwork.Type.valueOf(type)).name(name).mtu(mtu).gatewayIp(IpAddress.valueOf(gatewayIp)).defaultRoute(defaultRoute).cidr(cidr);
    if (!type.equals(KubevirtNetwork.Type.FLAT.name())) {
        JsonNode segmentIdJson = json.get(SEGMENT_ID);
        if (segmentIdJson != null) {
            networkBuilder.segmentId(segmentIdJson.asText());
        }
    }
    JsonNode ipPoolJson = json.get(IP_POOL);
    if (ipPoolJson != null) {
        final JsonCodec<KubevirtIpPool> ipPoolCodec = context.codec(KubevirtIpPool.class);
        networkBuilder.ipPool(ipPoolCodec.decode((ObjectNode) ipPoolJson.deepCopy(), context));
    }
    // parse host routes
    Set<KubevirtHostRoute> hostRoutes = new HashSet<>();
    JsonNode hostRoutesJson = json.get(HOST_ROUTES);
    if (hostRoutesJson != null) {
        final JsonCodec<KubevirtHostRoute> hostRouteCodec = context.codec(KubevirtHostRoute.class);
        IntStream.range(0, hostRoutesJson.size()).forEach(i -> {
            ObjectNode routeJson = get(hostRoutesJson, i);
            hostRoutes.add(hostRouteCodec.decode(routeJson, context));
        });
    }
    networkBuilder.hostRoutes(hostRoutes);
    // parse DNSes
    Set<IpAddress> dnses = new HashSet<>();
    JsonNode dnsesJson = json.get(DNSES);
    if (dnsesJson != null) {
        for (int i = 0; i < dnsesJson.size(); i++) {
            JsonNode dnsJson = dnsesJson.get(i);
            if (dnsJson != null) {
                dnses.add(IpAddress.valueOf(dnsJson.asText()));
            }
        }
    }
    networkBuilder.dnses(dnses);
    log.trace("Network is {}", networkBuilder.build().toString());
    return networkBuilder.build();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DefaultKubevirtNetwork(org.onosproject.kubevirtnetworking.api.DefaultKubevirtNetwork) KubevirtNetwork(org.onosproject.kubevirtnetworking.api.KubevirtNetwork) JsonNode(com.fasterxml.jackson.databind.JsonNode) KubevirtHostRoute(org.onosproject.kubevirtnetworking.api.KubevirtHostRoute) IpAddress(org.onlab.packet.IpAddress) KubevirtIpPool(org.onosproject.kubevirtnetworking.api.KubevirtIpPool) HashSet(java.util.HashSet)

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