Search in sources :

Example 1 with KubevirtHostRoute

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

the class KubevirtHostRouteCodec method decode.

@Override
public KubevirtHostRoute decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }
    String destination = nullIsIllegal(json.get(DESTINATION).asText(), DESTINATION + MISSING_MESSAGE);
    JsonNode nexthopJson = json.get(NEXTHOP);
    IpAddress nexthop = null;
    if (nexthopJson != null) {
        nexthop = IpAddress.valueOf(nexthopJson.asText());
    }
    return new KubevirtHostRoute(IpPrefix.valueOf(destination), nexthop);
}
Also used : KubevirtHostRoute(org.onosproject.kubevirtnetworking.api.KubevirtHostRoute) JsonNode(com.fasterxml.jackson.databind.JsonNode) IpAddress(org.onlab.packet.IpAddress)

Example 2 with KubevirtHostRoute

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

the class KubevirtNetworkJsonMatcher method matchesSafely.

@Override
protected boolean matchesSafely(JsonNode jsonNode, Description description) {
    // check network ID
    String jsonNetworkId = jsonNode.get(NETWORK_ID).asText();
    String networkId = network.networkId();
    if (!jsonNetworkId.equals(networkId)) {
        description.appendText("network ID was " + jsonNetworkId);
        return false;
    }
    // check type
    String jsonType = jsonNode.get(TYPE).asText();
    String type = network.type().name();
    if (!jsonType.equals(type)) {
        description.appendText("network type was " + jsonType);
        return false;
    }
    // check name
    String jsonName = jsonNode.get(NAME).asText();
    String name = network.name();
    if (!jsonName.equals(name)) {
        description.appendText("network name was " + jsonName);
        return false;
    }
    // check MTU
    int jsonMtu = jsonNode.get(MTU).asInt();
    int mtu = network.mtu();
    if (jsonMtu != mtu) {
        description.appendText("network MTU was " + jsonMtu);
        return false;
    }
    // check gateway IP
    String jsonGatewayIp = jsonNode.get(GATEWAY_IP).asText();
    String gatewayIp = network.gatewayIp().toString();
    if (!jsonGatewayIp.equals(gatewayIp)) {
        description.appendText("gateway IP was " + jsonGatewayIp);
        return false;
    }
    // check default route
    boolean jsonDefaultRoute = jsonNode.get(DEFAULT_ROUTE).asBoolean();
    boolean defaultRoute = network.defaultRoute();
    if (jsonDefaultRoute != defaultRoute) {
        description.appendText("Default route was " + jsonDefaultRoute);
        return false;
    }
    // check CIDR
    String jsonCidr = jsonNode.get(CIDR).asText();
    String cidr = network.cidr();
    if (!jsonCidr.equals(cidr)) {
        description.appendText("CIDR was " + jsonCidr);
        return false;
    }
    // check segment ID
    JsonNode jsonSegmentId = jsonNode.get(SEGMENT_ID);
    if (jsonSegmentId != null) {
        String segmentId = network.segmentId();
        if (!jsonSegmentId.asText().equals(segmentId)) {
            description.appendText("segment ID was " + jsonSegmentId.asText());
            return false;
        }
    }
    // check ip pool
    JsonNode jsonIpPool = jsonNode.get(IP_POOL);
    if (jsonIpPool != null) {
        KubevirtIpPool ipPool = network.ipPool();
        KubevirtIpPoolJsonMatcher ipPoolMatcher = KubevirtIpPoolJsonMatcher.matchesKubevirtIpPool(ipPool);
        if (ipPoolMatcher.matches(jsonIpPool)) {
            return true;
        } else {
            description.appendText("IP pool was " + jsonIpPool.toString());
            return false;
        }
    }
    // check host routes
    JsonNode jsonHostRoutes = jsonNode.get(HOST_ROUTES);
    if (jsonHostRoutes != null) {
        if (jsonHostRoutes.size() != network.hostRoutes().size()) {
            description.appendText("host routes size was " + jsonHostRoutes.size());
            return false;
        }
        for (KubevirtHostRoute hostRoute : network.hostRoutes()) {
            boolean routeFound = false;
            for (int routeIndex = 0; routeIndex < jsonHostRoutes.size(); routeIndex++) {
                KubevirtHostRouteJsonMatcher routeMatcher = KubevirtHostRouteJsonMatcher.matchesKubevirtHostRoute(hostRoute);
                if (routeMatcher.matches(jsonHostRoutes.get(routeIndex))) {
                    routeFound = true;
                    break;
                }
            }
            if (!routeFound) {
                description.appendText("Host route not found " + hostRoute.toString());
                return false;
            }
        }
    }
    // check dnses
    JsonNode jsonDnses = jsonNode.get(DNSES);
    if (jsonDnses != null) {
        if (jsonDnses.size() != network.dnses().size()) {
            description.appendText("DNSes size was " + jsonDnses.size());
            return false;
        }
        for (IpAddress dns : network.dnses()) {
            boolean dnsFound = false;
            for (int dnsIndex = 0; dnsIndex < jsonDnses.size(); dnsIndex++) {
                String jsonDns = jsonDnses.get(dnsIndex).asText();
                if (jsonDns.equals(dns.toString())) {
                    dnsFound = true;
                    break;
                }
            }
            if (!dnsFound) {
                description.appendText("DNS not found " + dns.toString());
                return false;
            }
        }
    }
    return true;
}
Also used : KubevirtHostRoute(org.onosproject.kubevirtnetworking.api.KubevirtHostRoute) JsonNode(com.fasterxml.jackson.databind.JsonNode) IpAddress(org.onlab.packet.IpAddress) KubevirtIpPool(org.onosproject.kubevirtnetworking.api.KubevirtIpPool)

Example 3 with KubevirtHostRoute

use of org.onosproject.kubevirtnetworking.api.KubevirtHostRoute 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)

Example 4 with KubevirtHostRoute

use of org.onosproject.kubevirtnetworking.api.KubevirtHostRoute 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)

Example 5 with KubevirtHostRoute

use of org.onosproject.kubevirtnetworking.api.KubevirtHostRoute 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)

Aggregations

KubevirtHostRoute (org.onosproject.kubevirtnetworking.api.KubevirtHostRoute)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 IpAddress (org.onlab.packet.IpAddress)3 DefaultKubevirtNetwork (org.onosproject.kubevirtnetworking.api.DefaultKubevirtNetwork)3 KubevirtIpPool (org.onosproject.kubevirtnetworking.api.KubevirtIpPool)3 KubevirtNetwork (org.onosproject.kubevirtnetworking.api.KubevirtNetwork)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 Test (org.junit.Test)2 KubevirtNetworkJsonMatcher.matchesKubevirtNetwork (org.onosproject.kubevirtnetworking.codec.KubevirtNetworkJsonMatcher.matchesKubevirtNetwork)2 HashSet (java.util.HashSet)1