blob: c6ba116e00a22671d8826735bd26f69705e308d9 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.protocol;
2
3import java.util.EnumMap;
4import java.util.Iterator;
5import java.util.List;
6import java.util.Map;
7
8import org.jboss.netty.buffer.ChannelBuffer;
9import org.projectfloodlight.openflow.exceptions.OFParseError;
10import org.projectfloodlight.openflow.protocol.match.MatchField;
11import org.projectfloodlight.openflow.protocol.match.MatchFields;
Yotam Harcholf3f11152013-09-05 16:47:16 -070012import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070013import org.projectfloodlight.openflow.types.OFValueType;
14import org.projectfloodlight.openflow.types.PrimitiveSinkable;
15import org.projectfloodlight.openflow.util.ChannelUtils;
Yotam Harcholf3f11152013-09-05 16:47:16 -070016
17import com.google.common.collect.ImmutableMap;
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070018import com.google.common.hash.PrimitiveSink;
Yotam Harcholf3f11152013-09-05 16:47:16 -070019
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070020public class OFOxmList implements Iterable<OFOxm<?>>, Writeable, PrimitiveSinkable {
Yotam Harcholf3f11152013-09-05 16:47:16 -070021 private final Map<MatchFields, OFOxm<?>> oxmMap;
22
23 public final static OFOxmList EMPTY = new OFOxmList(ImmutableMap.<MatchFields, OFOxm<?>>of());
24
25 private OFOxmList(Map<MatchFields, OFOxm<?>> oxmMap) {
26 this.oxmMap = oxmMap;
27 }
28
29 @SuppressWarnings("unchecked")
30 public <T extends OFValueType<T>> OFOxm<T> get(MatchField<T> matchField) {
31 return (OFOxm<T>) oxmMap.get(matchField.id);
32 }
33
34 public static class Builder {
35 private final Map<MatchFields, OFOxm<?>> oxmMap;
36
37 public Builder() {
38 oxmMap = new EnumMap<MatchFields, OFOxm<?>>(MatchFields.class);
39 }
40
41 public Builder(EnumMap<MatchFields, OFOxm<?>> oxmMap) {
42 this.oxmMap = oxmMap;
43 }
44
45 public <T extends OFValueType<T>> void set(OFOxm<T> oxm) {
46 oxmMap.put(oxm.getMatchField().id, oxm);
47 }
48
49 public <T extends OFValueType<T>> void unset(MatchField<T> matchField) {
50 oxmMap.remove(matchField.id);
51 }
52
53 public OFOxmList build() {
54 return new OFOxmList(oxmMap);
55 }
56 }
57
58 @Override
59 public Iterator<OFOxm<?>> iterator() {
60 return oxmMap.values().iterator();
61 }
62
63 public static OFOxmList ofList(List<OFOxm<?>> oxmList) {
64 Map<MatchFields, OFOxm<?>> map = new EnumMap<MatchFields, OFOxm<?>>(
65 MatchFields.class);
66 for (OFOxm<?> o : oxmList) {
67 // TODO: if fully masked, ignore oxm.
68 map.put(o.getMatchField().id, o);
69 }
70 return new OFOxmList(map);
71 }
72
73 public static OFOxmList of(OFOxm<?>... oxms) {
74 Map<MatchFields, OFOxm<?>> map = new EnumMap<MatchFields, OFOxm<?>>(
75 MatchFields.class);
76 for (OFOxm<?> o : oxms) {
77 // TODO: if fully masked, ignore oxm.
78 map.put(o.getMatchField().id, o);
79 }
80 return new OFOxmList(map);
81 }
82
83 public static OFOxmList readFrom(ChannelBuffer bb, int length,
84 OFMessageReader<OFOxm<?>> reader) throws OFParseError {
85 return ofList(ChannelUtils.readList(bb, length, reader));
86 }
87
88 @Override
89 public void writeTo(ChannelBuffer bb) {
90 for (OFOxm<?> o : this) {
91 o.writeTo(bb);
92 }
93 }
94
95 public OFOxmList.Builder createBuilder() {
96 return new OFOxmList.Builder(new EnumMap<MatchFields, OFOxm<?>>(oxmMap));
97 }
98
99 @Override
100 public int hashCode() {
101 final int prime = 31;
102 int result = 1;
103 result = prime * result + ((oxmMap == null) ? 0 : oxmMap.hashCode());
104 return result;
105 }
106
107 @Override
108 public boolean equals(Object obj) {
109 if (this == obj)
110 return true;
111 if (obj == null)
112 return false;
113 if (getClass() != obj.getClass())
114 return false;
115 OFOxmList other = (OFOxmList) obj;
116 if (oxmMap == null) {
117 if (other.oxmMap != null)
118 return false;
119 } else if (!oxmMap.equals(other.oxmMap))
120 return false;
121 return true;
122 }
123
124 @Override
125 public String toString() {
126 return "OFOxmList" + oxmMap;
127 }
128
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700129 @Override
130 public void putTo(PrimitiveSink sink) {
131 for (OFOxm<?> o : this) {
132 o.putTo(sink);
133 }
134 }
135
Yotam Harcholf3f11152013-09-05 16:47:16 -0700136
137}