ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame^] | 1 | package org.opencord.igmpproxy; |
| 2 | |
| 3 | import org.onlab.packet.IGMPMembership; |
| 4 | import org.onlab.packet.Ip4Address; |
| 5 | import org.onlab.packet.VlanId; |
| 6 | import org.onosproject.net.DeviceId; |
| 7 | import org.onosproject.net.PortNumber; |
| 8 | |
| 9 | import java.util.ArrayList; |
| 10 | import java.util.Iterator; |
| 11 | |
| 12 | /** |
| 13 | * Date struct to keep Igmp member infomations. |
| 14 | */ |
| 15 | public final class GroupMember { |
| 16 | |
| 17 | private final VlanId vlan; |
| 18 | private final DeviceId deviceId; |
| 19 | private final PortNumber portNumber; |
| 20 | private final Ip4Address groupIp; |
| 21 | private final boolean v2; |
| 22 | private byte recordType = IGMPMembership.MODE_IS_INCLUDE; |
| 23 | private ArrayList<Ip4Address> sourceList = new ArrayList<>(); |
| 24 | private int keepAliveQueryInterval = 0; |
| 25 | private int keepAliveQueryCount = 0; |
| 26 | private int lastQueryInterval = 0; |
| 27 | private int lastQueryCount = 0; |
| 28 | private boolean leave = false; |
| 29 | |
| 30 | public GroupMember(Ip4Address groupIp, VlanId vlan, DeviceId deviceId, PortNumber portNum, boolean isV2) { |
| 31 | this.groupIp = groupIp; |
| 32 | this.vlan = vlan; |
| 33 | this.deviceId = deviceId; |
| 34 | this.portNumber = portNum; |
| 35 | v2 = isV2; |
| 36 | } |
| 37 | |
| 38 | static String getkey(Ip4Address groupIp, DeviceId deviceId, PortNumber portNum) { |
| 39 | return groupIp.toString() + deviceId.toString() + portNum.toString(); |
| 40 | } |
| 41 | |
| 42 | public String getkey() { |
| 43 | return GroupMember.getkey(groupIp, deviceId, portNumber); |
| 44 | } |
| 45 | |
| 46 | public String getId() { |
| 47 | return getkey(); |
| 48 | } |
| 49 | |
| 50 | public VlanId getvlan() { |
| 51 | return vlan; |
| 52 | } |
| 53 | |
| 54 | public DeviceId getDeviceId() { |
| 55 | return deviceId; |
| 56 | } |
| 57 | |
| 58 | public PortNumber getPortNumber() { |
| 59 | return portNumber; |
| 60 | } |
| 61 | |
| 62 | public Ip4Address getGroupIp() { |
| 63 | return groupIp; |
| 64 | } |
| 65 | |
| 66 | public byte getRecordType() { |
| 67 | return recordType; |
| 68 | } |
| 69 | |
| 70 | public boolean getv2() { |
| 71 | return v2; |
| 72 | } |
| 73 | |
| 74 | public ArrayList<Ip4Address> getSourceList() { |
| 75 | return sourceList; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | public void updateList(byte recordType, ArrayList<Ip4Address> newSourceList) { |
| 80 | this.recordType = recordType; |
| 81 | this.sourceList.clear(); |
| 82 | this.sourceList.addAll(newSourceList); |
| 83 | |
| 84 | /*TODO : support SSM |
| 85 | if (this.recordType == IGMPMembership.MODE_IS_INCLUDE) { |
| 86 | switch (recordType) { |
| 87 | case IGMPMembership.MODE_IS_INCLUDE: |
| 88 | case IGMPMembership.CHANGE_TO_INCLUDE_MODE: |
| 89 | //however , set to include<B> anyway |
| 90 | this.sourceList = sourceList; |
| 91 | this.recordType = IGMPMembership.MODE_IS_INCLUDE; |
| 92 | break; |
| 93 | case IGMPMembership.MODE_IS_EXCLUDE: |
| 94 | case IGMPMembership.CHANGE_TO_EXCLUDE_MODE: |
| 95 | //set to exclude<B> |
| 96 | this.sourceList = sourceList; |
| 97 | this.recordType = IGMPMembership.MODE_IS_EXCLUDE; |
| 98 | break; |
| 99 | case IGMPMembership.ALLOW_NEW_SOURCES: |
| 100 | //set to include <A+B> |
| 101 | join(this.sourceList, sourceList); |
| 102 | break; |
| 103 | case IGMPMembership.BLOCK_OLD_SOURCES: |
| 104 | //set to include <A-B> |
| 105 | exclude(this.sourceList, sourceList); |
| 106 | break; |
| 107 | default: |
| 108 | break; |
| 109 | } |
| 110 | } else if (this.recordType == IGMPMembership.MODE_IS_EXCLUDE) { |
| 111 | switch (recordType) { |
| 112 | case IGMPMembership.MODE_IS_INCLUDE: |
| 113 | case IGMPMembership.CHANGE_TO_INCLUDE_MODE: |
| 114 | //set to include<B> |
| 115 | this.recordType = IGMPMembership.MODE_IS_INCLUDE; |
| 116 | this.sourceList = sourceList; |
| 117 | break; |
| 118 | case IGMPMembership.MODE_IS_EXCLUDE: |
| 119 | case IGMPMembership.CHANGE_TO_EXCLUDE_MODE: |
| 120 | this.sourceList = sourceList; |
| 121 | this.recordType = IGMPMembership.MODE_IS_EXCLUDE; |
| 122 | break; |
| 123 | case IGMPMembership.ALLOW_NEW_SOURCES: |
| 124 | //set to exclude <A-B> |
| 125 | exclude(this.sourceList, sourceList); |
| 126 | break; |
| 127 | case IGMPMembership.BLOCK_OLD_SOURCES: |
| 128 | //set to exclude <A+B> |
| 129 | join(this.sourceList, sourceList); |
| 130 | break; |
| 131 | default: |
| 132 | break; |
| 133 | } |
| 134 | }*/ |
| 135 | |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | /*join B to A (A+B)*/ |
| 141 | private void join(ArrayList<Integer> listA, ArrayList<Integer> listB) { |
| 142 | Iterator<Integer> iterA = null; |
| 143 | Iterator<Integer> iterB = listB.iterator(); |
| 144 | boolean exists; |
| 145 | while (iterB.hasNext()) { |
| 146 | iterA = listA.iterator(); |
| 147 | exists = false; |
| 148 | int ipToAdd = iterB.next(); |
| 149 | while (iterA.hasNext()) { |
| 150 | if (iterA.next().equals(ipToAdd)) { |
| 151 | exists = true; |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | if (!exists) { |
| 156 | listA.add(ipToAdd); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /* include A and B (A*B)*/ |
| 162 | private void intersection(ArrayList<Integer> listA, ArrayList<Integer> listB) { |
| 163 | Iterator<Integer> iterA = listA.iterator(); |
| 164 | Iterator<Integer> iterB; |
| 165 | boolean exists; |
| 166 | |
| 167 | while (iterA.hasNext()) { |
| 168 | iterB = listB.iterator(); |
| 169 | int ipToInclude = iterA.next(); |
| 170 | exists = false; |
| 171 | while (iterB.hasNext()) { |
| 172 | if (iterB.next().equals(ipToInclude)) { |
| 173 | exists = true; |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | if (!exists) { |
| 178 | iterA.remove(); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /*exclude B from A (A-B)*/ |
| 184 | private void exclude(ArrayList<Integer> listA, ArrayList<Integer> listB) { |
| 185 | Iterator<Integer> iterA = null; |
| 186 | Iterator<Integer> iterB = listB.iterator(); |
| 187 | |
| 188 | while (iterB.hasNext()) { |
| 189 | iterA = listA.iterator(); |
| 190 | int ipToDel = iterB.next(); |
| 191 | while (iterA.hasNext()) { |
| 192 | if (iterA.next().equals(ipToDel)) { |
| 193 | iterA.remove(); |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | public void setLeave(boolean l) { |
| 201 | leave = l; |
| 202 | } |
| 203 | |
| 204 | public boolean isLeave() { |
| 205 | return leave; |
| 206 | } |
| 207 | |
| 208 | public int getKeepAliveQueryInterval() { |
| 209 | return keepAliveQueryInterval; |
| 210 | } |
| 211 | |
| 212 | public int getKeepAliveQueryCount() { |
| 213 | return keepAliveQueryCount; |
| 214 | } |
| 215 | |
| 216 | public int getLastQueryInterval() { |
| 217 | return lastQueryInterval; |
| 218 | } |
| 219 | |
| 220 | public int getLastQueryCount() { |
| 221 | return lastQueryCount; |
| 222 | } |
| 223 | |
| 224 | public void keepAliveQueryCount(boolean add) { |
| 225 | if (add) { |
| 226 | keepAliveQueryCount++; |
| 227 | } else { |
| 228 | keepAliveQueryCount = 0; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | public void lastQueryCount(boolean add) { |
| 233 | if (add) { |
| 234 | lastQueryCount++; |
| 235 | } else { |
| 236 | lastQueryCount = 0; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | public void keepAliveInterval(boolean add) { |
| 241 | if (add) { |
| 242 | keepAliveQueryInterval++; |
| 243 | } else { |
| 244 | keepAliveQueryInterval = 0; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | public void lastQueryInterval(boolean add) { |
| 249 | if (add) { |
| 250 | lastQueryInterval++; |
| 251 | } else { |
| 252 | lastQueryInterval = 0; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | public void resetAllTimers() { |
| 257 | keepAliveQueryInterval = 0; |
| 258 | keepAliveQueryCount = 0; |
| 259 | lastQueryInterval = 0; |
| 260 | lastQueryCount = 0; |
| 261 | } |
| 262 | } |