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 | */ |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 16 | package org.opencord.igmpproxy.impl.store.groupmember; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 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; |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 23 | import org.opencord.igmpproxy.GroupMemberId; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.Iterator; |
| 27 | |
| 28 | /** |
| 29 | * Date struct to keep Igmp member infomations. |
| 30 | */ |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 31 | public final class GroupMember { |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 32 | |
| 33 | private final VlanId vlan; |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 34 | private final GroupMemberId groupMemberId; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 35 | private final boolean v2; |
| 36 | private byte recordType = IGMPMembership.MODE_IS_INCLUDE; |
| 37 | private ArrayList<Ip4Address> sourceList = new ArrayList<>(); |
| 38 | private int keepAliveQueryInterval = 0; |
| 39 | private int keepAliveQueryCount = 0; |
| 40 | private int lastQueryInterval = 0; |
| 41 | private int lastQueryCount = 0; |
| 42 | private boolean leave = false; |
| 43 | |
| 44 | public GroupMember(Ip4Address groupIp, VlanId vlan, DeviceId deviceId, PortNumber portNum, boolean isV2) { |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 45 | this.vlan = vlan; |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 46 | this.groupMemberId = GroupMemberId.of(groupIp, deviceId, portNum); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 47 | v2 = isV2; |
| 48 | } |
| 49 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 50 | public String getkey() { |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 51 | return groupMemberId.toString(); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 52 | } |
| 53 | |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 54 | public GroupMemberId getGroupMemberId() { |
| 55 | return groupMemberId; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | public VlanId getvlan() { |
| 59 | return vlan; |
| 60 | } |
| 61 | |
| 62 | public DeviceId getDeviceId() { |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 63 | return groupMemberId.getDeviceId(); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | public PortNumber getPortNumber() { |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 67 | return groupMemberId.getPortNum(); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | public Ip4Address getGroupIp() { |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 71 | return groupMemberId.getGroupIp(); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | public byte getRecordType() { |
| 75 | return recordType; |
| 76 | } |
| 77 | |
| 78 | public boolean getv2() { |
| 79 | return v2; |
| 80 | } |
| 81 | |
| 82 | public ArrayList<Ip4Address> getSourceList() { |
| 83 | return sourceList; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | public void updateList(byte recordType, ArrayList<Ip4Address> newSourceList) { |
| 88 | this.recordType = recordType; |
| 89 | this.sourceList.clear(); |
| 90 | this.sourceList.addAll(newSourceList); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 91 | } |
| 92 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 93 | /*join B to A (A+B)*/ |
| 94 | private void join(ArrayList<Integer> listA, ArrayList<Integer> listB) { |
| 95 | Iterator<Integer> iterA = null; |
| 96 | Iterator<Integer> iterB = listB.iterator(); |
| 97 | boolean exists; |
| 98 | while (iterB.hasNext()) { |
| 99 | iterA = listA.iterator(); |
| 100 | exists = false; |
| 101 | int ipToAdd = iterB.next(); |
| 102 | while (iterA.hasNext()) { |
| 103 | if (iterA.next().equals(ipToAdd)) { |
| 104 | exists = true; |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | if (!exists) { |
| 109 | listA.add(ipToAdd); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /* include A and B (A*B)*/ |
| 115 | private void intersection(ArrayList<Integer> listA, ArrayList<Integer> listB) { |
| 116 | Iterator<Integer> iterA = listA.iterator(); |
| 117 | Iterator<Integer> iterB; |
| 118 | boolean exists; |
| 119 | |
| 120 | while (iterA.hasNext()) { |
| 121 | iterB = listB.iterator(); |
| 122 | int ipToInclude = iterA.next(); |
| 123 | exists = false; |
| 124 | while (iterB.hasNext()) { |
| 125 | if (iterB.next().equals(ipToInclude)) { |
| 126 | exists = true; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | if (!exists) { |
| 131 | iterA.remove(); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /*exclude B from A (A-B)*/ |
| 137 | private void exclude(ArrayList<Integer> listA, ArrayList<Integer> listB) { |
| 138 | Iterator<Integer> iterA = null; |
| 139 | Iterator<Integer> iterB = listB.iterator(); |
| 140 | |
| 141 | while (iterB.hasNext()) { |
| 142 | iterA = listA.iterator(); |
| 143 | int ipToDel = iterB.next(); |
| 144 | while (iterA.hasNext()) { |
| 145 | if (iterA.next().equals(ipToDel)) { |
| 146 | iterA.remove(); |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | public void setLeave(boolean l) { |
| 154 | leave = l; |
| 155 | } |
| 156 | |
| 157 | public boolean isLeave() { |
| 158 | return leave; |
| 159 | } |
| 160 | |
| 161 | public int getKeepAliveQueryInterval() { |
| 162 | return keepAliveQueryInterval; |
| 163 | } |
| 164 | |
| 165 | public int getKeepAliveQueryCount() { |
| 166 | return keepAliveQueryCount; |
| 167 | } |
| 168 | |
| 169 | public int getLastQueryInterval() { |
| 170 | return lastQueryInterval; |
| 171 | } |
| 172 | |
| 173 | public int getLastQueryCount() { |
| 174 | return lastQueryCount; |
| 175 | } |
| 176 | |
| 177 | public void keepAliveQueryCount(boolean add) { |
| 178 | if (add) { |
| 179 | keepAliveQueryCount++; |
| 180 | } else { |
| 181 | keepAliveQueryCount = 0; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | public void lastQueryCount(boolean add) { |
| 186 | if (add) { |
| 187 | lastQueryCount++; |
| 188 | } else { |
| 189 | lastQueryCount = 0; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | public void keepAliveInterval(boolean add) { |
| 194 | if (add) { |
| 195 | keepAliveQueryInterval++; |
| 196 | } else { |
| 197 | keepAliveQueryInterval = 0; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | public void lastQueryInterval(boolean add) { |
| 202 | if (add) { |
| 203 | lastQueryInterval++; |
| 204 | } else { |
| 205 | lastQueryInterval = 0; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | public void resetAllTimers() { |
| 210 | keepAliveQueryInterval = 0; |
| 211 | keepAliveQueryCount = 0; |
| 212 | lastQueryInterval = 0; |
| 213 | lastQueryCount = 0; |
| 214 | } |
| 215 | } |