Search in sources :

Example 1 with PiExactFieldMatch

use of org.onosproject.net.pi.runtime.PiExactFieldMatch in project onos by opennetworkinglab.

the class CriterionJsonMatcher method matchCriterion.

/**
 * Matches a protocol-independent Type criterion object.
 *
 * @param criterion criterion to match
 * @return true if the JSON matches the criterion, false otherwise.
 */
private boolean matchCriterion(PiCriterion criterion) {
    Collection<PiFieldMatch> piFieldMatches = criterion.fieldMatches();
    JsonNode jsonMathes = jsonCriterion.get("matches");
    if (!jsonMathes.isArray()) {
        return false;
    }
    for (JsonNode matchNode : jsonMathes) {
        for (PiFieldMatch fieldMatch : piFieldMatches) {
            if (!Objects.equals(matchNode.get("field").textValue(), fieldMatch.fieldId().id())) {
                description.appendText("match field was " + fieldMatch.fieldId().id());
                return false;
            }
            if (!Objects.equals(matchNode.get("match").textValue(), fieldMatch.type().name().toLowerCase())) {
                description.appendText("match type was " + fieldMatch.type().name().toLowerCase());
                return false;
            }
            switch(fieldMatch.type()) {
                case EXACT:
                    if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("value").textValue(), null)), ((PiExactFieldMatch) fieldMatch).value())) {
                        description.appendText("match value was " + ((PiExactFieldMatch) fieldMatch).value());
                        return false;
                    }
                    break;
                case LPM:
                    if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("value").textValue(), null)), ((PiLpmFieldMatch) fieldMatch).value())) {
                        description.appendText("match value was " + ((PiLpmFieldMatch) fieldMatch).value());
                        return false;
                    }
                    if (!Objects.equals(matchNode.get("prefixLength").intValue(), ((PiLpmFieldMatch) fieldMatch).prefixLength())) {
                        description.appendText("match prefix was " + ((PiLpmFieldMatch) fieldMatch).prefixLength());
                        return false;
                    }
                    break;
                case TERNARY:
                    if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("value").textValue(), null)), ((PiTernaryFieldMatch) fieldMatch).value())) {
                        description.appendText("match value was " + ((PiTernaryFieldMatch) fieldMatch).value());
                        return false;
                    }
                    if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("mask").textValue(), null)), ((PiTernaryFieldMatch) fieldMatch).mask())) {
                        description.appendText("match mask was " + ((PiTernaryFieldMatch) fieldMatch).mask());
                        return false;
                    }
                    break;
                case RANGE:
                    if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("highValue").textValue(), null)), ((PiRangeFieldMatch) fieldMatch).highValue())) {
                        description.appendText("match high value was " + ((PiRangeFieldMatch) fieldMatch).highValue());
                        return false;
                    }
                    if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("lowValue").textValue(), null)), ((PiRangeFieldMatch) fieldMatch).lowValue())) {
                        description.appendText("match low value was " + ((PiRangeFieldMatch) fieldMatch).lowValue());
                        return false;
                    }
                    break;
                case OPTIONAL:
                    if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("value").textValue(), null)), ((PiOptionalFieldMatch) fieldMatch).value())) {
                        description.appendText("match value was " + ((PiOptionalFieldMatch) fieldMatch).value());
                        return false;
                    }
                    break;
                default:
                    description.appendText("match type was " + fieldMatch.type().name().toLowerCase());
                    return false;
            }
        }
    }
    return true;
}
Also used : PiRangeFieldMatch(org.onosproject.net.pi.runtime.PiRangeFieldMatch) PiOptionalFieldMatch(org.onosproject.net.pi.runtime.PiOptionalFieldMatch) PiFieldMatch(org.onosproject.net.pi.runtime.PiFieldMatch) JsonNode(com.fasterxml.jackson.databind.JsonNode) PiExactFieldMatch(org.onosproject.net.pi.runtime.PiExactFieldMatch) PiTernaryFieldMatch(org.onosproject.net.pi.runtime.PiTernaryFieldMatch) PiLpmFieldMatch(org.onosproject.net.pi.runtime.PiLpmFieldMatch)

Example 2 with PiExactFieldMatch

use of org.onosproject.net.pi.runtime.PiExactFieldMatch in project onos by opennetworkinglab.

the class CriterionTranslatorHelper method translateCriterion.

/**
 * Translates a given criterion instance to a PiFieldMatch with the given id, match type, and bit-width.
 *
 * @param fieldId   PI match field identifier
 * @param criterion criterion
 * @param matchType match type
 * @param bitWidth  size of the field match in bits
 * @return a PI field match
 * @throws PiTranslationException if the criterion cannot be translated (see exception message)
 */
static PiFieldMatch translateCriterion(Criterion criterion, PiMatchFieldId fieldId, PiMatchType matchType, int bitWidth) throws PiTranslationException {
    if (!TRANSLATORS.containsKey(criterion.getClass())) {
        throw new PiTranslationException(format("Translation of criterion class %s is not implemented.", criterion.getClass().getSimpleName()));
    }
    try {
        final CriterionTranslator translator = TRANSLATORS.get(criterion.getClass()).newInstance();
        translator.init(criterion, bitWidth);
        switch(matchType) {
            case EXACT:
                return new PiExactFieldMatch(fieldId, translator.exactMatch());
            case OPTIONAL:
                return new PiOptionalFieldMatch(fieldId, translator.exactMatch());
            case TERNARY:
                final Pair<ImmutableByteSequence, ImmutableByteSequence> tp = translator.ternaryMatch();
                return new PiTernaryFieldMatch(fieldId, tp.getLeft(), tp.getRight());
            case LPM:
                final Pair<ImmutableByteSequence, Integer> lp = translator.lpmMatch();
                return new PiLpmFieldMatch(fieldId, lp.getLeft(), lp.getRight());
            default:
                throw new PiTranslationException(format("Translation of criterion %s (%s class) to match type %s is not implemented.", criterion.type().name(), criterion.getClass().getSimpleName(), matchType.name()));
        }
    } catch (ByteSequenceTrimException e) {
        throw new PiTranslationException(format("Size mismatch for criterion %s: %s", criterion.type(), e.getMessage()));
    } catch (CriterionTranslatorException e) {
        throw new PiTranslationException(format("Unable to translate criterion %s: %s", criterion.type(), e.getMessage()));
    } catch (InstantiationException | IllegalAccessException e) {
        // Was not able to instantiate the criterion translator.
        throw new IllegalStateException(e);
    }
}
Also used : IPv6NDTargetAddressCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.IPv6NDTargetAddressCriterionTranslator) IPEcnCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.IPEcnCriterionTranslator) IpCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.IpCriterionTranslator) ArpPaCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.ArpPaCriterionTranslator) PbbIsidCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.PbbIsidCriterionTranslator) TcpPortCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.TcpPortCriterionTranslator) IPv6FlowLabelCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.IPv6FlowLabelCriterionTranslator) TunnelIdCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.TunnelIdCriterionTranslator) EthTypeCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.EthTypeCriterionTranslator) PortCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.PortCriterionTranslator) TcpFlagsCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.TcpFlagsCriterionTranslator) IPv6ExthdrFlagsCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.IPv6ExthdrFlagsCriterionTranslator) EthCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.EthCriterionTranslator) IPProtocolCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.IPProtocolCriterionTranslator) ArpOpCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.ArpOpCriterionTranslator) IPv6NDLinkLayerAddressCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.IPv6NDLinkLayerAddressCriterionTranslator) Icmpv6CodeCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.Icmpv6CodeCriterionTranslator) IcmpTypeCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.IcmpTypeCriterionTranslator) MplsBosCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.MplsBosCriterionTranslator) MetadataCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.MetadataCriterionTranslator) VlanPcpCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.VlanPcpCriterionTranslator) UdpPortCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.UdpPortCriterionTranslator) IPDscpCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.IPDscpCriterionTranslator) VlanIdCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.VlanIdCriterionTranslator) SctpPortCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.SctpPortCriterionTranslator) MplsTcCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.MplsTcCriterionTranslator) ArpHaCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.ArpHaCriterionTranslator) MplsCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.MplsCriterionTranslator) IcmpCodeCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.IcmpCodeCriterionTranslator) Icmpv6TypeCriterionTranslator(org.onosproject.net.pi.impl.CriterionTranslators.Icmpv6TypeCriterionTranslator) PiTernaryFieldMatch(org.onosproject.net.pi.runtime.PiTernaryFieldMatch) PiTranslationException(org.onosproject.net.pi.service.PiTranslationException) PiLpmFieldMatch(org.onosproject.net.pi.runtime.PiLpmFieldMatch) PiOptionalFieldMatch(org.onosproject.net.pi.runtime.PiOptionalFieldMatch) CriterionTranslatorException(org.onosproject.net.pi.impl.CriterionTranslator.CriterionTranslatorException) PiExactFieldMatch(org.onosproject.net.pi.runtime.PiExactFieldMatch) ByteSequenceTrimException(org.onlab.util.ImmutableByteSequence.ByteSequenceTrimException) ImmutableByteSequence(org.onlab.util.ImmutableByteSequence)

Example 3 with PiExactFieldMatch

use of org.onosproject.net.pi.runtime.PiExactFieldMatch in project onos by opennetworkinglab.

the class PiCriterionTranslatorsTest method testIPv6NDLinkLayerAddressCriterion.

@Test
public void testIPv6NDLinkLayerAddressCriterion() throws Exception {
    MacAddress mac = MacAddress.valueOf(random.nextLong());
    int bitWidth = mac.toBytes().length * 8;
    IPv6NDLinkLayerAddressCriterion criterion = (IPv6NDLinkLayerAddressCriterion) Criteria.matchIPv6NDSourceLinkLayerAddress(mac);
    PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
    assertThat(exactMatch.value().asArray(), is(criterion.mac().toBytes()));
}
Also used : IPv6NDLinkLayerAddressCriterion(org.onosproject.net.flow.criteria.IPv6NDLinkLayerAddressCriterion) PiExactFieldMatch(org.onosproject.net.pi.runtime.PiExactFieldMatch) MacAddress(org.onlab.packet.MacAddress) Test(org.junit.Test)

Example 4 with PiExactFieldMatch

use of org.onosproject.net.pi.runtime.PiExactFieldMatch in project onos by opennetworkinglab.

the class PiCriterionTranslatorsTest method testMplsTcCriterion.

@Test
public void testMplsTcCriterion() throws Exception {
    byte[] mplsTc = new byte[1];
    random.nextBytes(mplsTc);
    int bitWidth = 16;
    MplsTcCriterion criterion = (MplsTcCriterion) Criteria.matchMplsTc(mplsTc[0]);
    PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
    assertThat(exactMatch.value().asReadOnlyBuffer().get(1), is(criterion.tc()));
}
Also used : MplsTcCriterion(org.onosproject.net.flow.criteria.MplsTcCriterion) PiExactFieldMatch(org.onosproject.net.pi.runtime.PiExactFieldMatch) Test(org.junit.Test)

Example 5 with PiExactFieldMatch

use of org.onosproject.net.pi.runtime.PiExactFieldMatch in project onos by opennetworkinglab.

the class PiCriterionTranslatorsTest method testMplsBosCriterion.

@Test
public void testMplsBosCriterion() throws Exception {
    boolean mplsBos = random.nextBoolean();
    int bitWidth = 32;
    int bMplsBos = 0;
    MplsBosCriterion criterion = (MplsBosCriterion) Criteria.matchMplsBos(mplsBos);
    PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
    bMplsBos = (criterion.mplsBos()) ? 1 : 0;
    assertThat(exactMatch.value().asReadOnlyBuffer().getInt(), is(bMplsBos));
}
Also used : MplsBosCriterion(org.onosproject.net.flow.criteria.MplsBosCriterion) PiExactFieldMatch(org.onosproject.net.pi.runtime.PiExactFieldMatch) Test(org.junit.Test)

Aggregations

PiExactFieldMatch (org.onosproject.net.pi.runtime.PiExactFieldMatch)36 Test (org.junit.Test)30 PiTernaryFieldMatch (org.onosproject.net.pi.runtime.PiTernaryFieldMatch)10 PiLpmFieldMatch (org.onosproject.net.pi.runtime.PiLpmFieldMatch)6 PiOptionalFieldMatch (org.onosproject.net.pi.runtime.PiOptionalFieldMatch)5 PiRangeFieldMatch (org.onosproject.net.pi.runtime.PiRangeFieldMatch)4 MacAddress (org.onlab.packet.MacAddress)3 TpPort (org.onlab.packet.TpPort)3 ImmutableByteSequence (org.onlab.util.ImmutableByteSequence)3 PiFieldMatch (org.onosproject.net.pi.runtime.PiFieldMatch)3 ByteString (com.google.protobuf.ByteString)2 ByteSequenceTrimException (org.onlab.util.ImmutableByteSequence.ByteSequenceTrimException)2 SctpPortCriterion (org.onosproject.net.flow.criteria.SctpPortCriterion)2 TcpPortCriterion (org.onosproject.net.flow.criteria.TcpPortCriterion)2 UdpPortCriterion (org.onosproject.net.flow.criteria.UdpPortCriterion)2 P4InfoOuterClass (p4.config.v1.P4InfoOuterClass)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 EthType (org.onlab.packet.EthType)1 Ip4Address (org.onlab.packet.Ip4Address)1 Ip4Prefix (org.onlab.packet.Ip4Prefix)1