[CORD-3226] Add support for multi IP / dual stack to the fabric synchronizer

Change-Id: Iba774d58624b261aea237429eb31464f36cbfe42
diff --git a/VERSION b/VERSION
index cd57a8b..08ec77d 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-2.1.5
+2.1.6-dev
diff --git a/xos/synchronizer/models/fabric.xproto b/xos/synchronizer/models/fabric.xproto
index 9c408fc..aff47f8 100644
--- a/xos/synchronizer/models/fabric.xproto
+++ b/xos/synchronizer/models/fabric.xproto
@@ -10,36 +10,43 @@
 message Switch(XOSBase) {
     option verbose_name = "Fabric Switch";
 
-    required string ofId = 1 [help_text = "The unique OpenFlow ID of the fabric switch", max_length = 19, null = False, db_index = False, blank = False];
-    required string name = 2 [help_text = "The unique name of the fabric switch", max_length = 254, null = False, db_index = False, blank = False];
-    required string driver = 3 [help_text = "The driver used by the SDN controller", max_length = 254, null = False, db_index = False, blank = False, default = "ofdpa3"];
-    required int32 ipv4NodeSid = 4 [help_text = "The MPLS label used by the switch [17 - 1048576]", null = False, db_index = False, blank = False];
-    required string ipv4Loopback = 5 [help_text = "Fabric loopback interface", max_length = 17, null = False, db_index = False, blank = False];
-    required string routerMac = 6 [help_text = "MAC address of the fabric switch used for all interfaces", max_length = 17, null = False, db_index = False, blank = False];
-    required bool isEdgeRouter = 7 [default = True, help_text="Whether the fabric switch is a leaf or a spine", null=False, blank = False];
+    required string ofId = 1 [help_text = "The unique OpenFlow ID of the fabric switch", max_length = 19,db_index = False];
+    required string name = 2 [help_text = "The unique name of the fabric switch", max_length = 254,db_index = False];
+    required string driver = 3 [help_text = "The driver used by the SDN controller", max_length = 254,db_index = False, default = "ofdpa3"];
+    required int32 ipv4NodeSid = 4 [help_text = "The MPLS label used by the switch [17 - 1048576]",db_index = False];
+    required string ipv4Loopback = 5 [help_text = "Fabric loopback interface", max_length = 17,db_index = False];
+    required string routerMac = 6 [help_text = "MAC address of the fabric switch used for all interfaces", max_length = 17,db_index = False];
+    required bool isEdgeRouter = 7 [default = True, help_text="Whether the fabric switch is a leaf or a spine", null=False];
 }
 
 message SwitchPort(XOSBase) {
     option verbose_name = "Fabric Switch Port";
 
-    required manytoone switch->Switch:ports = 1:1001 [help_text = "The fabric switch the port belongs to", db_index = True, null = False, blank = False, tosca_key=True];
-    required int32 portId = 2 [help_text = "The unique port OpenFlow port ID", null = False, db_index = False, blank = False, tosca_key=True];
-    required bool host_learning = 3 [help_text = "whether or not to enable autodiscovery", default = True, null = False, db_index = False, blank = False];
+    required manytoone switch->Switch:ports = 1:1001 [help_text = "The fabric switch the port belongs to", db_index = True, tosca_key=True];
+    required int32 portId = 2 [help_text = "The unique port OpenFlow port ID",db_index = False, tosca_key=True];
+    required bool host_learning = 3 [help_text = "whether or not to enable autodiscovery", default = True,db_index = False];
 }
 
 message PortInterface(XOSBase) {
     option verbose_name = "Fabric Port Interface";
 
-    required manytoone port->SwitchPort:interfaces = 1:1001 [help_text = "The fabric switch port the interface belongs to", db_index = True, null = False, blank = False];
-    required string name = 2 [help_text = "The unique name of the fabric switch port", max_length = 254, null = False, db_index = False, blank = False];
-    required string ips = 3 [help_text = "The interface IP address (xxx.yyy.www.zzz/nm)", max_length = 20, null = False, db_index = False, blank = False, unique_with = "port"];
-    optional int32 vlanUntagged = 4 [help_text = "The optional untagged VLAN ID for the interface", null = True, db_index = False, blank = True];
+    required manytoone port->SwitchPort:interfaces = 1:1001 [help_text = "The fabric switch port the interface belongs to", db_index = True];
+    required string name = 2 [help_text = "The unique name of the fabric switch port", max_length = 254,db_index = False];
+    optional int32 vlanUntagged = 3 [help_text = "The optional untagged VLAN ID for the interface", db_index = False];
 }
 
 message NodeToSwitchPort(XOSBase) {
     option verbose_name = "Node to switch port";
     option description = "Compute Node connection to a Fabric switch port";
 
-    required manytoone port->SwitchPort:node_to_switch_ports = 1:1002 [help_text = "The fabric switch port the node is connected to", db_index = True, null = False, blank = False, tosca_key=True];
-    required manytoone node->Node:node_to_switch_ports = 2:1004 [help_text = "The ComputeNode this port is connected to", db_index = True, null = False, blank = False, tosca_key=True];
-}
\ No newline at end of file
+    required manytoone port->SwitchPort:node_to_switch_ports = 1:1002 [help_text = "The fabric switch port the node is connected to", db_index = True, tosca_key=True];
+    required manytoone node->Node:node_to_switch_ports = 2:1004 [help_text = "The ComputeNode this port is connected to", db_index = True, tosca_key=True];
+}
+
+message FabricIpAddress(XOSBase) {
+    option verbose_name = "IP address";
+
+    required manytoone interface->PortInterface:ips = 1:1001 [help_text = "The port interface the IP address belongs to", db_index = True];
+    required string ip = 2 [help_text = "The unique IP address (either IPv4 or IPv6 / netmask)", max_length = 52,db_index = False, unique_with = "interface"];
+    optional string description = 3 [help_text = "A short description of the IP address", max_length = 254,db_index = False];
+}
diff --git a/xos/synchronizer/steps/sync_fabric_port.py b/xos/synchronizer/steps/sync_fabric_port.py
index 199f8cf..9c37ffc 100644
--- a/xos/synchronizer/steps/sync_fabric_port.py
+++ b/xos/synchronizer/steps/sync_fabric_port.py
@@ -17,7 +17,7 @@
 import urllib
 from requests.auth import HTTPBasicAuth
 from synchronizers.new_base.syncstep import SyncStep, DeferredException, model_accessor
-from synchronizers.new_base.modelaccessor import FabricService, SwitchPort, PortInterface
+from synchronizers.new_base.modelaccessor import FabricService, SwitchPort, PortInterface, FabricIpAddress
 
 from xosconfig import Config
 from multistructlog import create_logger
@@ -28,20 +28,24 @@
 
 class SyncFabricPort(SyncStep):
     provides = [SwitchPort]
-    observes = [SwitchPort, PortInterface]
+    observes = [SwitchPort, PortInterface, FabricIpAddress]
 
     def sync_record(self, model):
 
         if model.leaf_model_name == "PortInterface":
-            log.info("Receivent update for PortInterface", name=model, port=model.port.portId)
+            log.info("Receivent update for PortInterface", port=model.port.portId, interface=model)
             return self.sync_record(model.port)
 
+        if model.leaf_model_name == "FabricIpAddress":
+            log.info("Receivent update for FabricIpAddress", port=model.interface.port.portId, interface=model.interface.name, ip=model.ip)
+            return self.sync_record(model.interface.port)
+
         log.info("Adding port %s/%s to onos-fabric" % (model.switch.ofId, model.portId))
         interfaces = []
         for intf in model.interfaces.all():
             i = {
                 "name" : intf.name,
-                "ips" : [ intf.ips ]
+                "ips" : [ i.ip for i in intf.ips.all() ]
             }
             if intf.vlanUntagged:
                 i["vlan-untagged"] = intf.vlanUntagged
@@ -87,21 +91,21 @@
             raise Exception("Failed to %s port %s from ONOS" % url)
 
     def delete_record(self, model):
-
         if model.leaf_model_name == "PortInterface":
-            # TODO add unit tests
-            log.info("Receivent update for PortInterface", name=model, port=model.port.portId)
+            log.info("Received update for PortInterface", port=model.port.portId, interface=model.name)
             log.info("Removing port interface %s from port %s/%s in onos-fabric" % (model.name, model.port.switch.ofId, model.port.portId))
 
-            key = "%s/%s" % (model.port.switch.ofId, model.port.portId)
-            key = urllib.quote(key, safe='') + "/interfaces"
-
-            # deleting all the interfaces
-            self.delete_netcfg_item(key)
-
             # resync the existing interfaces
             return self.sync_record(model.port)
 
+        if model.leaf_model_name == "FabricIpAddress":
+            # TODO add unit tests
+            log.info("Received update for FabricIpAddress", port=model.interface.port.portId, interface=model.interface.name, ip=model.ip)
+            log.info("Removing IP %s from interface %s, on port %s/%s in onos-fabric" % (model.ip, model.interface.name, model.interface.port.switch.ofId, model.interface.port.portId))
+
+            # resync the existing interfaces
+            return self.sync_record(model.interface.port)
+
         log.info("Removing port %s/%s from onos-fabric" % (model.switch.ofId, model.portId))
 
         key = "%s/%s" % (model.switch.ofId, model.portId)
diff --git a/xos/synchronizer/steps/test_sync_fabric_port.py b/xos/synchronizer/steps/test_sync_fabric_port.py
index 2358d4a..8c6e773 100644
--- a/xos/synchronizer/steps/test_sync_fabric_port.py
+++ b/xos/synchronizer/steps/test_sync_fabric_port.py
@@ -76,11 +76,9 @@
         for (k, v) in model_accessor.all_model_classes.items():
             globals()[k] = v
 
-
         self.sync_step = SyncFabricPort
         self.sync_step.log = Mock()
 
-
         # mock onos-fabric
         onos_fabric = Mock()
         onos_fabric.name = "onos-fabric"
@@ -96,41 +94,55 @@
         self.fabric.name = "fabric"
         self.fabric.provider_services = [onos_fabric_base]
 
-        # create a mock SwitchPort instance
-        self.o = Mock()
-        self.o.id = 1
-        self.o.tologdict.return_value = {}
-        self.o.host_learning = True
-
-
-
-
     def tearDown(self):
-        self.o = None
         sys.path = self.sys_path_save
 
     @requests_mock.Mocker()
     def test_sync_port(self, m):
+        # IPs
+        ip1 = Mock()
+        ip1.ip = "1.1.1.1/16"
+        ip1.description = "My IPv4 ip"
+        ip2 = Mock()
+        ip2.ip = "2001:0db8:85a3:0000:0000:8a2e:0370:7334/64"
+        ip2.description = "My IPv6 ip"
+        ip3 = Mock()
+        ip3.ip = "2.2.2.2/8"
+        ip3.description = "My other IPv4 ip"
+
         intf1 = Mock()
         intf1.name = "intf1"
-        intf1.ips = "1.1.1.1/16"
         intf1.vlanUntagged = None
+        intf1.ips.all.return_value = [ip1, ip2]
+        intf2 = Mock()
+        intf2.name = "intf2"
+        intf2.vlanUntagged = 42
+        intf2.ips.all.return_value = [ip3]
 
-        self.o.interfaces.all.return_value = [intf1]
-        self.o.switch.ofId = "of:1234"
-        self.o.portId = "1"
+        port = Mock()
+        port.id = 1
+        port.tologdict.return_value = {}
+        port.host_learning = True
+        port.interfaces.all.return_value = [intf1, intf2]
+        port.switch.ofId = "of:1234"
+        port.portId = "1"
 
         expected_conf = {
             "ports": {
-                "%s/%s" % (self.o.switch.ofId, self.o.portId): {
+                "%s/%s" % (port.switch.ofId, port.portId): {
                     "interfaces": [
                         {
                             "name": intf1.name,
-                            "ips": [ intf1.ips ]
+                            "ips": [ ip1.ip, ip2.ip ]
+                        },
+                        {
+                            "name": intf2.name,
+                            "ips": [ip3.ip],
+                            "vlan-untagged": intf2.vlanUntagged
                         }
                     ],
                     "hostLearning": {
-                        "enabled": self.o.host_learning
+                        "enabled": port.host_learning
                     }
                 }
             }
@@ -142,65 +154,83 @@
 
         with patch.object(Service.objects, "get") as onos_fabric_get:
             onos_fabric_get.return_value = self.fabric
-
-            self.sync_step().sync_record(self.o)
-
-            self.assertTrue(m.called)
-
-    @requests_mock.Mocker()
-    def test_sync_port_with_vlan(self, m):
-        intf1 = Mock()
-        intf1.name = "intf1"
-        intf1.ips = "1.1.1.1/16"
-        intf1.vlanUntagged = 42
-
-        self.o.interfaces.all.return_value = [intf1]
-        self.o.switch.ofId = "of:1234"
-        self.o.portId = "1"
-        self.o.host_learning = False
-
-        expected_conf = {
-            "ports": {
-                "%s/%s" % (self.o.switch.ofId, self.o.portId): {
-                    "interfaces": [
-                        {
-                            "name": intf1.name,
-                            "ips": [intf1.ips],
-                            "vlan-untagged": intf1.vlanUntagged
-                        }
-                    ],
-                    "hostLearning": {
-                        "enabled": self.o.host_learning
-                    }
-                }
-            }
-        }
-
-        m.post("http://onos-fabric:8181/onos/v1/network/configuration/",
-               status_code=200,
-               additional_matcher=functools.partial(match_json, expected_conf))
-
-        with patch.object(Service.objects, "get") as onos_fabric_get:
-            onos_fabric_get.return_value = self.fabric
-
-            self.sync_step().sync_record(self.o)
-
+            self.sync_step().sync_record(port)
             self.assertTrue(m.called)
 
     @requests_mock.Mocker()
     def test_delete_port(self, m):
-
-        self.o.switch.ofId = "of:1234"
-        self.o.portId = "1"
+        # create a mock SwitchPort instance
+        port = Mock()
+        port.id = 1
+        port.tologdict.return_value = {}
+        port.host_learning = True
+        port.switch.ofId = "of:1234"
+        port.portId = "1"
 
         key = urllib.quote("of:1234/1", safe='')
-
         m.delete("http://onos-fabric:8181/onos/v1/network/configuration/ports/%s" % key,
             status_code=204)
 
         with patch.object(Service.objects, "get") as onos_fabric_get:
             onos_fabric_get.return_value = self.fabric
+            self.sync_step().delete_record(port)
+            self.assertTrue(m.called)
 
-            self.sync_step().delete_record(self.o)
+    @requests_mock.Mocker()
+    def test_delete_interface(self, m):
+        ip1 = Mock()
+        ip1.ip = "1.1.1.1/16"
+        ip1.description = "My IPv4 ip"
+        ip2 = Mock()
+        ip2.ip = "2001:0db8:85a3:0000:0000:8a2e:0370:7334/64"
+        ip2.description = "My IPv6 ip"
 
+        # interfaces
+        intf1 = Mock()
+        intf1.name = "intf1"
+        intf1.vlanUntagged = None
+        intf1.ips.all.return_value = [ip1, ip2]
+
+        # bindings
+        # create a mock SwitchPort instance
+        interface_to_remove = Mock()
+        interface_to_remove.id = 1
+        interface_to_remove.tologdict.return_value = {}
+        interface_to_remove.leaf_model_name = "PortInterface"
+        interface_to_remove.port.interfaces.all.return_value = [intf1]
+        interface_to_remove.port.switch.ofId = "of:1234"
+        interface_to_remove.port.portId = "1"
+        interface_to_remove.port.host_learning = True
+
+        m.post("http://onos-fabric:8181/onos/v1/network/configuration/", status_code=200)
+
+        with patch.object(Service.objects, "get") as onos_fabric_get:
+            onos_fabric_get.return_value = self.fabric
+            self.sync_step().delete_record(interface_to_remove)
+            self.assertTrue(m.called)
+
+    @requests_mock.Mocker()
+    def test_delete_ip(self, m):
+        ip1 = Mock()
+        ip1.ip = "1.1.1.1/16"
+        ip1.description = "My IPv4 ip"
+
+        intf1 = Mock()
+        intf1.name = "intf1"
+        intf1.vlanUntagged = None
+        intf1.ips.all.return_value = [ip1]
+
+        ip_to_remove = Mock()
+        ip_to_remove.id = 1
+        ip_to_remove.leaf_model_name = "FabricIpAddress"
+        ip_to_remove.interface.port.interfaces.all.return_value = [intf1] 
+        ip_to_remove.interface.port.switch.ofId = "of:1234"
+        ip_to_remove.interface.port.portId = "1"
+        ip_to_remove.interface.port.host_learning = True
+
+        m.post("http://onos-fabric:8181/onos/v1/network/configuration/", status_code=200)
+
+        with patch.object(Service.objects, "get") as onos_fabric_get:
+            onos_fabric_get.return_value = self.fabric
+            self.sync_step().delete_record(ip_to_remove)
             self.assertTrue(m.called)