Search in sources :

Example 1 with Session

use of org.jdiameter.api.Session 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);
}
Also used : DiameterAvpImpl(org.mobicents.slee.resource.diameter.base.events.avp.DiameterAvpImpl) HashMap(java.util.HashMap) AccountingRequestImpl(org.mobicents.slee.resource.diameter.base.events.AccountingRequestImpl) AvpNotAllowedException(net.java.slee.resource.diameter.base.events.avp.AvpNotAllowedException) AvpNotAllowedException(net.java.slee.resource.diameter.base.events.avp.AvpNotAllowedException) Session(org.jdiameter.api.Session) Test(org.junit.Test)

Example 2 with Session

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

the class DiameterBaseResourceAdaptor method raActive.

public void raActive() {
    if (tracer.isFineEnabled()) {
        tracer.fine("Diameter Base RA :: raActive.");
    }
    try {
        if (tracer.isInfoEnabled()) {
            tracer.info("Activating Diameter Base RA Entity");
        }
        this.diameterMultiplexerObjectName = new ObjectName("diameter.mobicents:service=DiameterStackMultiplexer");
        Object object = null;
        if (ManagementFactory.getPlatformMBeanServer().isRegistered(this.diameterMultiplexerObjectName)) {
            // trying to get via MBeanServer
            object = ManagementFactory.getPlatformMBeanServer().invoke(this.diameterMultiplexerObjectName, "getMultiplexerMBean", new Object[] {}, new String[] {});
            if (tracer.isInfoEnabled()) {
                tracer.info("Trying to get via Platform MBeanServer: " + this.diameterMultiplexerObjectName + ", object: " + object);
            }
        } else {
            // trying to get via locateJBoss
            object = MBeanServerLocator.locateJBoss().invoke(this.diameterMultiplexerObjectName, "getMultiplexerMBean", new Object[] {}, new String[] {});
            if (tracer.isInfoEnabled()) {
                tracer.info("Trying to get via JBoss MBeanServer: " + this.diameterMultiplexerObjectName + ", object: " + object);
            }
        }
        if (object != null && object instanceof DiameterStackMultiplexerMBean) {
            this.diameterMux = (DiameterStackMultiplexerMBean) object;
        }
        // this.activities = new ConcurrentHashMap<ActivityHandle, DiameterActivity>();
        // Initialize the protocol stack
        initStack();
        // Initialize activity storage
        initActivitiesMgmt();
        // Initialize factories
        this.messageFactory = new DiameterMessageFactoryImpl(stack);
        this.avpFactory = new DiameterAvpFactoryImpl();
        // Setup session factories
        this.sessionFactory = this.stack.getSessionFactory();
        this.accSessionFactory = AccountingSessionFactory.INSTANCE;
        this.accSessionFactory.registerListener(this, messageTimeout, sessionFactory);
        this.authSessionFactory = new AuthorizationSessionFactory(this, messageTimeout, sessionFactory);
        this.proxySessionFactory = new SessionFactory() {

            public <T extends AppSession> T getNewAppSession(ApplicationId applicationId, Class<? extends AppSession> userSession) throws InternalException {
                return (T) sessionFactory.getNewAppSession(applicationId, userSession);
            }

            public <T extends AppSession> T getNewAppSession(String sessionId, ApplicationId applicationId, Class<? extends AppSession> userSession) throws InternalException {
                return (T) sessionFactory.getNewAppSession(sessionId, applicationId, userSession);
            }

            public RawSession getNewRawSession() throws InternalException {
                try {
                    return stack.getSessionFactory().getNewRawSession();
                } catch (IllegalDiameterStateException e) {
                    tracer.severe("Failure while obtaining Session Factory for new Raw Session.", e);
                    return null;
                }
            }

            public Session getNewSession() throws InternalException {
                Session session = sessionFactory.getNewSession();
                sessionCreated(session);
                return session;
            }

            public Session getNewSession(String sessionId) throws InternalException {
                Session session = sessionFactory.getNewSession(sessionId);
                sessionCreated(session);
                return session;
            }

            public String getSessionId() {
                return sessionFactory.getSessionId();
            }

            public String getSessionId(String customPart) {
                return sessionFactory.getSessionId(customPart);
            }
        };
        // Register Accounting App Session Factories
        ((ISessionFactory) sessionFactory).registerAppFacory(ServerAccSession.class, accSessionFactory);
        ((ISessionFactory) sessionFactory).registerAppFacory(ClientAccSession.class, accSessionFactory);
        // Register Authorization App Session Factories
        ((ISessionFactory) sessionFactory).registerAppFacory(ServerAuthSession.class, authSessionFactory);
        ((ISessionFactory) sessionFactory).registerAppFacory(ClientAuthSession.class, authSessionFactory);
    } catch (Exception e) {
        tracer.severe("Error Activating Diameter Base RA Entity", e);
    }
}
Also used : AccountingSessionFactory(org.mobicents.slee.resource.diameter.base.handlers.AccountingSessionFactory) AuthorizationSessionFactory(org.mobicents.slee.resource.diameter.base.handlers.AuthorizationSessionFactory) SessionFactory(org.jdiameter.api.SessionFactory) ISessionFactory(org.jdiameter.client.api.ISessionFactory) DiameterStackMultiplexerMBean(org.mobicents.diameter.stack.DiameterStackMultiplexerMBean) AuthorizationSessionFactory(org.mobicents.slee.resource.diameter.base.handlers.AuthorizationSessionFactory) RawSession(org.jdiameter.api.RawSession) AvpDataException(org.jdiameter.api.AvpDataException) InternalException(org.jdiameter.api.InternalException) AvpNotAllowedException(net.java.slee.resource.diameter.base.events.avp.AvpNotAllowedException) IllegalDiameterStateException(org.jdiameter.api.IllegalDiameterStateException) IOException(java.io.IOException) CreateActivityException(net.java.slee.resource.diameter.base.CreateActivityException) InvalidConfigurationException(javax.slee.resource.InvalidConfigurationException) ObjectName(javax.management.ObjectName) InternalException(org.jdiameter.api.InternalException) IllegalDiameterStateException(org.jdiameter.api.IllegalDiameterStateException) ISessionFactory(org.jdiameter.client.api.ISessionFactory) ApplicationId(org.jdiameter.api.ApplicationId) ServerAuthSession(org.jdiameter.api.auth.ServerAuthSession) ClientAuthSession(org.jdiameter.api.auth.ClientAuthSession) AppSession(org.jdiameter.api.app.AppSession) RawSession(org.jdiameter.api.RawSession) ClientAccSession(org.jdiameter.api.acc.ClientAccSession) Session(org.jdiameter.api.Session) ServerAccSession(org.jdiameter.api.acc.ServerAccSession)

Aggregations

AvpNotAllowedException (net.java.slee.resource.diameter.base.events.avp.AvpNotAllowedException)2 Session (org.jdiameter.api.Session)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ObjectName (javax.management.ObjectName)1 InvalidConfigurationException (javax.slee.resource.InvalidConfigurationException)1 CreateActivityException (net.java.slee.resource.diameter.base.CreateActivityException)1 ApplicationId (org.jdiameter.api.ApplicationId)1 AvpDataException (org.jdiameter.api.AvpDataException)1 IllegalDiameterStateException (org.jdiameter.api.IllegalDiameterStateException)1 InternalException (org.jdiameter.api.InternalException)1 RawSession (org.jdiameter.api.RawSession)1 SessionFactory (org.jdiameter.api.SessionFactory)1 ClientAccSession (org.jdiameter.api.acc.ClientAccSession)1 ServerAccSession (org.jdiameter.api.acc.ServerAccSession)1 AppSession (org.jdiameter.api.app.AppSession)1 ClientAuthSession (org.jdiameter.api.auth.ClientAuthSession)1 ServerAuthSession (org.jdiameter.api.auth.ServerAuthSession)1 ISessionFactory (org.jdiameter.client.api.ISessionFactory)1 Test (org.junit.Test)1