blob: 81ad1d41e18025c26212271cb0ac984fd931971b [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 {
29 private StateMachine() {
30
31 }
32 private static Map<String, SingleStateMachine> map = Maps.newConcurrentMap();
33
34 private static String getId(DeviceId devId, Ip4Address groupIp) {
35 return devId.toString() + "Group" + groupIp.toString();
36 }
37
38 private static SingleStateMachine get(DeviceId devId, Ip4Address groupIp) {
39 String id = getId(devId, groupIp);
40 return map.get(id);
41 }
42
43 public static void destorySingle(DeviceId devId, Ip4Address groupIp) {
44 SingleStateMachine machine = get(devId, groupIp);
45 if (null == machine) {
46 return;
47 }
48 machine.cancelTimer();
49 map.remove(getId(devId, groupIp));
50 }
51
52 public static boolean join(DeviceId devId, Ip4Address groupIp, Ip4Address srcIP) {
53 SingleStateMachine machine = get(devId, groupIp);
54 if (null == machine) {
55 machine = new SingleStateMachine(devId, groupIp, srcIP);
56 map.put(getId(devId, groupIp), machine);
57 machine.join();
58 return true;
59 }
60 machine.increaseCounter();
61 return false;
62 }
63
64 public static boolean leave(DeviceId devId, Ip4Address groupIp) {
65 SingleStateMachine machine = get(devId, groupIp);
66 if (null == machine) {
67 return false;
68 }
69 machine.decreaseCounter();
70
71 if (machine.getCounter() == 0) {
72 machine.leave();
73 destorySingle(devId, groupIp);
74 return true;
75 }
76 return false;
77 }
78
79 static void specialQuery(DeviceId devId, Ip4Address groupIp, int maxResp) {
80 SingleStateMachine machine = get(devId, groupIp);
81 if (null == machine) {
82 return;
83 }
84 machine.query(maxResp);
85 }
86
87 static void generalQuery(DeviceId devId, int maxResp) {
88 for (Map.Entry<String, SingleStateMachine> entry : map.entrySet()) {
89 SingleStateMachine machine = entry.getValue();
90 if (devId.equals(machine.getDeviceId())) {
91 machine.query(maxResp);
92 }
93 }
94 }
95
Deepa Vaddireddy8467a922017-09-21 05:04:48 +000096 static void generalQuery(int maxResp) {
97 for (Map.Entry<String, SingleStateMachine> entry : map.entrySet()) {
98 SingleStateMachine machine = entry.getValue();
99 machine.query(maxResp);
100 }
101 }
102
ke han81a38b92017-03-10 18:41:44 +0800103 public static Set<Map.Entry<String, SingleStateMachine>> entrySet() {
104 return map.entrySet();
105 }
106
107 public static void timeOut(DeviceId devId, Ip4Address groupIp) {
108 SingleStateMachine machine = get(devId, groupIp);
109 if (null == machine) {
110 return;
111 }
112 machine.timeOut();
113 }
114
115 public static void clearMap() {
116 map.clear();
117 }
118
119}