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

http://mobicents.googlecode.com/ · Java · 173 lines · 79 code · 25 blank · 69 comment · 20 complexity · 0bc74a9a002a5c52bd45e150ebb785f3 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.AsState;
  26. import org.mobicents.protocols.ss7.m3ua.impl.Asp;
  27. import org.mobicents.protocols.ss7.m3ua.impl.AspState;
  28. import org.mobicents.protocols.ss7.m3ua.impl.fsm.FSM;
  29. import org.mobicents.protocols.ss7.m3ua.impl.fsm.State;
  30. import org.mobicents.protocols.ss7.m3ua.impl.fsm.TransitionHandler;
  31. import org.mobicents.protocols.ss7.m3ua.message.MessageClass;
  32. import org.mobicents.protocols.ss7.m3ua.message.MessageType;
  33. import org.mobicents.protocols.ss7.m3ua.message.mgmt.Notify;
  34. import org.mobicents.protocols.ss7.m3ua.parameter.Status;
  35. import org.mobicents.protocols.ss7.m3ua.parameter.TrafficModeType;
  36. /**
  37. * <p>
  38. * Transitions the {@link RemAsImpl} from {@link AsState.ACTIVE} to
  39. * {@link AsState.PENDING}. The transition depends on {@link TrafficModeType}
  40. * defined for this {@link RemAsImpl} and {@link AspState} of enclosed
  41. * {@link RemAsImpl}
  42. * </p>
  43. *
  44. *
  45. * <ul>
  46. *
  47. * <li>
  48. * <p> If the {@link TrafficModeType} for this {@link RemAsImpl} is Loadshare and
  49. * there are {@link RemAsImpl} that are still {@link AspState.ACTIVE}, {@link RemAsImpl}
  50. * remains {@link AsState.ACTIVE}. If number of {@link RemAsImpl} in {@link AspState.ACTIVE}
  51. * is greater than minimum Asp required for load balancing, it just returns false and
  52. * transition doesn't happen. However if number of {@link RemAsImpl} in {@link AspState.ACTIVE}
  53. * is less than minimum Asp required for load balancing, it sends {@link Status.INFO_Insufficient_ASP_Resources_Active}
  54. * {@link Notify} to remote ASP who are {@link AspState.INACTIVE} and transition doesn't happen.
  55. * </p>
  56. *
  57. * <p>
  58. * If there are no {@link RemAsImpl} in {@link AspState.ACTIVE}, it transitions to {@link AsState.PENDIING} and
  59. * sends {@link Status.INFO_AS_PENDING} {@link Notify} to remote ASP who are {@link AspState.INACTIVE}.
  60. * </p>
  61. * </li>
  62. *
  63. * <li>
  64. * <p> If the {@link TrafficModeType} for this {@link RemAsImpl} is Override
  65. * it transitions to {@link AsState.PENDIING} and sends {@link Status.INFO_AS_PENDING} {@link Notify}
  66. * to remote ASP who are {@link AspState.INACTIVE}.
  67. * </p>
  68. * </li>
  69. *
  70. * </ul>
  71. *
  72. * @author amit bhayani
  73. *
  74. */
  75. public class RemAsTransActToPendRemAspInac implements TransitionHandler {
  76. private static final Logger logger = Logger.getLogger(RemAsTransActToPendRemAspInac.class);
  77. private RemAsImpl as = null;
  78. private FSM fsm;
  79. private int lbCount = 0;
  80. public RemAsTransActToPendRemAspInac(RemAsImpl as, FSM fsm) {
  81. this.as = as;
  82. this.fsm = fsm;
  83. }
  84. public boolean process(State state) {
  85. try {
  86. RemAspImpl remAsp = (RemAspImpl) this.fsm.getAttribute(RemAsImpl.ATTRIBUTE_ASP);
  87. if (this.as.getTrafficModeType().getMode() == TrafficModeType.Broadcast) {
  88. // We don't support this
  89. return false;
  90. }
  91. if (this.as.getTrafficModeType().getMode() == TrafficModeType.Loadshare) {
  92. this.lbCount = 0;
  93. for (FastList.Node<Asp> n = this.as.getAspList().head(), end = this.as.getAspList().tail(); (n = n
  94. .getNext()) != end;) {
  95. RemAspImpl remAspImpl = (RemAspImpl) n.getValue();
  96. if (remAspImpl.getState() == AspState.ACTIVE) {
  97. this.lbCount++;
  98. }
  99. }// for
  100. if (this.lbCount >= this.as.getMinAspActiveForLb()) {
  101. // we still have more ASP's ACTIVE for lb. Don't change
  102. // state
  103. return false;
  104. }
  105. // We are below minAspActiveForLb required for LB
  106. if (this.lbCount > 0) {
  107. // But In any case if we have at least one ASP that can take
  108. // care of traffic, don't change state but send the "Ins.
  109. // ASPs" to INACTIVE ASP's
  110. for (FastList.Node<Asp> n = this.as.getAspList().head(), end = this.as.getAspList().tail(); (n = n
  111. .getNext()) != end;) {
  112. remAsp = (RemAspImpl) n.getValue();
  113. if (remAsp.getState() == AspState.INACTIVE) {
  114. Notify notify = this.createNotify(remAsp, Status.STATUS_Other,
  115. Status.INFO_Insufficient_ASP_Resources_Active);
  116. remAsp.getAspFactory().write(notify);
  117. }
  118. }
  119. return false;
  120. }
  121. }// If Loadshare
  122. // We have reached here means AS is transitioning to be PENDING.
  123. // Send new AS STATUS to all INACTIVE APS's
  124. for (FastList.Node<Asp> n = this.as.getAspList().head(), end = this.as.getAspList().tail(); (n = n
  125. .getNext()) != end;) {
  126. remAsp = (RemAspImpl) n.getValue();
  127. if (remAsp.getState() == AspState.INACTIVE) {
  128. Notify notify = this.createNotify(remAsp, Status.STATUS_AS_State_Change, Status.INFO_AS_PENDING);
  129. remAsp.getAspFactory().write(notify);
  130. }
  131. }
  132. } catch (Exception e) {
  133. logger.error(String.format("Error while translating Rem AS to PENDING. %s", this.fsm.toString()), e);
  134. }
  135. return true;
  136. }
  137. private Notify createNotify(RemAspImpl remAsp, int type, int info) {
  138. Notify msg = (Notify) this.as.getM3UAProvider().getMessageFactory()
  139. .createMessage(MessageClass.MANAGEMENT, MessageType.NOTIFY);
  140. Status status = this.as.getM3UAProvider().getParameterFactory().createStatus(type, info);
  141. msg.setStatus(status);
  142. if (remAsp.getASPIdentifier() != null) {
  143. msg.setASPIdentifier(remAsp.getASPIdentifier());
  144. }
  145. msg.setRoutingContext(this.as.getRoutingContext());
  146. return msg;
  147. }
  148. }