19 |
19 |
20 import logging |
20 import logging |
21 |
21 |
22 logger = logging.getLogger('cone.ruleplugin.evals.accesspoint_id_counter') |
22 logger = logging.getLogger('cone.ruleplugin.evals.accesspoint_id_counter') |
23 |
23 |
24 def get_apindex_by_apname(aps, dns, apname): |
24 def get_apindex_by_apname(wlan_aps, aps, dns, apname): |
25 """ |
25 """ |
26 Returns AccessPoint index by given AccessPoint name |
26 Returns AccessPoint index by given AccessPoint name |
27 """ |
27 """ |
28 cnt = _get_ApDnContainer_(aps, dns) |
28 cnt = _get_ApDnContainer_(wlan_aps, aps, dns) |
29 return cnt.get_apindex_by_apname(apname) |
29 return cnt.get_apindex_by_apname(apname) |
30 |
30 |
31 def get_apid_by_apname(aps, dns, apname, wlan_support=True): |
31 def get_apid_by_apname(wlan_aps, aps, dns, apname): |
32 """ |
32 """ |
33 Returns AccessPoint id by given AccessPoint name |
33 Returns AccessPoint id by given AccessPoint name |
34 """ |
34 """ |
35 cnt = _get_ApDnContainer_(aps, dns, wlan_support) |
35 cnt = _get_ApDnContainer_(wlan_aps, aps, dns) |
36 return cnt.get_apid_by_apname(apname) |
36 return cnt.get_apid_by_apname(apname) |
37 |
37 |
38 def get_dnid_by_dnname(aps, dns, dnname, wlan_support=True): |
38 def get_dnid_by_dnname(aps, dns, dnname): |
39 """ |
39 """ |
40 Return DestinationNetwork id by given DestinationNetworks name |
40 Return DestinationNetwork id by given DestinationNetworks name |
41 """ |
41 """ |
42 cnt = _get_ApDnContainer_(aps, dns, wlan_support) |
42 cnt = _get_ApDnContainer_(wlan_aps, aps, dns) |
43 return cnt.get_dnid_by_dnname(dnname) |
43 return cnt.get_dnid_by_dnname(dnname) |
44 |
44 |
45 def get_apid_by_dnname_and_apname(aps, dns, dnname, apname, wlan_support=True): |
45 def get_apid_by_dnname_and_apname(wlan_aps, aps, dns, dnname, apname): |
46 """ |
46 """ |
47 Returns AccessPoint id by given DestinationNetwork name and AccessPoint name. |
47 Returns AccessPoint id by given DestinationNetwork name and AccessPoint name. |
48 """ |
48 """ |
49 cnt = _get_ApDnContainer_(aps, dns, wlan_support) |
49 cnt = _get_ApDnContainer_(wlan_aps, aps, dns) |
50 return cnt.get_apid_by_dnname_and_apname(dnname, apname) |
50 return cnt.get_apid_by_dnname_and_apname(dnname, apname) |
51 |
51 |
52 def get_all_in_array(aps, dns, wlan_support=True): |
52 def get_all_in_array(wlan_aps, aps, dns): |
53 """ |
53 """ |
54 Returns array containing all data: |
54 Returns array containing all data: |
55 [DN name],[DN id], [IAPS names], [IAPS ids], [IAPS indexes] |
55 [DN name],[DN id], [IAPS names], [IAPS ids], [IAPS indexes] |
56 """ |
56 """ |
57 cnt = _get_ApDnContainer_(aps, dns, wlan_support) |
57 cnt = _get_ApDnContainer_(wlan_aps, aps, dns) |
58 return cnt.get_all_in_array() |
58 return cnt.get_all_in_array() |
59 |
59 |
60 def _get_ApDnContainer_(aps, dns, wlan_support=True): |
60 def _get_ApDnContainer_(wlan_aps, aps, dns): |
61 """ |
61 """ |
62 Returns populated ApDnContainer |
62 Returns populated ApDnContainer |
63 """ |
63 """ |
64 cnt = ApDnContainer() |
64 cnt = ApDnContainer() |
65 |
65 |
66 _read_dns_(dns, cnt) |
66 _read_dns_(dns, cnt) |
|
67 _read_wlan_aps_(wlan_aps, cnt) |
67 _read_aps_(aps, cnt) |
68 _read_aps_(aps, cnt) |
68 |
69 |
69 cnt._calc_dn_ids_() |
70 cnt._calc_dn_ids_() |
70 |
71 cnt._calc_ap_ids_() |
71 if wlan_support: |
72 cnt._calc_ap_indexes_() |
72 cnt._calc_ap_ids_(2) |
|
73 else: |
|
74 cnt._calc_ap_ids_(1) |
|
75 |
|
76 cnt._calc_ap_indexes_(1) |
|
77 |
73 |
78 return cnt |
74 return cnt |
79 |
75 |
80 def _read_dns_(dns, cnt): |
76 def _read_dns_(dns, cnt): |
81 """ |
77 """ |
157 myap.set_id(ap_ids2[i]) |
159 myap.set_id(ap_ids2[i]) |
158 myap.set_name(ap_names[i]) |
160 myap.set_name(ap_names[i]) |
159 cnt.add_ap(myap) |
161 cnt.add_ap(myap) |
160 return cnt |
162 return cnt |
161 |
163 |
162 def _get_next_free_id_(bases, start_index=1): |
164 def _read_wlan_aps_(wlan_aps, cnt): |
|
165 """ |
|
166 Reads APs to internal objects to ApDnContainer. |
|
167 """ |
|
168 ap_names = None |
|
169 ap_ids1 = None |
|
170 |
|
171 for ap in wlan_aps.WLAN_AP: |
|
172 if ap.ref == 'ConnectionName': |
|
173 ap_names = ap.value |
|
174 if ap.ref == 'ConnectionId': |
|
175 ap_ids1 = ap.value |
|
176 |
|
177 ap_ids2 = [None]*len(ap_names) |
|
178 if ap_ids1 == None: |
|
179 ap_ids1 = [] |
|
180 |
|
181 |
|
182 for i in range(len(ap_ids1)): |
|
183 ap_ids2[i] = ap_ids1[i] |
|
184 |
|
185 |
|
186 logger.info('Parsed WLAN_AP names: %s' % ap_names) |
|
187 logger.info('Parsed WLAN_AP ids: %s' % ap_ids2) |
|
188 |
|
189 for i in range(len(ap_names)): |
|
190 myap = Ap() |
|
191 myap.set_id(ap_ids2[i]) |
|
192 myap.set_name(ap_names[i]) |
|
193 cnt.add_ap(myap) |
|
194 return cnt |
|
195 |
|
196 def _get_next_free_id_(bases, start_index=0): |
163 """ |
197 """ |
164 Returns next id as a string that is not in use. |
198 Returns next id as a string that is not in use. |
165 """ |
199 """ |
166 |
200 |
167 biggest_id = int(start_index) |
201 biggest_id = int(start_index) |
204 if dn.get_id() == None or dn.get_id() == '': |
238 if dn.get_id() == None or dn.get_id() == '': |
205 dn.set_id(_get_next_free_id_(self.dns, 1)) |
239 dn.set_id(_get_next_free_id_(self.dns, 1)) |
206 |
240 |
207 def _calc_ap_indexes_(self, ind=1): |
241 def _calc_ap_indexes_(self, ind=1): |
208 index = ind |
242 index = ind |
209 |
|
210 for dn in self.dns: |
243 for dn in self.dns: |
211 for iap in dn.get_iaps(): |
244 for iap in dn.get_iaps(): |
212 if iap != None: |
245 if iap != None: |
213 for ap in self.aps: |
246 for ap in self.aps: |
214 if ap.get_name() == iap and ap.get_index() == '': |
247 if ap.get_name() == iap and ap.get_index() == '': |
215 ap.set_index(str(index)) |
248 ap.set_index(str(index)) |
216 index += 1 |
249 index += 1 |
217 |
250 |
218 def _calc_ap_ids_(self, start_index=1): |
251 def _calc_ap_ids_(self, start_index=0): |
219 """ |
252 """ |
220 Calculates unique index for every AccessPoint, if Easy_WLAN is given it always have index 1. |
253 Calculates unique index for every AccessPoint. |
221 """ |
254 """ |
222 |
255 |
223 for ap in self.aps: |
|
224 if ap.name == 'Easy WLAN': |
|
225 ap.set_id('1') |
|
226 logger.info('Easy_WLAN AP found. Setting 1 to AP id.') |
|
227 |
|
228 for ap in self.aps: |
256 for ap in self.aps: |
229 if ap.get_id() == None or ap.get_id() == '': |
257 if ap.get_id() == None or ap.get_id() == '': |
230 ap.set_id(_get_next_free_id_(self.aps, int(start_index))) |
258 ap.set_id(_get_next_free_id_(self.aps, int(start_index))) |
231 |
259 |
232 def get_apid_by_apname(self, apname): |
260 def get_apid_by_apname(self, apname): |
235 """ |
263 """ |
236 |
264 |
237 for ap in self.aps: |
265 for ap in self.aps: |
238 if ap.name == apname: |
266 if ap.name == apname: |
239 return ap.get_id() |
267 return ap.get_id() |
|
268 logger.warning('ApId not found by ApName: %s' % apname) |
240 return None |
269 return None |
241 |
270 |
242 def get_apindex_by_apname(self, apname): |
271 def get_apindex_by_apname(self, apname): |
243 """ |
272 """ |
244 Returns Accesspoint index by given AccessPoint name |
273 Returns Accesspoint index by given AccessPoint name |
245 """ |
274 """ |
246 |
275 |
247 for ap in self.aps: |
276 for ap in self.aps: |
248 if ap.get_name() == apname: |
277 if ap.get_name() == apname: |
249 return ap.get_index() |
278 return ap.get_index() |
|
279 logger.warning('ApIndex not found by ApName: %s' % apname) |
250 return None |
280 return None |
251 |
281 |
252 |
282 |
253 def get_dnid_by_dnname(self, dnname): |
283 def get_dnid_by_dnname(self, dnname): |
254 """ |
284 """ |
255 Return DestinationNetwork id by given DestinationNetworks name |
285 Return DestinationNetwork id by given DestinationNetworks name |
256 """ |
286 """ |
257 for dn in self.dns: |
287 for dn in self.dns: |
258 if dn.name == dnname: |
288 if dn.name == dnname: |
259 return dn.id |
289 return dn.id |
|
290 logger.warning('DnId not found by DnName: %s' % dnname) |
260 return None |
291 return None |
261 |
292 |
262 def get_apid_by_dnname_and_apname(self, dnname, apname): |
293 def get_apid_by_dnname_and_apname(self, dnname, apname): |
263 """ |
294 """ |
264 Returns AccessPoint id by given DestinationNetwork name and AccessPoint name. |
295 Returns AccessPoint id by given DestinationNetwork name and AccessPoint name. |