SEBA-113 s-tag support for "any" and ranges

Change-Id: Ifcec2c42b16a835338b2e7f703dad0bc31931cfe
diff --git a/samples/bng_mapping.yaml b/samples/bng_mapping_any.yaml
similarity index 97%
copy from samples/bng_mapping.yaml
copy to samples/bng_mapping_any.yaml
index f61bfdb..cc9c2b8 100644
--- a/samples/bng_mapping.yaml
+++ b/samples/bng_mapping_any.yaml
@@ -23,5 +23,5 @@
     bngmapping:
       type: tosca.nodes.BNGPortMapping
       properties:
-        s_tag: 222
+        s_tag: "any"
         switch_port: 4
diff --git a/samples/bng_mapping.yaml b/samples/bng_mapping_range.yaml
similarity index 97%
rename from samples/bng_mapping.yaml
rename to samples/bng_mapping_range.yaml
index f61bfdb..5286ff5 100644
--- a/samples/bng_mapping.yaml
+++ b/samples/bng_mapping_range.yaml
@@ -23,5 +23,5 @@
     bngmapping:
       type: tosca.nodes.BNGPortMapping
       properties:
-        s_tag: 222
+        s_tag: "220-225"
         switch_port: 4
diff --git a/samples/bng_mapping.yaml b/samples/bng_mapping_single.yaml
similarity index 97%
copy from samples/bng_mapping.yaml
copy to samples/bng_mapping_single.yaml
index f61bfdb..92c24d8 100644
--- a/samples/bng_mapping.yaml
+++ b/samples/bng_mapping_single.yaml
@@ -23,5 +23,5 @@
     bngmapping:
       type: tosca.nodes.BNGPortMapping
       properties:
-        s_tag: 222
+        s_tag: "222"
         switch_port: 4
diff --git a/xos/synchronizer/models/fabric-crossconnect.xproto b/xos/synchronizer/models/fabric-crossconnect.xproto
old mode 100644
new mode 100755
index 4a7e8dc..989ebc1
--- a/xos/synchronizer/models/fabric-crossconnect.xproto
+++ b/xos/synchronizer/models/fabric-crossconnect.xproto
@@ -1,5 +1,6 @@
 option name = "fabric-crossconnect";
 option app_label = "fabric-crossconnect";
+option legacy = "True";
 
 message FabricCrossconnectService (Service){
     option verbose_name = "Fabric Crossconnect Service";
@@ -12,6 +13,6 @@
 }
 
 message BNGPortMapping (XOSBase) {
-    required int32 s_tag = 1 [help_text = "S Tag", null = False, db_index = False, blank = False, unique=True, tosca_key=True];
+    required string s_tag = 1 [help_text = "Single s-tag, range of s-tags, or ANY", null = False, db_index = False, blank = False, unique=True, tosca_key=True];
     required int32 switch_port = 2 [help_text = "Port Number", null = False, db_index = False, blank = False];
 }
diff --git a/xos/synchronizer/models/models.py b/xos/synchronizer/models/models.py
new file mode 100755
index 0000000..75ebe1d
--- /dev/null
+++ b/xos/synchronizer/models/models.py
@@ -0,0 +1,52 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from xos.exceptions import XOSValidationError
+
+from models_decl import FabricCrossconnectService_decl, FabricCrossconnectServiceInstance_decl, BNGPortMapping_decl
+
+class FabricCrossconnectService(FabricCrossconnectService_decl):
+    class Meta:
+        proxy = True
+
+class FabricCrossconnectServiceInstance(FabricCrossconnectServiceInstance_decl):
+   class Meta:
+        proxy = True
+
+class BNGPortMapping(BNGPortMapping_decl):
+    class Meta:
+        proxy = True
+
+    def validate_range(self, pattern):
+        for this_range in pattern.split(","):
+            this_range = this_range.strip()
+            if "-" in this_range:
+                (first, last) = this_range.split("-")
+                try:
+                    int(first.strip())
+                    int(last.strip())
+                except ValueError:
+                    raise XOSValidationError("Malformed range %s" % pattern)
+            elif this_range.lower()=="any":
+                pass
+            else:
+                try:
+                    int(this_range)
+                except ValueError:
+                    raise XOSValidationError("Malformed range %s" % pattern)
+
+    def save(self, *args, **kwargs):
+        self.validate_range(self.s_tag)
+        super(BNGPortMapping, self).save(*args, **kwargs)
+
diff --git a/xos/synchronizer/models/test_models.py b/xos/synchronizer/models/test_models.py
new file mode 100755
index 0000000..3d42609
--- /dev/null
+++ b/xos/synchronizer/models/test_models.py
@@ -0,0 +1,124 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import unittest
+import os, sys
+from mock import patch, Mock, MagicMock
+
+test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
+service_dir=os.path.join(test_path, "../../../..")
+xos_dir=os.path.join(test_path, "../../..")
+if not os.path.exists(os.path.join(test_path, "new_base")):
+    xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
+    services_dir=os.path.join(xos_dir, "../../xos_services")
+
+# mocking XOS exception, as they're based in Django
+class Exceptions:
+    XOSValidationError = Exception
+    XOSProgrammingError = Exception
+    XOSPermissionDenied = Exception
+
+class XOS:
+    exceptions = Exceptions
+
+class TestFabricCrossconnectModels(unittest.TestCase):
+    def setUp(self):
+
+        self.sys_path_save = sys.path
+        sys.path.append(xos_dir)
+
+        self.xos = XOS
+
+        self.models_decl = Mock()
+        self.models_decl.BNGPortMapping_decl = MagicMock
+        self.models_decl.BNGPortMapping_decl.save = Mock()
+        self.models_decl.BNGPortMapping_decl.objects = Mock()
+        self.models_decl.BNGPortMapping_decl.objects.filter.return_value = []
+
+
+        modules = {
+            'xos.exceptions': self.xos.exceptions,
+            'models_decl': self.models_decl
+        }
+
+        self.module_patcher = patch.dict('sys.modules', modules)
+        self.module_patcher.start()
+
+        self.volt = Mock()
+
+        from models import BNGPortMapping
+
+        self.BNGPortMapping = BNGPortMapping()
+
+    def tearDown(self):
+        sys.path = self.sys_path_save
+
+    def test_validate_range_single(self):
+        bpm = self.BNGPortMapping()
+        bpm.validate_range("123")
+
+    def test_validate_range_commas(self):
+        bpm = self.BNGPortMapping()
+        bpm.validate_range("123, 456")
+
+    def test_validate_range_ANY(self):
+        bpm = self.BNGPortMapping()
+        bpm.validate_range("ANY")
+        bpm.validate_range("any")
+
+    def test_validate_range_dash(self):
+        bpm = self.BNGPortMapping()
+        bpm.validate_range("123-456")
+
+    def test_validate_dash_commas(self):
+        bpm = self.BNGPortMapping()
+        bpm.validate_range("123-456, 789 - 1000")
+
+    def test_validate_range_empty(self):
+        bpm = self.BNGPortMapping()
+        with self.assertRaises(Exception) as e:
+            bpm.validate_range("")
+
+        self.assertEqual(e.exception.message, 'Malformed range ')
+
+    def test_validate_range_none(self):
+        bpm = self.BNGPortMapping()
+        with self.assertRaises(Exception) as e:
+            bpm.validate_range("")
+
+        self.assertEqual(e.exception.message, 'Malformed range ')
+
+    def test_validate_range_all(self):
+        bpm = self.BNGPortMapping()
+        with self.assertRaises(Exception) as e:
+            bpm.validate_range("badstring")
+
+        self.assertEqual(e.exception.message, 'Malformed range badstring')
+
+    def test_validate_half_range(self):
+        bpm = self.BNGPortMapping()
+        with self.assertRaises(Exception) as e:
+            bpm.validate_range("123-")
+
+        self.assertEqual(e.exception.message, 'Malformed range 123-')
+
+    def test_validate_half_comma(self):
+        bpm = self.BNGPortMapping()
+        with self.assertRaises(Exception) as e:
+            bpm.validate_range("123,")
+
+        self.assertEqual(e.exception.message, 'Malformed range 123,')
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/xos/synchronizer/steps/sync_fabric_crossconnect_service_instance.py b/xos/synchronizer/steps/sync_fabric_crossconnect_service_instance.py
old mode 100644
new mode 100755
index 4a8a4cf..ee067de
--- a/xos/synchronizer/steps/sync_fabric_crossconnect_service_instance.py
+++ b/xos/synchronizer/steps/sync_fabric_crossconnect_service_instance.py
@@ -66,6 +66,39 @@
         s_tag = int(s_tag)
         return (s_tag, dpid)
 
+    def range_matches(self, value, pattern):
+        value=int(value)
+        for this_range in pattern.split(","):
+            this_range = this_range.strip()
+            if "-" in this_range:
+                (first, last) = this_range.split("-")
+                first = int(first.strip())
+                last = int(last.strip())
+                if (value>=first) and (value<=last):
+                    return True
+            elif this_range.lower()=="any":
+                return True
+            else:
+                if (value==int(this_range)):
+                    return True
+        return False
+
+    def find_bng(self, s_tag):
+        # See if there's a mapping for our s-tag directly
+        bng_mappings = BNGPortMapping.objects.filter(s_tag=str(s_tag))
+        if bng_mappings:
+            return bng_mappings[0]
+
+        # TODO(smbaker): Examine miss performance, and if necessary set a flag in the save method to allow filtering
+        # of mappings based on whether they are ranges or any.
+
+        # See if there are any ranges or "any" that match
+        for bng_mapping in BNGPortMapping.objects.all():
+            if self.range_matches(s_tag, bng_mapping.s_tag):
+                 return bng_mapping
+
+        return None
+
     def sync_record(self, o):
         self.log.info("Sync'ing Fabric Crossconnect Service Instance", service_instance=o)
 
@@ -77,10 +110,10 @@
         dpid = si.get_westbound_service_instance_properties("switch_datapath_id")
         west_port = si.get_westbound_service_instance_properties("switch_port")
 
-        bng_mappings = BNGPortMapping.objects.filter(s_tag = s_tag)
-        if not bng_mappings:
+        bng_mapping = self.find_bng(s_tag = s_tag)
+        if not bng_mapping:
             raise Exception("Unable to determine BNG port for s_tag %s" % s_tag)
-        east_port = bng_mappings[0].switch_port
+        east_port = bng_mapping.switch_port
 
         data = { "deviceId": dpid,
                  "vlanId": s_tag,
diff --git a/xos/synchronizer/steps/test_sync_fabric_crossconnect_service_instance.py b/xos/synchronizer/steps/test_sync_fabric_crossconnect_service_instance.py
old mode 100644
new mode 100755
index 4455497..a1b3e23
--- a/xos/synchronizer/steps/test_sync_fabric_crossconnect_service_instance.py
+++ b/xos/synchronizer/steps/test_sync_fabric_crossconnect_service_instance.py
@@ -125,6 +125,54 @@
         self.assertEqual(d["user"], "onos")
         self.assertEqual(d["pass"], "rocks")
 
+    def test_range_matches_single(self):
+        self.assertTrue(self.sync_step().range_matches(123, "123"))
+
+    def test_range_matches_single_incorrect(self):
+        self.assertFalse(self.sync_step().range_matches(123, "456"))
+
+    def test_range_matches_range(self):
+        self.assertTrue(self.sync_step().range_matches(123, "122-124"))
+
+    def test_range_matches_range_incorrect(self):
+        self.assertFalse(self.sync_step().range_matches(123, "110-113"))
+
+    def test_range_matches_any(self):
+        self.assertTrue(self.sync_step().range_matches(123, "ANY"))
+        self.assertTrue(self.sync_step().range_matches(123, "any"))
+
+    def test_find_bng_single(self):
+        with patch.object(BNGPortMapping.objects, "get_items") as bng_objects, \
+                patch.object(self.sync_step, "range_matches") as range_matches:
+            bngmapping = BNGPortMapping(s_tag="111", switch_port=4)
+            bng_objects.return_value = [bngmapping]
+
+            # this should not be called
+            range_matches.return_value = False
+
+            found_bng = self.sync_step().find_bng(111)
+            self.assertTrue(found_bng)
+            self.assertEqual(found_bng.switch_port, 4)
+
+            range_matches.assert_not_called()
+
+    def test_find_bng_any(self):
+        with patch.object(BNGPortMapping.objects, "get_items") as bng_objects:
+            bngmapping = BNGPortMapping(s_tag="ANY", switch_port=4)
+            bng_objects.return_value = [bngmapping]
+
+            found_bng = self.sync_step().find_bng(111)
+            self.assertTrue(found_bng)
+            self.assertEqual(found_bng.switch_port, 4)
+
+    def test_find_bng_range(self):
+        with patch.object(BNGPortMapping.objects, "get_items") as bng_objects:
+            bngmapping = BNGPortMapping(s_tag="100-200", switch_port=4)
+            bng_objects.return_value = [bngmapping]
+
+            found_bng = self.sync_step().find_bng(111)
+            self.assertTrue(found_bng)
+            self.assertEqual(found_bng.switch_port, 4)
 
     @requests_mock.Mocker()
     def test_sync(self, m):
@@ -137,7 +185,7 @@
             si = self.mock_westbound(fsi, s_tag=111, switch_datapath_id = "of:0000000000000201", switch_port = 3)
             serviceinstance_objects.return_value = [si]
 
-            bngmapping = BNGPortMapping(s_tag=111, switch_port=4)
+            bngmapping = BNGPortMapping(s_tag="111", switch_port=4)
             bng_objects.return_value = [bngmapping]
 
             desired_data = {"deviceId": "of:0000000000000201",
@@ -160,7 +208,7 @@
 
             fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service)
 
-            si = self.mock_westbound(fsi, s_tag=111, switch_datapath_id = "of:0000000000000201", switch_port = 3)
+            si = self.mock_westbound(fsi, s_tag="111", switch_datapath_id = "of:0000000000000201", switch_port = 3)
             serviceinstance_objects.return_value = [si]
 
             with self.assertRaises(Exception) as e:
diff --git a/xos/unittest.cfg b/xos/unittest.cfg
index 0dc4ba0..e35af11 100644
--- a/xos/unittest.cfg
+++ b/xos/unittest.cfg
@@ -3,6 +3,7 @@
 code-directories=synchronizer
                  model_policies
                  steps
+                 models
 
 [coverage]
 always-on = True