1 | /* |
2 | SPDX-FileCopyrightText: Pranav Gade <pranavgade20@gmail.com> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
5 | */ |
6 | |
7 | #include "teamportsetting.h" |
8 | #include "teamportsetting_p.h" |
9 | |
10 | #include <QDebug> |
11 | |
12 | #if !NM_CHECK_VERSION(1, 10, 0) |
13 | #define NM_SETTING_TEAM_PORT_SETTING_NAME "team-port" |
14 | |
15 | #define NM_SETTING_TEAM_PORT_CONFIG "config" |
16 | #define NM_SETTING_TEAM_PORT_QUEUE_ID "queue-id" |
17 | #define NM_SETTING_TEAM_PORT_PRIO "prio" |
18 | #define NM_SETTING_TEAM_PORT_STICKY "sticky" |
19 | #define NM_SETTING_TEAM_PORT_LACP_PRIO "lacp-prio" |
20 | #define NM_SETTING_TEAM_PORT_LACP_KEY "lacp-key" |
21 | #define NM_SETTING_TEAM_PORT_LINK_WATCHERS "link-watchers" |
22 | #endif |
23 | |
24 | NetworkManager::TeamPortSettingPrivate::TeamPortSettingPrivate() |
25 | : name(NM_SETTING_TEAM_PORT_SETTING_NAME) |
26 | , lacpKey(0) |
27 | , lacpPrio(255) |
28 | , prio(0) |
29 | , queueId(-1) |
30 | , sticky(false) |
31 | { |
32 | } |
33 | |
34 | NetworkManager::TeamPortSetting::TeamPortSetting() |
35 | : Setting(Setting::TeamPort) |
36 | , d_ptr(new TeamPortSettingPrivate()) |
37 | { |
38 | } |
39 | |
40 | NetworkManager::TeamPortSetting::TeamPortSetting(const Ptr &other) |
41 | : Setting(other) |
42 | , d_ptr(new TeamPortSettingPrivate()) |
43 | { |
44 | config(config: other->config()); |
45 | lacpKey(key: other->lacpKey()); |
46 | lacpPrio(priority: other->lacpPrio()); |
47 | prio(prio: other->prio()); |
48 | queueId(id: other->queueId()); |
49 | sticky(sticky: other->sticky()); |
50 | setLinkWatchers(other->linkWatchers()); |
51 | } |
52 | |
53 | NetworkManager::TeamPortSetting::~TeamPortSetting() |
54 | { |
55 | delete d_ptr; |
56 | } |
57 | |
58 | QString NetworkManager::TeamPortSetting::name() const |
59 | { |
60 | Q_D(const TeamPortSetting); |
61 | |
62 | return d->name; |
63 | } |
64 | |
65 | void NetworkManager::TeamPortSetting::config(const QString &config) |
66 | { |
67 | Q_D(TeamPortSetting); |
68 | |
69 | d->config = config; |
70 | } |
71 | |
72 | QString NetworkManager::TeamPortSetting::config() const |
73 | { |
74 | Q_D(const TeamPortSetting); |
75 | |
76 | return d->config; |
77 | } |
78 | |
79 | void NetworkManager::TeamPortSetting::lacpKey(qint32 key) |
80 | { |
81 | Q_D(TeamPortSetting); |
82 | |
83 | d->lacpKey = key; |
84 | } |
85 | |
86 | qint32 NetworkManager::TeamPortSetting::lacpKey() const |
87 | { |
88 | Q_D(const TeamPortSetting); |
89 | |
90 | return d->lacpKey; |
91 | } |
92 | |
93 | void NetworkManager::TeamPortSetting::lacpPrio(qint32 priority) |
94 | { |
95 | Q_D(TeamPortSetting); |
96 | |
97 | d->lacpPrio = priority; |
98 | } |
99 | |
100 | qint32 NetworkManager::TeamPortSetting::lacpPrio() const |
101 | { |
102 | Q_D(const TeamPortSetting); |
103 | |
104 | return d->lacpPrio; |
105 | } |
106 | |
107 | void NetworkManager::TeamPortSetting::prio(qint32 prio) |
108 | { |
109 | Q_D(TeamPortSetting); |
110 | |
111 | d->prio = prio; |
112 | } |
113 | |
114 | qint32 NetworkManager::TeamPortSetting::prio() const |
115 | { |
116 | Q_D(const TeamPortSetting); |
117 | |
118 | return d->prio; |
119 | } |
120 | |
121 | void NetworkManager::TeamPortSetting::queueId(qint32 id) |
122 | { |
123 | Q_D(TeamPortSetting); |
124 | |
125 | d->queueId = id; |
126 | } |
127 | |
128 | qint32 NetworkManager::TeamPortSetting::queueId() const |
129 | { |
130 | Q_D(const TeamPortSetting); |
131 | |
132 | return d->queueId; |
133 | } |
134 | |
135 | void NetworkManager::TeamPortSetting::sticky(bool sticky) |
136 | { |
137 | Q_D(TeamPortSetting); |
138 | |
139 | d->sticky = sticky; |
140 | } |
141 | |
142 | bool NetworkManager::TeamPortSetting::sticky() const |
143 | { |
144 | Q_D(const TeamPortSetting); |
145 | |
146 | return d->sticky; |
147 | } |
148 | |
149 | void NetworkManager::TeamPortSetting::setLinkWatchers(const NMVariantMapList &linkWatchers) |
150 | { |
151 | Q_D(TeamPortSetting); |
152 | |
153 | d->linkWatchers = linkWatchers; |
154 | } |
155 | |
156 | NMVariantMapList NetworkManager::TeamPortSetting::linkWatchers() const |
157 | { |
158 | Q_D(const TeamPortSetting); |
159 | |
160 | return d->linkWatchers; |
161 | } |
162 | |
163 | void NetworkManager::TeamPortSetting::fromMap(const QVariantMap &setting) |
164 | { |
165 | if (setting.contains(key: QLatin1String(NM_SETTING_TEAM_PORT_CONFIG))) { |
166 | config(config: setting.value(key: QLatin1String(NM_SETTING_TEAM_PORT_CONFIG)).toString()); |
167 | } |
168 | |
169 | if (setting.contains(key: QLatin1String(NM_SETTING_TEAM_PORT_LACP_KEY))) { |
170 | lacpKey(key: setting.value(key: QLatin1String(NM_SETTING_TEAM_PORT_LACP_KEY)).toUInt()); |
171 | } |
172 | |
173 | if (setting.contains(key: QLatin1String(NM_SETTING_TEAM_PORT_LACP_PRIO))) { |
174 | lacpPrio(priority: setting.value(key: QLatin1String(NM_SETTING_TEAM_PORT_LACP_PRIO)).toUInt()); |
175 | } |
176 | |
177 | if (setting.contains(key: QLatin1String(NM_SETTING_TEAM_PORT_PRIO))) { |
178 | prio(prio: setting.value(key: QLatin1String(NM_SETTING_TEAM_PORT_PRIO)).toUInt()); |
179 | } |
180 | |
181 | if (setting.contains(key: QLatin1String(NM_SETTING_TEAM_PORT_QUEUE_ID))) { |
182 | queueId(id: setting.value(key: QLatin1String(NM_SETTING_TEAM_PORT_QUEUE_ID)).toUInt()); |
183 | } |
184 | |
185 | if (setting.contains(key: QLatin1String(NM_SETTING_TEAM_PORT_STICKY))) { |
186 | sticky(sticky: setting.value(key: QLatin1String(NM_SETTING_TEAM_PORT_STICKY)).toBool()); |
187 | } |
188 | |
189 | if (setting.contains(key: QLatin1String(NM_SETTING_TEAM_PORT_LINK_WATCHERS))) { |
190 | setLinkWatchers(qdbus_cast<NMVariantMapList>(v: setting.value(key: QLatin1String(NM_SETTING_TEAM_PORT_LINK_WATCHERS)))); |
191 | } |
192 | } |
193 | |
194 | QVariantMap NetworkManager::TeamPortSetting::toMap() const |
195 | { |
196 | QVariantMap setting; |
197 | |
198 | if (!config().isEmpty()) { |
199 | setting.insert(key: QLatin1String(NM_SETTING_TEAM_PORT_CONFIG), value: config()); |
200 | } |
201 | |
202 | if (lacpKey() != 0) { |
203 | setting.insert(key: QLatin1String(NM_SETTING_TEAM_PORT_LACP_KEY), value: lacpKey()); |
204 | } |
205 | |
206 | if (lacpPrio() != 255) { |
207 | setting.insert(key: QLatin1String(NM_SETTING_TEAM_PORT_LACP_PRIO), value: lacpPrio()); |
208 | } |
209 | |
210 | if (prio() != 0) { |
211 | setting.insert(key: QLatin1String(NM_SETTING_TEAM_PORT_PRIO), value: prio()); |
212 | } |
213 | |
214 | if (queueId() != -1) { |
215 | setting.insert(key: QLatin1String(NM_SETTING_TEAM_PORT_QUEUE_ID), value: queueId()); |
216 | } |
217 | |
218 | if (sticky()) { |
219 | setting.insert(key: QLatin1String(NM_SETTING_TEAM_PORT_STICKY), value: sticky()); |
220 | } |
221 | |
222 | if (!linkWatchers().empty()) { |
223 | setting.insert(key: QLatin1String(NM_SETTING_TEAM_PORT_LINK_WATCHERS), value: QVariant::fromValue(value: linkWatchers())); |
224 | } |
225 | |
226 | return setting; |
227 | } |
228 | |
229 | QDebug NetworkManager::operator<<(QDebug dbg, const NetworkManager::TeamPortSetting &setting) |
230 | { |
231 | dbg.nospace() << "type: " << setting.typeAsString(type: setting.type()) << '\n'; |
232 | dbg.nospace() << "initialized: " << !setting.isNull() << '\n'; |
233 | |
234 | dbg.nospace() << NM_SETTING_TEAM_PORT_CONFIG << ": " << setting.config() << '\n'; |
235 | dbg.nospace() << NM_SETTING_TEAM_PORT_LACP_KEY << ": " << setting.lacpKey() << '\n'; |
236 | dbg.nospace() << NM_SETTING_TEAM_PORT_LACP_PRIO << ": " << setting.lacpPrio() << '\n'; |
237 | dbg.nospace() << NM_SETTING_TEAM_PORT_PRIO << ": " << setting.prio() << '\n'; |
238 | dbg.nospace() << NM_SETTING_TEAM_PORT_QUEUE_ID << ": " << setting.queueId() << '\n'; |
239 | dbg.nospace() << NM_SETTING_TEAM_PORT_STICKY << ": " << setting.sticky() << '\n'; |
240 | |
241 | dbg.nospace() << NM_SETTING_TEAM_PORT_LINK_WATCHERS << ": " << '\n'; |
242 | const NMVariantMapList variantMapList = setting.linkWatchers(); |
243 | for (const QVariantMap &linkWatcher : variantMapList) { |
244 | QVariantMap::const_iterator i = linkWatcher.constBegin(); |
245 | while (i != linkWatcher.constEnd()) { |
246 | dbg.nospace() << i.key() << ": " << i.value() << '\n'; |
247 | } |
248 | } |
249 | |
250 | return dbg.maybeSpace(); |
251 | } |
252 | |