summaryrefslogtreecommitdiffstats
path: root/src/mtp2_lsc.erl
blob: 04b21654d66820c43aa6ad2e435d75bb99b072c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
% MTP2 Link State Control according to Q.703 Figure 3 / Figure 8

% (C) 2011-2012 by Harald Welte <laforge@gnumonks.org>
%
% All Rights Reserved
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU Affero General Public License as
% published by the Free Software Foundation; either version 3 of the
% License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU Affero General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.
%
% Additional Permission under GNU AGPL version 3 section 7:
%
% If you modify this Program, or any covered work, by linking or
% combining it with runtime libraries of Erlang/OTP as released by
% Ericsson on http://www.erlang.org (or a modified version of these
% libraries), containing parts covered by the terms of the Erlang Public
% License (http://www.erlang.org/EPLICENSE), the licensors of this
% Program grant you additional permission to convey the resulting work
% without the need to license the runtime libraries of Erlang/OTP under
% the GNU Affero General Public License. Corresponding Source for a
% non-source form of such a combination shall include the source code
% for the parts of the runtime libraries of Erlang/OTP used as well as
% that of the covered work.

-module(mtp2_lsc).
-author('Harald Welte <laforge@gnumonks.org>').
-behaviour(gen_fsm).

% gen_fsm exports 
-export([init/1, terminate/3, code_change/4, handle_event/3, handle_info/3]).

% individual FSM states
-export([power_off/2, out_of_service/2, initial_alignment/2,
	 aligned_not_ready/2, aligned_ready/2, in_service/2,
	 processor_outage/2]).

% sync event handlers
-export([power_off/3]).

-record(lsc_state, {
		t1_timeout,
		t1,
		iac_pid,
		aerm_pid,
		l3_pid,
		poc_pid,
		txc_pid,
		rc_pid,
		local_proc_out,
		proc_out,
		emergency
	}).

-define(M2PA_T1_DEF,	300000).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% gen_fsm callbacks
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

init([Aerm, Rc, Txc, L3, Poc]) ->
	{ok, Iac} = gen_fsm:start_link(mtp2_iac, [self(), Aerm, Txc], [{debug, [trace]}]),
	LscState = #lsc_state{t1_timeout = ?M2PA_T1_DEF,
			      iac_pid = Iac,
			      aerm_pid = Aerm,
			      l3_pid = L3,
			      poc_pid = L3,
			      txc_pid = Txc,
			      rc_pid = Rc,
		      	      local_proc_out = 0,
		      	      proc_out = 0,
		      	      emergency = 0},
	{ok, power_off, LscState}.

terminate(Reason, State, _LoopDat) ->
	io:format("Terminating ~p in State ~p (Reason: ~p)~n",
		  [?MODULE, State, Reason]),
	ok.

code_change(_OldVsn, StateName, LoopDat, _Extra) ->
	{ok, StateName, LoopDat}.

handle_event(Event, State, LoopDat) ->
	io:format("Unknown Event ~p in state ~p~n", [Event, State]),
	{next_state, State, LoopDat}.


handle_info(Info, State, LoopDat) ->
	io:format("Unknown Info ~p in state ~p~n", [Info, State]),
	{next_state, State, LoopDat}.



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STATE: power_off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

power_off(power_on, LoopDat) ->
	% Power On from MGMT
	send_to(txc, start, LoopDat),
	send_to(txc, si_os, LoopDat),
	send_to(aerm, set_ti_to_tin, LoopDat),
	% Cancel local processor outage, cancel emergency
	{next_state, out_of_service, LoopDat#lsc_state{local_proc_out=0, emergency=0}}.

power_off(get_iac_pid, From, LoopDat) ->
	Iac = LoopDat#lsc_state.iac_pid,
	{reply, {ok, Iac}, power_off, LoopDat}.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STATE: out_of_service
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

out_of_service(start, LoopDat) ->
	% Start from L3
	send_to(rc, start, LoopDat),
	send_to(txc, start, LoopDat),
	case LoopDat#lsc_state.emergency of
		1 ->
			send_to(iac, emergency, LoopDat);
		_ ->
			ok
	end,
	send_to(iac, start, LoopDat),
	{next_state, initial_alignment, LoopDat};

out_of_service(retrieve_bsnt, LoopDat) ->
	send_to(rc, retrieve_bsnt, LoopDat),
	{next_state, out_of_service, LoopDat};

out_of_service(retrieval_request_and_fsnc, LoopDat) ->
	send_to(txc, retrieval_request_and_fsnc, LoopDat),
	{next_state, out_of_service, LoopDat};

out_of_service(emergency, LoopDat) ->
	{next_state, out_of_service, LoopDat#lsc_state{emergency=1}};

out_of_service(emergency_ceases, LoopDat) ->
	{next_state, out_of_service, LoopDat#lsc_state{emergency=0}};

out_of_service(What, LoopDat) when	What == local_processor_outage;
					What == level3_failure ->
	{next_state, out_of_service, LoopDat#lsc_state{local_proc_out=1}};

out_of_service(si_os, LoopDat) ->
	% this transition is not specified in Q.703, but it makes
	% quite a bit of sense.  yate M2PA requires it, too.
	{next_state, out_of_service, LoopDat};

out_of_service(local_processor_recovered, LoopDat) ->
	{next_state, out_of_service, LoopDat#lsc_state{local_proc_out=0}}.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STATE: initial_alignment
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

initial_alignment(What, LoopDat) when	What == local_processor_outage;
					What == level3_failure ->
	{next_state, initial_alignment, LoopDat#lsc_state{local_proc_out=1}};

initial_alignment(local_processor_recovered, LoopDat) ->
	{next_state, initial_alignment, LoopDat#lsc_state{local_proc_out=0}};

initial_alignment(emergency, LoopDat) ->
	send_to(iac, emergency, LoopDat),
	{next_state, initial_alignment, LoopDat#lsc_state{emergency=1}};

initial_alignment(alignment_complete, LoopDat) ->
	send_to(suerm, start, LoopDat),
	{ok, T1} = timer:apply_after(LoopDat#lsc_state.t1_timeout,
				     gen_fsm, send_event,
				     [self(), {timer_expired, t1}]),
	case LoopDat#lsc_state.local_proc_out of
		1 ->
			send_to(poc, local_processor_outage, LoopDat),
			send_to(txc, si_po, LoopDat),
			send_to(rc, reject_msu_fisu, LoopDat),
			NextState = aligned_not_ready;
		_ ->
			send_to(txc, fisu, LoopDat),
			send_to(rc, accept_msu_fisu, LoopDat),
			NextState = aligned_ready
	end,
	{next_state, NextState, LoopDat#lsc_state{t1=T1}};

initial_alignment(stop, LoopDat) ->
	send_to(iac, stop, LoopDat),
	send_to(rc, stop, LoopDat),
	send_to(txc, si_os, LoopDat),
	{next_state, out_of_service, LoopDat#lsc_state{local_proc_out=0, emergency=0}};

initial_alignment(link_failure, LoopDat) ->
	send_to(l3, out_of_service, LoopDat),
	send_to(iac, stop, LoopDat),
	send_to(rc, stop, LoopDat),
	send_to(txc, si_os, LoopDat),
	{next_state, out_of_service, LoopDat#lsc_state{local_proc_out=0, emergency=0}};

initial_alignment(alignment_not_possible, LoopDat) ->
	send_to(rc, stop, LoopDat),
	send_to(txc, si_os, LoopDat),
	{next_state, out_of_service, LoopDat#lsc_state{local_proc_out=0, emergency=0}};

% forward into IAC sub-state-machine
initial_alignment(What, LoopDat) when
		What == si_n; What == si_e; What == si_o; What == si_os;
		What == fisu_msu_received ->
	Iac = LoopDat#lsc_state.iac_pid,
	gen_fsm:send_event(Iac, What),
	{next_state, initial_alignment, LoopDat}.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STATE: aligned_ready
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

aligned_ready(SioOrSios, LoopDat) when SioOrSios == si_o;
					SioOrSios == si_os;
			       		SioOrSios == link_failure ->
	timer:cancel(LoopDat#lsc_state.t1),
	send_to(l3, out_of_service, LoopDat),
	send_to(rc, stop, LoopDat),
	send_to(suerm, stop, LoopDat),
	send_to(txc, si_os, LoopDat),
	{next_state, out_of_service, LoopDat};

aligned_ready(stop, LoopDat) ->
	timer:cancel(LoopDat#lsc_state.t1),
	send_to(rc, stop, LoopDat),
	send_to(suerm, stop, LoopDat),
	send_to(txc, si_os, LoopDat),
	{next_state, out_of_service, LoopDat};

aligned_ready({timer_expired, t1}, LoopDat) ->
	send_to(l3, out_of_service, LoopDat),
	send_to(rc, stop, LoopDat),
	send_to(suerm, stop, LoopDat),
	send_to(txc, si_os, LoopDat),
	{next_state, out_of_service, LoopDat};

aligned_ready(si_po, LoopDat) ->
	timer:cancel(LoopDat#lsc_state.t1),
	send_to(l3, remote_processor_outage, LoopDat),
	send_to(poc, remote_processor_outage, LoopDat),
	{next_state, processor_outage, LoopDat};

aligned_ready(fisu_msu_received, LoopDat) ->
	send_to(l3, in_service, LoopDat),
	timer:cancel(LoopDat#lsc_state.t1),
	send_to(txc, msu, LoopDat),
	{next_state, in_service, LoopDat};
aligned_ready(What, LoopDat) when	What == local_processor_outage;
					What == level3_failure ->
	send_to(poc, local_processor_outage, LoopDat),
	send_to(txc, si_po, LoopDat),
	send_to(rc, reject_msu_fisu, LoopDat),
	{next_state, aligned_not_ready, LoopDat}.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STATE: aligned_not_ready
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

aligned_not_ready(Err, LoopDat) when 	Err == link_failure;
					Err == si_o;
					Err == si_os ->
	timer:cancel(LoopDat#lsc_state.t1),
	send_to(l3, out_of_service, LoopDat),
	send_to(l3, stop, LoopDat),
	send_to(suerm, stop, LoopDat),
	send_to(txc, si_os, LoopDat),
	send_to(poc, stop, LoopDat),
	{next_state, out_of_service, LoopDat#lsc_state{local_proc_out=0, emergency=0}};

aligned_not_ready(stop, LoopDat) ->
	timer:cancel(LoopDat#lsc_state.t1),
	send_to(l3, stop, LoopDat),
	send_to(suerm, stop, LoopDat),
	send_to(txc, si_os, LoopDat),
	send_to(poc, stop, LoopDat),
	{next_state, out_of_service, LoopDat#lsc_state{local_proc_out=0, emergency=0}};

aligned_not_ready({timer_expired, t1}, LoopDat) ->
	send_to(l3, stop, LoopDat),
	send_to(suerm, stop, LoopDat),
	send_to(txc, si_os, LoopDat),
	send_to(poc, stop, LoopDat),
	{next_state, out_of_service, LoopDat#lsc_state{local_proc_out=0, emergency=0}};

aligned_not_ready(local_processor_recovered, LoopDat) ->
	send_to(poc, local_processor_recovered, LoopDat),
	send_to(txc, fisu, LoopDat),
	send_to(rc, accept_msu_fisu, LoopDat),
	{next_state, aligned_ready, LoopDat#lsc_state{local_proc_out=0}};

aligned_not_ready(fisu_msu_received, LoopDat) ->
	send_to(l3, in_service, LoopDat),
	timer:cancel(LoopDat#lsc_state.t1),
	{next_state, processor_outage, LoopDat};

aligned_not_ready(si_po, LoopDat) ->
	send_to(l3, remote_processor_outage, LoopDat),
	send_to(poc, remote_processor_outage, LoopDat),
	timer:cancel(LoopDat#lsc_state.t1),
	{next_state, processor_outage, LoopDat}.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STATE: in_service
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

in_service(What, LoopDat) when	What == link_failure;
				What == si_o;
				What == si_n;
				What == si_e;
				What == si_os ->
	send_to(l3, out_of_service, LoopDat),
	send_to(suerm, stop, LoopDat),
	send_to(rc, stop, LoopDat),
	send_to(txc, si_os, LoopDat),
	{next_state, out_of_service, LoopDat#lsc_state{emergency=0}};

in_service(stop, LoopDat) ->
	send_to(suerm, stop, LoopDat),
	send_to(rc, stop, LoopDat),
	send_to(txc, si_os, LoopDat),
	{next_state, out_of_service, LoopDat#lsc_state{emergency=0}};

in_service(What, LoopDat) when	What == local_processor_outage;
				What == level3_failure ->
	send_to(poc, local_processor_outage, LoopDat),
	send_to(txc, si_po, LoopDat),
	send_to(rc, reject_msu_fisu, LoopDat),
	{next_state, aligned_not_ready, LoopDat#lsc_state{local_proc_out=1}};

in_service(si_po, LoopDat) ->
	send_to(txc, fisu, LoopDat),
	send_to(l3, remote_processor_outage, LoopDat),
	send_to(poc, remote_processor_outage, LoopDat),
	{next_state, processor_outage, LoopDat#lsc_state{proc_out=1}}.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STATE: processor_outage
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

processor_outage(retrieval_request_and_fsnc, LoopDat) ->
	send_to(txc, retrieval_request_and_fsnc, LoopDat),
	{next_state, processor_outage, LoopDat};

processor_outage(fisu_msu_received, LoopDat) ->
	send_to(poc, remote_processor_recovered, LoopDat),
	send_to(l3, remote_processor_recovered, LoopDat),
	{next_state, processor_outage, LoopDat};

processor_outage(retrieve_bsnt, LoopDat) ->
	send_to(rc, retrieve_bsnt, LoopDat),
	{next_state, processor_outage, LoopDat};

processor_outage(What, LoopDat) when	What == local_processor_outage;
					What == level3_failure ->
	send_to(poc, local_processor_outage, LoopDat),
	send_to(txc, si_po, LoopDat),
	{next_state, processor_outage, LoopDat#lsc_state{local_proc_out=1}};

processor_outage(si_po, LoopDat) ->
	send_to(l3, remote_processor_outage, LoopDat),
	send_to(poc, remote_processor_outage, LoopDat),
	{next_state, processor_outage, LoopDat#lsc_state{proc_out=1}};

processor_outage(local_processor_recovered, LoopDat) ->
	send_to(poc, local_processor_recovered, LoopDat),
	send_to(rc, retrieve_fsnx, LoopDat),
	send_to(txc, fisu, LoopDat),
	{next_state, processor_outage, LoopDat};

processor_outage(flush_buffers, LoopDat) ->
	send_to(txc, flush_buffers, LoopDat),
	% FIXME: mark L3 ind recv
	{next_state, processor_outage, LoopDat};

processor_outage(no_processor_outage, LoopDat) ->
	% FIXME: check L3 ind
	send_to(txc, msu, LoopDat),
	send_to(rc, accept_msu_fisu, LoopDat),
	{next_state, in_service, LoopDat#lsc_state{local_proc_out=0, proc_out=0}};

processor_outage(What, LoopDat) when	What == link_failure;
					What == si_o;
					What == si_n;
					What == si_e;
					What == si_os ->
	send_to(l3, out_of_service, LoopDat),
	send_to(suerm, stop, LoopDat),
	send_to(rc, stop, LoopDat),
	send_to(poc, stop, LoopDat),
	send_to(txc, si_os, LoopDat),
	{next_state, out_of_service, LoopDat#lsc_state{emergency=0, local_proc_out=0}};

processor_outage(stop, LoopDat) ->
	send_to(suerm, stop, LoopDat),
	send_to(rc, stop, LoopDat),
	send_to(poc, stop, LoopDat),
	send_to(txc, si_os, LoopDat),
	{next_state, out_of_service, LoopDat#lsc_state{emergency=0, local_proc_out=0}}.



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% helper functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
send_to(txc, What, #lsc_state{txc_pid = Txc}) ->
	Txc ! {lsc_txc, What};
send_to(iac, What, #lsc_state{iac_pid = Iac}) ->
	gen_fsm:send_event(Iac, What);
send_to(rc, What, #lsc_state{rc_pid = Rc}) ->
	Rc ! {lsc_rc, What};
send_to(Who, What, _LoopDat) ->
	io:format("Not sending LSC -> ~p: ~p~n", [Who, What]).