blob: 0add46c9eb247ff6eb34dbd9ea5592c3f07147ab [file] [log] [blame]
David K. Bainbridged77028f2017-08-01 12:47:55 -07001/*
Brian O'Connor4d084702017-08-03 22:45:58 -07002 * Copyright 2017-present Open Networking Foundation
David K. Bainbridged77028f2017-08-01 12:47:55 -07003 *
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 Ozdemir4c5947c2020-05-05 13:14:32 +000016package org.opencord.igmpproxy.impl.store.groupmember;
ke han81a38b92017-03-10 18:41:44 +080017
18import org.onlab.packet.IGMPMembership;
19import org.onlab.packet.Ip4Address;
20import org.onlab.packet.VlanId;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.PortNumber;
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +000023import org.opencord.igmpproxy.GroupMemberId;
ke han81a38b92017-03-10 18:41:44 +080024
25import java.util.ArrayList;
26import java.util.Iterator;
27
28/**
29 * Date struct to keep Igmp member infomations.
30 */
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +000031public final class GroupMember {
ke han81a38b92017-03-10 18:41:44 +080032
33 private final VlanId vlan;
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +000034 private final GroupMemberId groupMemberId;
ke han81a38b92017-03-10 18:41:44 +080035 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 han81a38b92017-03-10 18:41:44 +080045 this.vlan = vlan;
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +000046 this.groupMemberId = GroupMemberId.of(groupIp, deviceId, portNum);
ke han81a38b92017-03-10 18:41:44 +080047 v2 = isV2;
48 }
49
ke han81a38b92017-03-10 18:41:44 +080050 public String getkey() {
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +000051 return groupMemberId.toString();
ke han81a38b92017-03-10 18:41:44 +080052 }
53
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +000054 public GroupMemberId getGroupMemberId() {
55 return groupMemberId;
ke han81a38b92017-03-10 18:41:44 +080056 }
57
58 public VlanId getvlan() {
59 return vlan;
60 }
61
62 public DeviceId getDeviceId() {
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +000063 return groupMemberId.getDeviceId();
ke han81a38b92017-03-10 18:41:44 +080064 }
65
66 public PortNumber getPortNumber() {
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +000067 return groupMemberId.getPortNum();
ke han81a38b92017-03-10 18:41:44 +080068 }
69
70 public Ip4Address getGroupIp() {
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +000071 return groupMemberId.getGroupIp();
ke han81a38b92017-03-10 18:41:44 +080072 }
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 han81a38b92017-03-10 18:41:44 +080091 }
92
ke han81a38b92017-03-10 18:41:44 +080093 /*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}