use of org.mobicents.slee.resource.diameter.base.events.AccountingRequestImpl in project jain-slee.diameter by RestComm.
the class DiameterMessageFactoryImpl method createDiameterMessage.
protected DiameterMessage createDiameterMessage(DiameterHeader diameterHeader, DiameterAvp[] avps, int _commandCode, ApplicationId appId) throws IllegalArgumentException {
boolean creatingRequest = diameterHeader == null;
Message msg = null;
if (!creatingRequest) {
Message raw = createMessage(diameterHeader, avps, 0, appId);
// raw.setProxiable(true);
raw.setRequest(false);
// just in case. answers never have T flag set
raw.setReTransmitted(false);
msg = raw;
} else {
Message raw = createMessage(diameterHeader, avps, _commandCode, appId);
boolean isPeerManagementMessage = _commandCode == CapabilitiesExchangeRequest.commandCode || _commandCode == DeviceWatchdogRequest.commandCode || _commandCode == DisconnectPeerRequest.commandCode;
raw.setProxiable(!isPeerManagementMessage);
raw.setRequest(true);
msg = raw;
}
int commandCode = creatingRequest ? _commandCode : diameterHeader.getCommandCode();
DiameterMessage diamMessage = null;
switch(commandCode) {
case Message.ABORT_SESSION_REQUEST:
diamMessage = creatingRequest ? new AbortSessionRequestImpl(msg) : new AbortSessionAnswerImpl(msg);
break;
case Message.ACCOUNTING_REQUEST:
diamMessage = creatingRequest ? new AccountingRequestImpl(msg) : new AccountingAnswerImpl(msg);
break;
case Message.CAPABILITIES_EXCHANGE_REQUEST:
diamMessage = creatingRequest ? new CapabilitiesExchangeRequestImpl(msg) : new CapabilitiesExchangeAnswerImpl(msg);
break;
case Message.DEVICE_WATCHDOG_REQUEST:
diamMessage = creatingRequest ? new DeviceWatchdogRequestImpl(msg) : new DeviceWatchdogAnswerImpl(msg);
break;
case Message.DISCONNECT_PEER_REQUEST:
diamMessage = creatingRequest ? new DisconnectPeerRequestImpl(msg) : new DisconnectPeerAnswerImpl(msg);
break;
case Message.RE_AUTH_REQUEST:
diamMessage = creatingRequest ? new ReAuthRequestImpl(msg) : new ReAuthAnswerImpl(msg);
break;
case Message.SESSION_TERMINATION_REQUEST:
diamMessage = creatingRequest ? new SessionTerminationRequestImpl(msg) : new SessionTerminationAnswerImpl(msg);
break;
default:
diamMessage = new ExtensionDiameterMessageImpl(msg);
}
// Finally, add Origin-Host and Origin-Realm, if not present.
addOriginHostAndRealm(diamMessage);
return diamMessage;
}
use of org.mobicents.slee.resource.diameter.base.events.AccountingRequestImpl in project jain-slee.diameter by RestComm.
the class ExpectedAvp method testOperationsAddWithValidatorOnAndRemovalNotAllowed.
/* ammendonca: removal allowed is gone...
@Test
public void testOperationsAddWithValidatorOnAndRemovalAllowed() {
instance.configure(this.getClass().getClassLoader().getResourceAsStream(validatorOnFile));
instance.setEnabled(true);
// It has session id
AccountingRequestImpl request = (AccountingRequestImpl) baseFactory.createAccountingRequest();
// <avp name="Session-Id" code="263" vendor="0" multiplicity="1" index="0"/>
AvpUtilities.setAvpAsUTF8String(request.getGenericData(), 263, request.getGenericData().getAvps(), "1346ferg5y");
// <avp name="Origin-Host" code="264" vendor="0" multiplicity="1" index="-1"/>
AvpUtilities.setAvpAsOctetString(request.getGenericData(), 264, request.getGenericData().getAvps(), clientURI);
// <avp name="Origin-Realm" code="296" vendor="0" multiplicity="1" index="-1"/>
AvpUtilities.setAvpAsOctetString(request.getGenericData(), 296, request.getGenericData().getAvps(), realmName);
AvpUtilities.setAvpAsOctetString(request.getGenericData(), 296, request.getGenericData().getAvps(), realmName);
// <avp name="Destination-Realm" code="283" vendor="0" multiplicity="1" index="-1"/>
AvpUtilities.setAvpAsOctetString(request.getGenericData(), 283, request.getGenericData().getAvps(), realmName);
// <avp name="Destination-Host" code="293" vendor="0" multiplicity="0-1" index="-1"/>
// AvpUtilities.setAvpAsOctetString(request.getGenericData(), 293, request.getGenericData().getAvps(), serverURI);
AvpUtilities.setAvpAsOctetString(request.getGenericData(), 293, request.getGenericData().getAvps(), serverHost);
// <avp name="Accounting-Record-Type" code="480" vendor="0" multiplicity="1" index="-1"/>
AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 480, request.getGenericData().getAvps(), 1);
String sessionId = AvpUtilities.getAvpAsUTF8String(263, request.getGenericData().getAvps());
Session localSession = null;
try {
localSession = stack.getSessionFactory().getNewSession(sessionId);
sendMessage(localSession, request);
System.out.println(request);
fail("Should not send this message. Message MUST contain Accounting-Record-Number AVP (485), and it is not present.");
}
catch (org.jdiameter.api.validation.AvpNotAllowedException e) {
if (e.getAvpCode() != 485 && e.getVendorId() != 0) {
fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (485:0), Received (" + e.getAvpCode() + ":" + e.getVendorId()
+ ").");
}
}
catch (Exception e) {
Throwable cause = e;
boolean wasAvpNotAllowed = false;
while ((cause = cause.getCause()) != null) {
if (cause instanceof org.jdiameter.api.validation.AvpNotAllowedException) {
wasAvpNotAllowed = true;
org.jdiameter.api.validation.AvpNotAllowedException exc = (org.jdiameter.api.validation.AvpNotAllowedException) cause;
if (exc.getAvpCode() != 485 && exc.getVendorId() != 0) {
fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (485:0), Received (" + exc.getAvpCode() + ":"
+ exc.getVendorId() + ").");
}
}
}
if (!wasAvpNotAllowed) {
fail("Message failed to be sent for wrong reason. Expected AvpNotAllowedException, Received " + e);
}
}
// <avp name="Accounting-Record-Number" code="485" vendor="0" multiplicity="1" index="-1"/>
AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 485, request.getGenericData().getAvps(), 1);
// In here we send the message actually. It is OK!
try {
sendMessage(localSession, request);
}
catch (Exception e) {
fail("Failed to send message when it should succeed! Exception: " + e);
}
// <avp name="Acct-Application-Id" code="259" vendor="0" multiplicity="0-1" index="-1"/>
AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 259, request.getGenericData().getAvps(), 1);
// Again, in here we send the message actually. It is OK!
try {
sendMessage(localSession, request);
}
catch (Exception e) {
fail("Failed to send message when it should succeed! Exception: " + e);
}
// <!-- FORBBIDEN -->
// <avp name="Auth-Application-Id" code="258" vendor="0" multiplicity="0" index="-1"/>
try {
AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 258, request.getGenericData().getAvps(), 1);
// Now send should fail. Message has AVP "Auth-Application-Id", not valid in ACR.
sendMessage(localSession, request);
fail("Should not send this message. Message MUST NOT contain Auth-Application-Id AVP (258), and it is present.");
}
catch (AvpNotAllowedException e) {
if (e.getAvpCode() != 258 && e.getVendorId() != 0) {
fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (258:0), Received (" + e.getAvpCode() + ":" + e.getVendorId()
+ ").");
}
}
catch (Exception e) {
fail("Message failed to be sent for wrong reason. Expected AvpNotAllowedException, Received " + e);
}
// This is just in case
Map<ExpectedAvp, ExpectedAvp> expectedAvps = new HashMap<ExpectedAvp, ExpectedAvp>();
ExpectedAvp a = new ExpectedAvp();
a.code = 263;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 264;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 296;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 283;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 480;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 485;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 259;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 293;
a.count = 1;
expectedAvps.put(a, a);
testPresentAvps(request.getGenericData().getAvps(), expectedAvps);
}
*/
@Test
public void testOperationsAddWithValidatorOnAndRemovalNotAllowed() {
instance.configure(this.getClass().getClassLoader().getResourceAsStream(validatorOnFile));
instance.setEnabled(true);
// It has session id
AccountingRequestImpl request = (AccountingRequestImpl) baseFactory.createAccountingRequest(new DiameterAvpImpl[] { new DiameterAvpImpl(263, 0L, 0, 1, "xxx".getBytes(), DiameterAvpType.UTF8_STRING) });
// <avp name="Session-Id" code="263" vendor="0" multiplicity="1" index="0"/>
try {
AvpUtilities.setAvpAsUTF8String(request.getGenericData(), 263, request.getGenericData().getAvps(), "1346ferg5y");
fail("Session-Id can not be set twice.");
} catch (AvpNotAllowedException e) {
if (e.getAvpCode() != 258 && e.getVendorId() != 0) {
fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (258:0), Received (" + e.getAvpCode() + ":" + e.getVendorId() + ").");
}
} catch (Exception e) {
fail("Message failed to be sent for wrong reason. Expected AvpNotAllowedException, Received " + e);
}
// <avp name="Origin-Host" code="264" vendor="0" multiplicity="1" index="-1"/>
if (!request.hasOriginHost())
AvpUtilities.setAvpAsOctetString(request.getGenericData(), 264, request.getGenericData().getAvps(), clientURI);
// <avp name="Origin-Realm" code="296" vendor="0" multiplicity="1" index="-1"/>
if (!request.hasOriginRealm())
AvpUtilities.setAvpAsOctetString(request.getGenericData(), 296, request.getGenericData().getAvps(), realmName);
// <avp name="Destination-Host" code="293" vendor="0" multiplicity="0-1" index="-1"/>
AvpUtilities.setAvpAsOctetString(request.getGenericData(), 293, request.getGenericData().getAvps(), serverURI);
// <avp name="Destination-Realm" code="283" vendor="0" multiplicity="1" index="-1"/>
AvpUtilities.setAvpAsOctetString(request.getGenericData(), 283, request.getGenericData().getAvps(), realmName);
// <avp name="Accounting-Record-Type" code="480" vendor="0" multiplicity="1" index="-1"/>
AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 480, request.getGenericData().getAvps(), 1);
String sessionId = AvpUtilities.getAvpAsUTF8String(263, request.getGenericData().getAvps());
Session localSession = null;
try {
localSession = stack.getSessionFactory().getNewSession(sessionId);
sendMessage(localSession, request);
fail("Should not send this message. Message MUST contain Accounting-Record-Number AVP (485), and it is not present.");
} catch (org.jdiameter.api.validation.AvpNotAllowedException e) {
if (e.getAvpCode() != 485 && e.getVendorId() != 0) {
fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (485:0), Received (" + e.getAvpCode() + ":" + e.getVendorId() + ").");
}
} catch (Exception e) {
Throwable cause = e;
boolean wasAvpNotAllowed = false;
while ((cause = cause.getCause()) != null) {
if (cause instanceof org.jdiameter.api.validation.AvpNotAllowedException) {
wasAvpNotAllowed = true;
org.jdiameter.api.validation.AvpNotAllowedException exc = (org.jdiameter.api.validation.AvpNotAllowedException) cause;
if (exc.getAvpCode() != 485 && exc.getVendorId() != 0) {
fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (485:0), Received (" + exc.getAvpCode() + ":" + exc.getVendorId() + ").");
}
}
}
if (!wasAvpNotAllowed) {
fail("Message failed to be sent for wrong reason. Expected AvpNotAllowedException, Received " + e);
}
}
// <avp name="Accounting-Record-Number" code="485" vendor="0" multiplicity="1" index="-1"/>
AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 485, request.getGenericData().getAvps(), 1);
// In here we send the message actually. It is OK!
try {
sendMessage(localSession, request);
} catch (Exception e) {
fail("Failed to send message when it should succeed! Exception: " + e);
}
// Message should already come with Acct-Application-Id.. but still..
try {
// <avp name="Acct-Application-Id" code="259" vendor="0" multiplicity="0-1" index="-1"/>
AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 259, request.getGenericData().getAvps(), 1);
} catch (AvpNotAllowedException e) {
if (e.getAvpCode() != 259 && e.getVendorId() != 0) {
fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (259:0), Received (" + e.getAvpCode() + ":" + e.getVendorId() + ").");
}
}
// Again, in here we send the message actually. It is OK!
try {
sendMessage(localSession, request);
} catch (Exception e) {
fail("Failed to send message when it should succeed! Exception: " + e);
}
// <avp name="Auth-Application-Id" code="258" vendor="0" multiplicity="0" index="-1"/>
try {
AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 258, request.getGenericData().getAvps(), 1);
// Now send should fail. Message has AVP "Auth-Application-Id", not valid in ACR.
sendMessage(localSession, request);
fail("Should not send this message. Message MUST NOT contain Auth-Application-Id AVP (258), and it is present.");
} catch (AvpNotAllowedException e) {
if (e.getAvpCode() != 258 && e.getVendorId() != 0) {
fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (258:0), Received (" + e.getAvpCode() + ":" + e.getVendorId() + ").");
}
} catch (Exception e) {
fail("Message failed to be sent for wrong reason. Expected AvpNotAllowedException, Received " + e);
}
// Just in case.
Map<ExpectedAvp, ExpectedAvp> expectedAvps = new HashMap<ExpectedAvp, ExpectedAvp>();
ExpectedAvp a = new ExpectedAvp();
a.code = 263;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 264;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 296;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 283;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 480;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 485;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 259;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 293;
a.count = 1;
expectedAvps.put(a, a);
testPresentAvps(request.getGenericData().getAvps(), expectedAvps);
}
use of org.mobicents.slee.resource.diameter.base.events.AccountingRequestImpl in project jain-slee.diameter by RestComm.
the class DiameterRoResourceAdaptor method createEvent.
/**
* Create Event object from a JDiameter message (request or answer)
*
* @return a DiameterMessage object wrapping the request/answer
* @throws OperationNotSupportedException
*/
private DiameterMessage createEvent(Message message) {
if (message == null) {
throw new NullPointerException("Message argument cannot be null while creating event.");
}
int commandCode = message.getCommandCode();
if (message.isError()) {
return new ErrorAnswerImpl(message);
}
boolean isRequest = message.isRequest();
switch(commandCode) {
case // CCR/CCA
RoCreditControlRequest.commandCode:
return isRequest ? new RoCreditControlRequestImpl(message) : new RoCreditControlAnswerImpl(message);
case // ASR/ASA
AbortSessionAnswer.commandCode:
return isRequest ? new AbortSessionRequestImpl(message) : new AbortSessionAnswerImpl(message);
case // STR/STA
SessionTerminationAnswer.commandCode:
return isRequest ? new SessionTerminationRequestImpl(message) : new SessionTerminationAnswerImpl(message);
case // RAR/RAA
ReAuthAnswer.commandCode:
return isRequest ? new ReAuthRequestImpl(message) : new ReAuthAnswerImpl(message);
case // ACR/ACA
AccountingAnswer.commandCode:
return isRequest ? new AccountingRequestImpl(message) : new AccountingAnswerImpl(message);
default:
return new ExtensionDiameterMessageImpl(message);
}
}
use of org.mobicents.slee.resource.diameter.base.events.AccountingRequestImpl in project jain-slee.diameter by RestComm.
the class DiameterGxResourceAdaptor method createEvent.
/**
* Create Event object from a JDiameter message (request or answer).
*
* @return a DiameterMessage object wrapping the request/answer
* @throws OperationNotSupportedException
*/
private DiameterMessage createEvent(final Message message) {
if (message == null) {
throw new NullPointerException("Message argument cannot be null while creating event.");
}
final int commandCode = message.getCommandCode();
if (message.isError()) {
return new ErrorAnswerImpl(message);
}
final boolean isRequest = message.isRequest();
switch(commandCode) {
case // CCR/CCA
GxCreditControlRequest.commandCode:
return isRequest ? new GxCreditControlRequestImpl(message) : new GxCreditControlAnswerImpl(message);
case // ASR/ASA
AbortSessionAnswer.commandCode:
return isRequest ? new AbortSessionRequestImpl(message) : new AbortSessionAnswerImpl(message);
case // STR/STA
SessionTerminationAnswer.commandCode:
return isRequest ? new SessionTerminationRequestImpl(message) : new SessionTerminationAnswerImpl(message);
case // RAR/RAA
GxReAuthAnswer.commandCode:
return isRequest ? new GxReAuthRequestImpl(message) : new GxReAuthAnswerImpl(message);
case // ACR/ACA
AccountingAnswer.commandCode:
return isRequest ? new AccountingRequestImpl(message) : new AccountingAnswerImpl(message);
default:
return new ExtensionDiameterMessageImpl(message);
}
}
Aggregations