@@ -189,11 +189,6 @@ def verify(config):
189
189
if 'rule' not in config :
190
190
raise ConfigError (f'Rule must be defined!' )
191
191
192
- # As PoC allow only one rule for CGNAT translations
193
- # one internal pool and one external pool
194
- if len (config ['rule' ]) > 1 :
195
- raise ConfigError (f'Only one rule is allowed for translations!' )
196
-
197
192
for pool in ('external' , 'internal' ):
198
193
if pool not in config ['pool' ]:
199
194
raise ConfigError (f'{ pool } pool must be defined!' )
@@ -208,6 +203,8 @@ def verify(config):
208
203
internal_pools_query = "keys(pool.internal)"
209
204
internal_pools : list = jmespath .search (internal_pools_query , config )
210
205
206
+ used_external_pools = {}
207
+ used_internal_pools = {}
211
208
for rule , rule_config in config ['rule' ].items ():
212
209
if 'source' not in rule_config :
213
210
raise ConfigError (f'Rule "{ rule } " source pool must be defined!' )
@@ -217,57 +214,82 @@ def verify(config):
217
214
if 'translation' not in rule_config :
218
215
raise ConfigError (f'Rule "{ rule } " translation pool must be defined!' )
219
216
217
+ # Check if pool exists
220
218
internal_pool = rule_config ['source' ]['pool' ]
221
219
if internal_pool not in internal_pools :
222
220
raise ConfigError (f'Internal pool "{ internal_pool } " does not exist!' )
223
-
224
221
external_pool = rule_config ['translation' ]['pool' ]
225
222
if external_pool not in external_pools :
226
223
raise ConfigError (f'External pool "{ external_pool } " does not exist!' )
227
224
225
+ # Check pool duplication in different rules
226
+ if external_pool in used_external_pools :
227
+ raise ConfigError (
228
+ f'External pool "{ external_pool } " is already used in rule '
229
+ f'{ used_external_pools [external_pool ]} and cannot be used in '
230
+ f'rule { rule } !'
231
+ )
232
+
233
+ if internal_pool in used_internal_pools :
234
+ raise ConfigError (
235
+ f'Internal pool "{ internal_pool } " is already used in rule '
236
+ f'{ used_internal_pools [internal_pool ]} and cannot be used in '
237
+ f'rule { rule } !'
238
+ )
239
+
240
+ used_external_pools [external_pool ] = rule
241
+ used_internal_pools [internal_pool ] = rule
242
+
228
243
229
244
def generate (config ):
230
245
if not config :
231
246
return None
232
- # first external pool as we allow only one as PoC
233
- ext_pool_name = jmespath .search ("rule.*.translation | [0]" , config ).get ('pool' )
234
- int_pool_name = jmespath .search ("rule.*.source | [0]" , config ).get ('pool' )
235
- ext_query = f'pool.external."{ ext_pool_name } ".range | keys(@)'
236
- int_query = f'pool.internal."{ int_pool_name } ".range'
237
- external_ranges = jmespath .search (ext_query , config )
238
- internal_ranges = [jmespath .search (int_query , config )]
239
-
240
- external_list_count = []
241
- external_list_hosts = []
242
- internal_list_count = []
243
- internal_list_hosts = []
244
- for ext_range in external_ranges :
245
- # External hosts count
246
- e_count = IPOperations (ext_range ).get_ips_count ()
247
- external_list_count .append (e_count )
248
- # External hosts list
249
- e_hosts = IPOperations (ext_range ).convert_prefix_to_list_ips ()
250
- external_list_hosts .extend (e_hosts )
251
- for int_range in internal_ranges :
252
- # Internal hosts count
253
- i_count = IPOperations (int_range ).get_ips_count ()
254
- internal_list_count .append (i_count )
255
- # Internal hosts list
256
- i_hosts = IPOperations (int_range ).convert_prefix_to_list_ips ()
257
- internal_list_hosts .extend (i_hosts )
258
-
259
- external_host_count = sum (external_list_count )
260
- internal_host_count = sum (internal_list_count )
261
- ports_per_user = int (
262
- jmespath .search (f'pool.external."{ ext_pool_name } ".per_user_limit.port' , config )
263
- )
264
- external_port_range : str = jmespath .search (
265
- f'pool.external."{ ext_pool_name } ".external_port_range' , config
266
- )
267
247
268
- proto_maps , other_maps = generate_port_rules (
269
- external_list_hosts , internal_list_hosts , ports_per_user , external_port_range
270
- )
248
+ proto_maps = []
249
+ other_maps = []
250
+
251
+ for rule , rule_config in config ['rule' ].items ():
252
+ ext_pool_name : str = rule_config ['translation' ]['pool' ]
253
+ int_pool_name : str = rule_config ['source' ]['pool' ]
254
+
255
+ external_ranges : list = [range for range in config ['pool' ]['external' ][ext_pool_name ]['range' ]]
256
+ internal_ranges : list = [range for range in config ['pool' ]['internal' ][int_pool_name ]['range' ]]
257
+ external_list_hosts_count = []
258
+ external_list_hosts = []
259
+ internal_list_hosts_count = []
260
+ internal_list_hosts = []
261
+
262
+ for ext_range in external_ranges :
263
+ # External hosts count
264
+ e_count = IPOperations (ext_range ).get_ips_count ()
265
+ external_list_hosts_count .append (e_count )
266
+ # External hosts list
267
+ e_hosts = IPOperations (ext_range ).convert_prefix_to_list_ips ()
268
+ external_list_hosts .extend (e_hosts )
269
+
270
+ for int_range in internal_ranges :
271
+ # Internal hosts count
272
+ i_count = IPOperations (int_range ).get_ips_count ()
273
+ internal_list_hosts_count .append (i_count )
274
+ # Internal hosts list
275
+ i_hosts = IPOperations (int_range ).convert_prefix_to_list_ips ()
276
+ internal_list_hosts .extend (i_hosts )
277
+
278
+ external_host_count = sum (external_list_hosts_count )
279
+ internal_host_count = sum (internal_list_hosts_count )
280
+ ports_per_user = int (
281
+ jmespath .search (f'pool.external."{ ext_pool_name } ".per_user_limit.port' , config )
282
+ )
283
+ external_port_range : str = jmespath .search (
284
+ f'pool.external."{ ext_pool_name } ".external_port_range' , config
285
+ )
286
+
287
+ rule_proto_maps , rule_other_maps = generate_port_rules (
288
+ external_list_hosts , internal_list_hosts , ports_per_user , external_port_range
289
+ )
290
+
291
+ proto_maps .extend (rule_proto_maps )
292
+ other_maps .extend (rule_other_maps )
271
293
272
294
config ['proto_map_elements' ] = ', ' .join (proto_maps )
273
295
config ['other_map_elements' ] = ', ' .join (other_maps )
0 commit comments