use of org.mobicents.diameter.dictionary.AvpRepresentation in project jain-slee.diameter by RestComm.
the class AvpUtilities method createAvp.
public static DiameterAvp createAvp(int avpCode, long vendorId, byte[] value, DiameterAvp[] childAVPs, Class avpImplClass) {
try {
AvpRepresentation avpRep = AvpDictionary.INSTANCE.getAvp(avpCode, vendorId);
int mandatoryAvp = avpRep.getRuleMandatoryAsInt();
int protectedAvp = avpRep.getRuleProtectedAsInt();
if (avpImplClass == DiameterAvpImpl.class) {
Constructor avpConstructor = avpImplClass.getConstructor(int.class, long.class, int.class, int.class, byte[].class, DiameterAvpType.class);
return (DiameterAvp) avpConstructor.newInstance(avpCode, vendorId, mandatoryAvp, protectedAvp, value != null ? value : new byte[] {}, DiameterAvpType.fromString(avpRep.getType()));
} else {
Constructor avpConstructor = avpImplClass.getConstructor(int.class, long.class, int.class, int.class, byte[].class);
GroupedAvp returnAvp = (GroupedAvp) avpConstructor.newInstance(avpCode, vendorId, mandatoryAvp, protectedAvp, value != null ? value : new byte[] {});
returnAvp.setExtensionAvps(childAVPs);
return returnAvp;
}
} catch (NoSuchMethodException e) {
logger.error("Failure while trying to create AVP with Code " + avpCode + " and Vendor-Id " + vendorId, e);
} catch (IllegalArgumentException e) {
logger.error("Failure while trying to create AVP with Code " + avpCode + " and Vendor-Id " + vendorId, e);
} catch (InstantiationException e) {
logger.error("Failure while trying to create AVP with Code " + avpCode + " and Vendor-Id " + vendorId, e);
} catch (IllegalAccessException e) {
logger.error("Failure while trying to create AVP with Code " + avpCode + " and Vendor-Id " + vendorId, e);
} catch (InvocationTargetException e) {
logger.error("Failure while trying to create AVP with Code " + avpCode + " and Vendor-Id " + vendorId, e);
}
return null;
}
use of org.mobicents.diameter.dictionary.AvpRepresentation in project jain-slee.diameter by RestComm.
the class AvpUtilities method getAvpAsCustom.
public static Object getAvpAsCustom(int avpCode, long vendorId, AvpSet set, Class clazz) {
try {
Avp avp = set.getAvp(avpCode, vendorId);
if (avp != null) {
AvpRepresentation rep = AvpDictionary.INSTANCE.getAvp(avpCode, vendorId);
Constructor c = null;
c = clazz.getConstructor(int.class, long.class, int.class, int.class, byte[].class);
return c.newInstance(rep.getCode(), rep.getVendorId(), rep.getRuleMandatoryAsInt(), rep.getRuleProtectedAsInt(), avp.getRawData());
}
} catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to obtain AVP with code " + avpCode + " and Vendor-Id " + vendorId + " as type Custom (Class '" + clazz.getName() + "').", e);
}
}
return null;
}
use of org.mobicents.diameter.dictionary.AvpRepresentation in project jain-slee.diameter by RestComm.
the class AvpUtilities method getAvpsAsCustom.
public static Object[] getAvpsAsCustom(int avpCode, long vendorId, AvpSet set, Class clazz) {
try {
AvpSet avpSet = set.getAvps(avpCode, vendorId);
Object array = Array.newInstance(clazz, avpSet.size());
int i = 0;
AvpRepresentation rep = AvpDictionary.INSTANCE.getAvp(avpCode, vendorId);
Constructor c = clazz.getConstructor(int.class, long.class, int.class, int.class, byte[].class);
for (Avp avp : avpSet) {
Array.set(array, i++, c.newInstance(rep.getCode(), rep.getVendorId(), rep.getRuleMandatoryAsInt(), rep.getRuleProtectedAsInt(), avp.getRawData()));
}
return (Object[]) array;
} catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to obtain AVP with code " + avpCode + " and Vendor-Id " + vendorId + " as type Custom (Class '" + clazz.getName() + "').", e);
}
return (Object[]) Array.newInstance(clazz, 0);
}
}
use of org.mobicents.diameter.dictionary.AvpRepresentation in project jain-slee.diameter by RestComm.
the class DiameterMessageImpl method getAvpsInternal.
// ===== AVP Management =====
private DiameterAvp[] getAvpsInternal(AvpSet set) throws Exception {
List<DiameterAvp> avps = new ArrayList<DiameterAvp>();
for (Avp a : set) {
AvpRepresentation avpRep = AvpDictionary.INSTANCE.getAvp(a.getCode(), a.getVendorId());
if (avpRep == null) {
// log.error("Avp with code: " + a.getCode() + " VendorId: " + a.getVendorId() + " is not listed in dictionary, skipping!");
continue;
} else {
if (avpRep.getType().equals("Grouped")) {
// TODO: There's no info about if AVP has mandatory or protected flags set...
GroupedAvpImpl gAVP = new GroupedAvpImpl(a.getCode(), a.getVendorId(), avpRep.getRuleMandatoryAsInt(), avpRep.getRuleProtectedAsInt(), a.getRaw());
gAVP.setExtensionAvps(getAvpsInternal(a.getGrouped()));
// This is a grouped AVP... let's make it like that.
avps.add(gAVP);
} else {
// TODO: There's no info about if AVP has mandatory or protected flags set...
avps.add(new DiameterAvpImpl(a.getCode(), a.getVendorId(), avpRep.getRuleMandatoryAsInt(), avpRep.getRuleProtectedAsInt(), a.getRaw(), DiameterAvpType.fromString(avpRep.getType())));
}
}
}
return avps.toArray(new DiameterAvp[avps.size()]);
}
use of org.mobicents.diameter.dictionary.AvpRepresentation in project jain-slee.diameter by RestComm.
the class AvpUtilities method addAvp.
/**
* Method for adding AVP with given code and Vendor-Id to the given set.
*
* @param parent the Message/Grouped AVP where AVP will be added to, for validation purposes. if null, no validation is performed.
* @param avpCode the code of the AVP to look for
* @param vendorId the Vendor-Id of the AVP to be added
* @param avp the AVP object
* @param set the AvpSet where to add the AVP
*/
public static void addAvp(Object parent, int avpCode, long vendorId, AvpSet set, Object avp) {
AvpRepresentation avpRep = AvpDictionary.INSTANCE.getAvp(avpCode, vendorId);
if (avpRep != null) {
DiameterAvpType avpType = DiameterAvpType.fromString(avpRep.getType());
boolean isMandatoryAvp = !(avpRep.getRuleMandatory().equals("mustnot") || avpRep.getRuleMandatory().equals("shouldnot"));
boolean isProtectedAvp = avpRep.getRuleProtected().equals("must");
if (avp instanceof byte[]) {
setAvpAsRaw(parent, avpCode, vendorId, set, isMandatoryAvp, isProtectedAvp, (byte[]) avp);
} else {
switch(avpType.getType()) {
case DiameterAvpType._ADDRESS:
case DiameterAvpType._DIAMETER_IDENTITY:
case DiameterAvpType._DIAMETER_URI:
case DiameterAvpType._IP_FILTER_RULE:
case DiameterAvpType._OCTET_STRING:
case DiameterAvpType._QOS_FILTER_RULE:
if (avp instanceof Address) {
// issue: http://code.google.com/p/mobicents/issues/detail?id=2758
setAvpAsRaw(parent, avpCode, vendorId, set, isMandatoryAvp, isProtectedAvp, ((Address) avp).encode());
} else {
setAvpAsOctetString(parent, avpCode, vendorId, set, isMandatoryAvp, isProtectedAvp, avp.toString());
}
break;
case DiameterAvpType._ENUMERATED:
case DiameterAvpType._INTEGER_32:
setAvpAsInteger32(parent, avpCode, vendorId, set, isMandatoryAvp, isProtectedAvp, (Integer) avp);
break;
case DiameterAvpType._FLOAT_32:
setAvpAsFloat32(parent, avpCode, vendorId, set, isMandatoryAvp, isProtectedAvp, (Float) avp);
break;
case DiameterAvpType._FLOAT_64:
setAvpAsFloat64(parent, avpCode, vendorId, set, isMandatoryAvp, isProtectedAvp, (Float) avp);
break;
case DiameterAvpType._GROUPED:
setAvpAsGrouped(parent, avpCode, vendorId, set, isMandatoryAvp, isProtectedAvp, (DiameterAvp[]) avp);
break;
case DiameterAvpType._INTEGER_64:
setAvpAsInteger64(parent, avpCode, vendorId, set, isMandatoryAvp, isProtectedAvp, (Long) avp);
break;
case DiameterAvpType._TIME:
setAvpAsTime(parent, avpCode, vendorId, set, isMandatoryAvp, isProtectedAvp, (Date) avp);
break;
case DiameterAvpType._UNSIGNED_32:
setAvpAsUnsigned32(parent, avpCode, vendorId, set, isMandatoryAvp, isProtectedAvp, (Long) avp);
break;
case DiameterAvpType._UNSIGNED_64:
setAvpAsUnsigned64(parent, avpCode, vendorId, set, isMandatoryAvp, isProtectedAvp, (Long) avp);
break;
case DiameterAvpType._UTF8_STRING:
setAvpAsUTF8String(parent, avpCode, vendorId, set, isMandatoryAvp, isProtectedAvp, (String) avp);
break;
}
}
}
}
Aggregations