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 | */ |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 16 | package org.opencord.igmpproxy; |
| 17 | |
| 18 | import com.google.common.collect.Maps; |
| 19 | import org.onlab.packet.Ip4Address; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | import java.util.Map; |
| 22 | import java.util.Set; |
| 23 | |
| 24 | /** |
David K. Bainbridge | d77028f | 2017-08-01 12:47:55 -0700 | [diff] [blame] | 25 | * State machine for whole IGMP process. The state machine is implemented on |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 26 | * RFC 2236 "6. Host State Diagram". |
| 27 | */ |
| 28 | public 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 Vaddireddy | 8467a92 | 2017-09-21 05:04:48 +0000 | [diff] [blame] | 96 | 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 han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 103 | 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 | } |