Andreas Wundsam | 40bca24 | 2013-11-19 09:45:28 -0800 | [diff] [blame] | 1 | //:: from generic_utils import OrderedSet |
| 2 | //:: from java_gen.java_model import model |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 3 | @Override |
| 4 | public <F extends OFValueType<F>> F get(MatchField<F> field) |
| 5 | throws UnsupportedOperationException { |
Yotam Harchol | 98af775 | 2013-08-22 14:59:38 -0700 | [diff] [blame] | 6 | if (!supports(field)) |
| 7 | throw new UnsupportedOperationException("OFMatchV3Ver13 does not support matching on field " + field.getName()); |
| 8 | |
| 9 | OFOxm<F> oxm = this.oxmList.get(field); |
| 10 | |
| 11 | if (oxm == null || !field.arePrerequisitesOK(this)) |
| 12 | return null; |
| 13 | |
| 14 | return oxm.getValue(); |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | @Override |
| 18 | public <F extends OFValueType<F>> Masked<F> getMasked(MatchField<F> field) |
| 19 | throws UnsupportedOperationException { |
Yotam Harchol | 98af775 | 2013-08-22 14:59:38 -0700 | [diff] [blame] | 20 | if (!supportsMasked(field)) |
| 21 | throw new UnsupportedOperationException("OFMatchV3Ver13 does not support masked matching on field " + field.getName()); |
| 22 | |
| 23 | OFOxm<F> oxm = this.oxmList.get(field); |
| 24 | |
| 25 | if (oxm == null || !field.arePrerequisitesOK(this)) |
| 26 | return null; |
| 27 | |
| 28 | if (oxm.getMask() == null) |
| 29 | return null; |
| 30 | |
| 31 | // TODO: Make OfOxm extend Masked and just return the OXM? |
| 32 | return Masked.of(oxm.getValue(), oxm.getMask()); |
| 33 | } |
| 34 | |
| 35 | private static boolean supportsField(MatchField<?> field) { |
| 36 | switch (field.id) { |
Andreas Wundsam | 40bca24 | 2013-11-19 09:45:28 -0800 | [diff] [blame] | 37 | //:: for id_constant in sorted(set(id_constant for _, id_constant, _ in model.oxm_map.values())): |
| 38 | case ${id_constant}: |
| 39 | //:: #endfor |
Yotam Harchol | 98af775 | 2013-08-22 14:59:38 -0700 | [diff] [blame] | 40 | return true; |
| 41 | default: |
| 42 | return false; |
| 43 | } |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public boolean supports(MatchField<?> field) { |
Yotam Harchol | 98af775 | 2013-08-22 14:59:38 -0700 | [diff] [blame] | 48 | return supportsField(field); |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public boolean supportsMasked(MatchField<?> field) { |
Yotam Harchol | 98af775 | 2013-08-22 14:59:38 -0700 | [diff] [blame] | 53 | return supportsField(field); |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public boolean isExact(MatchField<?> field) { |
Yotam Harchol | 98af775 | 2013-08-22 14:59:38 -0700 | [diff] [blame] | 58 | if (!supports(field)) |
| 59 | throw new UnsupportedOperationException("OFMatchV3Ver13 does not support matching on field " + field.getName()); |
| 60 | |
| 61 | OFOxm<?> oxm = this.oxmList.get(field); |
| 62 | |
| 63 | return oxm != null && !oxm.isMasked(); |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public boolean isFullyWildcarded(MatchField<?> field) { |
Yotam Harchol | 98af775 | 2013-08-22 14:59:38 -0700 | [diff] [blame] | 68 | if (!supports(field)) |
| 69 | throw new UnsupportedOperationException("OFMatchV3Ver13 does not support matching on field " + field.getName()); |
| 70 | |
| 71 | OFOxm<?> oxm = this.oxmList.get(field); |
| 72 | |
| 73 | return oxm == null; |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public boolean isPartiallyMasked(MatchField<?> field) { |
Yotam Harchol | 98af775 | 2013-08-22 14:59:38 -0700 | [diff] [blame] | 78 | if (!supports(field)) |
| 79 | throw new UnsupportedOperationException("OFMatchV3Ver13 does not support matching on field " + field.getName()); |
| 80 | |
| 81 | OFOxm<?> oxm = this.oxmList.get(field); |
| 82 | |
| 83 | return oxm != null && oxm.isMasked(); |
Andreas Wundsam | 2fdf99d | 2013-10-27 06:15:47 -0700 | [diff] [blame] | 84 | } |
Rob Vaterlaus | 934b4ad | 2013-11-12 10:08:59 -0800 | [diff] [blame] | 85 | |
| 86 | private class MatchFieldIterator extends UnmodifiableIterator<MatchField<?>> { |
| 87 | private Iterator<OFOxm<?>> oxmIterator; |
| 88 | |
| 89 | MatchFieldIterator() { |
| 90 | oxmIterator = oxmList.iterator(); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public boolean hasNext() { |
| 95 | return oxmIterator.hasNext(); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public MatchField<?> next() { |
| 100 | OFOxm<?> next = oxmIterator.next(); |
| 101 | return next.getMatchField(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public Iterable<MatchField<?>> getMatchFields() { |
| 107 | return new Iterable<MatchField<?>>() { |
| 108 | public Iterator<MatchField<?>> iterator() { |
| 109 | return new MatchFieldIterator(); |
| 110 | } |
| 111 | }; |
| 112 | } |