| 24 | |
| 25 | |
| 26 | class CsStaticRoutes(CsDataBag): |
| 27 | |
| 28 | def process(self): |
| 29 | logging.debug("Processing CsStaticRoutes file ==> %s" % self.dbag) |
| 30 | for item in self.dbag: |
| 31 | if item == "id": |
| 32 | continue |
| 33 | self.__update(self.dbag[item]) |
| 34 | |
| 35 | |
| 36 | |
| 37 | def __update(self, route): |
| 38 | network = route['network'] |
| 39 | gateway = route['gateway'] |
| 40 | |
| 41 | if route['revoke']: |
| 42 | # Delete from main table |
| 43 | command = "ip route del %s via %s" % (network, gateway) |
| 44 | CsHelper.execute(command) |
| 45 | |
| 46 | # Delete from PBR table if applicable |
| 47 | device = CsHelper.find_device_for_gateway(self.config, gateway) |
| 48 | if device: |
| 49 | cs_route = CsRoute() |
| 50 | table_name = cs_route.get_tablename(device) |
| 51 | command = "ip route del %s via %s table %s" % (network, gateway, table_name) |
| 52 | CsHelper.execute(command) |
| 53 | logging.info("Deleted static route %s via %s from PBR table %s" % (network, gateway, table_name)) |
| 54 | else: |
| 55 | # Add to main table (existing logic) |
| 56 | command = "ip route show | grep '^%s' | awk '{print $1, $3}'" % network |
| 57 | result = CsHelper.execute(command) |
| 58 | if not result: |
| 59 | route_command = "ip route add %s via %s" % (network, gateway) |
| 60 | CsHelper.execute(route_command) |
| 61 | logging.info("Added static route %s via %s to main table" % (network, gateway)) |
| 62 | |
| 63 | # Add to PBR table if applicable |
| 64 | device = CsHelper.find_device_for_gateway(self.config, gateway) |
| 65 | if device: |
| 66 | cs_route = CsRoute() |
| 67 | table_name = cs_route.get_tablename(device) |
| 68 | # Check if route already exists in the PBR table |
| 69 | check_command = "ip route show table %s | grep '^%s' | awk '{print $1, $3}'" % (table_name, network) |
| 70 | result = CsHelper.execute(check_command) |
| 71 | if not result: |
| 72 | # Add route to the interface-specific table |
| 73 | route_command = "ip route add %s via %s dev %s table %s" % (network, gateway, device, table_name) |
| 74 | CsHelper.execute(route_command) |
| 75 | logging.info("Added static route %s via %s to PBR table %s" % (network, gateway, table_name)) |
| 76 | else: |
| 77 | logging.info("Static route %s via %s added to main table only (no matching interface found for PBR table)" % (network, gateway)) |
no outgoing calls
no test coverage detected