Andreas Wundsam | 6b87735 | 2014-10-10 21:31:47 -0700 | [diff] [blame] | 1 | //:: from generic_utils import OrderedSet |
| 2 | //:: from java_gen.java_model import model |
| 3 | @Override |
| 4 | public <F extends OFValueType<F>> F get(MatchField<F> field) |
| 5 | throws UnsupportedOperationException { |
| 6 | if (!supports(field)) |
| 7 | throw new UnsupportedOperationException("${msg.name} 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(); |
| 15 | } |
| 16 | |
| 17 | @Override |
| 18 | public <F extends OFValueType<F>> Masked<F> getMasked(MatchField<F> field) |
| 19 | throws UnsupportedOperationException { |
| 20 | if (!supportsMasked(field)) |
| 21 | throw new UnsupportedOperationException("${msg.name} 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) { |
| 37 | //:: for id_constant in sorted(set(id_constant for _, id_constant, _ in model.oxm_map.values())): |
| 38 | case ${id_constant}: |
| 39 | //:: #endfor |
| 40 | return true; |
| 41 | default: |
| 42 | return false; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public boolean supports(MatchField<?> field) { |
| 48 | return supportsField(field); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public boolean supportsMasked(MatchField<?> field) { |
| 53 | return supportsField(field); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public boolean isExact(MatchField<?> field) { |
| 58 | if (!supports(field)) |
| 59 | throw new UnsupportedOperationException("${msg.name} does not support matching on field " + field.getName()); |
| 60 | |
| 61 | OFOxm<?> oxm = this.oxmList.get(field); |
| 62 | |
| 63 | return oxm != null && !oxm.isMasked(); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public boolean isFullyWildcarded(MatchField<?> field) { |
| 68 | if (!supports(field)) |
| 69 | throw new UnsupportedOperationException("${msg.name} does not support matching on field " + field.getName()); |
| 70 | |
| 71 | OFOxm<?> oxm = this.oxmList.get(field); |
| 72 | |
| 73 | return oxm == null; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public boolean isPartiallyMasked(MatchField<?> field) { |
| 78 | if (!supports(field)) |
| 79 | throw new UnsupportedOperationException("${msg.name} does not support matching on field " + field.getName()); |
| 80 | |
| 81 | OFOxm<?> oxm = this.oxmList.get(field); |
| 82 | |
| 83 | return oxm != null && oxm.isMasked(); |
| 84 | } |
| 85 | |
| 86 | private class MatchFieldIterator extends AbstractIterator<MatchField<?>> { |
| 87 | private Iterator<OFOxm<?>> oxmIterator; |
| 88 | |
| 89 | MatchFieldIterator() { |
| 90 | oxmIterator = oxmList.iterator(); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | protected MatchField<?> computeNext() { |
| 95 | while(oxmIterator.hasNext()) { |
| 96 | OFOxm<?> oxm = oxmIterator.next(); |
| 97 | if(oxm.getMatchField().arePrerequisitesOK(${msg.name}.this)) |
| 98 | return oxm.getMatchField(); |
| 99 | } |
| 100 | endOfData(); |
| 101 | return null; |
| 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 | } |