/protocols/ss7/m3ua/impl/src/main/java/org/mobicents/protocols/ss7/m3ua/impl/sg/RemAsTransActToPendRemAspDwn.java

http://mobicents.googlecode.com/ · Java · 155 lines · 84 code · 30 blank · 41 comment · 24 complexity · c213ae1a5cbe578a339de8a73e254fc8 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.mobicents.protocols.ss7.m3ua.impl.sg;
  23. import javolution.util.FastList;
  24. import org.apache.log4j.Logger;
  25. import org.mobicents.protocols.ss7.m3ua.impl.Asp;
  26. import org.mobicents.protocols.ss7.m3ua.impl.AspState;
  27. import org.mobicents.protocols.ss7.m3ua.impl.fsm.FSM;
  28. import org.mobicents.protocols.ss7.m3ua.impl.fsm.State;
  29. import org.mobicents.protocols.ss7.m3ua.impl.fsm.TransitionHandler;
  30. import org.mobicents.protocols.ss7.m3ua.message.MessageClass;
  31. import org.mobicents.protocols.ss7.m3ua.message.MessageType;
  32. import org.mobicents.protocols.ss7.m3ua.message.mgmt.Notify;
  33. import org.mobicents.protocols.ss7.m3ua.parameter.Status;
  34. import org.mobicents.protocols.ss7.m3ua.parameter.TrafficModeType;
  35. /**
  36. *
  37. * @author amit bhayani
  38. *
  39. */
  40. public class RemAsTransActToPendRemAspDwn implements TransitionHandler {
  41. private static final Logger logger = Logger.getLogger(RemAsTransActToPendRemAspDwn.class);
  42. private RemAsImpl as = null;
  43. private FSM fsm;
  44. private int lbCount = 0;
  45. public RemAsTransActToPendRemAspDwn(RemAsImpl as, FSM fsm) {
  46. this.as = as;
  47. this.fsm = fsm;
  48. }
  49. public boolean process(State state) {
  50. try {
  51. RemAspImpl remAsp = (RemAspImpl) this.fsm.getAttribute(RemAsImpl.ATTRIBUTE_ASP);
  52. if (this.as.getTrafficModeType().getMode() == TrafficModeType.Broadcast) {
  53. // We don't support this
  54. return false;
  55. }
  56. if (this.as.getTrafficModeType().getMode() == TrafficModeType.Loadshare) {
  57. this.lbCount = 0;
  58. for (FastList.Node<Asp> n = this.as.getAspList().head(), end = this.as.getAspList()
  59. .tail(); (n = n.getNext()) != end;) {
  60. RemAspImpl remAspImpl = (RemAspImpl) n.getValue();
  61. if (remAspImpl.getState() == AspState.ACTIVE) {
  62. this.lbCount++;
  63. }
  64. }// for
  65. // TODO : yet not sure. May be the communication died with this
  66. // ASP who is causing this Transition; so the state will still
  67. // be ACTIVE for it
  68. if (remAsp.getState() == AspState.ACTIVE) {
  69. this.lbCount--;
  70. }
  71. if (this.lbCount >= this.as.getMinAspActiveForLb()) {
  72. // we still have more ASP's ACTIVE for lb. Don't change
  73. // state
  74. return false;
  75. }
  76. if (this.lbCount > 0) {
  77. // In any case if we have at least one ASP that can take
  78. // care of traffic, don't change state
  79. // But we are below threshold. Send "Ins. ASPs" to INACTIVE
  80. // ASP's but not to ASP that caused this transition as it is
  81. // already DOWN
  82. for (FastList.Node<Asp> n = this.as.getAspList().head(), end = this.as
  83. .getAspList().tail(); (n = n.getNext()) != end;) {
  84. RemAspImpl remAspTemp = (RemAspImpl) n.getValue();
  85. // TODO : needed? The state should be changed before
  86. // signalling AS transition
  87. if (remAspTemp.getName().compareTo(remAsp.getName()) == 0) {
  88. continue;
  89. }
  90. if (remAspTemp.getState() == AspState.INACTIVE) {
  91. Notify notify = this.createNotify(remAsp, Status.STATUS_Other,
  92. Status.INFO_Insufficient_ASP_Resources_Active);
  93. remAspTemp.getAspFactory().write(notify);
  94. }
  95. }
  96. return false;
  97. }
  98. }//If Loadshare
  99. // We have reached here means AS is transitioning to be PENDING.
  100. // Send new AS STATUS to all INACTIVE APS's
  101. for (FastList.Node<Asp> n = this.as.getAspList().head(), end = this.as.getAspList()
  102. .tail(); (n = n.getNext()) != end;) {
  103. remAsp = (RemAspImpl) n.getValue();
  104. if (remAsp.getState() == AspState.INACTIVE) {
  105. Notify notify = this.createNotify(remAsp, Status.STATUS_AS_State_Change, Status.INFO_AS_PENDING);
  106. remAsp.getAspFactory().write(notify);
  107. }
  108. }
  109. } catch (Exception e) {
  110. logger.error(String.format("Error while translating Rem AS to PENDING. %s", this.fsm.toString()), e);
  111. }
  112. return true;
  113. }
  114. private Notify createNotify(RemAspImpl remAsp, int type, int info) {
  115. Notify msg = (Notify) this.as.getM3UAProvider().getMessageFactory().createMessage(MessageClass.MANAGEMENT,
  116. MessageType.NOTIFY);
  117. Status status = this.as.getM3UAProvider().getParameterFactory().createStatus(type, info);
  118. msg.setStatus(status);
  119. if (remAsp.getASPIdentifier() != null) {
  120. msg.setASPIdentifier(remAsp.getASPIdentifier());
  121. }
  122. msg.setRoutingContext(this.as.getRoutingContext());
  123. return msg;
  124. }
  125. }