blob: bc97ca2b88121c49e1fd878e60c9a241ac6bbff2 [file] [log] [blame]
Andreas Wundsam6b877352014-10-10 21:31:47 -07001//:: 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
Murat Parlakisik34863382016-12-05 00:53:17 -080061 if(!field.arePrerequisitesOK(this))
62 return false;
63
Andreas Wundsam6b877352014-10-10 21:31:47 -070064 OFOxm<?> oxm = this.oxmList.get(field);
65
66 return oxm != null && !oxm.isMasked();
67 }
68
69 @Override
70 public boolean isFullyWildcarded(MatchField<?> field) {
71 if (!supports(field))
72 throw new UnsupportedOperationException("${msg.name} does not support matching on field " + field.getName());
Murat Parlakisik34863382016-12-05 00:53:17 -080073 if(!field.arePrerequisitesOK(this))
74 return true;
Andreas Wundsam6b877352014-10-10 21:31:47 -070075
76 OFOxm<?> oxm = this.oxmList.get(field);
77
78 return oxm == null;
79 }
80
81 @Override
82 public boolean isPartiallyMasked(MatchField<?> field) {
83 if (!supports(field))
84 throw new UnsupportedOperationException("${msg.name} does not support matching on field " + field.getName());
Murat Parlakisik34863382016-12-05 00:53:17 -080085 if(!field.arePrerequisitesOK(this))
86 return false;
Andreas Wundsam6b877352014-10-10 21:31:47 -070087
88 OFOxm<?> oxm = this.oxmList.get(field);
89
90 return oxm != null && oxm.isMasked();
91 }
92
93 private class MatchFieldIterator extends AbstractIterator<MatchField<?>> {
94 private Iterator<OFOxm<?>> oxmIterator;
95
96 MatchFieldIterator() {
97 oxmIterator = oxmList.iterator();
98 }
99
100 @Override
101 protected MatchField<?> computeNext() {
102 while(oxmIterator.hasNext()) {
103 OFOxm<?> oxm = oxmIterator.next();
104 if(oxm.getMatchField().arePrerequisitesOK(${msg.name}.this))
105 return oxm.getMatchField();
106 }
107 endOfData();
108 return null;
109 }
110 }
111
112 @Override
113 public Iterable<MatchField<?>> getMatchFields() {
114 return new Iterable<MatchField<?>>() {
115 public Iterator<MatchField<?>> iterator() {
116 return new MatchFieldIterator();
117 }
118 };
119 }