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