Search in sources :

Example 26 with PortNumber

use of org.onosproject.net.PortNumber in project TFG by mattinelorza.

the class Ipv6SimpleRoutingComponent method setUpPath.

private void setUpPath(HostId srcId, HostId dstId) {
    Host src = hostService.getHost(srcId);
    Host dst = hostService.getHost(dstId);
    // Check if hosts are located at the same switch
    log.info("Src switch id={} and Dst switch id={}", src.location().deviceId(), dst.location().deviceId());
    if (src.location().deviceId().toString().equals(dst.location().deviceId().toString())) {
        PortNumber outPort = dst.location().port();
        DeviceId devId = dst.location().deviceId();
        FlowRule nextHopRule = createL2NextHopRule(devId, dst.mac(), outPort);
        flowRuleService.applyFlowRules(nextHopRule);
        log.info("Hosts in the same switch");
        return;
    }
    // Get all the available paths between two given hosts
    // A path is a collection of links
    Set<Path> paths = topologyService.getPaths(topologyService.currentTopology(), src.location().deviceId(), dst.location().deviceId());
    if (paths.isEmpty()) {
        // If there are no paths, display a warn and exit
        log.warn("No path found");
        return;
    }
    // Pick a path that does not lead back to where we
    // came from; if no such path,display a warn and exit
    Path path = pickForwardPathIfPossible(paths, src.location().port());
    if (path == null) {
        log.warn("Don't know where to go from here {} for {} -> {}", src.location(), srcId, dstId);
        return;
    }
    // Install rules in the path
    List<Link> pathLinks = path.links();
    for (Link l : pathLinks) {
        PortNumber outPort = l.src().port();
        DeviceId devId = l.src().deviceId();
        FlowRule nextHopRule = createL2NextHopRule(devId, dst.mac(), outPort);
        flowRuleService.applyFlowRules(nextHopRule);
    }
    // Install rule in the last device (where dst is located)
    PortNumber outPort = dst.location().port();
    DeviceId devId = dst.location().deviceId();
    FlowRule nextHopRule = createL2NextHopRule(devId, dst.mac(), outPort);
    flowRuleService.applyFlowRules(nextHopRule);
}
Also used : Path(org.onosproject.net.Path) DeviceId(org.onosproject.net.DeviceId) Host(org.onosproject.net.Host) FlowRule(org.onosproject.net.flow.FlowRule) PortNumber(org.onosproject.net.PortNumber) Link(org.onosproject.net.Link)

Example 27 with PortNumber

use of org.onosproject.net.PortNumber in project onos by opennetworkinglab.

the class VirtualPortCodec method decode.

@Override
public VirtualPort decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }
    NetworkId nId = NetworkId.networkId(Long.parseLong(extractMember(NETWORK_ID, json)));
    DeviceId dId = DeviceId.deviceId(extractMember(DEVICE_ID, json));
    VirtualNetworkService vnetService = context.getService(VirtualNetworkService.class);
    Set<VirtualDevice> vDevs = vnetService.getVirtualDevices(nId);
    VirtualDevice vDev = vDevs.stream().filter(virtualDevice -> virtualDevice.id().equals(dId)).findFirst().orElse(null);
    nullIsIllegal(vDev, dId.toString() + INVALID_VIRTUAL_DEVICE);
    PortNumber portNum = PortNumber.portNumber(extractMember(PORT_NUM, json));
    DeviceId physDId = DeviceId.deviceId(extractMember(PHYS_DEVICE_ID, json));
    PortNumber physPortNum = PortNumber.portNumber(extractMember(PHYS_PORT_NUM, json));
    ConnectPoint realizedBy = new ConnectPoint(physDId, physPortNum);
    return new DefaultVirtualPort(nId, vDev, portNum, realizedBy);
}
Also used : DeviceId(org.onosproject.net.DeviceId) VirtualNetworkService(org.onosproject.incubator.net.virtual.VirtualNetworkService) VirtualDevice(org.onosproject.incubator.net.virtual.VirtualDevice) NetworkId(org.onosproject.incubator.net.virtual.NetworkId) PortNumber(org.onosproject.net.PortNumber) ConnectPoint(org.onosproject.net.ConnectPoint) DefaultVirtualPort(org.onosproject.incubator.net.virtual.DefaultVirtualPort)

Example 28 with PortNumber

use of org.onosproject.net.PortNumber in project onos by opennetworkinglab.

the class DecodeInstructionCodecHelper method getPortNumber.

/**
 * Extracts port number of the given json node.
 *
 * @param jsonNode json node
 * @return port number
 */
private PortNumber getPortNumber(ObjectNode jsonNode) {
    PortNumber portNumber;
    JsonNode portNode = nullIsIllegal(jsonNode.get(InstructionCodec.PORT), InstructionCodec.PORT + InstructionCodec.ERROR_MESSAGE);
    if (portNode.isLong() || portNode.isInt()) {
        portNumber = PortNumber.portNumber(portNode.asLong());
    } else if (portNode.isTextual()) {
        portNumber = PortNumber.fromString(portNode.textValue());
    } else {
        throw new IllegalArgumentException("Port value " + portNode.toString() + " is not supported");
    }
    return portNumber;
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) PortNumber(org.onosproject.net.PortNumber)

Example 29 with PortNumber

use of org.onosproject.net.PortNumber in project onos by opennetworkinglab.

the class GroupBucketCodec method decode.

@Override
public GroupBucket decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }
    // build traffic treatment
    ObjectNode treatmentJson = get(json, TREATMENT);
    TrafficTreatment trafficTreatment = null;
    if (treatmentJson != null) {
        JsonCodec<TrafficTreatment> treatmentCodec = context.codec(TrafficTreatment.class);
        trafficTreatment = treatmentCodec.decode(treatmentJson, context);
    }
    // parse group type
    String type = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText();
    GroupBucket groupBucket = null;
    switch(type) {
        case "SELECT":
            // parse weight
            int weightInt = nullIsIllegal(json.get(WEIGHT), WEIGHT + MISSING_MEMBER_MESSAGE).asInt();
            groupBucket = DefaultGroupBucket.createSelectGroupBucket(trafficTreatment, (short) weightInt);
            break;
        case "INDIRECT":
            groupBucket = DefaultGroupBucket.createIndirectGroupBucket(trafficTreatment);
            break;
        case "ALL":
            groupBucket = DefaultGroupBucket.createAllGroupBucket(trafficTreatment);
            break;
        case "FAILOVER":
            // parse watchPort
            PortNumber watchPort = PortNumber.portNumber(nullIsIllegal(json.get(WATCH_PORT), WATCH_PORT + MISSING_MEMBER_MESSAGE).asText());
            // parse watchGroup
            int groupIdInt = nullIsIllegal(json.get(WATCH_GROUP), WATCH_GROUP + MISSING_MEMBER_MESSAGE).asInt();
            GroupId watchGroup = new GroupId((short) groupIdInt);
            groupBucket = DefaultGroupBucket.createFailoverGroupBucket(trafficTreatment, watchPort, watchGroup);
            break;
        default:
            DefaultGroupBucket.createAllGroupBucket(trafficTreatment);
    }
    return groupBucket;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) GroupBucket(org.onosproject.net.group.GroupBucket) DefaultGroupBucket(org.onosproject.net.group.DefaultGroupBucket) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) PortNumber(org.onosproject.net.PortNumber) GroupId(org.onosproject.core.GroupId)

Example 30 with PortNumber

use of org.onosproject.net.PortNumber in project onos by opennetworkinglab.

the class Ofdpa2GroupHandler method verifyGroup.

/**
 *  Checks existing buckets in {@link NextGroup}  to verify if they match
 *  the buckets in the given {@link NextObjective}. Adds or removes buckets
 *  to ensure that the buckets match up.
 *
 * @param nextObjective the next objective to verify
 * @param next the representation of the existing group which has to be
 *             modified to match the given next objective
 */
protected void verifyGroup(NextObjective nextObjective, NextGroup next) {
    if (nextObjective.type() == NextObjective.Type.SIMPLE) {
        log.warn("verification not supported for indirect group");
        fail(nextObjective, ObjectiveError.UNSUPPORTED);
        return;
    }
    log.trace("Call to verify device:{} nextId:{}", deviceId, nextObjective.id());
    List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
    List<TrafficTreatment> bucketsToCreate = Lists.newArrayList();
    List<Integer> indicesToRemove = Lists.newArrayList();
    // to detect missing buckets and/or duplicate buckets (to be removed)
    for (TrafficTreatment bkt : nextObjective.next()) {
        PortNumber portNumber = readOutPortFromTreatment(bkt);
        int label = readLabelFromTreatment(bkt);
        if (portNumber == null) {
            log.warn("treatment {} of next objective {} has no outport.. " + "cannot remove bucket from group in dev: {}", bkt, nextObjective.id(), deviceId);
            fail(nextObjective, ObjectiveError.BADPARAMS);
            return;
        }
        List<Integer> existing = existingPortAndLabel(allActiveKeys, groupService, deviceId, portNumber, label);
        if (existing.isEmpty()) {
            // if it doesn't exist, mark this bucket for creation
            bucketsToCreate.add(bkt);
        }
        if (existing.size() > 1) {
            // if it exists but there are duplicates, mark the others for removal
            existing.remove(0);
            indicesToRemove.addAll(existing);
        }
    }
    // (not duplicates) respect to the next objective
    if (allActiveKeys.size() > nextObjective.next().size() && // ignore specific case of empty group
    !(nextObjective.next().size() == 0 && allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1)) {
        log.warn("Mismatch detected between next and flowobjstore for device {}: " + "nextId:{}, nextObjective-size:{} next-size:{} .. correcting", deviceId, nextObjective.id(), nextObjective.next().size(), allActiveKeys.size());
        List<Integer> otherIndices = indicesToRemoveFromNextGroup(allActiveKeys, nextObjective, groupService, deviceId);
        // Filter out the indices not present
        otherIndices = otherIndices.stream().filter(index -> !indicesToRemove.contains(index)).collect(Collectors.toList());
        // Add all to the final list
        indicesToRemove.addAll(otherIndices);
    }
    log.trace("Buckets to create {}", bucketsToCreate);
    log.trace("Indices to remove {}", indicesToRemove);
    if (!bucketsToCreate.isEmpty()) {
        log.info("creating {} buckets as part of nextId: {} verification", bucketsToCreate.size(), nextObjective.id());
        // create a nextObjective only with these buckets
        NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder().withId(nextObjective.id()).withType(nextObjective.type()).withMeta(nextObjective.meta()).fromApp(nextObjective.appId());
        bucketsToCreate.forEach(nextObjBuilder::addTreatment);
        // According to the next type we call the proper add function
        if (nextObjective.type() == NextObjective.Type.HASHED) {
            if (isL2Hash(nextObjective)) {
                addBucketToL2HashGroup(nextObjBuilder.addToExisting(), allActiveKeys);
            } else {
                addBucketToEcmpHashGroup(nextObjBuilder.addToExisting(), allActiveKeys);
            }
        } else {
            addBucketToBroadcastGroup(nextObjBuilder.addToExisting(), allActiveKeys);
        }
    }
    if (!indicesToRemove.isEmpty()) {
        log.info("removing {} buckets as part of nextId: {} verification", indicesToRemove.size(), nextObjective.id());
        List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
        indicesToRemove.forEach(index -> chainsToRemove.add(allActiveKeys.get(index)));
        removeBucket(chainsToRemove, nextObjective);
    }
    log.trace("Checking mismatch with GroupStore device:{} nextId:{}", deviceId, nextObjective.id());
    if (bucketsToCreate.isEmpty() && indicesToRemove.isEmpty()) {
        // flowObjective store record is in-sync with nextObjective passed-in
        // Nevertheless groupStore may not be in sync due to bug in the store
        // - see CORD-1844. XXX When this bug is fixed, the rest of this verify
        // method will not be required.
        GroupKey topGroupKey = allActiveKeys.get(0).peekFirst();
        Group topGroup = groupService.getGroup(deviceId, topGroupKey);
        // topGroup should not be null - adding guard
        if (topGroup == null) {
            log.warn("topGroup {} not found in GroupStore device:{}, nextId:{}", topGroupKey, deviceId, nextObjective.id());
            fail(nextObjective, ObjectiveError.GROUPMISSING);
            return;
        }
        int actualGroupSize = topGroup.buckets().buckets().size();
        int objGroupSize = nextObjective.next().size();
        if (actualGroupSize != objGroupSize) {
            log.warn("Mismatch detected in device:{}, nextId:{}, nextObjective-size" + ":{} group-size:{} .. correcting", deviceId, nextObjective.id(), objGroupSize, actualGroupSize);
        }
        if (actualGroupSize > objGroupSize) {
            // Group in the device has more chains
            List<GroupBucket> bucketsToRemove = Lists.newArrayList();
            // check every bucket in the actual group
            for (GroupBucket bucket : topGroup.buckets().buckets()) {
                GroupInstruction g = (GroupInstruction) bucket.treatment().allInstructions().iterator().next();
                // the group pointed to
                GroupId gidToCheck = g.groupId();
                boolean matches = false;
                for (Deque<GroupKey> validChain : allActiveKeys) {
                    if (validChain.size() < 2) {
                        continue;
                    }
                    GroupKey pointedGroupKey = validChain.stream().collect(Collectors.toList()).get(1);
                    Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
                    if (pointedGroup != null && gidToCheck.equals(pointedGroup.id())) {
                        matches = true;
                        break;
                    }
                }
                if (!matches) {
                    log.warn("Removing bucket pointing to groupId:{}", gidToCheck);
                    bucketsToRemove.add(bucket);
                }
            }
            // remove buckets for which there was no record in the obj store
            if (bucketsToRemove.isEmpty()) {
                log.warn("Mismatch detected but could not determine which" + "buckets to remove");
            } else {
                GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
                groupService.removeBucketsFromGroup(deviceId, topGroupKey, removeBuckets, topGroupKey, nextObjective.appId());
            }
        } else if (actualGroupSize < objGroupSize) {
            // Group in the device has less chains
            // should also add buckets not in group-store but in obj-store
            List<GroupBucket> bucketsToAdd = Lists.newArrayList();
            // check every bucket in the obj
            for (Deque<GroupKey> validChain : allActiveKeys) {
                if (validChain.size() < 2) {
                    continue;
                }
                GroupKey pointedGroupKey = validChain.stream().collect(Collectors.toList()).get(1);
                Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
                if (pointedGroup == null) {
                    // group should exist, otherwise cannot be added as bucket
                    continue;
                }
                boolean matches = false;
                for (GroupBucket bucket : topGroup.buckets().buckets()) {
                    GroupInstruction g = (GroupInstruction) bucket.treatment().allInstructions().iterator().next();
                    // the group pointed to
                    GroupId gidToCheck = g.groupId();
                    if (pointedGroup.id().equals(gidToCheck)) {
                        matches = true;
                        break;
                    }
                }
                if (!matches) {
                    log.warn("Adding bucket pointing to groupId:{}", pointedGroup);
                    TrafficTreatment t = DefaultTrafficTreatment.builder().group(pointedGroup.id()).build();
                    // Create the proper bucket according to the next type
                    if (nextObjective.type() == NextObjective.Type.HASHED) {
                        bucketsToAdd.add(DefaultGroupBucket.createSelectGroupBucket(t));
                    } else {
                        bucketsToAdd.add(DefaultGroupBucket.createAllGroupBucket(t));
                    }
                }
            }
            if (bucketsToAdd.isEmpty()) {
                log.warn("Mismatch detected but could not determine which " + "buckets to add");
            } else {
                GroupBuckets addBuckets = new GroupBuckets(bucketsToAdd);
                groupService.addBucketsToGroup(deviceId, topGroupKey, addBuckets, topGroupKey, nextObjective.appId());
            }
        }
    }
    log.trace("Verify done for device:{} nextId:{}", deviceId, nextObjective.id());
    pass(nextObjective);
}
Also used : DefaultNextObjective(org.onosproject.net.flowobjective.DefaultNextObjective) NextObjective(org.onosproject.net.flowobjective.NextObjective) NextGroup(org.onosproject.net.behaviour.NextGroup) Group(org.onosproject.net.group.Group) GroupKey(org.onosproject.net.group.GroupKey) DefaultGroupKey(org.onosproject.net.group.DefaultGroupKey) OfdpaGroupHandlerUtility.l2MulticastGroupKey(org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.l2MulticastGroupKey) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) GroupBuckets(org.onosproject.net.group.GroupBuckets) Deque(java.util.Deque) ArrayDeque(java.util.ArrayDeque) GroupId(org.onosproject.core.GroupId) GroupBucket(org.onosproject.net.group.GroupBucket) DefaultGroupBucket(org.onosproject.net.group.DefaultGroupBucket) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) PortNumber(org.onosproject.net.PortNumber) GroupInstruction(org.onosproject.net.flow.instructions.Instructions.GroupInstruction)

Aggregations

PortNumber (org.onosproject.net.PortNumber)336 DeviceId (org.onosproject.net.DeviceId)136 TrafficTreatment (org.onosproject.net.flow.TrafficTreatment)109 DefaultTrafficTreatment (org.onosproject.net.flow.DefaultTrafficTreatment)103 ConnectPoint (org.onosproject.net.ConnectPoint)82 TrafficSelector (org.onosproject.net.flow.TrafficSelector)80 DefaultTrafficSelector (org.onosproject.net.flow.DefaultTrafficSelector)74 Port (org.onosproject.net.Port)67 ArrayList (java.util.ArrayList)64 Set (java.util.Set)64 List (java.util.List)59 Logger (org.slf4j.Logger)58 Collectors (java.util.stream.Collectors)51 DeviceService (org.onosproject.net.device.DeviceService)49 VlanId (org.onlab.packet.VlanId)42 MacAddress (org.onlab.packet.MacAddress)41 Device (org.onosproject.net.Device)40 FlowRule (org.onosproject.net.flow.FlowRule)40 Optional (java.util.Optional)39 Sets (com.google.common.collect.Sets)35