Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #249. 2 different sorting algorithms available. #260

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public class RouterImpl implements Router {

private String persistDir = null;

private final RuleComparator ruleComparator = new RuleComparator();
private RuleComparatorFactory ruleComparatorFactory = null;
// rule list
private RuleMap<Integer, Rule> rulesMap = new RuleMap<Integer, Rule>();
private SccpAddressMap<Integer, SccpAddressImpl> routingAddresses = new SccpAddressMap<Integer, SccpAddressImpl>();
Expand All @@ -221,6 +221,7 @@ public class RouterImpl implements Router {
public RouterImpl(String name, SccpStack sccpStack) {
this.name = name;
this.sccpStack = sccpStack;
this.ruleComparatorFactory = RuleComparatorFactory.getInstance("RuleComparatorFactory");

binding.setAlias(RuleImpl.class, RULE);
binding.setClassAttribute(CLASS_ATTRIBUTE);
Expand Down Expand Up @@ -447,7 +448,7 @@ public void addRule(int id, RuleType ruleType, LoadSharingAlgorithm algo, Origin
rulesArray[count++] = rule;

// Sort
Arrays.sort(rulesArray, ruleComparator);
Arrays.sort(rulesArray, this.ruleComparatorFactory.getRuleComparator());

RuleMap<Integer, Rule> newRule = new RuleMap<Integer, Rule>();
for (int i = 0; i < rulesArray.length; i++) {
Expand Down Expand Up @@ -523,7 +524,7 @@ public void modifyRule(int id, RuleType ruleType, LoadSharingAlgorithm algo, Ori
rulesArray[count++] = rule;

// Sort
Arrays.sort(rulesArray, ruleComparator);
Arrays.sort(rulesArray, this.ruleComparatorFactory.getRuleComparator());

RuleMap<Integer, Rule> newRule = new RuleMap<Integer, Rule>();
for (int i = 0; i < rulesArray.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.mobicents.protocols.ss7.sccp.impl.router;

import java.util.Comparator;

/**
* Created by faizan on 11/08/17.
*/
public class RuleByIdComparator implements Comparator<RuleImpl> {

@Override
public int compare( RuleImpl o1, RuleImpl o2 ) {
return (o1.getRuleId() < o2.getRuleId() ? -1 : 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ public int compare(RuleImpl o1, RuleImpl o2) {
return 1;

// Check if digits are exactly same. In that case we sort based on the callingDigits
if ( digits1.equals( digits2 )) {
// if rule1 has calling party and rule2 doesn't then we put rule1 first
if ( o1.getPatternCallingAddress() != null && o2.getPatternCallingAddress() == null ) {
return -1;
} else if ( o1.getPatternCallingAddress() == null && o2.getPatternCallingAddress() != null ) {
return 1;
} else if ( o1.getPatternCallingAddress() != null && o2.getPatternCallingAddress() != null ) {
if ( digits1.equals( digits2 ) && (o1.getPatternCallingAddress() != null || o2.getPatternCallingAddress() != null )) {
if ( o1.getPatternCallingAddress() != null &&
o2.getPatternCallingAddress() == null ) {
return -1;
} else if ( o1.getPatternCallingAddress() == null &&
o2.getPatternCallingAddress() != null ) {
return 1;
}
// both have calling party addresses. lets compare these 2
digits1 = o1.getPatternCallingAddress().getGlobalTitle().getDigits();
digits2 = o2.getPatternCallingAddress().getGlobalTitle().getDigits();
Expand All @@ -101,7 +102,6 @@ public int compare(RuleImpl o1, RuleImpl o2) {
digits2 = digits2.replaceAll(SECTION_SEPARTOR, "");

return compareDigits( digits1, digits2 );
}
}
return compareDigits( digits1, digits2 );
}
Expand Down Expand Up @@ -129,7 +129,8 @@ private int compareDigits( String digits1, String digits2 ) {
return -1;
}

return 1;
return digits1.compareTo( digits2 );
// return 1;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.mobicents.protocols.ss7.sccp.impl.router;

import java.util.Comparator;

/**
* Created by faizan on 11/08/17.
*/
public class RuleComparatorFactory {

private final String name;

private static RuleComparatorFactory instance = null;
private Comparator<RuleImpl> ruleComparator = new RuleComparator(); // by default we have this one

public RuleComparatorFactory( String name ) {
this.name = name;
}

public String getName() {
return this.name;
}
public static RuleComparatorFactory getInstance( String name) {
if ( instance == null ) {
instance = new RuleComparatorFactory( name);
}
return instance;
}

public static RuleComparatorFactory getInstance() {
return instance;
}

public void setRuleComparator(Comparator<RuleImpl> comparator) {
this.ruleComparator = comparator;
}

public Comparator<RuleImpl> getRuleComparator() {
return this.ruleComparator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public boolean matches(SccpAddress address, SccpAddress callingAddress, boolean
return false; // Called GT didn't match. No point in going forward to match calling
}

if ( patternCallingAddress == null || callingAddress == null) {
if ( patternCallingAddress == null || callingAddress == null ) {
// callingAddress or pattern is null then we consider it a match
return true;
}
Expand All @@ -369,6 +369,12 @@ public boolean matches(SccpAddress address, SccpAddress callingAddress, boolean
return false;
}

if ( patternCallingAddress.getGlobalTitle() == null ||
callingAddress.getGlobalTitle() == null ) {
// callingAddress or pattern have no GT then no point in matching them
return true;
}

// finally match on calling GT
return matchGt( callingAddress, patternCallingAddress );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public void testOrdering() throws Exception {
SccpAddress pattern1 = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE,
factory.createGlobalTitle("800/????/9", 1), 0, 0);
router.addRule(1, RuleType.SOLITARY, LoadSharingAlgorithm.Undefined, OriginationType.ALL, pattern1, "R/K/R", 1, -1,
null, 0, patternDefaultCalling);
null, 0, null);

// Rule 2
SccpAddress pattern2 = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE,
Expand All @@ -308,7 +308,7 @@ public void testOrdering() throws Exception {
router.addRoutingAddress(2, primaryAddr2);

router.addRule(2, RuleType.SOLITARY, LoadSharingAlgorithm.Undefined, OriginationType.ALL, pattern2, "K", 2, -1,
null, 0, patternDefaultCalling);
null, 0, null);

// Rule 3
SccpAddress pattern3 = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE,
Expand All @@ -317,45 +317,45 @@ public void testOrdering() throws Exception {
factory.createGlobalTitle("-/-/-/-", 1), 123, 0);
router.addRoutingAddress(3, primaryAddr3);
router.addRule(3, RuleType.SOLITARY, LoadSharingAlgorithm.Undefined, OriginationType.ALL, pattern3, "K/K/K/K", 3, -1,
null, 0, patternDefaultCalling);
null, 0, null);

// Rule 4
SccpAddress pattern4 = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, factory.createGlobalTitle("80/??/0/???/9", 1),0, 0);
SccpAddress primaryAddr4 = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, factory.createGlobalTitle( "90/-/1/-/7", 1),123,
0);
router.addRoutingAddress(4, primaryAddr4);
router.addRule(4, RuleType.SOLITARY, LoadSharingAlgorithm.Undefined, OriginationType.ALL, pattern4, "R/K/R/K/R", 4, -1,
null, 0, patternDefaultCalling);
null, 0, null);

// Rule 5
SccpAddress pattern5 = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE,factory.createGlobalTitle("800/?????/9", 1), 0, 0);
SccpAddress primaryAddr5 = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, factory.createGlobalTitle( "90/-/7",1), 123,
0);
router.addRoutingAddress(5, primaryAddr5);
router.addRule(5, RuleType.SOLITARY, LoadSharingAlgorithm.Undefined, OriginationType.ALL, pattern5, "R/K/R", 5, -1,
null, 0, patternDefaultCalling);
null, 0, null);

// Rule 6
SccpAddress pattern6 = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, factory.createGlobalTitle("123456",1), 0, 0);
SccpAddress primaryAddr6 = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, factory.createGlobalTitle("-", 1),123, 0);
router.addRoutingAddress(6, primaryAddr6);
router.addRule(6, RuleType.SOLITARY, LoadSharingAlgorithm.Undefined, OriginationType.ALL, pattern6, "K", 6,
-1, null, 0, patternDefaultCalling);
-1, null, 0, null);

// Rule 7
SccpAddress pattern7 = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, factory.createGlobalTitle("1234567890", 1), 0, 0);
SccpAddress primaryAddr7 = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, factory.createGlobalTitle("-", 1), 123, 0);
router.addRoutingAddress(7, primaryAddr7);
router.addRule(7, RuleType.SOLITARY, LoadSharingAlgorithm.Undefined, OriginationType.ALL, pattern7, "K", 7,
-1, null, 0, patternDefaultCalling);
-1, null, 0, null);

// Rule 8

SccpAddress pattern8 = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, factory.createGlobalTitle("999/*", 1), 0, 0);
SccpAddress primaryAddr8 = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, factory.createGlobalTitle("111/-", 1), 123, 0);
router.addRoutingAddress(8, primaryAddr8);
router.addRule(8, RuleType.SOLITARY, LoadSharingAlgorithm.Undefined, OriginationType.ALL, pattern8, "R/K", 8,
-1, null, 0, patternDefaultCalling);
-1, null, 0, null);

// Rule 9 // with missing callingAddress

Expand Down Expand Up @@ -456,7 +456,7 @@ public void testOrderingByCallingAddress() throws Exception {
router.addRoutingAddress(1, primaryAddr1);

SccpAddress patternCalling1 = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE,
factory.createGlobalTitle("900/????", 1), 0, 0);
factory.createGlobalTitle("90012345", 1), 0, 0);

SccpAddress pattern1 = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE,
factory.createGlobalTitle("800/????/9", 1), 0, 0);
Expand All @@ -479,7 +479,7 @@ public void testOrderingByCallingAddress() throws Exception {
factory.createGlobalTitle("800/????/9", 1), 0, 0);
router.addRoutingAddress(3, primaryAddr1);
SccpAddress patternCalling3 = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE,
factory.createGlobalTitle("900/????/2", 1), 0, 0);
factory.createGlobalTitle("900/?????/2", 1), 0, 0);

router.addRule(3, RuleType.SOLITARY, LoadSharingAlgorithm.Bit0, OriginationType.ALL, pattern3, "K/R/K", 3, -1,
null, 0, patternCalling3);
Expand All @@ -489,7 +489,7 @@ public void testOrderingByCallingAddress() throws Exception {
factory.createGlobalTitle("800/????/9", 1),0, 0);
router.addRoutingAddress(4, primaryAddr1);
SccpAddress patternCalling4 = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE,
factory.createGlobalTitle("900/????/1", 1), 0, 0);
factory.createGlobalTitle("700/????/1", 1), 0, 0);

router.addRule(4, RuleType.SOLITARY, LoadSharingAlgorithm.Undefined, OriginationType.ALL, pattern4, "K/K/R", 4, -1,
null, 0, patternCalling4);
Expand All @@ -510,18 +510,18 @@ public void testOrderingByCallingAddress() throws Exception {

// Rule 6
SccpAddress pattern6 = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE,
factory.createGlobalTitle("800/????/9", 1),0, 0);
factory.createGlobalTitle("700/????/9", 1),0, 0);
router.addRoutingAddress(6, primaryAddr1);

router.addRule(6, RuleType.SOLITARY, LoadSharingAlgorithm.Undefined, OriginationType.ALL, pattern4, "K/K/R", 6, -1,
router.addRule(6, RuleType.SOLITARY, LoadSharingAlgorithm.Undefined, OriginationType.ALL, pattern6, "K/K/R", 6, -1,
null, 0, null);


// Rule Tests

// Rule 3
SccpAddress calledParty = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, factory.createGlobalTitle( "80012039", 1), 0, 0);
SccpAddress callingParty = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, factory.createGlobalTitle( "90012032", 1), 0, 0);
SccpAddress callingParty = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, factory.createGlobalTitle( "900120342", 1), 0, 0);
Rule rule = router.findRule(calledParty, callingParty, false, 0);
assertEquals(LoadSharingAlgorithm.Bit0, rule.getLoadSharingAlgorithm());
assertEquals(pattern3, rule.getPattern());
Expand All @@ -530,8 +530,9 @@ public void testOrderingByCallingAddress() throws Exception {
assertEquals("K/R/K", rule.getMask());

// Rule 6
SccpAddress calledParty1 = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, factory.createGlobalTitle( "70012039", 1), 0, 0);
callingParty = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, factory.createGlobalTitle( "90012031", 1), 0, 0);
rule = router.findRule(calledParty, callingParty, false, 0);
rule = router.findRule(calledParty1, callingParty, false, 0);
assertEquals(LoadSharingAlgorithm.Undefined, rule.getLoadSharingAlgorithm());
assertEquals(pattern6, rule.getPattern());
assertEquals(RuleType.SOLITARY, rule.getRuleType());
Expand All @@ -540,7 +541,7 @@ public void testOrderingByCallingAddress() throws Exception {


// Rule 1
callingParty = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, factory.createGlobalTitle( "9001203", 1), 0, 0);
callingParty = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, factory.createGlobalTitle( "90012345", 1), 0, 0);
rule = router.findRule(calledParty, callingParty, false, 0);
assertEquals(LoadSharingAlgorithm.Undefined, rule.getLoadSharingAlgorithm());
assertEquals(pattern4, rule.getPattern());
Expand All @@ -560,7 +561,7 @@ public void testOrderingByCallingAddress() throws Exception {
assertEquals("R", rule.getMask());

// Rule 4
callingParty = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, factory.createGlobalTitle( "90012031", 1), 0, 0);
callingParty = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, factory.createGlobalTitle( "70012031", 1), 0, 0);
rule = router.findRule(calledParty, callingParty, false, 0);
assertEquals(LoadSharingAlgorithm.Undefined, rule.getLoadSharingAlgorithm());
assertEquals(pattern4, rule.getPattern());
Expand Down Expand Up @@ -592,7 +593,7 @@ public void testOrderingWithOriginationType() throws Exception {
SccpAddress patternDefaultCalling = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE,
factory.createGlobalTitle("*", 1), 0, 0);
// Rule 1
primaryAddr1 = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, factory.createGlobalTitle("999", 1),123,
primaryAddr1 = factory.createSccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, factory.createGlobalTitle("999", 1),123,
0);
router.addRoutingAddress(1, primaryAddr1);

Expand Down
Loading