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 = ("Vlan: %s" % 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.name = pseudowirename
        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(name=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(name=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(name=handle)
        edgetoedgeconnectivity.deleted = True
        edgetoedgeconnectivity.save()

