PageRenderTime 59ms CodeModel.GetById 10ms app.highlight 42ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/m3ua/impl/src/test/java/org/mobicents/protocols/ss7/m3ua/impl/sg/SgFSMTest.java

http://mobicents.googlecode.com/
Java | 757 lines | 468 code | 130 blank | 159 comment | 25 complexity | bf91a7dbe5f4cfdeb6f1e575d52e434d MD5 | raw file
  1/*
  2 * JBoss, Home of Professional Open Source
  3 * Copyright 2011, Red Hat, Inc. and individual contributors
  4 * by the @authors tag. See the copyright.txt in the distribution for a
  5 * full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22
 23package org.mobicents.protocols.ss7.m3ua.impl.sg;
 24
 25import static org.junit.Assert.assertEquals;
 26import static org.junit.Assert.assertNotNull;
 27import static org.junit.Assert.assertNull;
 28import static org.junit.Assert.assertTrue;
 29
 30import java.io.IOException;
 31
 32import org.junit.After;
 33import org.junit.AfterClass;
 34import org.junit.Before;
 35import org.junit.BeforeClass;
 36import org.junit.Test;
 37import org.mobicents.protocols.ss7.m3ua.impl.As;
 38import org.mobicents.protocols.ss7.m3ua.impl.AsState;
 39import org.mobicents.protocols.ss7.m3ua.impl.Asp;
 40import org.mobicents.protocols.ss7.m3ua.impl.AspFactory;
 41import org.mobicents.protocols.ss7.m3ua.impl.AspState;
 42import org.mobicents.protocols.ss7.m3ua.impl.CommunicationListener.CommunicationState;
 43import org.mobicents.protocols.ss7.m3ua.impl.message.M3UAMessageImpl;
 44import org.mobicents.protocols.ss7.m3ua.impl.message.MessageFactoryImpl;
 45import org.mobicents.protocols.ss7.m3ua.impl.message.asptm.ASPActiveImpl;
 46import org.mobicents.protocols.ss7.m3ua.impl.message.asptm.ASPInactiveImpl;
 47import org.mobicents.protocols.ss7.m3ua.impl.message.transfer.PayloadDataImpl;
 48import org.mobicents.protocols.ss7.m3ua.impl.parameter.ParameterFactoryImpl;
 49import org.mobicents.protocols.ss7.m3ua.impl.parameter.ProtocolDataImpl;
 50import org.mobicents.protocols.ss7.m3ua.message.M3UAMessage;
 51import org.mobicents.protocols.ss7.m3ua.message.MessageClass;
 52import org.mobicents.protocols.ss7.m3ua.message.MessageType;
 53import org.mobicents.protocols.ss7.m3ua.message.mgmt.Notify;
 54import org.mobicents.protocols.ss7.m3ua.parameter.DestinationPointCode;
 55import org.mobicents.protocols.ss7.m3ua.parameter.ErrorCode;
 56import org.mobicents.protocols.ss7.m3ua.parameter.LocalRKIdentifier;
 57import org.mobicents.protocols.ss7.m3ua.parameter.RoutingContext;
 58import org.mobicents.protocols.ss7.m3ua.parameter.RoutingKey;
 59import org.mobicents.protocols.ss7.m3ua.parameter.ServiceIndicators;
 60import org.mobicents.protocols.ss7.m3ua.parameter.Status;
 61import org.mobicents.protocols.ss7.m3ua.parameter.TrafficModeType;
 62
 63/**
 64 * 
 65 * @author amit bhayani
 66 * 
 67 */
 68public class SgFSMTest {
 69
 70	private ParameterFactoryImpl parmFactory = new ParameterFactoryImpl();
 71	private MessageFactoryImpl messageFactory = new MessageFactoryImpl();
 72	private ServerM3UAManagement serverM3UAMgmt = new ServerM3UAManagement();
 73
 74	public SgFSMTest() {
 75	}
 76
 77	@BeforeClass
 78	public static void setUpClass() throws Exception {
 79	}
 80
 81	@AfterClass
 82	public static void tearDownClass() throws Exception {
 83	}
 84
 85	@Before
 86	public void setUp() throws IOException {
 87		serverM3UAMgmt.start();
 88
 89	}
 90
 91	@After
 92	public void tearDown() throws IOException {
 93		serverM3UAMgmt.getAppServers().clear();
 94		serverM3UAMgmt.getAspfactories().clear();
 95		serverM3UAMgmt.stop();
 96	}
 97
 98	@Test
 99	public void testSingleAspInAs() throws Exception {
100		// 5.1.1. Single ASP in an Application Server ("1+0" sparing),
101
102		RoutingContext rc = parmFactory.createRoutingContext(new long[] { 100 });
103
104		DestinationPointCode[] dpc = new DestinationPointCode[] { parmFactory
105				.createDestinationPointCode(123, (short) 0) };
106
107		ServiceIndicators[] servInds = new ServiceIndicators[] { parmFactory.createServiceIndicators(new short[] { 3 }) };
108
109		TrafficModeType trModType = parmFactory.createTrafficModeType(TrafficModeType.Override);
110		LocalRKIdentifier lRkId = parmFactory.createLocalRKIdentifier(1);
111		RoutingKey rKey = parmFactory.createRoutingKey(lRkId, rc, null, null, dpc, servInds, null);
112
113		// As remAs = sgw.createAppServer("testas", rc, rKey, trModType);
114		As remAs = serverM3UAMgmt.createAppServer("m3ua ras create rc 100 rk dpc 123 si 3 traffic-mode override testas"
115				.split(" "));
116		// AspFactory aspFactory = sgw.createAspFactory("testasp", "127.0.0.1",
117		// 2777);
118		AspFactory aspFactory = serverM3UAMgmt.createAspFactory("m3ua rasp create ip 127.0.0.1 port 2777 testasp"
119				.split(" "));
120
121		Asp remAsp = serverM3UAMgmt.assignAspToAs("testas", "testasp");
122
123		// Check for Communication UP
124		aspFactory.onCommStateChange(CommunicationState.UP);
125
126		assertEquals(AspState.DOWN, remAsp.getState());
127
128		// Check for ASP_UP
129		M3UAMessageImpl message = messageFactory.createMessage(MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_UP);
130		aspFactory.read(message);
131
132		assertEquals(AspState.INACTIVE, remAsp.getState());
133		assertTrue(validateMessage(aspFactory, MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_UP_ACK, -1, -1));
134		// also the AS should be INACTIVE now
135		assertEquals(AsState.INACTIVE, remAs.getState());
136		assertTrue(validateMessage(aspFactory, MessageClass.MANAGEMENT, MessageType.NOTIFY,
137				Status.STATUS_AS_State_Change, Status.INFO_AS_INACTIVE));
138
139		// Check for ASP_ACTIVE
140		message = messageFactory.createMessage(MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE);
141		((ASPActiveImpl) message).setRoutingContext(rc);
142		aspFactory.read(message);
143		assertEquals(AspState.ACTIVE, remAsp.getState());
144		assertTrue(validateMessage(aspFactory, MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE_ACK, -1, -1));
145		// also the AS should be ACTIVE now
146		assertEquals(AsState.ACTIVE, remAs.getState());
147		assertTrue(validateMessage(aspFactory, MessageClass.MANAGEMENT, MessageType.NOTIFY,
148				Status.STATUS_AS_State_Change, Status.INFO_AS_ACTIVE));
149
150		// Check for ASP_INACTIVE
151		message = messageFactory.createMessage(MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_INACTIVE);
152		aspFactory.read(message);
153		assertEquals(AspState.INACTIVE, remAsp.getState());
154		assertTrue(validateMessage(aspFactory, MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_INACTIVE_ACK, -1,
155				-1));
156		// also the AS should be PENDING now
157		assertEquals(AsState.PENDING, remAs.getState());
158		assertTrue(validateMessage(aspFactory, MessageClass.MANAGEMENT, MessageType.NOTIFY,
159				Status.STATUS_AS_State_Change, Status.INFO_AS_PENDING));
160
161		// Check for ASP_DOWN
162		message = messageFactory.createMessage(MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_DOWN);
163		aspFactory.read(message);
164		assertEquals(AspState.DOWN, remAsp.getState());
165		assertTrue(validateMessage(aspFactory, MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_DOWN_ACK, -1, -1));
166
167		// Make sure we don't have any more
168		assertNull(aspFactory.txPoll());
169	}
170
171	@Test
172	public void testSingleAspInMultipleAs() throws Exception {
173		// 5.1.1.3. Single ASP in Multiple Application Servers (Each with "1+0"
174		// Sparing)
175
176		// Define 1st AS
177		RoutingContext rc1 = parmFactory.createRoutingContext(new long[] { 100 });
178
179		DestinationPointCode[] dpc1 = new DestinationPointCode[] { parmFactory.createDestinationPointCode(123,
180				(short) 0) };
181
182		ServiceIndicators[] servInds1 = new ServiceIndicators[] { parmFactory
183				.createServiceIndicators(new short[] { 3 }) };
184
185		TrafficModeType trModType1 = parmFactory.createTrafficModeType(TrafficModeType.Override);
186		LocalRKIdentifier lRkId1 = parmFactory.createLocalRKIdentifier(1);
187		RoutingKey rKey1 = parmFactory.createRoutingKey(lRkId1, rc1, null, null, dpc1, servInds1, null);
188
189		// As remAs1 = sgw.createAppServer("testas1", rc1, rKey1, trModType1);
190		As remAs1 = serverM3UAMgmt
191				.createAppServer("m3ua ras create rc 100 rk dpc 123 si 3 traffic-mode override testas1".split(" "));
192
193		// Define 2nd AS
194		RoutingContext rc2 = parmFactory.createRoutingContext(new long[] { 200 });
195
196		DestinationPointCode[] dpc2 = new DestinationPointCode[] { parmFactory.createDestinationPointCode(124,
197				(short) 0) };
198
199		ServiceIndicators[] servInds2 = new ServiceIndicators[] { parmFactory
200				.createServiceIndicators(new short[] { 3 }) };
201
202		TrafficModeType trModType2 = parmFactory.createTrafficModeType(TrafficModeType.Override);
203		LocalRKIdentifier lRkId2 = parmFactory.createLocalRKIdentifier(1);
204		RoutingKey rKey2 = parmFactory.createRoutingKey(lRkId2, rc2, null, null, dpc2, servInds2, null);
205
206		// As remAs2 = sgw.createAppServer("testas2", rc2, rKey2, trModType2);
207		As remAs2 = serverM3UAMgmt
208				.createAppServer("m3ua ras create rc 200 rk dpc 124 si 3 traffic-mode override testas2".split(" "));
209
210		// AspFactory aspFactory = sgw.createAspFactory("testasp", "127.0.0.1",
211		// 2777);
212		AspFactory aspFactory = serverM3UAMgmt.createAspFactory("m3ua rasp create ip 127.0.0.1 port 2777 testasp"
213				.split(" "));
214
215		// Both ASP uses same underlying M3UAChannel
216		Asp remAsp1 = serverM3UAMgmt.assignAspToAs("testas1", "testasp");
217		Asp remAsp2 = serverM3UAMgmt.assignAspToAs("testas2", "testasp");
218
219		// Check for Communication UP
220		aspFactory.onCommStateChange(CommunicationState.UP);
221		assertEquals(AspState.DOWN, remAsp1.getState());
222		assertEquals(AspState.DOWN, remAsp2.getState());
223		// Both AS are yet DOWN
224		assertEquals(AsState.DOWN, remAs1.getState());
225		assertEquals(AsState.DOWN, remAs2.getState());
226
227		// Check for ASP_UP
228		M3UAMessageImpl message = messageFactory.createMessage(MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_UP);
229		aspFactory.read(message);
230		assertEquals(AspState.INACTIVE, remAsp1.getState());
231		assertEquals(AspState.INACTIVE, remAsp2.getState());
232		assertTrue(validateMessage(aspFactory, MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_UP_ACK, -1, -1));
233		// also both the AS should be INACTIVE now
234		assertEquals(AsState.INACTIVE, remAs1.getState());
235		assertTrue(validateMessage(aspFactory, MessageClass.MANAGEMENT, MessageType.NOTIFY,
236				Status.STATUS_AS_State_Change, Status.INFO_AS_INACTIVE));
237		assertEquals(AsState.INACTIVE, remAs2.getState());
238		assertTrue(validateMessage(aspFactory, MessageClass.MANAGEMENT, MessageType.NOTIFY,
239				Status.STATUS_AS_State_Change, Status.INFO_AS_INACTIVE));
240
241		// Check for ASP_ACTIVE for both Routing Contexts
242		message = messageFactory.createMessage(MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE);
243		((ASPActiveImpl) message).setRoutingContext(this.parmFactory.createRoutingContext(new long[] { 100, 200 }));
244		aspFactory.read(message);
245		assertEquals(AspState.ACTIVE, remAsp1.getState());
246		assertEquals(AspState.ACTIVE, remAsp2.getState());
247		assertTrue(validateMessage(aspFactory, MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE_ACK, -1, -1));
248		// also both the AS should be ACTIVE now
249		assertEquals(AsState.ACTIVE, remAs1.getState());
250		assertTrue(validateMessage(aspFactory, MessageClass.MANAGEMENT, MessageType.NOTIFY,
251				Status.STATUS_AS_State_Change, Status.INFO_AS_ACTIVE));
252		// We will have two ACK's one each for each RC
253		assertTrue(validateMessage(aspFactory, MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE_ACK, -1, -1));
254
255		assertEquals(AsState.ACTIVE, remAs2.getState());
256		assertTrue(validateMessage(aspFactory, MessageClass.MANAGEMENT, MessageType.NOTIFY,
257				Status.STATUS_AS_State_Change, Status.INFO_AS_ACTIVE));
258
259		// Check for ASP_INACTIVE for ASP1
260		message = messageFactory.createMessage(MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_INACTIVE);
261		((ASPInactiveImpl) message).setRoutingContext(this.parmFactory.createRoutingContext(new long[] { 100 }));
262		aspFactory.read(message);
263
264		assertEquals(AspState.INACTIVE, remAsp1.getState());
265		// The ASP2 should still be ACTIVE as we sent ASP_INACTIVE only for 100
266		// RC
267		assertEquals(AspState.ACTIVE, remAsp2.getState());
268		assertTrue(validateMessage(aspFactory, MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_INACTIVE_ACK, -1,
269				-1));
270		// AS1 should be PENDING now
271		assertEquals(AsState.PENDING, remAs1.getState());
272		assertTrue(validateMessage(aspFactory, MessageClass.MANAGEMENT, MessageType.NOTIFY,
273				Status.STATUS_AS_State_Change, Status.INFO_AS_PENDING));
274
275		// But AS2 is still ACTIVE
276		assertEquals(AsState.ACTIVE, remAs2.getState());
277
278		// Check for ASP_DOWN
279		message = messageFactory.createMessage(MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_DOWN);
280		aspFactory.read(message);
281		assertEquals(AspState.DOWN, remAsp1.getState());
282		assertEquals(AspState.DOWN, remAsp2.getState());
283		assertTrue(validateMessage(aspFactory, MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_DOWN_ACK, -1, -1));
284
285		// Make sure we don't have any more messages to be sent
286		assertNull(aspFactory.txPoll());
287
288	}
289
290	@Test
291	public void testTwoAspInAsOverride() throws Exception {
292		// 5.1.2. Two ASPs in Application Server ("1+1" Sparing)
293
294		RoutingContext rc = parmFactory.createRoutingContext(new long[] { 100 });
295
296		DestinationPointCode[] dpc = new DestinationPointCode[] { parmFactory
297				.createDestinationPointCode(123, (short) 0) };
298
299		ServiceIndicators[] servInds = new ServiceIndicators[] { parmFactory.createServiceIndicators(new short[] { 3 }) };
300
301		TrafficModeType trModType = parmFactory.createTrafficModeType(TrafficModeType.Override);
302		LocalRKIdentifier lRkId = parmFactory.createLocalRKIdentifier(1);
303		RoutingKey rKey = parmFactory.createRoutingKey(lRkId, rc, null, null, dpc, servInds, null);
304
305		// As remAs = sgw.createAppServer("testas", rc, rKey, trModType);
306		As remAs = serverM3UAMgmt.createAppServer("m3ua ras create rc 100 rk dpc 123 si 3 traffic-mode override testas"
307				.split(" "));
308
309		// AspFactory aspFactory1 = sgw.createAspFactory("testasp1",
310		// "127.0.0.1", 2777);
311		AspFactory aspFactory1 = serverM3UAMgmt.createAspFactory("m3ua rasp create ip 127.0.0.1 port 2777 testasp1"
312				.split(" "));
313
314		// AspFactory aspFactory2 = sgw.createAspFactory("testasp2",
315		// "127.0.0.1", 2778);
316		AspFactory aspFactory2 = serverM3UAMgmt.createAspFactory("m3ua rasp create ip 127.0.0.1 port 2778 testasp2"
317				.split(" "));
318
319		Asp remAsp1 = serverM3UAMgmt.assignAspToAs("testas", "testasp1");
320		Asp remAsp2 = serverM3UAMgmt.assignAspToAs("testas", "testasp2");
321
322		// Check for Communication UP for ASP1
323		aspFactory1.onCommStateChange(CommunicationState.UP);
324		assertEquals(AspState.DOWN, remAsp1.getState());
325
326		// Check for Communication UP for ASP2
327		aspFactory2.onCommStateChange(CommunicationState.UP);
328		assertEquals(AspState.DOWN, remAsp2.getState());
329
330		// Check for ASP_UP for ASP1
331		M3UAMessageImpl message = messageFactory.createMessage(MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_UP);
332		aspFactory1.read(message);
333		assertEquals(AspState.INACTIVE, remAsp1.getState());
334		assertTrue(validateMessage(aspFactory1, MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_UP_ACK, -1, -1));
335		// also the AS should be INACTIVE now
336		assertEquals(AsState.INACTIVE, remAs.getState());
337		assertTrue(validateMessage(aspFactory1, MessageClass.MANAGEMENT, MessageType.NOTIFY,
338				Status.STATUS_AS_State_Change, Status.INFO_AS_INACTIVE));
339
340		// Check for ASP_UP for ASP2
341		aspFactory2.read(message);
342		assertEquals(AspState.INACTIVE, remAsp2.getState());
343		assertTrue(validateMessage(aspFactory2, MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_UP_ACK, -1, -1));
344		// also the AS should be INACTIVE now
345		assertEquals(AsState.INACTIVE, remAs.getState());
346		assertTrue(validateMessage(aspFactory2, MessageClass.MANAGEMENT, MessageType.NOTIFY,
347				Status.STATUS_AS_State_Change, Status.INFO_AS_INACTIVE));
348
349		// Check for ASP_ACTIVE for ASP1
350		message = messageFactory.createMessage(MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE);
351		((ASPActiveImpl) message).setRoutingContext(rc);
352		aspFactory1.read(message);
353		assertEquals(AspState.ACTIVE, remAsp1.getState());
354		assertTrue(validateMessage(aspFactory1, MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE_ACK, -1,
355				-1));
356		// also the AS should be ACTIVE now and ACTIVE should be delivered to
357		// both the ASPs
358		assertEquals(AsState.ACTIVE, remAs.getState());
359		assertTrue(validateMessage(aspFactory1, MessageClass.MANAGEMENT, MessageType.NOTIFY,
360				Status.STATUS_AS_State_Change, Status.INFO_AS_ACTIVE));
361		assertTrue(validateMessage(aspFactory2, MessageClass.MANAGEMENT, MessageType.NOTIFY,
362				Status.STATUS_AS_State_Change, Status.INFO_AS_ACTIVE));
363
364		// INACTIVATE the ASP1
365		// 5.2.1. 1+1 Sparing, Withdrawal of ASP, Backup Override
366		message = messageFactory.createMessage(MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_INACTIVE);
367		aspFactory1.read(message);
368		assertEquals(AspState.INACTIVE, remAsp1.getState());
369		assertTrue(validateMessage(aspFactory1, MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_INACTIVE_ACK, -1,
370				-1));
371		// also the AS should be PENDING now and should send PENDING NTFY to
372		// both the ASPS
373		assertEquals(AsState.PENDING, remAs.getState());
374		assertTrue(validateMessage(aspFactory1, MessageClass.MANAGEMENT, MessageType.NOTIFY,
375				Status.STATUS_AS_State_Change, Status.INFO_AS_PENDING));
376		assertTrue(validateMessage(aspFactory2, MessageClass.MANAGEMENT, MessageType.NOTIFY,
377				Status.STATUS_AS_State_Change, Status.INFO_AS_PENDING));
378
379		// ACTIVATE ASP2
380		message = messageFactory.createMessage(MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE);
381		((ASPActiveImpl) message).setRoutingContext(rc);
382		aspFactory2.read(message);
383		assertEquals(AspState.ACTIVE, remAsp2.getState());
384		assertTrue(validateMessage(aspFactory2, MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE_ACK, -1,
385				-1));
386		// also the AS should be ACTIVE now and ACTIVE should be delivered to
387		// both the ASPs
388		assertEquals(AsState.ACTIVE, remAs.getState());
389		assertTrue(validateMessage(aspFactory2, MessageClass.MANAGEMENT, MessageType.NOTIFY,
390				Status.STATUS_AS_State_Change, Status.INFO_AS_ACTIVE));
391		assertTrue(validateMessage(aspFactory1, MessageClass.MANAGEMENT, MessageType.NOTIFY,
392				Status.STATUS_AS_State_Change, Status.INFO_AS_ACTIVE));
393
394		// 5.2.2. 1+1 Sparing, Backup Override
395		// ACTIVATE ASP1 also
396		message = messageFactory.createMessage(MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE);
397		((ASPActiveImpl) message).setRoutingContext(rc);
398		aspFactory1.read(message);
399		assertEquals(AspState.ACTIVE, remAsp1.getState());
400		assertTrue(validateMessage(aspFactory1, MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE_ACK, -1,
401				-1));
402		// The AS remains ACTIVE and sends NTFY(Alt ASP-Act) to ASP2
403		assertEquals(AsState.ACTIVE, remAs.getState());
404
405		// ASP2 should get Alternate ASP is active
406		assertTrue(validateMessage(aspFactory2, MessageClass.MANAGEMENT, MessageType.NOTIFY, Status.STATUS_Other,
407				Status.INFO_Alternate_ASP_Active));
408		// The state of ASP2 now should be INACTIVE
409		assertEquals(AspState.INACTIVE, remAsp2.getState());
410
411		assertNull(aspFactory1.txPoll());
412		assertNull(aspFactory2.txPoll());
413
414	}
415
416	@Test
417	public void testTwoAspInAsLoadshare() throws Exception {
418		// 5.1.2. Two ASPs in Application Server ("1+1" Sparing)
419		RoutingContext rc = parmFactory.createRoutingContext(new long[] { 100 });
420
421		DestinationPointCode[] dpc = new DestinationPointCode[] { parmFactory
422				.createDestinationPointCode(123, (short) 0) };
423
424		ServiceIndicators[] servInds = new ServiceIndicators[] { parmFactory.createServiceIndicators(new short[] { 3 }) };
425
426		TrafficModeType trModType = parmFactory.createTrafficModeType(TrafficModeType.Loadshare);
427		LocalRKIdentifier lRkId = parmFactory.createLocalRKIdentifier(1);
428		RoutingKey rKey = parmFactory.createRoutingKey(lRkId, rc, null, null, dpc, servInds, null);
429
430		// As remAs = sgw.createAppServer("testas", rc, rKey, trModType);
431		As remAs = serverM3UAMgmt
432				.createAppServer("m3ua ras create rc 100 rk dpc 123 si 3 traffic-mode loadshare testas".split(" "));
433		// 2+0 sparing loadsharing
434		remAs.setMinAspActiveForLb(2);
435
436		// AspFactory aspFactory1 = sgw.createAspFactory("testasp1",
437		// "127.0.0.1", 2777);
438		AspFactory aspFactory1 = serverM3UAMgmt.createAspFactory("m3ua rasp create ip 127.0.0.1 port 2777 testasp1"
439				.split(" "));
440
441		// AspFactory aspFactory2 = sgw.createAspFactory("testasp2",
442		// "127.0.0.1", 2778);
443		AspFactory aspFactory2 = serverM3UAMgmt.createAspFactory("m3ua rasp create ip 127.0.0.1 port 2778 testasp2"
444				.split(" "));
445
446		Asp remAsp1 = serverM3UAMgmt.assignAspToAs("testas", "testasp1");
447		Asp remAsp2 = serverM3UAMgmt.assignAspToAs("testas", "testasp2");
448
449		// Check for Communication UP for ASP1
450		aspFactory1.onCommStateChange(CommunicationState.UP);
451		assertEquals(AspState.DOWN, remAsp1.getState());
452
453		// Check for Communication UP for ASP2
454		aspFactory2.onCommStateChange(CommunicationState.UP);
455		assertEquals(AspState.DOWN, remAsp2.getState());
456
457		// Check for ASP_UP for ASP1
458		M3UAMessageImpl message = messageFactory.createMessage(MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_UP);
459		aspFactory1.read(message);
460		assertEquals(AspState.INACTIVE, remAsp1.getState());
461		assertTrue(validateMessage(aspFactory1, MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_UP_ACK, -1, -1));
462		// also the AS should be INACTIVE now
463		assertEquals(AsState.INACTIVE, remAs.getState());
464		assertTrue(validateMessage(aspFactory1, MessageClass.MANAGEMENT, MessageType.NOTIFY,
465				Status.STATUS_AS_State_Change, Status.INFO_AS_INACTIVE));
466
467		// Check for ASP_UP for ASP2
468		message = messageFactory.createMessage(MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_UP);
469		aspFactory2.read(message);
470		assertEquals(AspState.INACTIVE, remAsp2.getState());
471		assertTrue(validateMessage(aspFactory2, MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_UP_ACK, -1, -1));
472		// also the AS should be INACTIVE
473		assertEquals(AsState.INACTIVE, remAs.getState());
474		assertTrue(validateMessage(aspFactory2, MessageClass.MANAGEMENT, MessageType.NOTIFY,
475				Status.STATUS_AS_State_Change, Status.INFO_AS_INACTIVE));
476
477		// Check for ASP_ACTIVE for ASP1
478		message = messageFactory.createMessage(MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE);
479		((ASPActiveImpl) message).setRoutingContext(rc);
480		aspFactory1.read(message);
481		assertEquals(AspState.ACTIVE, remAsp1.getState());
482		assertTrue(validateMessage(aspFactory1, MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE_ACK, -1,
483				-1));
484		// But AS still INACTIVE as atleast 2 ASP's should be ACTIVE
485		assertEquals(AsState.INACTIVE, remAs.getState());
486
487		// Check for ASP_ACTIVE for ASP2
488		message = messageFactory.createMessage(MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE);
489		((ASPActiveImpl) message).setRoutingContext(rc);
490		aspFactory2.read(message);
491		assertEquals(AspState.ACTIVE, remAsp2.getState());
492		assertTrue(validateMessage(aspFactory2, MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE_ACK, -1,
493				-1));
494		// Now AS will be ACTIVE and send NTFY to both the ASP's
495		assertEquals(AsState.ACTIVE, remAs.getState());
496		assertTrue(validateMessage(aspFactory1, MessageClass.MANAGEMENT, MessageType.NOTIFY,
497				Status.STATUS_AS_State_Change, Status.INFO_AS_ACTIVE));
498		assertTrue(validateMessage(aspFactory2, MessageClass.MANAGEMENT, MessageType.NOTIFY,
499				Status.STATUS_AS_State_Change, Status.INFO_AS_ACTIVE));
500
501		// INACTIVATE ASP1.But AS remains ACTIVE in any case
502		message = messageFactory.createMessage(MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_INACTIVE);
503		aspFactory1.read(message);
504		assertEquals(AspState.INACTIVE, remAsp1.getState());
505		assertTrue(validateMessage(aspFactory1, MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_INACTIVE_ACK, -1,
506				-1));
507		// ASP1 also receives NTFY Ins ASP Resource as we have fallen bellow
508		// threshold
509		assertTrue(validateMessage(aspFactory1, MessageClass.MANAGEMENT, MessageType.NOTIFY, Status.STATUS_Other,
510				Status.INFO_Insufficient_ASP_Resources_Active));
511		// AS remains ACTIVE
512		assertEquals(AsState.ACTIVE, remAs.getState());
513
514		// Bring down ASP1
515		message = messageFactory.createMessage(MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_DOWN);
516		aspFactory1.read(message);
517		assertEquals(AspState.DOWN, remAsp1.getState());
518		assertTrue(validateMessage(aspFactory1, MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_DOWN_ACK, -1, -1));
519		assertNull(aspFactory1.txPoll());
520		// AS remains ACTIVE
521		assertEquals(AsState.ACTIVE, remAs.getState());
522
523		// INACTIVATE ASP2.Now AS becomes PENDING and sends NTFY to all ASP's in
524		// INACTIVE state
525		message = messageFactory.createMessage(MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_INACTIVE);
526		aspFactory2.read(message);
527		assertEquals(AspState.INACTIVE, remAsp2.getState());
528		assertTrue(validateMessage(aspFactory2, MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_INACTIVE_ACK, -1,
529				-1));
530		// AS remains ACTIVE
531		assertEquals(AsState.PENDING, remAs.getState());
532		// AS state change NTFY message
533		assertTrue(validateMessage(aspFactory2, MessageClass.MANAGEMENT, MessageType.NOTIFY,
534				Status.STATUS_AS_State_Change, Status.INFO_AS_PENDING));
535
536		assertNull(aspFactory1.txPoll());
537		assertNull(aspFactory2.txPoll());
538	}
539
540	@Test
541	public void testAspUpReceivedWhileASPIsAlreadyUp() throws Exception {
542		// Test bug http://code.google.com/p/mobicents/issues/detail?id=2436
543
544		// 4.3.4.1. ASP Up Procedures from http://tools.ietf.org/html/rfc4666
545		RoutingContext rc = parmFactory.createRoutingContext(new long[] { 100 });
546
547		DestinationPointCode[] dpc = new DestinationPointCode[] { parmFactory
548				.createDestinationPointCode(123, (short) 0) };
549
550		ServiceIndicators[] servInds = new ServiceIndicators[] { parmFactory.createServiceIndicators(new short[] { 3 }) };
551
552		TrafficModeType trModType = parmFactory.createTrafficModeType(TrafficModeType.Override);
553		LocalRKIdentifier lRkId = parmFactory.createLocalRKIdentifier(1);
554		RoutingKey rKey = parmFactory.createRoutingKey(lRkId, rc, null, null, dpc, servInds, null);
555
556		// As remAs = sgw.createAppServer("testas", rc, rKey, trModType);
557		As remAs = serverM3UAMgmt.createAppServer("m3ua ras create rc 100 rk dpc 123 si 3 traffic-mode override testas"
558				.split(" "));
559		// AspFactory aspFactory = sgw.createAspFactory("testasp", "127.0.0.1",
560		// 2777);
561		AspFactory aspFactory = serverM3UAMgmt.createAspFactory("m3ua rasp create ip 127.0.0.1 port 2777 testasp"
562				.split(" "));
563
564		Asp remAsp = serverM3UAMgmt.assignAspToAs("testas", "testasp");
565
566		// Check for Communication UP
567		aspFactory.onCommStateChange(CommunicationState.UP);
568
569		assertEquals(AspState.DOWN, remAsp.getState());
570
571		// Check for ASP_UP
572		M3UAMessageImpl message = messageFactory.createMessage(MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_UP);
573		aspFactory.read(message);
574
575		assertEquals(AspState.INACTIVE, remAsp.getState());
576		assertTrue(validateMessage(aspFactory, MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_UP_ACK, -1, -1));
577		// also the AS should be INACTIVE now
578		assertEquals(AsState.INACTIVE, remAs.getState());
579		assertTrue(validateMessage(aspFactory, MessageClass.MANAGEMENT, MessageType.NOTIFY,
580				Status.STATUS_AS_State_Change, Status.INFO_AS_INACTIVE));
581
582		// Check for ASP_ACTIVE
583		message = messageFactory.createMessage(MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE);
584		((ASPActiveImpl) message).setRoutingContext(rc);
585		aspFactory.read(message);
586		assertEquals(AspState.ACTIVE, remAsp.getState());
587		assertTrue(validateMessage(aspFactory, MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE_ACK, -1, -1));
588		// also the AS should be ACTIVE now
589		assertEquals(AsState.ACTIVE, remAs.getState());
590		assertTrue(validateMessage(aspFactory, MessageClass.MANAGEMENT, MessageType.NOTIFY,
591				Status.STATUS_AS_State_Change, Status.INFO_AS_ACTIVE));
592
593		// Check for ASP_UP received while ASP is already UP
594		message = messageFactory.createMessage(MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_UP);
595		aspFactory.read(message);
596		// The ASP Transitions to INACTIVE
597		assertEquals(AspState.INACTIVE, remAsp.getState());
598		// Receives ASP_UP Ack messages
599		assertTrue(validateMessage(aspFactory, MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_UP_ACK, -1, -1));
600		// As well as receives Error message
601		assertTrue(validateMessage(aspFactory, MessageClass.MANAGEMENT, MessageType.ERROR,
602				ErrorCode.Unexpected_Message, 100));
603
604		// also the AS should be PENDING now
605		assertEquals(AsState.PENDING, remAs.getState());
606		assertTrue(validateMessage(aspFactory, MessageClass.MANAGEMENT, MessageType.NOTIFY,
607				Status.STATUS_AS_State_Change, Status.INFO_AS_PENDING));
608
609		// Make sure we don't have any more
610		assertNull(aspFactory.txPoll());
611
612	}
613
614	@Test
615	public void testPendingQueue() throws Exception {
616		RoutingContext rc = parmFactory.createRoutingContext(new long[] { 100 });
617
618		DestinationPointCode[] dpc = new DestinationPointCode[] { parmFactory
619				.createDestinationPointCode(123, (short) 0) };
620
621		ServiceIndicators[] servInds = new ServiceIndicators[] { parmFactory.createServiceIndicators(new short[] { 3 }) };
622
623		TrafficModeType trModType = parmFactory.createTrafficModeType(TrafficModeType.Override);
624		LocalRKIdentifier lRkId = parmFactory.createLocalRKIdentifier(1);
625		RoutingKey rKey = parmFactory.createRoutingKey(lRkId, rc, null, null, dpc, servInds, null);
626
627		// As remAs = sgw.createAppServer("testas", rc, rKey, trModType);
628		As remAs = serverM3UAMgmt.createAppServer("m3ua ras create rc 100 rk dpc 123 si 3 traffic-mode override testas"
629				.split(" "));
630		// AspFactory aspFactory = sgw.createAspFactory("testasp", "127.0.0.1",
631		// 2777);
632		AspFactory aspFactory = serverM3UAMgmt.createAspFactory("m3ua rasp create ip 127.0.0.1 port 2777 testasp"
633				.split(" "));
634
635		Asp remAsp = serverM3UAMgmt.assignAspToAs("testas", "testasp");
636
637		// Check for Communication UP
638		aspFactory.onCommStateChange(CommunicationState.UP);
639
640		assertEquals(AspState.DOWN, remAsp.getState());
641
642		// Check for ASP_UP
643		M3UAMessageImpl message = messageFactory.createMessage(MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_UP);
644		aspFactory.read(message);
645
646		assertEquals(AspState.INACTIVE, remAsp.getState());
647		assertTrue(validateMessage(aspFactory, MessageClass.ASP_STATE_MAINTENANCE, MessageType.ASP_UP_ACK, -1, -1));
648		// also the AS should be INACTIVE now
649		assertEquals(AsState.INACTIVE, remAs.getState());
650		assertTrue(validateMessage(aspFactory, MessageClass.MANAGEMENT, MessageType.NOTIFY,
651				Status.STATUS_AS_State_Change, Status.INFO_AS_INACTIVE));
652
653		// Check for ASP_ACTIVE
654		message = messageFactory.createMessage(MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE);
655		((ASPActiveImpl) message).setRoutingContext(rc);
656		aspFactory.read(message);
657		assertEquals(AspState.ACTIVE, remAsp.getState());
658		assertTrue(validateMessage(aspFactory, MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE_ACK, -1, -1));
659		// also the AS should be ACTIVE now
660		assertEquals(AsState.ACTIVE, remAs.getState());
661		assertTrue(validateMessage(aspFactory, MessageClass.MANAGEMENT, MessageType.NOTIFY,
662				Status.STATUS_AS_State_Change, Status.INFO_AS_ACTIVE));
663
664		// Check for ASP_INACTIVE
665		message = messageFactory.createMessage(MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_INACTIVE);
666		aspFactory.read(message);
667		assertEquals(AspState.INACTIVE, remAsp.getState());
668		assertTrue(validateMessage(aspFactory, MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_INACTIVE_ACK, -1,
669				-1));
670		// also the AS should be PENDING now
671		assertEquals(AsState.PENDING, remAs.getState());
672		assertTrue(validateMessage(aspFactory, MessageClass.MANAGEMENT, MessageType.NOTIFY,
673				Status.STATUS_AS_State_Change, Status.INFO_AS_PENDING));
674
675		// Add PayloadData
676		PayloadDataImpl payload = (PayloadDataImpl) messageFactory.createMessage(MessageClass.TRANSFER_MESSAGES,
677				MessageType.PAYLOAD);
678		ProtocolDataImpl p1 = (ProtocolDataImpl) parmFactory.createProtocolData(1408, 123, 3, 1, 0, 1, new byte[] { 1,
679				2, 3, 4 });
680		payload.setRoutingContext(rc);
681		payload.setData(p1);
682
683		remAs.write(payload);
684
685		// Now bring UP the ASP
686		message = messageFactory.createMessage(MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE);
687		((ASPActiveImpl) message).setRoutingContext(rc);
688		aspFactory.read(message);
689
690		assertTrue(validateMessage(aspFactory, MessageClass.ASP_TRAFFIC_MAINTENANCE, MessageType.ASP_ACTIVE_ACK, -1, -1));
691		// also the AS should be ACTIVE now
692		assertEquals(AsState.ACTIVE, remAs.getState());
693		assertTrue(validateMessage(aspFactory, MessageClass.MANAGEMENT, MessageType.NOTIFY,
694				Status.STATUS_AS_State_Change, Status.INFO_AS_ACTIVE));
695
696		// Also we should have PayloadData
697		M3UAMessage payLoadTemp = aspFactory.txPoll();
698		assertNotNull(payLoadTemp);
699		assertEquals(MessageClass.TRANSFER_MESSAGES, payLoadTemp.getMessageClass());
700		assertEquals(MessageType.PAYLOAD, payLoadTemp.getMessageType());
701
702		// Make sure we don't have any more
703		assertNull(aspFactory.txPoll());
704	}
705
706	/**
707	 * 
708	 * @param factory
709	 * @param msgClass
710	 * @param msgType
711	 * @param type
712	 *            The type for Notify message Or Error Code for Error Messages
713	 * @param info
714	 *            The Info for Notify message Or RoutingContext for Error
715	 *            Message
716	 * @return
717	 */
718	private boolean validateMessage(AspFactory factory, int msgClass, int msgType, int type, int info) {
719		M3UAMessage message = factory.txPoll();
720		if (message == null) {
721			return false;
722		}
723
724		if (message.getMessageClass() != msgClass || message.getMessageType() != msgType) {
725			return false;
726		}
727
728		if (message.getMessageClass() == MessageClass.MANAGEMENT) {
729			if (message.getMessageType() == MessageType.NOTIFY) {
730				Status s = ((Notify) message).getStatus();
731				if (s.getType() != type || s.getInfo() != info) {
732					return false;
733				} else {
734					return true;
735				}
736			} else if (message.getMessageType() == MessageType.ERROR) {
737				ErrorCode errCode = ((org.mobicents.protocols.ss7.m3ua.message.mgmt.Error) message).getErrorCode();
738				if (errCode.getCode() != type) {
739					return false;
740				}
741
742				RoutingContext rc = ((org.mobicents.protocols.ss7.m3ua.message.mgmt.Error) message).getRoutingContext();
743				if (rc == null || rc.getRoutingContexts()[0] != info) {
744					return false;
745				}
746
747				return true;
748
749			}
750			return false;
751		} else {
752			return true;
753		}
754
755	}
756
757}