blob: 391bdee32486f46677398269dd25a4514147fd60 [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 */
ke han81a38b92017-03-10 18:41:44 +080016package org.opencord.igmpproxy;
17
18import com.google.common.collect.Maps;
19import org.onlab.packet.Ip4Address;
20import org.onosproject.net.DeviceId;
21import java.util.Map;
22import java.util.Set;
23
24/**
David K. Bainbridged77028f2017-08-01 12:47:55 -070025 * State machine for whole IGMP process. The state machine is implemented on
ke han81a38b92017-03-10 18:41:44 +080026 * RFC 2236 "6. Host State Diagram".
27 */
28public final class StateMachine {
Esin Karamana05342e2019-09-17 13:01:25 +000029
30 private static final String GROUP = "Group";
31
ke han81a38b92017-03-10 18:41:44 +080032 private StateMachine() {
33
34 }
35 private static Map<String, SingleStateMachine> map = Maps.newConcurrentMap();
36
37 private static String getId(DeviceId devId, Ip4Address groupIp) {
Esin Karamana05342e2019-09-17 13:01:25 +000038 return devId.toString() + GROUP + groupIp.toString();
ke han81a38b92017-03-10 18:41:44 +080039 }
40
41 private static SingleStateMachine get(DeviceId devId, Ip4Address groupIp) {
42 String id = getId(devId, groupIp);
43 return map.get(id);
44 }
45
Esin Karamana05342e2019-09-17 13:01:25 +000046 public static void destroySingle(DeviceId devId, Ip4Address groupIp) {
ke han81a38b92017-03-10 18:41:44 +080047 SingleStateMachine machine = get(devId, groupIp);
48 if (null == machine) {
49 return;
50 }
51 machine.cancelTimer();
52 map.remove(getId(devId, groupIp));
53 }
54
55 public static boolean join(DeviceId devId, Ip4Address groupIp, Ip4Address srcIP) {
56 SingleStateMachine machine = get(devId, groupIp);
Esin Karamana05342e2019-09-17 13:01:25 +000057
ke han81a38b92017-03-10 18:41:44 +080058 if (null == machine) {
59 machine = new SingleStateMachine(devId, groupIp, srcIP);
60 map.put(getId(devId, groupIp), machine);
Esin Karamana05342e2019-09-17 13:01:25 +000061
62 boolean shouldSendJoin = true;
63 if (IgmpManager.isIgmpOnPodBasis() &&
64 groupListenedByOtherDevices(devId, groupIp)) {
65 // unset the flag if igmp messages are evaluated on POD basis
66 // and there are already active members of this group
67 // across the entire POD
68 shouldSendJoin = false;
69 }
70 machine.join(shouldSendJoin);
ke han81a38b92017-03-10 18:41:44 +080071 return true;
72 }
73 machine.increaseCounter();
74 return false;
75 }
76
77 public static boolean leave(DeviceId devId, Ip4Address groupIp) {
78 SingleStateMachine machine = get(devId, groupIp);
79 if (null == machine) {
80 return false;
81 }
ke han81a38b92017-03-10 18:41:44 +080082
Esin Karamana05342e2019-09-17 13:01:25 +000083 machine.decreaseCounter();
84 // make sure machine instance still exists.
85 // it may be removed by the preceding thread
ke han81a38b92017-03-10 18:41:44 +080086 if (machine.getCounter() == 0) {
Esin Karamana05342e2019-09-17 13:01:25 +000087 boolean shouldSendLeave = true;
88 if (IgmpManager.isIgmpOnPodBasis() &&
89 groupListenedByOtherDevices(devId, groupIp)) {
90 // unset the flag if igmp messages are evaluated on POD basis
91 // and there are still active members of this group
92 // across the entire POD
93 shouldSendLeave = false;
94 }
95 machine.leave(shouldSendLeave);
96 destroySingle(devId, groupIp);
ke han81a38b92017-03-10 18:41:44 +080097 return true;
98 }
99 return false;
100 }
101
102 static void specialQuery(DeviceId devId, Ip4Address groupIp, int maxResp) {
103 SingleStateMachine machine = get(devId, groupIp);
104 if (null == machine) {
105 return;
106 }
107 machine.query(maxResp);
108 }
109
110 static void generalQuery(DeviceId devId, int maxResp) {
111 for (Map.Entry<String, SingleStateMachine> entry : map.entrySet()) {
112 SingleStateMachine machine = entry.getValue();
113 if (devId.equals(machine.getDeviceId())) {
114 machine.query(maxResp);
115 }
116 }
117 }
118
Deepa Vaddireddybcd52352017-09-21 05:04:48 +0000119 static void generalQuery(int maxResp) {
120 for (Map.Entry<String, SingleStateMachine> entry : map.entrySet()) {
121 SingleStateMachine machine = entry.getValue();
122 machine.query(maxResp);
123 }
124 }
125
ke han81a38b92017-03-10 18:41:44 +0800126 public static Set<Map.Entry<String, SingleStateMachine>> entrySet() {
127 return map.entrySet();
128 }
129
130 public static void timeOut(DeviceId devId, Ip4Address groupIp) {
131 SingleStateMachine machine = get(devId, groupIp);
132 if (null == machine) {
133 return;
134 }
135 machine.timeOut();
136 }
137
138 public static void clearMap() {
139 map.clear();
140 }
141
Esin Karamana05342e2019-09-17 13:01:25 +0000142 /**
143 * @param devId id of the device being excluded
144 * @param groupIp group IP address
145 * @return true if this group has at least one listener connected to
146 * any device in the map except for the device specified; false otherwise.
147 */
148 private static boolean groupListenedByOtherDevices(DeviceId devId, Ip4Address groupIp) {
149 for (SingleStateMachine machine : map.values()) {
150 if (machine.getDeviceId().equals(devId)) {
151 continue;
152 }
153 if (machine.getGroupIp().equals(groupIp)) {
154 //means group is being listened by other peers in the domain
155 return true;
156 }
157 }
158 return false;
159 }
160
ke han81a38b92017-03-10 18:41:44 +0800161}