blob: 16750692a61b7dca1232524df92cfb0ff3b122ba [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 org.onlab.packet.Ethernet;
19import org.onlab.packet.Ip4Address;
20import org.onosproject.net.DeviceId;
21
22import java.util.Random;
23
24/**
David K. Bainbridged77028f2017-08-01 12:47:55 -070025 * State machine for single IGMP group member. The state machine is implemented on
ke han81a38b92017-03-10 18:41:44 +080026 * RFC 2236 "6. Host State Diagram".
27 */
28public class SingleStateMachine {
29 static final int STATE_NON = 0;
30 static final int STATE_DELAY = 1;
31 static final int STATE_IDLE = 2;
32 static final int TRANSITION_JOIN = 0;
33 static final int TRANSITION_LEAVE = 1;
34 static final int TRANSITION_QUERY = 2;
35 static final int TRANSITION_TIMEOUT = 3;
36 static final int DEFAULT_MAX_RESP = 0xfffffff;
37 static final int DEFAULT_COUNT = 1;
38 private DeviceId devId;
39 private Ip4Address groupIp;
40 private Ip4Address srcIp;
41
42 private int count = DEFAULT_COUNT;
43 private int timerId = IgmpTimer.INVALID_TIMER_ID;
44 private int timeOut = DEFAULT_MAX_RESP;
45 private State[] states =
46 {
47 new NonMember(), new DelayMember(), new IdleMember()
48 };
49 private int[] nonTransition =
50 {STATE_DELAY, STATE_NON, STATE_NON, STATE_NON};
51 private int[] delayTransition =
52 {STATE_DELAY, STATE_NON, STATE_DELAY, STATE_IDLE};
53 private int[] idleTransition =
54 {STATE_IDLE, STATE_NON, STATE_DELAY, STATE_IDLE};
55 //THE TRANSITION TABLE
56 private int[][] transition =
57 {nonTransition, delayTransition, idleTransition};
58 private int currentState = STATE_NON;
59
60 public SingleStateMachine(DeviceId devId, Ip4Address groupIp, Ip4Address src) {
61 this.devId = devId;
62 this.groupIp = groupIp;
63 this.srcIp = src;
64 }
65
66
67 public DeviceId getDeviceId() {
68 return devId;
69 }
70 public boolean increaseCounter() {
71 count++;
72 return true;
73 }
74
75 public boolean decreaseCounter() {
76 if (count > 0) {
77 count--;
78 return true;
79 } else {
80 return false;
81 }
82 }
83
84 public int getCounter() {
85 return count;
86 }
87 public int currentState() {
88 return currentState;
89 }
90
91 private void next(int msg) {
92 currentState = transition[currentState][msg];
93 }
94
95 public void join() {
96 states[currentState].join();
97 next(TRANSITION_JOIN);
98 }
99
100 public void leave() {
101 states[currentState].leave();
102 next(TRANSITION_LEAVE);
103 }
104
105 public void query(int maxResp) {
106 states[currentState].query(maxResp);
107 next(TRANSITION_QUERY);
108 }
109
110 public void timeOut() {
111 states[currentState].timeOut();
112 next(TRANSITION_TIMEOUT);
113 }
114
115 int getTimeOut(int maxTimeOut) {
116 Random random = new Random();
117 return Math.abs(random.nextInt()) % maxTimeOut;
118 }
119
120 protected void cancelTimer() {
121 if (IgmpTimer.INVALID_TIMER_ID != timerId) {
122 IgmpTimer.cancel(timerId);
123 }
124 }
125
126 class State {
127 public void join() {
128 }
129
130 public void leave() {
131 Ethernet eth = IgmpSender.getInstance().buildIgmpV3Leave(groupIp, srcIp);
132 IgmpSender.getInstance().sendIgmpPacketUplink(eth, devId);
133 }
134
135 public void query(int maxResp) {
136 }
137
138 public void timeOut() {
139 }
140
141 }
142
143 class NonMember extends State {
144 public void join() {
145 Ethernet eth = IgmpSender.getInstance().buildIgmpV3Join(groupIp, srcIp);
146 IgmpSender.getInstance().sendIgmpPacketUplink(eth, devId);
147 timeOut = getTimeOut(IgmpManager.getUnsolicitedTimeout());
148 timerId = IgmpTimer.start(SingleStateMachine.this, timeOut);
149 }
150 }
151
152 class DelayMember extends State {
153 public void query(int maxResp) {
154 if (maxResp < timeOut) {
155 timeOut = getTimeOut(maxResp);
156 timerId = IgmpTimer.reset(timerId, SingleStateMachine.this, timeOut);
157 }
158 }
159
160 public void timeOut() {
161 Ethernet eth = IgmpSender.getInstance().buildIgmpV3ResponseQuery(groupIp, srcIp);
162 IgmpSender.getInstance().sendIgmpPacketUplink(eth, devId);
163 timeOut = DEFAULT_MAX_RESP;
164 }
165
166 }
167
168 class IdleMember extends State {
169 public void query(int maxResp) {
170 timeOut = getTimeOut(maxResp);
171 timerId = IgmpTimer.start(SingleStateMachine.this, timeOut);
172 }
173 }
174}