# Path: netman/adapters/switches/util.py
# class SubShell(object):
#     debug = False
# 
#     def __init__(self, ssh, enter, exit_cmd, validate=None):
#         self.ssh = ssh
#         self.enter = enter
#         self.exit = exit_cmd
#         self.validate = validate or (lambda x: None)
# 
#     def __enter__(self):
#         if isinstance(self.enter, list):
#             [self.validate(self.ssh.do(cmd)) for cmd in self.enter]
#         else:
#             self.validate(self.ssh.do(self.enter))
#         return self.ssh
# 
#     def __exit__(self, eType, eValue, eTrace):
#         if self.debug and eType is not None:
#             logging.error("Subshell exception {}: {}\n{}"
#                           .format(eType.__name__, eValue, "".join(traceback.format_tb(eTrace))))
# 
#         self.ssh.do(self.exit)
# 
# Path: netman/core/objects/port_modes.py
# ACCESS = "ACCESS"
# 
# TRUNK = "TRUNK"
# 
# Path: netman/core/objects/switch_descriptor.py
# class SwitchDescriptor(Model):
#     def __init__(self, model, hostname, username=None, password=None, port=None, netman_server=None):
#         self.model = model
#         self.hostname = hostname
#         self.username = username
#         self.password = password
#         self.port = port
#         self.netman_server = netman_server


import unittest
import mock
from flexmock import flexmock, flexmock_teardown
from hamcrest import assert_that, has_length, equal_to, is_, none, empty
from netaddr import IPNetwork
from netaddr.ip import IPAddress
from netman.adapters.switches import brocade_factory_ssh, brocade_factory_telnet
from netman.adapters.switches.brocade import Brocade, parse_if_ranges
from netman.adapters.switches.util import SubShell
from netman.core.objects.access_groups import IN, OUT
from netman.core.objects.exceptions import IPNotAvailable, UnknownVlan, UnknownIP, UnknownAccessGroup, BadVlanNumber, \
    BadVlanName, UnknownInterface, TrunkVlanNotSet, UnknownVrf, VlanVrfNotSet, VrrpAlreadyExistsForVlan, BadVrrpPriorityNumber, BadVrrpGroupNumber, \
    BadVrrpTimers, BadVrrpTracking, NoIpOnVlanForVrrp, VrrpDoesNotExistForVlan, UnknownDhcpRelayServer, DhcpRelayServerAlreadyExists, \
    VlanAlreadyExist, InvalidAccessGroupName, IPAlreadySet
from netman.core.objects.interface_states import OFF, ON
from netman.core.objects.port_modes import ACCESS, TRUNK
from netman.core.objects.switch_descriptor import SwitchDescriptor
        ])

        self.shell_mock.should_receive("do").with_args("show running-config vlan").once().ordered().and_return([
            "spanning-tree",
            "!",
            "vlan 1 name DEFAULT-VLAN",
            " no untagged ethe 1/3",
            "!",
            "vlan 100",
            " tagged ethe 1/2 ethe 1/4 to 1/6",
            "!",
            "vlan 200",
            " tagged ethe 1/2",
            "!",
            "vlan 300",
            " tagged ethe 1/2",
            "!",
            "vlan 1999",
            " untagged ethe 1/1",
            "!",
            "vlan 2999",
            " untagged ethe 1/2",
            "!",
            "!"
        ])

        interface = self.switch.get_interface("ethernet 1/2")

        assert_that(interface.name, equal_to("ethernet 1/2"))
        assert_that(interface.shutdown, equal_to(True))

# Path: netman/adapters/switches/util.py
# class SubShell(object):
#     debug = False
# 
#     def __init__(self, ssh, enter, exit_cmd, validate=None):
#         self.ssh = ssh
#         self.enter = enter
#         self.exit = exit_cmd
#         self.validate = validate or (lambda x: None)
# 
#     def __enter__(self):
#         if isinstance(self.enter, list):
#             [self.validate(self.ssh.do(cmd)) for cmd in self.enter]
#         else:
#             self.validate(self.ssh.do(self.enter))
#         return self.ssh
# 
#     def __exit__(self, eType, eValue, eTrace):
#         if self.debug and eType is not None:
#             logging.error("Subshell exception {}: {}\n{}"
#                           .format(eType.__name__, eValue, "".join(traceback.format_tb(eTrace))))
# 
#         self.ssh.do(self.exit)
# 
# Path: netman/core/objects/port_modes.py
# ACCESS = "ACCESS"
# 
# TRUNK = "TRUNK"
# 
# Path: netman/core/objects/switch_descriptor.py
# class SwitchDescriptor(Model):
#     def __init__(self, model, hostname, username=None, password=None, port=None, netman_server=None):
#         self.model = model
#         self.hostname = hostname
#         self.username = username
#         self.password = password
#         self.port = port
#         self.netman_server = netman_server


# Path: tests/adapters/switches/brocade_test.py
import unittest
import mock
from flexmock import flexmock, flexmock_teardown
from hamcrest import assert_that, has_length, equal_to, is_, none, empty
from netaddr import IPNetwork
from netaddr.ip import IPAddress
from netman.adapters.switches import brocade_factory_ssh, brocade_factory_telnet
from netman.adapters.switches.brocade import Brocade, parse_if_ranges
from netman.adapters.switches.util import SubShell
from netman.core.objects.access_groups import IN, OUT
from netman.core.objects.exceptions import IPNotAvailable, UnknownVlan, UnknownIP, UnknownAccessGroup, BadVlanNumber, \
    BadVlanName, UnknownInterface, TrunkVlanNotSet, UnknownVrf, VlanVrfNotSet, VrrpAlreadyExistsForVlan, BadVrrpPriorityNumber, BadVrrpGroupNumber, \
    BadVrrpTimers, BadVrrpTracking, NoIpOnVlanForVrrp, VrrpDoesNotExistForVlan, UnknownDhcpRelayServer, DhcpRelayServerAlreadyExists, \
    VlanAlreadyExist, InvalidAccessGroupName, IPAlreadySet
from netman.core.objects.interface_states import OFF, ON
from netman.core.objects.port_modes import ACCESS, TRUNK
from netman.core.objects.switch_descriptor import SwitchDescriptor

        ])

        self.shell_mock.should_receive("do").with_args("show running-config vlan").once().ordered().and_return([
            "spanning-tree",
            "!",
            "vlan 1 name DEFAULT-VLAN",
            " no untagged ethe 1/3",
            "!",
            "vlan 100",
            " tagged ethe 1/2 ethe 1/4 to 1/6",
            "!",
            "vlan 200",
            " tagged ethe 1/2",
            "!",
            "vlan 300",
            " tagged ethe 1/2",
            "!",
            "vlan 1999",
            " untagged ethe 1/1",
            "!",
            "vlan 2999",
            " untagged ethe 1/2",
            "!",
            "!"
        ])

        interface = self.switch.get_interface("ethernet 1/2")

        assert_that(interface.name, equal_to("ethernet 1/2"))
        assert_that(interface.shutdown, equal_to(True))

        assert_that(interface.port_mode, equal_to(TRUNK))