Revision: 7f5a8b82d8f3
Author:   [email protected] <[email protected]>
Date:     Tue May  1 14:06:34 2012
Log:      Issue 20 : SCCP Rule should follow best matching approach
http://code.google.com/p/jss7/source/detail?r=7f5a8b82d8f3

Added:
/sccp/sccp-impl/src/main/java/org/mobicents/protocols/ss7/sccp/impl/router/RuleComparator.java /sccp/sccp-impl/src/test/java/org/mobicents/protocols/ss7/sccp/impl/router/RuleComparatorTest.java
Modified:
/sccp/sccp-impl/src/main/java/org/mobicents/protocols/ss7/sccp/impl/router/Router.java /sccp/sccp-impl/src/main/java/org/mobicents/protocols/ss7/sccp/impl/router/Rule.java /sccp/sccp-impl/src/test/java/org/mobicents/protocols/ss7/sccp/impl/router/RouterTest.java

=======================================
--- /dev/null
+++ /sccp/sccp-impl/src/main/java/org/mobicents/protocols/ss7/sccp/impl/router/RuleComparator.java Tue May 1 14:06:34 2012
@@ -0,0 +1,205 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2011, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors as indicated by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a full listing
+ * of individual contributors.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU General Public License, v. 2.0.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License,
+ * v. 2.0 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ */
+package org.mobicents.protocols.ss7.sccp.impl.router;
+
+import java.util.Comparator;
+
+/**
+ * <p>
+ * A comparison function which imposes ordering on collection of {@link Rule} + * based on the GT digits defined while creating rule. The sorting algo is as
+ * defined bellow
+ * </p>
+ * <p>
+ * <ol>
+ * <li>
+ * GT digits having no wildcard (* or ?) is always on top of the list. Between + * two GT digits, both having no wildcards, one with shortest length is on top + * of list. For example Digit1 "123456" will be above Digit2 "1234567890" will
+ * be above Digit3 "999/*"</li>
+ * <li>
+ * GT digits having wildcard ? is always above digits having wildcard *. For
+ * example Digit1 "800/????/9" will be above Digit2 "999/*"</li>
+ * <li>
+ * Between two GT digits both having wildcard ?, one with least number of
+ * wildcard ? is on top of list. For example Digit1 "800/????/9" is above Digit2
+ * "800/?????/9"</li>
+ * <li>
+ * Between two GT digits both having equal number of wildcard ?, the digit who's + * first appearance of ? is after other, is on top of list. For example between
+ * Digit1 "80/??/0/???/9" and Digit 2 "800/?????/9", Digit2 is above Digit1
+ * <li>
+ * </ol>
+ * </p>
+ *
+ *
+ * @author amit bhayani
+ *
+ */
+public class RuleComparator implements Comparator<Rule> {
+
+       private static final String SECTION_SEPARTOR = "/";
+       private static final char WILDCARD_ANY = '*';
+       private static final String WILDCARD_ANY_STRING = "*";
+       private static final char WILDCARD_SINGLE = '?';
+
+       /*
+        * (non-Javadoc)
+        *
+        * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
+        */
+       @Override
+       public int compare(Rule o1, Rule o2) {
+               String digits1 = o1.getPattern().getGlobalTitle().getDigits();
+               String digits2 = o2.getPattern().getGlobalTitle().getDigits();
+
+               // Normalize rule. Remove all separator
+               digits1 = digits1.replaceAll(SECTION_SEPARTOR, "");
+               digits2 = digits2.replaceAll(SECTION_SEPARTOR, "");
+
+               // If any digit is just wildcard "*" return 1 indicating it 
should be
+               // below the other
+               if (digits1.equals(WILDCARD_ANY_STRING)) {
+                       return 1;
+               } else if (digits2.equals(WILDCARD_ANY_STRING)) {
+                       return -1;
+               }
+
+               // Truly speaking any digits will have just one asterisk max.
+               int asterisksFound1 = this.numberOfAsterisks(digits1);
+               int asterisksFound2 = this.numberOfAsterisks(digits2);
+
+               if (asterisksFound1 == asterisksFound2) {
+                       // If both digits have "*", lets compare if there are wildcard 
"?"
+                       return compareQuestionMarks(digits1, digits2);
+               } else if (asterisksFound1 < asterisksFound2) {
+                       // if digit 1 doesn't have *, return -1 indicating it 
should be
+                       // above other
+                       return -1;
+               }
+
+               return 1;
+
+       }
+
+       /**
+        * Compare number of wildcards "?" in passed digits
+        *
+        * @param digits1
+        * @param digits2
+        * @return
+        */
+       private int compareQuestionMarks(String digits1, String digits2) {
+               int questionMarksFound1 = this.numberOfQuestionMarks(digits1);
+               int questionMarksFound2 = this.numberOfQuestionMarks(digits2);
+
+               if (questionMarksFound1 == questionMarksFound2) {
+                       // If both the digits have equal number of "?", compare 
the index of
+                       // first occurrence of "?" in both digits. Digits for which 
"?"
+                       // occurred after other is on top
+                       return compareQuestionMarksIndex(digits1, 0, digits2, 
0);
+               } else if (questionMarksFound1 < questionMarksFound2) {
+                       return -1;
+               }
+               return 1;
+       }
+
+       /**
+        *
+        * @param digits1
+        * @param fromIndex1
+        * @param digits2
+        * @param fromIndex2
+        * @return
+        */
+ private int compareQuestionMarksIndex(String digits1, int fromIndex1, String digits2, int fromIndex2) {
+               int index1 = digits1.indexOf(WILDCARD_SINGLE, fromIndex1);
+               int index2 = digits2.indexOf(WILDCARD_SINGLE, fromIndex2);
+
+               if (index1 == -1 && index2 == -1) {
+                       // If both digits have exactly same occurrence of "?", 
compare
+                       // lengths. Shortest length digit is above other
+                       return comapreLength(digits1, digits2);
+               } else if (index1 == index2) {
+                       // Keep comparing occurrence of "?" till difference is 
reached
+                       compareQuestionMarksIndex(digits1, index1, digits2, 
index2);
+               } else if (index1 < index2) {
+                       return 1;
+               }
+               return -1;
+       }
+
+       /**
+        * Compare length of digits. Shorter length is above longer one
+        *
+        * @param digits1
+        * @param digits2
+        * @return
+        */
+       private int comapreLength(String digits1, String digits2) {
+
+               if (digits1.length() == digits2.length()) {
+                       return 0;
+               } else if (digits1.length() < digits2.length()) {
+                       return -1;
+               } else if (digits1.length() > digits2.length()) {
+                       return 1;
+               }
+
+               return digits1.compareTo(digits2);
+       }
+
+       /**
+        * Returns number of '*' occured in passed digits
+        *
+        * @param digits
+        * @return
+        */
+       private int numberOfAsterisks(String digits) {
+               int asterisksFound = 0;
+               char[] chars = digits.toCharArray();
+               for (int i = 0; i < chars.length; i++) {
+                       if (chars[i] == WILDCARD_ANY) {
+                               asterisksFound++;
+                       }
+               }
+               return asterisksFound;
+       }
+
+       /**
+        * Returns number of '?' occured in passed digits
+        *
+        * @param digits
+        * @return
+        */
+       private int numberOfQuestionMarks(String digits) {
+               int questionMarksFound = 0;
+               char[] chars = digits.toCharArray();
+               for (int i = 0; i < chars.length; i++) {
+                       if (chars[i] == WILDCARD_SINGLE) {
+                               questionMarksFound++;
+                       }
+               }
+               return questionMarksFound;
+       }
+
+}
=======================================
--- /dev/null
+++ /sccp/sccp-impl/src/test/java/org/mobicents/protocols/ss7/sccp/impl/router/RuleComparatorTest.java Tue May 1 14:06:34 2012
@@ -0,0 +1,102 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2011, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors as indicated by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a full listing
+ * of individual contributors.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU General Public License, v. 2.0.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License,
+ * v. 2.0 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ */
+package org.mobicents.protocols.ss7.sccp.impl.router;
+
+import java.util.Arrays;
+import static org.testng.Assert.*;
+
+import org.mobicents.protocols.ss7.indicator.RoutingIndicator;
+import org.mobicents.protocols.ss7.sccp.parameter.GlobalTitle;
+import org.mobicents.protocols.ss7.sccp.parameter.SccpAddress;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ * @author amit bhayani
+ *
+ */
+public class RuleComparatorTest {
+
+       RuleComparator ruleComparator = new RuleComparator();
+
+       @BeforeClass
+       public static void setUpClass() throws Exception {
+       }
+
+       @AfterClass
+       public static void tearDownClass() throws Exception {
+       }
+
+       @BeforeMethod
+       public void setUp() {
+       }
+
+       @AfterMethod
+       public void tearDown() {
+       }
+
+       @Test(groups = { "comparator", "functional.sort" })
+       public void testSorting() throws Exception {
+
+ SccpAddress pattern1 = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "800/????/9"), 0); + Rule rule1 = new Rule(RuleType.Solitary, LoadSharingAlgorithm.Undefined, pattern1, "R/K/R");
+
+ SccpAddress pattern2 = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "*"), 0); + Rule rule2 = new Rule(RuleType.Solitary, LoadSharingAlgorithm.Undefined, pattern2, "K");
+
+ SccpAddress pattern3 = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "9/?/9/*"), 0); + Rule rule3 = new Rule(RuleType.Solitary, LoadSharingAlgorithm.Undefined, pattern3, "K/K/K/K");
+
+ SccpAddress pattern4 = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "80/??/0/???/9"), 0); + Rule rule4 = new Rule(RuleType.Solitary, LoadSharingAlgorithm.Undefined, pattern4, "R/K/R/K/R");
+
+ SccpAddress pattern5 = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "800/?????/9"), 0); + Rule rule5 = new Rule(RuleType.Solitary, LoadSharingAlgorithm.Undefined, pattern5, "R/K/R");
+
+ SccpAddress pattern6 = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "123456"), 0); + Rule rule6 = new Rule(RuleType.Solitary, LoadSharingAlgorithm.Undefined, pattern6, "K");
+
+ SccpAddress pattern7 = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "1234567890"), 0); + Rule rule7 = new Rule(RuleType.Solitary, LoadSharingAlgorithm.Undefined, pattern7, "R/K/R");
+
+ SccpAddress pattern8 = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "999/*"), 0); + Rule rule8 = new Rule(RuleType.Solitary, LoadSharingAlgorithm.Undefined, pattern8, "R/K");
+
+               // This is unsorted
+ Rule[] rules = new Rule[] { rule1, rule2, rule3, rule4, rule5, rule6, rule7, rule8 };
+
+               // Sort
+               Arrays.sort(rules, ruleComparator);
+
+               assertEquals(rule6, rules[0]);
+               assertEquals(rule7, rules[1]);
+               assertEquals(rule1, rules[2]);
+               assertEquals(rule5, rules[3]);
+               assertEquals(rule4, rules[4]);
+               assertEquals(rule8, rules[5]);
+               assertEquals(rule3, rules[6]);
+               assertEquals(rule2, rules[7]);
+       }
+}
=======================================
--- /sccp/sccp-impl/src/main/java/org/mobicents/protocols/ss7/sccp/impl/router/Router.java Sat Mar 31 03:29:55 2012 +++ /sccp/sccp-impl/src/main/java/org/mobicents/protocols/ss7/sccp/impl/router/Router.java Tue May 1 14:06:34 2012
@@ -26,6 +26,7 @@
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
+import java.util.Arrays;

 import javolution.text.TextBuilder;
 import javolution.util.FastMap;
@@ -195,13 +196,14 @@

        private String persistDir = null;

+       private final RuleComparator ruleComparator = new RuleComparator();
        // rule list
-       private FastMap<Integer, Rule> rules = new FastMap<Integer, Rule>();
+       private FastMap<Integer, Rule> rulesMap = new FastMap<Integer, Rule>();
private FastMap<Integer, SccpAddress> primaryAddresses = new FastMap<Integer, SccpAddress>(); private FastMap<Integer, SccpAddress> backupAddresses = new FastMap<Integer, SccpAddress>(); private FastMap<Integer, LongMessageRule> longMessageRules = new FastMap<Integer, LongMessageRule>(); private FastMap<Integer, Mtp3ServiceAccessPoint> saps = new FastMap<Integer, Mtp3ServiceAccessPoint>();
-
+
        private final String name;

        public Router(String name) {
@@ -229,8 +231,8 @@
                if (persistDir != null) {
this.persistFile.append(persistDir).append(File.separator).append(this.name).append("_").append(PERSIST_FILE_NAME);
                } else {
- persistFile.append(System.getProperty(SCCP_ROUTER_PERSIST_DIR_KEY, System.getProperty(USER_DIR_KEY))) - .append(File.separator).append(this.name).append("_").append(PERSIST_FILE_NAME); + persistFile.append(System.getProperty(SCCP_ROUTER_PERSIST_DIR_KEY, System.getProperty(USER_DIR_KEY))).append(File.separator).append(this.name)
+                                       .append("_").append(PERSIST_FILE_NAME);
                }

logger.info(String.format("SCCP Router configuration file path %s", persistFile.toString()));
@@ -257,7 +259,7 @@
         */
        public Rule findRule(SccpAddress calledParty) {

- for (FastMap.Entry<Integer, Rule> e = this.rules.head(), end = this.rules.tail(); (e = e.getNext()) != end;) { + for (FastMap.Entry<Integer, Rule> e = this.rulesMap.head(), end = this.rulesMap.tail(); (e = e.getNext()) != end;) {
                        Rule rule = e.getValue();
                        if (rule.matches(calledParty)) {
                                return rule;
@@ -287,7 +289,7 @@
        }

        public Rule getRule(int id) {
-               return this.rules.get(id);
+               return this.rulesMap.get(id);
        }

        public SccpAddress getPrimaryAddress(int id) {
@@ -317,9 +319,9 @@
        }

        public FastMap<Integer, Rule> getRules() {
-               return rules;
-       }
-
+               return rulesMap;
+       }
+
        public FastMap<Integer, SccpAddress> getPrimaryAddresses() {
                return primaryAddresses;
        }
@@ -338,10 +340,29 @@

        public void addRule(int id, Rule rule) {
                synchronized (this) {
+                       rule.setRuleId(id);
+                       Rule[] rulesArray = new Rule[(this.rulesMap.size() + 
1)];
+                       int count = 0;
+
+ for (FastMap.Entry<Integer, Rule> e = this.rulesMap.head(), end = this.rulesMap.tail(); (e = e.getNext()) != end;) {
+                               Integer ruleId = e.getKey();
+                               Rule ruleTemp = e.getValue();
+                               ruleTemp.setRuleId(ruleId);
+                               rulesArray[count++] = ruleTemp;
+                       }
+
+                       //add latest rule
+                       rulesArray[count++] = rule;
+
+                       // Sort
+                       Arrays.sort(rulesArray, ruleComparator);
+
                        FastMap<Integer, Rule> newRule = new FastMap<Integer, 
Rule>();
-                       newRule.putAll(this.rules);
-                       newRule.put(id, rule);
-                       this.rules = newRule;
+                       for (int i = 0; i < rulesArray.length; i++) {
+                               Rule ruleTemp = rulesArray[i];
+                               newRule.put(ruleTemp.getRuleId(), ruleTemp);
+                       }
+                        this.rulesMap = newRule;
                        this.store();
                }
        }
@@ -349,12 +370,12 @@
        public void removeRule(int id) {
                synchronized (this) {
                        FastMap<Integer, Rule> newRule = new FastMap<Integer, 
Rule>();
-                       newRule.putAll(this.rules);
+                       newRule.putAll(this.rulesMap);
                        newRule.remove(id);
-                       this.rules = newRule;
+                       this.rulesMap = newRule;
                        this.store();
                }
-       }
+       }

        public void addPrimaryAddress(int id, SccpAddress primaryAddress) {
                synchronized (this) {
@@ -459,12 +480,12 @@
        public void removeAllResourses() {

                synchronized (this) {
- if (this.rules.size() == 0 && this.primaryAddresses.size() == 0 && this.backupAddresses.size() == 0 && this.longMessageRules.size() == 0 + if (this.rulesMap.size() == 0 && this.primaryAddresses.size() == 0 && this.backupAddresses.size() == 0 && this.longMessageRules.size() == 0
                                        && this.saps.size() == 0)
                                // no resources allocated - nothing to do
                                return;

-                       rules = new FastMap<Integer, Rule>();
+                       rulesMap = new FastMap<Integer, Rule>();
                        primaryAddresses = new FastMap<Integer, SccpAddress>();
                        backupAddresses = new FastMap<Integer, SccpAddress>();
                        longMessageRules = new FastMap<Integer, 
LongMessageRule>();
@@ -488,7 +509,7 @@
                        // Enables cross-references.
                        // writer.setReferenceResolver(new 
XMLReferenceResolver());
                        writer.setIndentation(TAB_INDENT);
-                       writer.write(rules, RULE, FastMap.class);
+                       writer.write(rulesMap, RULE, FastMap.class);
                        writer.write(primaryAddresses, PRIMARY_ADDRESS, 
FastMap.class);
                        writer.write(backupAddresses, BACKUP_ADDRESS, 
FastMap.class);

@@ -513,7 +534,7 @@
reader = XMLObjectReader.newInstance(new FileInputStream(persistFile.toString()));

                        reader.setBinding(binding);
-                       rules = reader.read(RULE, FastMap.class);
+                       rulesMap = reader.read(RULE, FastMap.class);
                        primaryAddresses = reader.read(PRIMARY_ADDRESS, 
FastMap.class);
                        backupAddresses = reader.read(BACKUP_ADDRESS, 
FastMap.class);

=======================================
--- /sccp/sccp-impl/src/main/java/org/mobicents/protocols/ss7/sccp/impl/router/Rule.java Tue Apr 17 00:14:50 2012 +++ /sccp/sccp-impl/src/main/java/org/mobicents/protocols/ss7/sccp/impl/router/Rule.java Tue May 1 14:06:34 2012
@@ -66,6 +66,8 @@

        /** Pattern used for selecting rule */
        private SccpAddress pattern;
+
+       private int ruleId;

        /** Translation method */
        private int primaryAddressId = 0;
@@ -100,6 +102,20 @@

                configure();
        }
+
+       /**
+        * @return the ruleId
+        */
+       protected int getRuleId() {
+               return ruleId;
+       }
+
+       /**
+        * @param ruleId the ruleId to set
+        */
+       protected void setRuleId(int ruleId) {
+               this.ruleId = ruleId;
+       }

        public RuleType getRuleType() {
                return ruleType;
@@ -391,7 +407,13 @@
                                j++;
                        }
                }
-               return true;
+
+               if(j == digits.length){
+                       //We compared all the digits and all of them matched, 
this Rule matches.
+                       return true;
+               }
+
+               return false;
        }

        /**
=======================================
--- /sccp/sccp-impl/src/test/java/org/mobicents/protocols/ss7/sccp/impl/router/RouterTest.java Tue Apr 3 01:53:33 2012 +++ /sccp/sccp-impl/src/test/java/org/mobicents/protocols/ss7/sccp/impl/router/RouterTest.java Tue May 1 14:06:34 2012
@@ -22,7 +22,6 @@

 package org.mobicents.protocols.ss7.sccp.impl.router;

-
 import java.io.IOException;
 import org.testng.annotations.*;
 import static org.testng.Assert.*;
@@ -43,7 +42,7 @@
        private Mtp3Destination dest1, dest2;

        private Router router = null;
-
+
        public RouterTest() {
        }

@@ -58,8 +57,7 @@
        @BeforeMethod
        public void setUp() throws IOException {

- SccpAddress pattern = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0,
-                               GlobalTitle.getInstance(1, "123456789"), 0);
+ SccpAddress pattern = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "123456789"), 0);

rule1 = new Rule(RuleType.Solitary, LoadSharingAlgorithm.Undefined, pattern, "R");
                rule1.setPrimaryAddressId(211);
@@ -67,14 +65,12 @@

rule2 = new Rule(RuleType.Loadshared, LoadSharingAlgorithm.Bit4, pattern, "K");

- primaryAddr1 = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 123, GlobalTitle.getInstance(1,
-                               "333/---/4"), 0);
- primaryAddr2 = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 321, GlobalTitle.getInstance(1,
-                               "333/---/4"), 0);
+ primaryAddr1 = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 123, GlobalTitle.getInstance(1, "333/---/4"), 0); + primaryAddr2 = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 321, GlobalTitle.getInstance(1, "333/---/4"), 0);

longMessageRule1 = new LongMessageRule(1, 2, LongMessageRuleType.XudtEnabled); longMessageRule2 = new LongMessageRule(3, 4, LongMessageRuleType.LudtEnabled);
-
+
                sap1 = new Mtp3ServiceAccessPoint(1, 11, 2);
                sap2 = new Mtp3ServiceAccessPoint(2, 12, 2);

@@ -91,80 +87,80 @@
        @AfterMethod
        public void tearDown() {
                router.removeAllResourses();
-
+
                router.stop();
        }

        /**
         * Test of add method, of class RouterImpl.
         */
-       @Test(groups = { "router","functional"})
+       @Test(groups = { "router", "functional" })
        public void testRouter() throws Exception {
                router.addRule(1, rule1);
-               assertEquals( router.getRules().size(),1);
+               assertEquals(router.getRules().size(), 1);
                router.addRule(2, rule2);
-               assertEquals( router.getRules().size(),2);
+               assertEquals(router.getRules().size(), 2);
                router.removeRule(2);
                Rule rule = router.getRules().values().iterator().next();
                assertNotNull(rule);
                assertEquals(rule.getRuleType(), RuleType.Solitary);
-               assertEquals( router.getRules().size(),1);
+               assertEquals(router.getRules().size(), 1);

                router.addPrimaryAddress(1, primaryAddr1);
-               assertEquals( router.getPrimaryAddresses().size(),1);
+               assertEquals(router.getPrimaryAddresses().size(), 1);
                router.addPrimaryAddress(2, primaryAddr2);
-               assertEquals( router.getPrimaryAddresses().size(),2);
+               assertEquals(router.getPrimaryAddresses().size(), 2);
                router.removePrimaryAddress(1);
                SccpAddress pa = 
router.getPrimaryAddresses().values().iterator().next();
                assertNotNull(pa);
                assertEquals(pa.getSignalingPointCode(), 321);
-               assertEquals( router.getPrimaryAddresses().size(),1);
-
-               assertEquals( router.getBackupAddresses().size(),0);
+               assertEquals(router.getPrimaryAddresses().size(), 1);
+
+               assertEquals(router.getBackupAddresses().size(), 0);
                router.addBackupAddress(1, primaryAddr1);
-               assertEquals( router.getBackupAddresses().size(),1);
+               assertEquals(router.getBackupAddresses().size(), 1);
                router.addBackupAddress(2, primaryAddr2);
-               assertEquals( router.getBackupAddresses().size(),2);
+               assertEquals(router.getBackupAddresses().size(), 2);
                router.removeBackupAddress(1);
                pa = router.getBackupAddresses().values().iterator().next();
                assertNotNull(pa);
                assertEquals(pa.getSignalingPointCode(), 321);
-               assertEquals( router.getBackupAddresses().size(),1);
+               assertEquals(router.getBackupAddresses().size(), 1);

                router.addLongMessageRule(1, longMessageRule1);
-               assertEquals( router.getLongMessageRules().size(),1);
+               assertEquals(router.getLongMessageRules().size(), 1);
                router.addLongMessageRule(2, longMessageRule2);
-               assertEquals( router.getLongMessageRules().size(),2);
+               assertEquals(router.getLongMessageRules().size(), 2);
                router.removeLongMessageRule(2);
LongMessageRule lmr = router.getLongMessageRules().values().iterator().next();
                assertNotNull(lmr);
assertEquals(lmr.getLongMessageRuleType(), LongMessageRuleType.XudtEnabled);
-               assertEquals( router.getLongMessageRules().size(),1);
+               assertEquals(router.getLongMessageRules().size(), 1);

                router.addMtp3ServiceAccessPoint(1, sap1);
-               assertEquals( router.getMtp3ServiceAccessPoints().size(),1);
+               assertEquals(router.getMtp3ServiceAccessPoints().size(), 1);
                router.addMtp3ServiceAccessPoint(2, sap2);
-               assertEquals( router.getMtp3ServiceAccessPoints().size(),2);
+               assertEquals(router.getMtp3ServiceAccessPoints().size(), 2);
                router.removeMtp3ServiceAccessPoint(2);
Mtp3ServiceAccessPoint sap = router.getMtp3ServiceAccessPoints().values().iterator().next();
                assertNotNull(sap);
                assertEquals(sap.getOpc(), 11);
-               assertEquals( router.getLongMessageRules().size(),1);
+               assertEquals(router.getLongMessageRules().size(), 1);

                router.addMtp3Destination(1, 1, dest1);
-               assertEquals( sap.getMtp3Destinations().size(),1);
+               assertEquals(sap.getMtp3Destinations().size(), 1);
                router.addMtp3Destination(1, 2, dest2);
-               assertEquals( sap.getMtp3Destinations().size(),2);
+               assertEquals(sap.getMtp3Destinations().size(), 2);
                router.removeMtp3Destination(1, 2);
Mtp3Destination dest = sap.getMtp3Destinations().values().iterator().next();
                assertNotNull(dest);
-               assertEquals(dest.getFirstDpc(),101);
-               assertEquals( sap.getMtp3Destinations().size(),1);
+               assertEquals(dest.getFirstDpc(), 101);
+               assertEquals(sap.getMtp3Destinations().size(), 1);
        }

-       @Test(groups = { "router","functional.encode"})
+       @Test(groups = { "router", "functional.encode" })
        public void testSerialization() throws Exception {
-               router.addRule(1, rule2);
+               router.addRule(1, rule2);
                router.addPrimaryAddress(1, primaryAddr1);
                router.addPrimaryAddress(2, primaryAddr2);
                router.addBackupAddress(1, primaryAddr1);
@@ -173,7 +169,6 @@
                router.addMtp3Destination(3, 1, dest1);
                router.stop();

-
                Router router1 = new Router(router.getName());
                router1.start();

@@ -184,15 +179,86 @@
                Mtp3ServiceAccessPoint sap = 
router1.getMtp3ServiceAccessPoint(3);
                Mtp3Destination dst = sap.getMtp3Destination(1);

-               assertEquals( rl.getPrimaryAddressId(), 
rule2.getPrimaryAddressId());
- assertEquals( rl.getLoadSharingAlgorithm(), rule2.getLoadSharingAlgorithm()); - assertEquals( adp.getSignalingPointCode(), primaryAddr2.getSignalingPointCode()); - assertEquals( adb.getSignalingPointCode(), primaryAddr1.getSignalingPointCode());
-               assertEquals( lmr.getFirstSpc(), 
longMessageRule1.getFirstSpc());
-               assertEquals( sap.getMtp3Destinations().size(), 1);
-               assertEquals( dst.getLastDpc(), 110);
-
+               assertEquals(rl.getPrimaryAddressId(), 
rule2.getPrimaryAddressId());
+ assertEquals(rl.getLoadSharingAlgorithm(), rule2.getLoadSharingAlgorithm()); + assertEquals(adp.getSignalingPointCode(), primaryAddr2.getSignalingPointCode()); + assertEquals(adb.getSignalingPointCode(), primaryAddr1.getSignalingPointCode());
+               assertEquals(lmr.getFirstSpc(), longMessageRule1.getFirstSpc());
+               assertEquals(sap.getMtp3Destinations().size(), 1);
+               assertEquals(dst.getLastDpc(), 110);
+
                router1.stop();
        }
-}
-
+
+       /**
+        * Test of Ordering.
+        */
+       @Test(groups = { "router", "functional.order" })
+       public void testOrdering() throws Exception {
+ SccpAddress pattern1 = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "800/????/9"), 0); + Rule rule1 = new Rule(RuleType.Solitary, LoadSharingAlgorithm.Undefined, pattern1, "R/K/R");
+
+ SccpAddress pattern2 = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "*"), 0); + Rule rule2 = new Rule(RuleType.Solitary, LoadSharingAlgorithm.Undefined, pattern2, "K");
+
+ SccpAddress pattern3 = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "9/?/9/*"), 0); + Rule rule3 = new Rule(RuleType.Solitary, LoadSharingAlgorithm.Undefined, pattern3, "K/K/K/K");
+
+ SccpAddress pattern4 = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "80/??/0/???/9"), 0); + Rule rule4 = new Rule(RuleType.Solitary, LoadSharingAlgorithm.Undefined, pattern4, "R/K/R/K/R");
+
+ SccpAddress pattern5 = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "800/?????/9"), 0); + Rule rule5 = new Rule(RuleType.Solitary, LoadSharingAlgorithm.Undefined, pattern5, "R/K/R");
+
+ SccpAddress pattern6 = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "123456"), 0); + Rule rule6 = new Rule(RuleType.Solitary, LoadSharingAlgorithm.Undefined, pattern6, "K");
+
+ SccpAddress pattern7 = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "1234567890"), 0); + Rule rule7 = new Rule(RuleType.Solitary, LoadSharingAlgorithm.Undefined, pattern7, "R/K/R");
+
+ SccpAddress pattern8 = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "999/*"), 0); + Rule rule8 = new Rule(RuleType.Solitary, LoadSharingAlgorithm.Undefined, pattern8, "R/K");
+
+               router.addRule(1, rule1);
+               router.addRule(2, rule2);
+               router.addRule(3, rule3);
+               router.addRule(4, rule4);
+               router.addRule(5, rule5);
+               router.addRule(6, rule6);
+               router.addRule(7, rule7);
+               router.addRule(8, rule8);
+
+ SccpAddress calledParty = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "123456"), 0);
+               Rule rule = router.findRule(calledParty);
+               assertEquals(rule6, rule);
+
+ calledParty = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "1234567890"), 0);
+               rule = router.findRule(calledParty);
+               assertEquals(rule7, rule);
+
+ calledParty = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "80012039"), 0);
+               rule = router.findRule(calledParty);
+               assertEquals(rule1, rule);
+
+ calledParty = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "80012039"), 0);
+               rule = router.findRule(calledParty);
+               assertEquals(rule1, rule);
+
+ calledParty = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "800120349"), 0);
+               rule = router.findRule(calledParty);
+               assertEquals(rule5, rule);
+
+ calledParty = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "801203459"), 0);
+               rule = router.findRule(calledParty);
+               assertEquals(rule4, rule);
+
+ calledParty = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "999123456"), 0);
+               rule = router.findRule(calledParty);
+               assertEquals(rule8, rule);
+
+ calledParty = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_GLOBAL_TITLE, 0, GlobalTitle.getInstance(1, "919123456"), 0);
+               rule = router.findRule(calledParty);
+               assertEquals(rule3, rule);
+
+       }
+}

Reply via email to