Search in sources :

Example 1 with AvpRepresentation

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;
}
Also used : Constructor(java.lang.reflect.Constructor) AvpRepresentation(org.mobicents.diameter.dictionary.AvpRepresentation) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with AvpRepresentation

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;
}
Also used : Constructor(java.lang.reflect.Constructor) Avp(org.jdiameter.api.Avp) AvpRepresentation(org.mobicents.diameter.dictionary.AvpRepresentation) URISyntaxException(java.net.URISyntaxException) ParseException(org.jdiameter.client.api.parser.ParseException) InvocationTargetException(java.lang.reflect.InvocationTargetException) AvpDataException(org.jdiameter.api.AvpDataException)

Example 3 with AvpRepresentation

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);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) AvpSet(org.jdiameter.api.AvpSet) Avp(org.jdiameter.api.Avp) AvpRepresentation(org.mobicents.diameter.dictionary.AvpRepresentation) URISyntaxException(java.net.URISyntaxException) ParseException(org.jdiameter.client.api.parser.ParseException) InvocationTargetException(java.lang.reflect.InvocationTargetException) AvpDataException(org.jdiameter.api.AvpDataException)

Example 4 with AvpRepresentation

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()]);
}
Also used : GroupedAvpImpl(org.mobicents.slee.resource.diameter.base.events.avp.GroupedAvpImpl) DiameterAvpImpl(org.mobicents.slee.resource.diameter.base.events.avp.DiameterAvpImpl) DiameterAvp(net.java.slee.resource.diameter.base.events.avp.DiameterAvp) ArrayList(java.util.ArrayList) VendorSpecificApplicationIdAvp(net.java.slee.resource.diameter.base.events.avp.VendorSpecificApplicationIdAvp) Avp(org.jdiameter.api.Avp) DiameterAvp(net.java.slee.resource.diameter.base.events.avp.DiameterAvp) ProxyInfoAvp(net.java.slee.resource.diameter.base.events.avp.ProxyInfoAvp) FailedAvp(net.java.slee.resource.diameter.base.events.avp.FailedAvp) AvpRepresentation(org.mobicents.diameter.dictionary.AvpRepresentation)

Example 5 with AvpRepresentation

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;
            }
        }
    }
}
Also used : InetAddress(java.net.InetAddress) AvpRepresentation(org.mobicents.diameter.dictionary.AvpRepresentation)

Aggregations

AvpRepresentation (org.mobicents.diameter.dictionary.AvpRepresentation)9 Avp (org.jdiameter.api.Avp)5 Constructor (java.lang.reflect.Constructor)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 AvpDataException (org.jdiameter.api.AvpDataException)4 URISyntaxException (java.net.URISyntaxException)3 AvpSet (org.jdiameter.api.AvpSet)3 ParseException (org.jdiameter.client.api.parser.ParseException)3 ArrayList (java.util.ArrayList)2 DiameterAvp (net.java.slee.resource.diameter.base.events.avp.DiameterAvp)2 DiameterMessageImpl (org.mobicents.slee.resource.diameter.base.events.DiameterMessageImpl)2 UserIdentityAvpImpl (org.mobicents.slee.resource.diameter.sh.events.avp.UserIdentityAvpImpl)2 InetAddress (java.net.InetAddress)1 FailedAvp (net.java.slee.resource.diameter.base.events.avp.FailedAvp)1 GroupedAvp (net.java.slee.resource.diameter.base.events.avp.GroupedAvp)1 ProxyInfoAvp (net.java.slee.resource.diameter.base.events.avp.ProxyInfoAvp)1 VendorSpecificApplicationIdAvp (net.java.slee.resource.diameter.base.events.avp.VendorSpecificApplicationIdAvp)1 SubscribeNotificationsRequest (net.java.slee.resource.diameter.sh.events.SubscribeNotificationsRequest)1 DiameterAvpImpl (org.mobicents.slee.resource.diameter.base.events.avp.DiameterAvpImpl)1 GroupedAvpImpl (org.mobicents.slee.resource.diameter.base.events.avp.GroupedAvpImpl)1