Search in sources :

Example 21 with Avp

use of org.jdiameter.api.Avp in project jain-slee.diameter by RestComm.

the class AvpUtilities method setAvpAsUTF8String.

/**
 * Adds AVP to {@link AvpSet} as OctetString with the given code and given Vendor-Id plus defined mandatory and protected flags.
 *
 * @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
 * @param vendorId the Vendor-Id of the AVP
 * @param set the AvpSet to add AVP
 * @param isMandatory the value for the mandatory bit
 * @param isProtected the value for the protected bit
 * @param value the value of the AVP to add
 */
public static void setAvpAsUTF8String(Object parent, int avpCode, long vendorId, AvpSet set, boolean isMandatory, boolean isProtected, String value) {
    performPreAddOperations(parent, avpCode, vendorId, set);
    switch(avpCode) {
        case Avp.SESSION_ID:
            // (...) All messages pertaining to a specific session MUST include only one Session-Id AVP (...)
            set.removeAvp(avpCode);
            // (...) the Session-Id SHOULD appear immediately following the Diameter Header
            set.insertAvp(0, avpCode, value, vendorId, isMandatory, isProtected, false);
            break;
        case Avp.ORIGIN_HOST:
        case Avp.ORIGIN_REALM:
        case Avp.DESTINATION_HOST:
        case Avp.DESTINATION_REALM:
            // This AVP SHOULD be placed as close to the Diameter header as possible.
            Avp firstAvp = set.size() > 0 ? set.getAvpByIndex(0) : null;
            int index = (firstAvp != null && firstAvp.getCode() == Avp.SESSION_ID) ? 1 : 0;
            set.insertAvp(index, avpCode, value, vendorId, isMandatory, isProtected, false);
            break;
        default:
            set.addAvp(avpCode, value, vendorId, isMandatory, isProtected, false);
            break;
    }
}
Also used : Avp(org.jdiameter.api.Avp)

Example 22 with Avp

use of org.jdiameter.api.Avp in project jain-slee.diameter by RestComm.

the class AvpUtilities method setAvpAsRaw.

/**
 * Adds AVP to {@link AvpSet} as raw data with the given code and given Vendor-Id plus defined mandatory and protected flags.
 *
 * @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
 * @param vendorId the Vendor-Id of the AVP
 * @param set the AvpSet to add AVP
 * @param isMandatory the value for the mandatory bit
 * @param isProtected the value for the protected bit
 * @param value the value of the AVP to add
 */
public static void setAvpAsRaw(Object parent, int avpCode, long vendorId, AvpSet set, boolean isMandatory, boolean isProtected, byte[] value) {
    performPreAddOperations(parent, avpCode, vendorId, set);
    switch(avpCode) {
        case Avp.SESSION_ID:
            // (...) All messages pertaining to a specific session MUST include only one Session-Id AVP (...)
            set.removeAvp(avpCode);
            // (...) the Session-Id SHOULD appear immediately following the Diameter Header
            set.insertAvp(0, avpCode, value, vendorId, isMandatory, isProtected);
            break;
        case Avp.ORIGIN_HOST:
        case Avp.ORIGIN_REALM:
        case Avp.DESTINATION_HOST:
        case Avp.DESTINATION_REALM:
            // This AVP SHOULD be placed as close to the Diameter header as possible.
            Avp firstAvp = set.size() > 0 ? set.getAvpByIndex(0) : null;
            int index = (firstAvp != null && firstAvp.getCode() == Avp.SESSION_ID) ? 1 : 0;
            set.insertAvp(index, avpCode, value, vendorId, isMandatory, isProtected);
            break;
        default:
            set.addAvp(avpCode, value, vendorId, isMandatory, isProtected);
            break;
    }
}
Also used : Avp(org.jdiameter.api.Avp)

Example 23 with Avp

use of org.jdiameter.api.Avp 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 24 with Avp

use of org.jdiameter.api.Avp in project jain-slee.diameter by RestComm.

the class AvpFlagsTest method checkAvpFlags.

/**
 * @param set
 *          the set to check
 * @return an array of offending AVPs
 */
protected ArrayList<String> checkAvpFlags(AvpSet set) {
    ArrayList<String> failedAvps = new ArrayList<String>();
    for (Avp avp : set) {
        // System.out.println(avp.getVendorId() + ":" + avp.getCode() + " V[" + avp.isVendorId() + "] M[" +
        // avp.isMandatory() + "] P[" + avp.isEncrypted() + "]");
        AvpRepresentation avpRep = validator.getAvp(avp.getCode(), avp.getVendorId());
        // Mandatory must not be set if rule is MUST NOT or SHOULD NOT
        if (avp.isMandatory() && (avpRep.getRuleMandatory().equals("mustnot") || avpRep.getRuleMandatory().equals("shouldnot"))) {
            failedAvps.add("- Code[" + avp.getCode() + "], Vendor-Id[" + avp.getVendorId() + "], Flag[M / '" + avp.isMandatory() + "' vs '" + avpRep.getRuleMandatory() + "']");
        }
        // Protected must not be set if rule is MUST or MAY
        if (avp.isEncrypted() && !(avpRep.getRuleProtected().equals("must") || avpRep.getRuleProtected().equals("may"))) {
            failedAvps.add("- Code[" + avp.getCode() + "], Vendor-Id[" + avp.getVendorId() + "], Flag[P / '" + avp.isEncrypted() + "' vs '" + avpRep.getRuleProtected() + "']");
        }
        // Vendor must be set if rule is MUST or MAY
        if (avp.isEncrypted() && !(avpRep.getRuleProtected().equals("must") || avpRep.getRuleProtected().equals("may"))) {
            failedAvps.add("- Code[" + avp.getCode() + "], Vendor-Id[" + avp.getVendorId() + "], Flag[P / '" + avp.isEncrypted() + "' vs '" + avpRep.getRuleProtected() + "']");
        }
        AvpSet subAvps = null;
        try {
            subAvps = avp.getGrouped();
        } catch (Exception e) {
        }
        if (subAvps != null) {
            failedAvps.addAll(checkAvpFlags(subAvps));
        }
    }
    return failedAvps;
}
Also used : ArrayList(java.util.ArrayList) AvpSet(org.jdiameter.api.AvpSet) Avp(org.jdiameter.api.Avp) DiameterAvp(net.java.slee.resource.diameter.base.events.avp.DiameterAvp) AvpRepresentation(org.jdiameter.api.validation.AvpRepresentation) NoSuchAvpException(net.java.slee.resource.diameter.base.NoSuchAvpException)

Example 25 with Avp

use of org.jdiameter.api.Avp in project jain-slee.diameter by RestComm.

the class ExpectedAvp method testPresentAvps.

/* ammendonca: removal allowed is gone...
	  @Test
	  public void testOperationsAddWithValidatorOffAndRemovalAllowed() {
	    instance.configure(this.getClass().getClassLoader().getResourceAsStream(validatorOffFile));
	    instance.setEnabled(false);
	    // 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"/>
	    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"/>
	    // We don't add this one, make it fail
	    // <avp name="Destination-Host" code="293" vendor="0" multiplicity="0" 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);

	      // this should fail eve so, but just in case
	      fail("Should not send this message. Message MUST contain Destination-Realm AVP (283), and it is not present.");
	    }
	    catch (RouteException e) {
	      // We want to come here, since we lack Destination-Realm AVP (283).
	    }
	    catch (Exception e) {
	      e.printStackTrace();
	      fail("Message failed to be sent for wrong reason. Expected RouteException due to lack of Destination-Realm AVP (283), Received " + e);
	    }

	    // <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-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);
	    }

	    int expectedAcctApplicationIdAvps = request.hasAcctApplicationId() ? 1 : 0;

	    // 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);
	      expectedAcctApplicationIdAvps++;
	    }
	    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()
	            + ").");
	      }
	      else {
	        fail("Wrong AVP not allowed to be added, with Validator in OFF state. It's smart, but not what we want.");
	      }
	    }

	    // 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);
	      // this should fail eve so, but just in case
	    }
	    catch (AvpNotAllowedException e) {
	      fail("Wrong AVP not allowed to be added, with Validator in OFF state. It's smart, but not what we want.");
	    }
	    catch (Exception e) {
	      fail("Failed to add AVP, when it should succeed, even being a not allowed AVP.");
	    }

	    // This is just in case
	    Map<ExpectedAvp, ExpectedAvp> expectedAvps = new HashMap<ExpectedAvp, ExpectedAvp>();
	    ExpectedAvp a = new ExpectedAvp();
	    a.code = 263;
	    a.count = 2;
	    expectedAvps.put(a, a);
	    a = new ExpectedAvp();
	    a.code = 264;
	    a.count = 2; // was 1 but request comes with one already...
	    expectedAvps.put(a, a);
	    a = new ExpectedAvp();
	    a.code = 296;
	    // cause its legal in this case.
	    a.count = 3; // was 2 but request comes with one already...
	    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 = expectedAcctApplicationIdAvps;
	    expectedAvps.put(a, a);
	    // yes, its legal also. we dont check
	    a = new ExpectedAvp();
	    a.code = 258;
	    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);
	  }
	   */
private void testPresentAvps(AvpSet set, Map<ExpectedAvp, ExpectedAvp> expected) {
    for (ExpectedAvp e : expected.values()) {
        AvpSet innerSet = set.removeAvp(e.code);
        if (innerSet.size() != e.count) {
            fail("Wrong count of avps, code: " + e.code + ", vendor:" + e.vendor + ". Expected: " + e.count + ", present: " + innerSet.size());
        }
        if (e.count > 0) {
            Avp avp = innerSet.getAvpByIndex(0);
            if (avp.getVendorId() != e.vendor) {
                fail("Wrong vendor of avp, code: " + e.code + ". Expected: " + e.vendor + ", present: " + avp.getVendorId());
            }
        }
    }
    if (set.size() > 0) {
        StringBuffer buf = new StringBuffer();
        for (Avp a : set) {
            buf.append("Code[").append(a.getCode()).append("] Vendor[").append("], ");
        }
        fail("Wrong count of avps, removed all expected, left overs: " + set.size() + " -- " + buf.toString());
    }
}
Also used : AvpSet(org.jdiameter.api.AvpSet) Avp(org.jdiameter.api.Avp)

Aggregations

Avp (org.jdiameter.api.Avp)37 AvpSet (org.jdiameter.api.AvpSet)30 AvpDataException (org.jdiameter.api.AvpDataException)28 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 DiameterAvp (net.java.slee.resource.diameter.base.events.avp.DiameterAvp)6 URISyntaxException (java.net.URISyntaxException)5 ParseException (org.jdiameter.client.api.parser.ParseException)5 AvpRepresentation (org.mobicents.diameter.dictionary.AvpRepresentation)5 ArrayList (java.util.ArrayList)4 FailedAvp (net.java.slee.resource.diameter.base.events.avp.FailedAvp)4 ProxyInfoAvp (net.java.slee.resource.diameter.base.events.avp.ProxyInfoAvp)4 VendorSpecificApplicationIdAvp (net.java.slee.resource.diameter.base.events.avp.VendorSpecificApplicationIdAvp)4 Constructor (java.lang.reflect.Constructor)3 Date (java.util.Date)2 AvpRepresentation (org.jdiameter.api.validation.AvpRepresentation)2 NoSuchAvpException (net.java.slee.resource.diameter.base.NoSuchAvpException)1 DiameterHeader (net.java.slee.resource.diameter.base.events.DiameterHeader)1 AvpNotAllowedException (net.java.slee.resource.diameter.base.events.avp.AvpNotAllowedException)1 ExperimentalResultAvp (net.java.slee.resource.diameter.base.events.avp.ExperimentalResultAvp)1 GroupedAvp (net.java.slee.resource.diameter.base.events.avp.GroupedAvp)1