Metronet Local Service

Change-Id: I92e13f49bbdfc60d27496b3c11207a72310731d4
diff --git a/xos/synchronizer/pseudowireproviders/__init__.py b/xos/synchronizer/pseudowireproviders/__init__.py
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/xos/synchronizer/pseudowireproviders/__init__.py
@@ -0,0 +1 @@
+
diff --git a/xos/synchronizer/pseudowireproviders/metronetworkpseudowireprovider.py b/xos/synchronizer/pseudowireproviders/metronetworkpseudowireprovider.py
new file mode 100644
index 0000000..f2762e2
--- /dev/null
+++ b/xos/synchronizer/pseudowireproviders/metronetworkpseudowireprovider.py
@@ -0,0 +1,66 @@
+from xos.logger import Logger, logging
+from synchronizers.vnodlocal.pseudowireproviders.pseudowireprovider import PseudowireProvider
+from services.metronetwork.models import NetworkEdgeToEdgePointConnection, NetworkEdgePort
+
+logger = Logger(level=logging.INFO)
+
+class MetronetworkPseudowireProvider(PseudowireProvider):
+
+    def __init__(self, **args):
+        pass
+
+    # Methods to support creation
+    #
+    # Returns: handle
+    #
+    def create(self, port1, port2, vlanid, psuedowireservice):
+        # Create method - create eline with the ports
+        # Vlan is TBD
+        pseudowirename = ("port1: %s, port2: %s, vlan: %s" % (port1, port2, vlanid))
+        logger.info("Metronetwork create called, name: %s" % pseudowirename )
+        # Edge to Edge Point Connectivity creation
+        edgetoedgeconnectivity = NetworkEdgeToEdgePointConnection()
+        uni1port = NetworkEdgePort.objects.filter(pid__icontains=port1)
+        if uni1port:
+            uni1port = uni1port[0]
+        uni2port = NetworkEdgePort.objects.filter(pid__icontains=port2)
+        if uni2port:
+            uni2port = uni2port[0]
+        edgetoedgeconnectivity.uni1 = uni1port
+        edgetoedgeconnectivity.uni2 = uni2port
+        edgetoedgeconnectivity.vlanid = vlanid
+        edgetoedgeconnectivity.type = 'Point_To_Point'
+        edgetoedgeconnectivity.operstate = 'inactive'
+        edgetoedgeconnectivity.adminstate = 'disabled'
+        edgetoedgeconnectivity.sid = pseudowirename
+        edgetoedgeconnectivity.name = 'Metronetwork'
+        edgetoedgeconnectivity.save()
+        return pseudowirename
+
+    # Method to support connect
+    #
+    def connect(self, handle):
+        # Connect method - simply transition the state of the underlying object - the Metronet sync will do the rest
+        logger.info("Metronetwork Pseudowire connect called, handle = %s" % handle)
+        edgetoedgeconnectivity = NetworkEdgeToEdgePointConnection.objects.get(sid=handle)
+        edgetoedgeconnectivity.adminstate = 'activationrequested'
+        edgetoedgeconnectivity.save()
+
+    # Method to support disconnect connect
+    #
+    def disconnect(self, handle):
+        # Connect method - simply transition the state of the underlying object - the Metronet sync will do the rest
+        logger.info("Metronetwork Pseudowire disconnect called, handle = %s" % handle)
+        edgetoedgeconnectivity = NetworkEdgeToEdgePointConnection.objects.get(sid=handle)
+        edgetoedgeconnectivity.adminstate = 'deactivationrequested'
+        edgetoedgeconnectivity.save()
+
+    # Method to support deletion
+    #
+    def delete(self, handle):
+        # Delete method - simply set the state to deleted and the Metronet sync will do the rest
+        logger.info("Metronetwork Pseudowire delete called, handle = %s" % handle)
+        edgetoedgeconnectivity = NetworkEdgeToEdgePointConnection.objects.get(sid=handle)
+        edgetoedgeconnectivity.deleted = True
+        edgetoedgeconnectivity.save()
+
diff --git a/xos/synchronizer/pseudowireproviders/providerfactory.py b/xos/synchronizer/pseudowireproviders/providerfactory.py
new file mode 100644
index 0000000..6735ecc
--- /dev/null
+++ b/xos/synchronizer/pseudowireproviders/providerfactory.py
@@ -0,0 +1,24 @@
+import sys
+
+from services.vnodlocal.models import VnodLocalSystem
+from synchronizers.vnodlocal.pseudowireproviders.metronetworkpseudowireprovider import MetronetworkPseudowireProvider
+from synchronizers.vnodlocal.pseudowireproviders.segmentroutingvlanxconnectpseudowireprovider import SegmentRoutingVlanXconnectPseudowireProvider
+
+
+class ProviderFactory(object):
+    @staticmethod
+    def getprovider():
+
+        # We look up the VnodLocal Configuration to see what to do
+        vnodlocalsystems = VnodLocalSystem.objects.all()
+        if not vnodlocalsystems:
+            return None
+
+        vnodlocalsystem = vnodlocalsystems[0]
+
+        if vnodlocalsystem.pseudowireprovider == 'metronetwork':
+            return MetronetworkPseudowireProvider()
+        elif vnodlocalsystem.pseudowireprovider == 'segmentroutingxconnect':
+            return SegmentRoutingVlanXconnectPseudowireProvider()
+        else:
+            return None
\ No newline at end of file
diff --git a/xos/synchronizer/pseudowireproviders/pseudowireprovider.py b/xos/synchronizer/pseudowireproviders/pseudowireprovider.py
new file mode 100644
index 0000000..5107499
--- /dev/null
+++ b/xos/synchronizer/pseudowireproviders/pseudowireprovider.py
@@ -0,0 +1,36 @@
+from xos.logger import Logger, logging
+
+logger = Logger(level=logging.INFO)
+
+
+class PseudowireProvider(object):
+
+    def __init__(self, **args):
+        pass
+
+    # Methods to support creation
+    #
+    # Returns: handle
+    #
+    def create(self, port1, port2, vlanid, pseudowireservice):
+        # Default method needs to be overriden
+        logger.info("create called - should be overriden")
+
+    # Method to support connection
+    #
+    def connect(self, handle):
+        # Default method needs to be overriden
+        logger.info("connect called - should be overriden")
+        return None
+
+    # Method to support disconnection
+    #
+    def disconnect(self, handle):
+        # Default method needs to be overriden
+        logger.info("discoconnect called - should be overriden")
+
+    # Methods to support deletion
+    #
+    def delete(self, handle):
+        # Default method needs to be overriden
+        logger.info("delete called - should be overriden")
\ No newline at end of file
diff --git a/xos/synchronizer/pseudowireproviders/segmentroutingvlanxconnectpseudowireprovider.py b/xos/synchronizer/pseudowireproviders/segmentroutingvlanxconnectpseudowireprovider.py
new file mode 100644
index 0000000..b98e1c5
--- /dev/null
+++ b/xos/synchronizer/pseudowireproviders/segmentroutingvlanxconnectpseudowireprovider.py
@@ -0,0 +1,94 @@
+from xos.logger import Logger, logging
+from synchronizers.vnodlocal.pseudowireproviders.pseudowireprovider import PseudowireProvider
+from services.metronetwork.models import NetworkEdgeToEdgePointConnection, NetworkEdgePort
+
+import requests, json
+from requests.auth import HTTPBasicAuth
+
+logger = Logger(level=logging.INFO)
+
+class SegmentRoutingVlanXconnectPseudowireProvider(PseudowireProvider):
+
+    def __init__(self, **args):
+        pass
+
+    # Methods to support creation
+    #
+    # Returns: handle
+    #
+    def create(self, port1, port2, vlanid, pseudowireservice):
+        # Create method - create xconnect
+        # Vlan is TBD
+        pseudowirename = ("port1: %s, port2: %s, vlan: %s" % (port1, port2, vlanid))
+        logger.info("SegmentRoutingXConnect create called, name: %s" % pseudowirename )
+
+        # Pull out Ports from FQN
+
+
+        # Pull out Device from FQN
+        # Use default user/password?
+
+        # curl --user onos:rocks -X POST -H "Content-Type: application/json" http://138.120.151.126:8181/onos/v1/network/configuration/apps/org.onosproject.segmentrouting/xconnect -d '{ "of:0000000000000001" : [{"vlan" : "100", "ports" : [1, 2], "name" : "Mike"}] }'
+
+        # Port 1 Device and Num
+        port1IdToken = port1.split('/', 1)
+        port1Devicename = port1IdToken[0]
+        port1Num = port1IdToken[1]
+
+        # Port 2 Device and Num
+        port2IdToken = port2.split('/', 1)
+        port2Devicename = port2IdToken[0]
+        port2Num = port2IdToken[1]
+
+        # Lets make sure the Devices are the same - otherwise its an error - Xconnect must be on same device
+
+        if (port1Devicename != port2Devicename):
+            Exception("XConnect Device must be the same. D1= % D2=%" % (port1Devicename, port2Devicename))
+
+        # Get URL from PwaaS Ojbect
+        restCtrlUrl = pseudowireservice.networkControllerUrl
+
+        data = {
+            port2Devicename : [
+                {
+                    "vlan" : vlanid,
+                    "ports" : [port1Num, port2Num],
+                    "name" : pseudowirename
+                }
+            ]
+           }
+
+        headers = {'Content-Type': 'application/json'}
+
+        resp = requests.post('{}/v1/network/configuration/apps/org.onosproject.segmentrouting/xconnect'.format(restCtrlUrl),
+                             data=json.dumps(data), headers=headers, auth=HTTPBasicAuth('karaf', 'karaf'))
+
+        if resp.status_code == 200:
+            logger.info("SegmentRoutingXConnect create successful")
+            return pseudowirename
+        else:
+            Exception("Pseudowire create failed Error Code: %s" % resp.status_code)
+
+
+    # Method to support connect
+    #
+    def connect(self, handle):
+        # Connect method - this is a no-op for this module, it does not support a complext state machine
+        logger.info("SegmentRoutingXConnect Pseudowire connect called, handle = %s" % handle)
+
+    # Method to support disconnect connect
+    #
+    def disconnect(self, handle):
+        # Disconnect method - impl is TBD
+        logger.info("SegmentRoutingXConnect Pseudowire disconnect called, handle = %s" % handle)
+
+        # Example command line syntax:
+        # curl --user onos:rocks -X DELETE http://138.120.151.126:8181/onos/v1/network/configuration/apps/org.onosproject.segmentrouting/xconnect
+
+    # Method to support deletion
+    #
+    def delete(self, handle):
+        # Delete method - impl is TBD
+        logger.info("SegmentRoutingXConnect Pseudowire delete called, handle = %s" % handle)
+
+