SEBA-206 manage and report switchport state

Change-Id: I9499cb24f66b4c64125cc5c8bfd97afe9104cdb6
diff --git a/VERSION b/VERSION
index 348fc11..39b2a23 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-2.1.12
+2.1.13-dev
diff --git a/xos/synchronizer/event_steps/onos_event.py b/xos/synchronizer/event_steps/onos_event.py
new file mode 100644
index 0000000..8458516
--- /dev/null
+++ b/xos/synchronizer/event_steps/onos_event.py
@@ -0,0 +1,59 @@
+
+# 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 json
+from xossynchronizer.event_steps.eventstep import EventStep
+from xosconfig import Config
+from multistructlog import create_logger
+
+log = create_logger(Config().get('logging'))
+
+
+class OnosPortEventStep(EventStep):
+    topics = ["onos.events.port"]
+    technology = "kafka"
+
+    def __init__(self, *args, **kwargs):
+        super(OnosPortEventStep, self).__init__(*args, **kwargs)
+
+    def process_event(self, event):
+        value = json.loads(event.value)
+
+        switch = self.model_accessor.Switch.objects.filter(
+            ofId=value["deviceId"]
+        )
+        if not switch:
+            log.info("Event for unknown switch", deviceId=value["deviceId"])
+            return
+
+        switch = switch[0]
+
+        port = self.model_accessor.SwitchPort.objects.filter(
+            switch_id=switch.id,
+            portId=value["portId"]
+        )
+        if not port:
+            log.info("Event for unknown port",
+                     deviceId=value["deviceId"],
+                     portId=value["portId"])
+            return
+
+        port = port[0]
+
+        oper_status = "enabled" if value["enabled"] else "disabled"
+        if oper_status != port.oper_status:
+            port.oper_status = oper_status
+            port.save_changed_fields()
diff --git a/xos/synchronizer/event_steps/test_onos_event.py b/xos/synchronizer/event_steps/test_onos_event.py
new file mode 100644
index 0000000..2174577
--- /dev/null
+++ b/xos/synchronizer/event_steps/test_onos_event.py
@@ -0,0 +1,167 @@
+# 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 json
+from mock import patch, Mock
+
+import os
+import sys
+
+test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
+
+
+class TestOnosPortEvent(unittest.TestCase):
+
+    def setUp(self):
+        global DeferredException
+
+        self.sys_path_save = sys.path
+
+        # Setting up the config module
+        from xosconfig import Config
+        config = os.path.join(test_path, "../test_config.yaml")
+        Config.clear()
+        Config.init(config, "synchronizer-config-schema.yaml")
+        # END Setting up the config module
+
+        from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
+        mock_modelaccessor_config(test_path, [("fabric", "fabric.xproto"),
+                                              ("onos-service", "onos.xproto")])
+
+        import xossynchronizer.modelaccessor
+        import mock_modelaccessor
+        reload(mock_modelaccessor)  # in case nose2 loaded it in a previous test
+        reload(xossynchronizer.modelaccessor)  # in case nose2 loaded it in a previous test
+
+        from xossynchronizer.modelaccessor import model_accessor
+        self.model_accessor = model_accessor
+
+        from mock_modelaccessor import MockObjectList
+        from onos_event import OnosPortEventStep
+
+        # import all class names to globals
+        for (k, v) in model_accessor.all_model_classes.items():
+            globals()[k] = v
+
+        self.event_step = OnosPortEventStep
+
+        self.fabric_service = FabricService(name="fabric",
+                                            id=1112,
+                                            backend_code=1,
+                                            backend_status="succeeded")
+
+        self.switch = Switch(name="switch1",
+                             ofId="of:0000000000000001",
+                             backend_code=1,
+                             backend_status="succeeded")
+
+        self.port1 = SwitchPort(name="switch1port1",
+                                switch=self.switch,
+                                switch_id=self.switch.id,
+                                portId="1",
+                                oper_status=None,
+                                backend_code=1,
+                                backend_status="succeeded")
+
+        self.port2 = SwitchPort(name="switch1port2",
+                                switch=self.switch,
+                                switch_id=self.switch.id,
+                                portId="2",
+                                oper_status=None,
+                                backend_code=1,
+                                backend_status="succeeded")
+
+        self.switch.ports = MockObjectList([self.port1, self.port2])
+
+        self.log = Mock()
+
+    def tearDown(self):
+        sys.path = self.sys_path_save
+
+    def test_process_event_enable(self):
+        with patch.object(Switch.objects, "get_items") as switch_objects, \
+             patch.object(SwitchPort.objects, "get_items") as switchport_objects:
+            switch_objects.return_value = [self.switch]
+            switchport_objects.return_value = [self.port1, self.port2]
+
+            event_dict = {"deviceId": self.switch.ofId,
+                          "portId": self.port1.portId,
+                          "enabled": True}
+            event = Mock()
+            event.value = json.dumps(event_dict)
+
+            step = self.event_step(model_accessor=self.model_accessor, log=self.log)
+            step.process_event(event)
+
+            self.assertEqual(self.port1.oper_status, "enabled")
+
+    def test_process_event_disable(self):
+        with patch.object(Switch.objects, "get_items") as switch_objects, \
+             patch.object(SwitchPort.objects, "get_items") as switchport_objects:
+            switch_objects.return_value = [self.switch]
+            switchport_objects.return_value = [self.port1, self.port2]
+
+            event_dict = {"deviceId": self.switch.ofId,
+                          "portId": self.port1.portId,
+                          "enabled": False}
+            event = Mock()
+            event.value = json.dumps(event_dict)
+
+            step = self.event_step(model_accessor=self.model_accessor, log=self.log)
+            step.process_event(event)
+
+            self.assertEqual(self.port1.oper_status, "disabled")
+
+    def test_process_event_no_switch(self):
+        with patch.object(Switch.objects, "get_items") as switch_objects, \
+             patch.object(SwitchPort.objects, "get_items") as switchport_objects:
+            switch_objects.return_value = [self.switch]
+            switchport_objects.return_value = [self.port1, self.port2]
+
+            event_dict = {"deviceId": "doesnotexist",
+                          "portId": self.port1.portId,
+                          "enabled": True}
+            event = Mock()
+            event.value = json.dumps(event_dict)
+
+            step = self.event_step(model_accessor=self.model_accessor, log=self.log)
+
+            step.process_event(event)
+
+            # should not have changed
+            self.assertEqual(self.port1.oper_status, None)
+
+    def test_process_event_no_port(self):
+        with patch.object(Switch.objects, "get_items") as switch_objects, \
+             patch.object(SwitchPort.objects, "get_items") as switchport_objects:
+            switch_objects.return_value = [self.switch]
+            switchport_objects.return_value = [self.port1, self.port2]
+
+            event_dict = {"deviceId": self.switch.ofId,
+                          "portId": "doesnotexist",
+                          "enabled": True}
+            event = Mock()
+            event.value = json.dumps(event_dict)
+
+            step = self.event_step(model_accessor=self.model_accessor, log=self.log)
+
+            step.process_event(event)
+
+            # should not have changed
+            self.assertEqual(self.port1.oper_status, None)
+
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/xos/synchronizer/migrations/0004_auto_20190320_1456.py b/xos/synchronizer/migrations/0004_auto_20190320_1456.py
new file mode 100644
index 0000000..2c20f66
--- /dev/null
+++ b/xos/synchronizer/migrations/0004_auto_20190320_1456.py
@@ -0,0 +1,39 @@
+# 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.
+
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.11 on 2019-03-20 18:56
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('fabric', '0003_auto_20190312_1839'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='switchport',
+            name='admin_state',
+            field=models.TextField(blank=True, choices=[(b'enabled', b'enabled'), (b'disabled', b'disabled')], default=b'enabled', help_text=b'desired administrative state of port', null=True),
+        ),
+        migrations.AddField(
+            model_name='switchport',
+            name='oper_status',
+            field=models.TextField(blank=True, choices=[(b'enabled', b'enabled'), (b'disabled', b'disabled')], help_text=b'operational status of port', null=True),
+        ),
+    ]
diff --git a/xos/synchronizer/models/fabric.xproto b/xos/synchronizer/models/fabric.xproto
index d07fdbb..4f305b9 100644
--- a/xos/synchronizer/models/fabric.xproto
+++ b/xos/synchronizer/models/fabric.xproto
@@ -51,6 +51,14 @@
     required bool host_learning = 3 [
         help_text = "whether or not to enable autodiscovery",
         default = True];
+    optional string admin_state = 4 [
+        help_text = "desired administrative state of port",
+        choices = "(('enabled', 'enabled'), ('disabled', 'disabled'))",
+        default = "enabled"];
+    optional string oper_status = 5 [
+        help_text = "operational status of port",
+        choices = "(('enabled', 'enabled'), ('disabled', 'disabled'))",
+        feedback_state = True];
 }
 
 message PortInterface(XOSBase) {
diff --git a/xos/synchronizer/steps/sync_fabric_port.py b/xos/synchronizer/steps/sync_fabric_port.py
index 2ccdbac..1ab0f52 100644
--- a/xos/synchronizer/steps/sync_fabric_port.py
+++ b/xos/synchronizer/steps/sync_fabric_port.py
@@ -16,8 +16,8 @@
 import requests
 import urllib
 from requests.auth import HTTPBasicAuth
-from xossynchronizer.steps.syncstep import SyncStep, DeferredException
-from xossynchronizer.modelaccessor import FabricService, SwitchPort, PortInterface, FabricIpAddress, model_accessor
+from xossynchronizer.steps.syncstep import SyncStep
+from xossynchronizer.modelaccessor import SwitchPort, PortInterface, FabricIpAddress, model_accessor
 
 from xosconfig import Config
 from multistructlog import create_logger
@@ -26,6 +26,7 @@
 
 log = create_logger(Config().get('logging'))
 
+
 class SyncFabricPort(SyncStep):
     provides = [SwitchPort]
     observes = [SwitchPort, PortInterface, FabricIpAddress]
@@ -33,19 +34,22 @@
     def sync_record(self, model):
 
         if model.leaf_model_name == "PortInterface":
-            log.info("Receivent update for PortInterface", port=model.port.portId, interface=model)
+            log.info("Received 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)
+            log.info("Received 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" : [ i.ip for i in intf.ips.all() ]
+                "name": intf.name,
+                "ips": [i.ip for i in intf.ips.all()]
             }
             if intf.vlanUntagged:
                 i["vlan-untagged"] = intf.vlanUntagged
@@ -54,7 +58,7 @@
         # Send port config to onos-fabric netcfg
         data = {
             "ports": {
-                "%s/%s" % (model.switch.ofId, model.portId) : {
+                "%s/%s" % (model.switch.ofId, model.portId): {
                     "interfaces": interfaces,
                     "hostLearning": {
                         "enabled": model.host_learning
@@ -80,6 +84,26 @@
             except Exception:
                 log.info("Port %s/%s response" % (model.switch.ofId, model.portId), text=r.text)
 
+        # Now set the port's administrative state.
+        # TODO(smbaker): See if netcfg allows us to specify the portstate instead of using a separate REST call
+
+        url = 'http://%s:%s/onos/v1/devices/%s/portstate/%s' % (onos.rest_hostname,
+                                                                onos.rest_port,
+                                                                model.switch.ofId,
+                                                                model.portId)
+        data = {"enabled": True if model.admin_state == "enabled" else False}
+        log.debug("Sending portstate %s to %s/%s" % (data, model.switch.ofId, model.portId))
+        r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password))
+
+        if r.status_code != 200:
+            log.error(r.text)
+            raise Exception("Failed to set portstate  %s/%s into ONOS" % (model.switch.ofId, model.portId))
+        else:
+            try:
+                log.info("Portstate %s/%s response" % (model.switch.ofId, model.portId), json=r.json())
+            except Exception:
+                log.info("Portstate %s/%s response" % (model.switch.ofId, model.portId), text=r.text)
+
     def delete_netcfg_item(self, partial_url):
         onos = Helpers.get_onos_fabric_service(self.model_accessor)
         url = 'http://%s:%s/onos/v1/network/configuration/ports/%s' % (onos.rest_hostname, onos.rest_port, partial_url)
diff --git a/xos/synchronizer/steps/test_sync_fabric_port.py b/xos/synchronizer/steps/test_sync_fabric_port.py
index 97c93c5..234a048 100644
--- a/xos/synchronizer/steps/test_sync_fabric_port.py
+++ b/xos/synchronizer/steps/test_sync_fabric_port.py
@@ -15,17 +15,17 @@
 import unittest
 import urllib
 import functools
-from mock import patch, call, Mock, PropertyMock
+from mock import patch, Mock
 import requests_mock
-import multistructlog
-from multistructlog import create_logger
 
-import os, sys
+import os
+import sys
 
-test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
+test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
+
 
 def match_json(desired, req):
-    if desired!=req.json():
+    if desired != req.json():
         raise Exception("Got request %s, but body is not matching" % req.url)
         return False
     return True
@@ -49,8 +49,8 @@
 
         import xossynchronizer.modelaccessor
         import mock_modelaccessor
-        reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
-        reload(xossynchronizer.modelaccessor)      # in case nose2 loaded it in a previous test
+        reload(mock_modelaccessor)  # in case nose2 loaded it in a previous test
+        reload(xossynchronizer.modelaccessor)  # in case nose2 loaded it in a previous test
 
         from xossynchronizer.modelaccessor import model_accessor
         self.model_accessor = model_accessor
@@ -111,6 +111,7 @@
         port.interfaces.all.return_value = [intf1, intf2]
         port.switch.ofId = "of:1234"
         port.portId = "1"
+        port.admin_state = "enabled"
 
         expected_conf = {
             "ports": {
@@ -118,7 +119,7 @@
                     "interfaces": [
                         {
                             "name": intf1.name,
-                            "ips": [ ip1.ip, ip2.ip ]
+                            "ips": [ip1.ip, ip2.ip]
                         },
                         {
                             "name": intf2.name,
@@ -137,6 +138,79 @@
                status_code=200,
                additional_matcher=functools.partial(match_json, expected_conf))
 
+        expected_activation = {"enabled": True}
+
+        m.post("http://onos-fabric:8181/onos/v1/devices/%s/portstate/%s" % (port.switch.ofId, port.portId),
+               status_code=200,
+               additional_matcher=functools.partial(match_json, expected_activation))
+
+        with patch.object(Service.objects, "get") as onos_fabric_get:
+            onos_fabric_get.return_value = self.fabric
+            self.sync_step(model_accessor=self.model_accessor).sync_record(port)
+            self.assertTrue(m.called)
+
+    @requests_mock.Mocker()
+    def test_sync_port_disabled(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.vlanUntagged = None
+        intf1.ips.all.return_value = [ip1, ip2]
+        intf2 = Mock()
+        intf2.name = "intf2"
+        intf2.vlanUntagged = 42
+        intf2.ips.all.return_value = [ip3]
+
+        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"
+        port.admin_state = "disabled"
+
+        expected_conf = {
+            "ports": {
+                "%s/%s" % (port.switch.ofId, port.portId): {
+                    "interfaces": [
+                        {
+                            "name": intf1.name,
+                            "ips": [ip1.ip, ip2.ip]
+                        },
+                        {
+                            "name": intf2.name,
+                            "ips": [ip3.ip],
+                            "vlan-untagged": intf2.vlanUntagged
+                        }
+                    ],
+                    "hostLearning": {
+                        "enabled": port.host_learning
+                    }
+                }
+            }
+        }
+
+        m.post("http://onos-fabric:8181/onos/v1/network/configuration/",
+               status_code=200,
+               additional_matcher=functools.partial(match_json, expected_conf))
+
+        expected_activation = {"enabled": False}
+
+        m.post("http://onos-fabric:8181/onos/v1/devices/%s/portstate/%s" % (port.switch.ofId, port.portId),
+               status_code=200,
+               additional_matcher=functools.partial(match_json, expected_activation))
+
         with patch.object(Service.objects, "get") as onos_fabric_get:
             onos_fabric_get.return_value = self.fabric
             self.sync_step(model_accessor=self.model_accessor).sync_record(port)
@@ -154,7 +228,7 @@
 
         key = urllib.quote("of:1234/1", safe='')
         m.delete("http://onos-fabric:8181/onos/v1/network/configuration/ports/%s" % key,
-            status_code=204)
+                 status_code=204)
 
         with patch.object(Service.objects, "get") as onos_fabric_get:
             onos_fabric_get.return_value = self.fabric
@@ -189,6 +263,10 @@
 
         m.post("http://onos-fabric:8181/onos/v1/network/configuration/", status_code=200)
 
+        m.post("http://onos-fabric:8181/onos/v1/devices/%s/portstate/%s" % (interface_to_remove.port.switch.ofId,
+                                                                            interface_to_remove.port.portId),
+               status_code=200)
+
         with patch.object(Service.objects, "get") as onos_fabric_get:
             onos_fabric_get.return_value = self.fabric
             self.sync_step(model_accessor=self.model_accessor).delete_record(interface_to_remove)
@@ -208,17 +286,22 @@
         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.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)
 
+        m.post("http://onos-fabric:8181/onos/v1/devices/%s/portstate/%s" % (ip_to_remove.interface.port.switch.ofId,
+                                                                            ip_to_remove.interface.port.portId),
+               status_code=200)
+
         with patch.object(Service.objects, "get") as onos_fabric_get:
             onos_fabric_get.return_value = self.fabric
             self.sync_step(model_accessor=self.model_accessor).delete_record(ip_to_remove)
             self.assertTrue(m.called)
 
+
 if __name__ == '__main__':
     unittest.main()