Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.protocol; |
| 2 | |
| 3 | import java.util.EnumMap; |
| 4 | import java.util.Iterator; |
| 5 | import java.util.List; |
| 6 | import java.util.Map; |
| 7 | |
| 8 | import org.jboss.netty.buffer.ChannelBuffer; |
| 9 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 10 | import org.projectfloodlight.openflow.protocol.match.MatchField; |
| 11 | import org.projectfloodlight.openflow.protocol.match.MatchFields; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 12 | import org.projectfloodlight.openflow.protocol.oxm.OFOxm; |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 13 | import org.projectfloodlight.openflow.types.OFValueType; |
| 14 | import org.projectfloodlight.openflow.types.PrimitiveSinkable; |
| 15 | import org.projectfloodlight.openflow.util.ChannelUtils; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 16 | |
| 17 | import com.google.common.collect.ImmutableMap; |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 18 | import com.google.common.hash.PrimitiveSink; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 19 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 20 | public class OFOxmList implements Iterable<OFOxm<?>>, Writeable, PrimitiveSinkable { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 21 | 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 Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 129 | @Override |
| 130 | public void putTo(PrimitiveSink sink) { |
| 131 | for (OFOxm<?> o : this) { |
| 132 | o.putTo(sink); |
| 133 | } |
| 134 | } |
| 135 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 136 | |
| 137 | } |