Search in sources :

Example 81 with IpAddress

use of org.onlab.packet.IpAddress 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 82 with IpAddress

use of org.onlab.packet.IpAddress in project onos by opennetworkinglab.

the class KubevirtLoadBalancerCodec method decode.

@Override
public KubevirtLoadBalancer decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }
    String name = nullIsIllegal(json.get(NAME).asText(), NAME + MISSING_MESSAGE);
    IpAddress vip = IpAddress.valueOf(nullIsIllegal(json.get(VIP).asText(), VIP + MISSING_MESSAGE));
    String networkId = nullIsIllegal(json.get(NETWORK_ID).asText(), NETWORK_ID + MISSING_MESSAGE);
    KubevirtLoadBalancer.Builder builder = DefaultKubevirtLoadBalancer.builder().name(name).vip(vip).networkId(networkId);
    JsonNode description = json.get(DESCRIPTION);
    if (description != null) {
        builder.description(description.asText());
    }
    ArrayNode membersJson = (ArrayNode) json.get(MEMBERS);
    if (membersJson != null) {
        Set<IpAddress> members = new HashSet<>();
        for (JsonNode memberJson : membersJson) {
            members.add(IpAddress.valueOf(memberJson.asText()));
        }
        builder.members(members);
    }
    JsonNode rulesJson = json.get(RULES);
    if (rulesJson != null) {
        Set<KubevirtLoadBalancerRule> rules = new HashSet<>();
        IntStream.range(0, rulesJson.size()).forEach(i -> {
            ObjectNode ruleJson = get(rulesJson, i);
            KubevirtLoadBalancerRule rule = context.codec(KubevirtLoadBalancerRule.class).decode(ruleJson, context);
            rules.add(rule);
        });
        builder.rules(rules);
    }
    return builder.build();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DefaultKubevirtLoadBalancer(org.onosproject.kubevirtnetworking.api.DefaultKubevirtLoadBalancer) KubevirtLoadBalancer(org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancer) KubevirtLoadBalancerRule(org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerRule) IpAddress(org.onlab.packet.IpAddress) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) HashSet(java.util.HashSet)

Example 83 with IpAddress

use of org.onlab.packet.IpAddress in project onos by opennetworkinglab.

the class KubevirtPortCodec method decode.

@Override
public KubevirtPort decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }
    String vmName = nullIsIllegal(json.get(VM_NAME).asText(), VM_NAME + MISSING_MESSAGE);
    String networkId = nullIsIllegal(json.get(NETWORK_ID).asText(), NETWORK_ID + MISSING_MESSAGE);
    String macAddress = nullIsIllegal(json.get(MAC_ADDRESS).asText(), MAC_ADDRESS + MISSING_MESSAGE);
    KubevirtPort.Builder builder = DefaultKubevirtPort.builder().vmName(vmName).networkId(networkId).macAddress(MacAddress.valueOf(macAddress));
    JsonNode ipAddressJson = json.get(IP_ADDRESS);
    if (ipAddressJson != null) {
        final IpAddress ipAddress = IpAddress.valueOf(ipAddressJson.asText());
        builder.ipAddress(ipAddress);
    }
    JsonNode deviceIdJson = json.get(DEVICE_ID);
    if (deviceIdJson != null) {
        final DeviceId deviceId = DeviceId.deviceId(deviceIdJson.asText());
        builder.deviceId(deviceId);
    }
    JsonNode portNumberJson = json.get(PORT_NUMBER);
    if (portNumberJson != null) {
        final PortNumber portNumber = PortNumber.portNumber(portNumberJson.asText());
        builder.portNumber(portNumber);
    }
    log.trace("Port is {}", builder.build().toString());
    return builder.build();
}
Also used : DeviceId(org.onosproject.net.DeviceId) JsonNode(com.fasterxml.jackson.databind.JsonNode) IpAddress(org.onlab.packet.IpAddress) KubevirtPort(org.onosproject.kubevirtnetworking.api.KubevirtPort) DefaultKubevirtPort(org.onosproject.kubevirtnetworking.api.DefaultKubevirtPort) PortNumber(org.onosproject.net.PortNumber)

Example 84 with IpAddress

use of org.onlab.packet.IpAddress 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 85 with IpAddress

use of org.onlab.packet.IpAddress 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)

Aggregations

IpAddress (org.onlab.packet.IpAddress)288 MacAddress (org.onlab.packet.MacAddress)63 VlanId (org.onlab.packet.VlanId)52 ConnectPoint (org.onosproject.net.ConnectPoint)48 Set (java.util.Set)46 DeviceId (org.onosproject.net.DeviceId)44 Logger (org.slf4j.Logger)40 Test (org.junit.Test)37 Collectors (java.util.stream.Collectors)36 Ethernet (org.onlab.packet.Ethernet)36 IpPrefix (org.onlab.packet.IpPrefix)36 HostId (org.onosproject.net.HostId)33 Host (org.onosproject.net.Host)32 Optional (java.util.Optional)30 HostLocation (org.onosproject.net.HostLocation)30 LoggerFactory (org.slf4j.LoggerFactory)30 ApplicationId (org.onosproject.core.ApplicationId)29 CoreService (org.onosproject.core.CoreService)29 TrafficTreatment (org.onosproject.net.flow.TrafficTreatment)29 JsonNode (com.fasterxml.jackson.databind.JsonNode)28