CreateConnectionCommand extends AbstractMgcpCommand { @Override public MgcpCommandResult call() { CrcxContext context = new CrcxContext(); try { validateParameters(this.requestParameters, context); executeCommand(context); context.setCode(MgcpResponseCode.TRANSACTION_WAS_EXECUTED.code()); context.setMessage(MgcpResponseCode.TRANSACTION_WAS_EXECUTED.message()); } catch (RuntimeException | MgcpConnectionException e) { log.error("Unexpected error occurred during tx=" + this.transactionId + " execution. Reason: " + e.getMessage() + ". Rolling back."); rollback(context); context.setCode(MgcpResponseCode.PROTOCOL_ERROR.code()); context.setMessage(MgcpResponseCode.PROTOCOL_ERROR.message()); } catch (MgcpCommandException e) { log.error("Protocol error occurred during tx=" + this.transactionId + " execution. Reason: " + e.getMessage()); context.setCode(e.getCode()); context.setMessage(e.getMessage()); } return respond(context); } CreateConnectionCommand(int transactionId, Parameters<MgcpParameterType> parameters, MgcpEndpointManager endpointManager); @Override MgcpCommandResult call();  }
@Test public void testCreateLocalConnectionsBetweenTwoEndpoints() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("CRCX 147483655 mobicents/bridge/$@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("C:1").append(System.lineSeparator()); builder.append("M:sendonly").append(System.lineSeparator()); builder.append("N:restcomm@127.0.0.1:2727").append(System.lineSeparator()); builder.append("Z2:mobicents/ivr/$@127.0.0.1:2427"); final int callId = 1; final int transactionId = 147483655; final MgcpRequest request = new MgcpMessageParser().parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final MgcpConnectionProvider connectionProvider = mock(MgcpConnectionProvider.class); final MgcpEndpoint bridgeEndpoint = mock(MgcpEndpoint.class); final MgcpEndpoint ivrEndpoint = mock(MgcpEndpoint.class); final MgcpLocalConnection connection1 = mock(MgcpLocalConnection.class); final MgcpLocalConnection connection2 = mock(MgcpLocalConnection.class); final EndpointIdentifier bridgeEndpointId = new EndpointIdentifier("mobicents/bridge/1", "127.0.0.1:2427"); final EndpointIdentifier ivrEndpointId = new EndpointIdentifier("mobicents/ivr/1", "127.0.0.1:2427"); final CreateConnectionCommand crcx = new CreateConnectionCommand(transactionId, request.getParameters(), endpointManager); when(connectionProvider.provideLocal(callId)).thenReturn(connection1, connection2); when(connection1.getIdentifier()).thenReturn(1); when(connection1.getCallIdentifier()).thenReturn(callId); when(connection2.getIdentifier()).thenReturn(2); when(connection2.getCallIdentifier()).thenReturn(callId); when(endpointManager.registerEndpoint("mobicents/bridge/")).thenReturn(bridgeEndpoint); when(endpointManager.registerEndpoint("mobicents/ivr/")).thenReturn(ivrEndpoint); when(bridgeEndpoint.getEndpointId()).thenReturn(bridgeEndpointId); when(ivrEndpoint.getEndpointId()).thenReturn(ivrEndpointId); when(bridgeEndpoint.createConnection(callId, true)).thenReturn(connection1); when(ivrEndpoint.createConnection(callId, true)).thenReturn(connection2); MgcpCommandResult result = crcx.call(); verify(endpointManager, times(1)).registerEndpoint("mobicents/bridge/"); verify(endpointManager, times(1)).registerEndpoint("mobicents/ivr/"); verify(bridgeEndpoint, times(1)).createConnection(callId, true); verify(ivrEndpoint, times(1)).createConnection(callId, true); verify(connection1, times(1)).join(connection2); assertNotNull(result); assertEquals(MgcpResponseCode.TRANSACTION_WAS_EXECUTED.code(), result.getCode()); Parameters<MgcpParameterType> parameters = result.getParameters(); assertEquals(4, result.getParameters().size()); assertEquals(bridgeEndpoint.getEndpointId().toString(), parameters.getString(MgcpParameterType.ENDPOINT_ID).or("")); assertEquals(ivrEndpoint.getEndpointId().toString(), parameters.getString(MgcpParameterType.SECOND_ENDPOINT).or("")); assertEquals(connection1.getIdentifier(), parameters.getInteger(MgcpParameterType.CONNECTION_ID).or(0).intValue()); assertEquals(connection2.getIdentifier(), parameters.getInteger(MgcpParameterType.CONNECTION_ID2).or(0).intValue()); }
@Test public void testCreateOutboundRemoteConnection() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("CRCX 147483655 mobicents/bridge/$@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("C:1").append(System.lineSeparator()); builder.append("M:sendrecv").append(System.lineSeparator()); builder.append("N:restcomm@127.0.0.1:2727").append(System.lineSeparator()); builder.append("L:webrtc:false").append(System.lineSeparator()); final int callId = 1; final int transactionId = 147483655; final MgcpRequest request = new MgcpMessageParser().parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final MgcpConnectionProvider connectionProvider = mock(MgcpConnectionProvider.class); final MgcpEndpoint bridgeEndpoint = mock(MgcpEndpoint.class); final EndpointIdentifier bridgeEndpointId = new EndpointIdentifier("mobicents/bridge/1", "127.0.0.1:2427"); final MgcpRemoteConnection connection = mock(MgcpRemoteConnection.class); final CreateConnectionCommand crcx = new CreateConnectionCommand(transactionId, request.getParameters(), endpointManager); when(connection.getIdentifier()).thenReturn(1); when(connection.halfOpen(any(LocalConnectionOptions.class))).thenReturn("answer"); when(connectionProvider.provideRemote(callId)).thenReturn(connection); when(endpointManager.registerEndpoint("mobicents/bridge/")).thenReturn(bridgeEndpoint); when(bridgeEndpoint.getEndpointId()).thenReturn(bridgeEndpointId); when(bridgeEndpoint.createConnection(callId, false)).thenReturn(connection); MgcpCommandResult result = crcx.call(); verify(endpointManager, times(1)).registerEndpoint("mobicents/bridge/"); verify(bridgeEndpoint, times(1)).createConnection(callId, false); verify(connection, times(1)).halfOpen(any(LocalConnectionOptions.class)); assertNotNull(result); assertEquals(MgcpResponseCode.TRANSACTION_WAS_EXECUTED.code(), result.getCode()); Parameters<MgcpParameterType> parameters = result.getParameters(); assertEquals(3, result.getParameters().size()); assertEquals(bridgeEndpoint.getEndpointId().toString(), parameters.getString(MgcpParameterType.ENDPOINT_ID).or("")); assertEquals(connection.getIdentifier(), parameters.getInteger(MgcpParameterType.CONNECTION_ID).or(0).intValue()); assertEquals("answer", parameters.getString(MgcpParameterType.SDP).or("")); }
@Test public void testCreateInboundRemoteConnection() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("CRCX 147483655 mobicents/bridge/$@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("C:1").append(System.lineSeparator()); builder.append("M:sendrecv").append(System.lineSeparator()); builder.append("N:restcomm@127.0.0.1:2727").append(System.lineSeparator()); builder.append("L:webrtc:false").append(System.lineSeparator()); builder.append(System.lineSeparator()); final StringBuilder builderSdp = new StringBuilder(); builderSdp.append("v=0").append(System.lineSeparator()); builderSdp.append("o=hrosa 3616 1899 IN IP4 127.0.0.1").append(System.lineSeparator()); builderSdp.append("s=Talk").append(System.lineSeparator()); builderSdp.append("c=IN IP4 127.0.0.1").append(System.lineSeparator()); builderSdp.append("t=0 0").append(System.lineSeparator()); builderSdp.append("m=audio 7070 RTP/AVP 8 0 101").append(System.lineSeparator()); builderSdp.append("a=rtpmap:101 telephone-event/8000"); builder.append(builderSdp.toString()); final int callId = 1; final int transactionId = 147483655; final MgcpRequest request = new MgcpMessageParser().parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final MgcpConnectionProvider connectionProvider = mock(MgcpConnectionProvider.class); final MgcpEndpoint bridgeEndpoint = mock(MgcpEndpoint.class); final EndpointIdentifier bridgeEndpointId = new EndpointIdentifier("mobicents/bridge/1", "127.0.0.1:2427"); final MgcpRemoteConnection connection = mock(MgcpRemoteConnection.class); final CreateConnectionCommand crcx = new CreateConnectionCommand(transactionId, request.getParameters(), endpointManager); when(connectionProvider.provideRemote(callId)).thenReturn(connection); when(connection.getIdentifier()).thenReturn(1); when(connection.open(builderSdp.toString())).thenReturn("answer"); when(endpointManager.registerEndpoint("mobicents/bridge/")).thenReturn(bridgeEndpoint); when(bridgeEndpoint.getEndpointId()).thenReturn(bridgeEndpointId); when(bridgeEndpoint.createConnection(callId, false)).thenReturn(connection); MgcpCommandResult result = crcx.call(); verify(endpointManager, times(1)).registerEndpoint("mobicents/bridge/"); verify(bridgeEndpoint, times(1)).createConnection(callId, false); verify(connection, times(1)).open(builderSdp.toString()); assertNotNull(result); assertEquals(MgcpResponseCode.TRANSACTION_WAS_EXECUTED.code(), result.getCode()); assertEquals("answer", result.getParameters().getString(MgcpParameterType.SDP).or("")); Parameters<MgcpParameterType> parameters = result.getParameters(); assertEquals(3, result.getParameters().size()); assertEquals(bridgeEndpoint.getEndpointId().toString(), parameters.getString(MgcpParameterType.ENDPOINT_ID).or("")); assertEquals(connection.getIdentifier(), parameters.getInteger(MgcpParameterType.CONNECTION_ID).or(0).intValue()); assertEquals("answer", parameters.getString(MgcpParameterType.SDP).or("")); }
@Test public void testValidateRequestWithZ2AndSdpPresent() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("CRCX 147483655 mobicents/bridge/$@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("C:1").append(System.lineSeparator()); builder.append("M:sendrecv").append(System.lineSeparator()); builder.append("N:restcomm@127.0.0.1:2727").append(System.lineSeparator()); builder.append("Z2:mobicents/ivr/$@127.0.0.1:2427"); builder.append("L:webrtc:false").append(System.lineSeparator()); builder.append(System.lineSeparator()); final StringBuilder builderSdp = new StringBuilder(); builderSdp.append("v=0").append(System.lineSeparator()); builderSdp.append("o=hrosa 3616 1899 IN IP4 127.0.0.1").append(System.lineSeparator()); builderSdp.append("s=Talk").append(System.lineSeparator()); builderSdp.append("c=IN IP4 127.0.0.1").append(System.lineSeparator()); builderSdp.append("t=0 0").append(System.lineSeparator()); builderSdp.append("m=audio 7070 RTP/AVP 8 0 101").append(System.lineSeparator()); builderSdp.append("a=rtpmap:101 telephone-event/8000"); builder.append(builderSdp.toString()); final int transactionId = 147483655; final MgcpRequest request = new MgcpMessageParser().parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final CreateConnectionCommand crcx = new CreateConnectionCommand(transactionId, request.getParameters(), endpointManager); MgcpCommandResult result = crcx.call(); assertNotNull(result); assertEquals(MgcpResponseCode.PROTOCOL_ERROR.code(), result.getCode()); assertEquals(0, result.getParameters().size()); }
@Test public void testValidateRequestWithMissingCallId() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("CRCX 147483655 mobicents/bridge/$@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("M:sendonly").append(System.lineSeparator()); builder.append("N:restcomm@127.0.0.1:2727").append(System.lineSeparator()); builder.append("Z2:mobicents/ivr/$@127.0.0.1:2427"); final int transactionId = 147483655; final MgcpRequest request = new MgcpMessageParser().parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final CreateConnectionCommand crcx = new CreateConnectionCommand(transactionId, request.getParameters(), endpointManager); MgcpCommandResult result = crcx.call(); assertNotNull(result); assertEquals(MgcpResponseCode.INCORRECT_CALL_ID.code(), result.getCode()); assertEquals(0, result.getParameters().size()); }
@Test public void testValidateRequestWithInvalidConnectionMode() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("CRCX 147483655 mobicents/bridge/$@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("C:1").append(System.lineSeparator()); builder.append("M:xywz").append(System.lineSeparator()); builder.append("N:restcomm@127.0.0.1:2727").append(System.lineSeparator()); builder.append("Z2:mobicents/ivr/$@127.0.0.1:2427"); final int transactionId = 147483655; final MgcpRequest request = new MgcpMessageParser().parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final CreateConnectionCommand crcx = new CreateConnectionCommand(transactionId, request.getParameters(), endpointManager); MgcpCommandResult result = crcx.call(); assertNotNull(result); assertEquals(MgcpResponseCode.INVALID_OR_UNSUPPORTED_MODE.code(), result.getCode()); assertEquals(0, result.getParameters().size()); }
@Test public void testValidateRequestWithComplicatedWildcardOnPrimaryEndpoint() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("CRCX 147483655 mobicents/bridge/*@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("C:1").append(System.lineSeparator()); builder.append("M:sendrecv").append(System.lineSeparator()); builder.append("N:restcomm@127.0.0.1:2727").append(System.lineSeparator()); builder.append("Z2:mobicents/ivr/$@127.0.0.1:2427"); final int transactionId = 147483655; final MgcpRequest request = new MgcpMessageParser().parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final CreateConnectionCommand crcx = new CreateConnectionCommand(transactionId, request.getParameters(), endpointManager); MgcpCommandResult result = crcx.call(); assertNotNull(result); assertEquals(MgcpResponseCode.WILDCARD_TOO_COMPLICATED.code(), result.getCode()); assertEquals(0, result.getParameters().size()); }
@Test public void testValidateRequestWithComplicatedWildcardOnSecondaryEndpoint() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("CRCX 147483655 mobicents/bridge/$@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("C:1").append(System.lineSeparator()); builder.append("M:sendrecv").append(System.lineSeparator()); builder.append("N:restcomm@127.0.0.1:2727").append(System.lineSeparator()); builder.append("Z2:mobicents/ivr/*@127.0.0.1:2427"); final int transactionId = 147483655; final MgcpRequest request = new MgcpMessageParser().parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final CreateConnectionCommand crcx = new CreateConnectionCommand(transactionId, request.getParameters(), endpointManager); MgcpCommandResult result = crcx.call(); assertNotNull(result); assertEquals(MgcpResponseCode.WILDCARD_TOO_COMPLICATED.code(), result.getCode()); assertEquals(0, result.getParameters().size()); }
@Test public void testValidateRequestWithUnknownPrimaryEndpoint() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("CRCX 147483655 mobicents/bridge/$@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("C:1").append(System.lineSeparator()); builder.append("M:sendrecv").append(System.lineSeparator()); builder.append("N:restcomm@127.0.0.1:2727").append(System.lineSeparator()); builder.append("Z2:mobicents/ivr/$@127.0.0.1:2427"); final int transactionId = 147483655; final MgcpRequest request = new MgcpMessageParser().parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final CreateConnectionCommand crcx = new CreateConnectionCommand(transactionId, request.getParameters(), endpointManager); when(endpointManager.registerEndpoint("mobicents/bridge/")).thenThrow(new UnrecognizedMgcpNamespaceException("")); MgcpCommandResult result = crcx.call(); assertNotNull(result); assertEquals(MgcpResponseCode.ENDPOINT_NOT_AVAILABLE.code(), result.getCode()); assertEquals(0, result.getParameters().size()); }
@Test public void testValidateRequestWithUnknownSecondaryEndpoint() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("CRCX 147483655 mobicents/bridge/$@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("C:1").append(System.lineSeparator()); builder.append("M:sendrecv").append(System.lineSeparator()); builder.append("N:restcomm@127.0.0.1:2727").append(System.lineSeparator()); builder.append("Z2:mobicents/ivr/$@127.0.0.1:2427"); final int transactionId = 147483655; final MgcpRequest request = new MgcpMessageParser().parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final MgcpEndpoint bridgeEndpoint = mock(MgcpEndpoint.class); final EndpointIdentifier bridgeEndpointId = new EndpointIdentifier("mobicents/bridge/1", "127.0.0.1:2427"); final CreateConnectionCommand crcx = new CreateConnectionCommand(transactionId, request.getParameters(), endpointManager); when(endpointManager.registerEndpoint("mobicents/bridge/")).thenReturn(bridgeEndpoint); when(endpointManager.registerEndpoint("mobicents/ivr/")).thenThrow(new UnrecognizedMgcpNamespaceException("")); when(bridgeEndpoint.getEndpointId()).thenReturn(bridgeEndpointId); MgcpCommandResult result = crcx.call(); assertNotNull(result); assertEquals(MgcpResponseCode.ENDPOINT_NOT_AVAILABLE.code(), result.getCode()); assertEquals(0, result.getParameters().size()); }