blob: 7f66110482f881830360e6518a49d9ecefb5aa75 [file] [log] [blame]
tom0eb04ca2014-08-25 14:34:51 -07001package org.projectfloodlight.openflow.protocol;
2
3import java.util.EnumMap;
4import java.util.Iterator;
5import java.util.Map;
6
7import org.jboss.netty.buffer.ChannelBuffer;
8import org.projectfloodlight.openflow.exceptions.OFParseError;
9import org.projectfloodlight.openflow.protocol.match.MatchField;
10import org.projectfloodlight.openflow.protocol.match.MatchFields;
11import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
12import org.projectfloodlight.openflow.types.OFValueType;
13import org.projectfloodlight.openflow.types.PrimitiveSinkable;
14import org.projectfloodlight.openflow.util.ChannelUtils;
15import org.slf4j.Logger;
16import org.slf4j.LoggerFactory;
17
18import com.google.common.base.Objects;
19import com.google.common.collect.ImmutableMap;
20import com.google.common.hash.PrimitiveSink;
21
22public class OFOxmList implements Iterable<OFOxm<?>>, Writeable, PrimitiveSinkable {
23 private static final Logger logger = LoggerFactory.getLogger(OFOxmList.class);
24
25 private final Map<MatchFields, OFOxm<?>> oxmMap;
26
27 public final static OFOxmList EMPTY = new OFOxmList(ImmutableMap.<MatchFields, OFOxm<?>>of());
28
29 private OFOxmList(Map<MatchFields, OFOxm<?>> oxmMap) {
30 this.oxmMap = oxmMap;
31 }
32
33 @SuppressWarnings("unchecked")
34 public <T extends OFValueType<T>> OFOxm<T> get(MatchField<T> matchField) {
35 return (OFOxm<T>) oxmMap.get(matchField.id);
36 }
37
38 public static class Builder {
39 private final Map<MatchFields, OFOxm<?>> oxmMap;
40
41 public Builder() {
42 oxmMap = new EnumMap<MatchFields, OFOxm<?>>(MatchFields.class);
43 }
44
45 public Builder(EnumMap<MatchFields, OFOxm<?>> oxmMap) {
46 this.oxmMap = oxmMap;
47 }
48
49 public <T extends OFValueType<T>> void set(OFOxm<T> oxm) {
50 oxmMap.put(oxm.getMatchField().id, oxm);
51 }
52
53 public <T extends OFValueType<T>> void unset(MatchField<T> matchField) {
54 oxmMap.remove(matchField.id);
55 }
56
57 public OFOxmList build() {
58 return OFOxmList.ofList(oxmMap.values());
59 }
60 }
61
62 @Override
63 public Iterator<OFOxm<?>> iterator() {
64 return oxmMap.values().iterator();
65 }
66
67 public static OFOxmList ofList(Iterable<OFOxm<?>> oxmList) {
68 Map<MatchFields, OFOxm<?>> map = new EnumMap<MatchFields, OFOxm<?>>(
69 MatchFields.class);
70 for (OFOxm<?> o : oxmList) {
71 OFOxm<?> canonical = o.getCanonical();
72
73 if(logger.isDebugEnabled() && !Objects.equal(o, canonical)) {
74 logger.debug("OFOxmList: normalized non-canonical OXM {} to {}", o, canonical);
75 }
76
77 if(canonical != null)
78 map.put(canonical.getMatchField().id, canonical);
79
80 }
81 return new OFOxmList(map);
82 }
83
84 public static OFOxmList of(OFOxm<?>... oxms) {
85 Map<MatchFields, OFOxm<?>> map = new EnumMap<MatchFields, OFOxm<?>>(
86 MatchFields.class);
87 for (OFOxm<?> o : oxms) {
88 OFOxm<?> canonical = o.getCanonical();
89
90 if(logger.isDebugEnabled() && !Objects.equal(o, canonical)) {
91 logger.debug("OFOxmList: normalized non-canonical OXM {} to {}", o, canonical);
92 }
93
94 if(canonical != null)
95 map.put(canonical.getMatchField().id, canonical);
96 }
97 return new OFOxmList(map);
98 }
99
100 public static OFOxmList readFrom(ChannelBuffer bb, int length,
101 OFMessageReader<OFOxm<?>> reader) throws OFParseError {
102 return ofList(ChannelUtils.readList(bb, length, reader));
103 }
104
105 @Override
106 public void writeTo(ChannelBuffer bb) {
107 for (OFOxm<?> o : this) {
108 o.writeTo(bb);
109 }
110 }
111
112 public OFOxmList.Builder createBuilder() {
113 return new OFOxmList.Builder(new EnumMap<MatchFields, OFOxm<?>>(oxmMap));
114 }
115
116 @Override
117 public int hashCode() {
118 final int prime = 31;
119 int result = 1;
120 result = prime * result + ((oxmMap == null) ? 0 : oxmMap.hashCode());
121 return result;
122 }
123
124 @Override
125 public boolean equals(Object obj) {
126 if (this == obj)
127 return true;
128 if (obj == null)
129 return false;
130 if (getClass() != obj.getClass())
131 return false;
132 OFOxmList other = (OFOxmList) obj;
133 if (oxmMap == null) {
134 if (other.oxmMap != null)
135 return false;
136 } else if (!oxmMap.equals(other.oxmMap))
137 return false;
138 return true;
139 }
140
141 @Override
142 public String toString() {
143 return "OFOxmList" + oxmMap;
144 }
145
146 @Override
147 public void putTo(PrimitiveSink sink) {
148 for (OFOxm<?> o : this) {
149 o.putTo(sink);
150 }
151 }
152
153
154}