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; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 5 | import java.util.Map; |
| 6 | |
| 7 | import org.jboss.netty.buffer.ChannelBuffer; |
| 8 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 9 | import org.projectfloodlight.openflow.protocol.match.MatchField; |
| 10 | import org.projectfloodlight.openflow.protocol.match.MatchFields; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 11 | import org.projectfloodlight.openflow.protocol.oxm.OFOxm; |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 12 | import org.projectfloodlight.openflow.types.OFValueType; |
| 13 | import org.projectfloodlight.openflow.types.PrimitiveSinkable; |
| 14 | import org.projectfloodlight.openflow.util.ChannelUtils; |
Andreas Wundsam | 3b6d90e | 2013-11-05 17:20:54 -0800 | [diff] [blame] | 15 | import org.slf4j.Logger; |
| 16 | import org.slf4j.LoggerFactory; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 17 | |
Andreas Wundsam | 3b6d90e | 2013-11-05 17:20:54 -0800 | [diff] [blame] | 18 | import com.google.common.base.Objects; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 20 | import com.google.common.hash.PrimitiveSink; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 21 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 22 | public class OFOxmList implements Iterable<OFOxm<?>>, Writeable, PrimitiveSinkable { |
Andreas Wundsam | 3b6d90e | 2013-11-05 17:20:54 -0800 | [diff] [blame] | 23 | private static final Logger logger = LoggerFactory.getLogger(OFOxmList.class); |
| 24 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 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() { |
Andreas Wundsam | 3b6d90e | 2013-11-05 17:20:54 -0800 | [diff] [blame] | 58 | return OFOxmList.ofList(oxmMap.values()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public Iterator<OFOxm<?>> iterator() { |
| 64 | return oxmMap.values().iterator(); |
| 65 | } |
| 66 | |
Andreas Wundsam | 3b6d90e | 2013-11-05 17:20:54 -0800 | [diff] [blame] | 67 | public static OFOxmList ofList(Iterable<OFOxm<?>> oxmList) { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 68 | Map<MatchFields, OFOxm<?>> map = new EnumMap<MatchFields, OFOxm<?>>( |
| 69 | MatchFields.class); |
| 70 | for (OFOxm<?> o : oxmList) { |
Andreas Wundsam | 3b6d90e | 2013-11-05 17:20:54 -0800 | [diff] [blame] | 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 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 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) { |
Andreas Wundsam | 3b6d90e | 2013-11-05 17:20:54 -0800 | [diff] [blame] | 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); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 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 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 146 | @Override |
| 147 | public void putTo(PrimitiveSink sink) { |
| 148 | for (OFOxm<?> o : this) { |
| 149 | o.putTo(sink); |
| 150 | } |
| 151 | } |
| 152 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 153 | |
| 154 | } |