forked from rgl/ansible-collection-tp-link-easy-smart-switch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch_client.py
More file actions
172 lines (150 loc) · 6.94 KB
/
switch_client.py
File metadata and controls
172 lines (150 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Copyright (c) 2021 Rui Lopes (ruilopes.com).
# GNU General Public License v3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
from .network import Network
from .protocol import Protocol
from .binary import mac_to_bytes
def ports_to_byte(ports):
b = 0
for n in ports:
b |= 1 << (n - 1)
return b
class SmrtSwitchClientInterface(object):
def get_ports(self):
pass
def set_ports(self, ports):
pass
def get_pvids(self):
pass
def set_pvids(self, pvids):
pass
def get_vlans(self):
pass
def set_vlans(self, vlans):
pass
class SmrtSwitchClient(SmrtSwitchClientInterface):
def __init__(self, host_interface, switch_mac_address, username, password):
self._username = username
self._password = password
self._network = Network(host_interface, switch_mac_address)
self._network.login(username, password)
def get_ports(self):
# get the actual ports from the switch.
_header, payload = self._network.query(Protocol.GET, [(Protocol.get_id('ports'), b'')])
# payload is an array of tuples (property_id, property_name, ...depend on property_id...):
# (4096, 'ports', enabled) # e.g. (4096, 'ports', '01:01:00:01:06:00:00')
# ^^ ^^ ^^ ^^ ^^ ^^ ^^
# | | | | | | |
# port | | | | | actual flow control
# status | | | flow control
# 00 == Disabled | | | 00 == Off
# 01 == Enabled | | | 01 == On
# LAG | actual speed
# speed
# 01 == Auto
# 02 == 10MH
# 03 == 10MF
# 04 == 100MH
# 05 == 100MF
# 06 == 1000MF
for p in payload:
if p[1] == 'ports':
data = p[2].split(':')
yield {
'port': int(data[0], 16),
'status': int(data[1], 16),
'lag': int(data[2], 16),
'speed': int(data[3], 16),
'actual_speed': int(data[4], 16),
'flow_control': int(data[5], 16),
'actual_flow_control': int(data[6], 16),
}
def set_ports(self, ports):
set_payload = []
for p in ports:
set_payload.append((
Protocol.get_id('ports'),
mac_to_bytes(
'%02x:%02x:%02x:%02x:00:%02x:00' % (
p['port'],
p['status'],
p['lag'],
p['speed'],
p['flow_control']))))
_header, _payload = self._network.set(
self._username,
self._password,
set_payload)
# TODO verify the returned payload.
def get_pvids(self):
# get the actual pvids from the switch and set them in actual_ports.
_header, payload = self._network.query(Protocol.GET, [(Protocol.get_id('pvid'), b'')])
# payload is an array of tuples (property_id, property_name, ...depend on property_id...):
# (8706, 'pvid', (port, pvid)) # e.g. (8706, 'pvid', (1, 1))
# (8707, 'vlan_filler', vlan_filler) # e.g. (8707, 'vlan_filler', ' ')
for p in payload:
if p[1] == 'pvid':
yield {
'port': int(p[2][0]),
'pvid': int(p[2][1]),
}
def set_pvids(self, pvids):
set_payload = []
for v in pvids:
set_payload.append((
Protocol.get_id('pvid'),
Protocol.set_pvid(
v['pvid'],
v['port'])))
_header, _payload = self._network.set(
self._username,
self._password,
set_payload)
# TODO verify the returned payload. it returns the settings after our change, we should verify if they match our expectation.
def get_vlan_enabled(self):
# get the actual vlans from the switch.
_header, payload = self._network.query(Protocol.GET, [(Protocol.get_id('vlan_enabled'), b'')])
# payload is an array of tuples (property_id, property_name, ...depend on property_id...):
# (8704, 'vlan_enabled', enabled) # e.g. (8704, 'vlan_enabled', '01')
for p in payload:
if p[1] == 'vlan_enabled':
return int(p[2], 16) != 0
return False
def set_vlan_enabled(self, enabled):
set_payload = [(Protocol.get_id('vlan_enabled'), b'\x01' if enabled else b'\x00')]
_header, _payload = self._network.set(
self._username,
self._password,
set_payload)
# TODO verify the returned payload.
def get_vlans(self):
# get the actual vlans from the switch.
_header, payload = self._network.query(Protocol.GET, [(Protocol.get_id('vlan'), b'')])
# payload is an array of tuples (property_id, property_name, ...depend on property_id...):
# (8704, 'vlan_enabled', enabled) # e.g. (8704, 'vlan_enabled', '01')
# (8705, 'vlan', (vlan_id, member_ports, tagged_ports, vlan_name)) # e.g. (8705, 'vlan', (1, '1,2,4', '', 'Default'))
for p in payload:
if p[1] == 'vlan':
yield {
'vlan_id': int(p[2][0]),
'name': p[2][3],
'member_ports': [int(s) for s in p[2][1].split(',') if s],
'tagged_ports': [int(s) for s in p[2][2].split(',') if s],
}
def set_vlans(self, vlans):
# NB we have to set each vlan individually, because there's a bug
# somewhere in the firmware, that for some reason, the vlan name
# is messed up when we try to set several vlans at once.
for v in vlans:
set_payload = []
set_payload.append((
Protocol.get_id('vlan'),
Protocol.set_vlan(
v['vlan_id'],
ports_to_byte(v['member_ports']),
ports_to_byte(v['tagged_ports']),
v['name'] or '')))
_header, _payload = self._network.set(
self._username,
self._password,
set_payload)
# TODO verify the returned payload.