blob: 10975603cafcbe0903972f3cd98d9ca92ba93437 [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;
Esin Karaman305908c2020-02-24 14:42:52 +000021import org.onosproject.net.PortNumber;
22
ke han81a38b92017-03-10 18:41:44 +080023import java.util.Map;
24import java.util.Set;
25
26/**
David K. Bainbridged77028f2017-08-01 12:47:55 -070027 * State machine for whole IGMP process. The state machine is implemented on
ke han81a38b92017-03-10 18:41:44 +080028 * RFC 2236 "6. Host State Diagram".
29 */
30public final class StateMachine {
Esin Karamana05342e2019-09-17 13:01:25 +000031
32 private static final String GROUP = "Group";
33
ke han81a38b92017-03-10 18:41:44 +080034 private StateMachine() {
35
36 }
37 private static Map<String, SingleStateMachine> map = Maps.newConcurrentMap();
38
39 private static String getId(DeviceId devId, Ip4Address groupIp) {
Esin Karamana05342e2019-09-17 13:01:25 +000040 return devId.toString() + GROUP + groupIp.toString();
ke han81a38b92017-03-10 18:41:44 +080041 }
42
43 private static SingleStateMachine get(DeviceId devId, Ip4Address groupIp) {
44 String id = getId(devId, groupIp);
45 return map.get(id);
46 }
47
Esin Karamana05342e2019-09-17 13:01:25 +000048 public static void destroySingle(DeviceId devId, Ip4Address groupIp) {
ke han81a38b92017-03-10 18:41:44 +080049 SingleStateMachine machine = get(devId, groupIp);
50 if (null == machine) {
51 return;
52 }
53 machine.cancelTimer();
54 map.remove(getId(devId, groupIp));
55 }
56
Esin Karaman305908c2020-02-24 14:42:52 +000057 public static boolean join(DeviceId devId, Ip4Address groupIp, Ip4Address srcIP, PortNumber upLinkPort) {
ke han81a38b92017-03-10 18:41:44 +080058 SingleStateMachine machine = get(devId, groupIp);
Esin Karamana05342e2019-09-17 13:01:25 +000059
ke han81a38b92017-03-10 18:41:44 +080060 if (null == machine) {
Esin Karaman305908c2020-02-24 14:42:52 +000061 machine = new SingleStateMachine(devId, groupIp, srcIP, upLinkPort);
ke han81a38b92017-03-10 18:41:44 +080062 map.put(getId(devId, groupIp), machine);
Esin Karamana05342e2019-09-17 13:01:25 +000063
64 boolean shouldSendJoin = true;
65 if (IgmpManager.isIgmpOnPodBasis() &&
66 groupListenedByOtherDevices(devId, groupIp)) {
67 // unset the flag if igmp messages are evaluated on POD basis
68 // and there are already active members of this group
69 // across the entire POD
70 shouldSendJoin = false;
71 }
72 machine.join(shouldSendJoin);
ke han81a38b92017-03-10 18:41:44 +080073 return true;
74 }
75 machine.increaseCounter();
76 return false;
77 }
78
79 public static boolean leave(DeviceId devId, Ip4Address groupIp) {
80 SingleStateMachine machine = get(devId, groupIp);
81 if (null == machine) {
82 return false;
83 }
ke han81a38b92017-03-10 18:41:44 +080084
Esin Karamana05342e2019-09-17 13:01:25 +000085 machine.decreaseCounter();
86 // make sure machine instance still exists.
87 // it may be removed by the preceding thread
ke han81a38b92017-03-10 18:41:44 +080088 if (machine.getCounter() == 0) {
Esin Karamana05342e2019-09-17 13:01:25 +000089 boolean shouldSendLeave = true;
90 if (IgmpManager.isIgmpOnPodBasis() &&
91 groupListenedByOtherDevices(devId, groupIp)) {
92 // unset the flag if igmp messages are evaluated on POD basis
93 // and there are still active members of this group
94 // across the entire POD
95 shouldSendLeave = false;
96 }
97 machine.leave(shouldSendLeave);
98 destroySingle(devId, groupIp);
ke han81a38b92017-03-10 18:41:44 +080099 return true;
100 }
101 return false;
102 }
103
104 static void specialQuery(DeviceId devId, Ip4Address groupIp, int maxResp) {
105 SingleStateMachine machine = get(devId, groupIp);
106 if (null == machine) {
107 return;
108 }
109 machine.query(maxResp);
110 }
111
112 static void generalQuery(DeviceId devId, int maxResp) {
113 for (Map.Entry<String, SingleStateMachine> entry : map.entrySet()) {
114 SingleStateMachine machine = entry.getValue();
115 if (devId.equals(machine.getDeviceId())) {
116 machine.query(maxResp);
117 }
118 }
119 }
120
Deepa Vaddireddybcd52352017-09-21 05:04:48 +0000121 static void generalQuery(int maxResp) {
122 for (Map.Entry<String, SingleStateMachine> entry : map.entrySet()) {
123 SingleStateMachine machine = entry.getValue();
124 machine.query(maxResp);
125 }
126 }
127
ke han81a38b92017-03-10 18:41:44 +0800128 public static Set<Map.Entry<String, SingleStateMachine>> entrySet() {
129 return map.entrySet();
130 }
131
132 public static void timeOut(DeviceId devId, Ip4Address groupIp) {
133 SingleStateMachine machine = get(devId, groupIp);
134 if (null == machine) {
135 return;
136 }
137 machine.timeOut();
138 }
139
140 public static void clearMap() {
141 map.clear();
142 }
143
Esin Karamana05342e2019-09-17 13:01:25 +0000144 /**
145 * @param devId id of the device being excluded
146 * @param groupIp group IP address
147 * @return true if this group has at least one listener connected to
148 * any device in the map except for the device specified; false otherwise.
149 */
150 private static boolean groupListenedByOtherDevices(DeviceId devId, Ip4Address groupIp) {
151 for (SingleStateMachine machine : map.values()) {
152 if (machine.getDeviceId().equals(devId)) {
153 continue;
154 }
155 if (machine.getGroupIp().equals(groupIp)) {
156 //means group is being listened by other peers in the domain
157 return true;
158 }
159 }
160 return false;
161 }
162
ke han81a38b92017-03-10 18:41:44 +0800163}