Wei-Yu Chen | ad55cb8 | 2022-02-15 20:07:01 +0800 | [diff] [blame^] | 1 | # SPDX-FileCopyrightText: 2020 The Magma Authors. |
| 2 | # SPDX-FileCopyrightText: 2022 Open Networking Foundation <support@opennetworking.org> |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 5 | |
| 6 | import logging |
| 7 | import subprocess |
| 8 | |
| 9 | from configuration.service_configs import ( |
| 10 | load_override_config, |
| 11 | load_service_config, |
| 12 | save_override_config, |
| 13 | ) |
| 14 | from orc8r.protos import magmad_pb2 |
| 15 | |
| 16 | STATELESS_SERVICE_CONFIGS = [ |
| 17 | ("mme", "use_stateless", True), |
| 18 | ("mobilityd", "persist_to_redis", True), |
| 19 | ("pipelined", "clean_restart", False), |
| 20 | ("pipelined", "redis_enabled", True), |
| 21 | ("sessiond", "support_stateless", True), |
| 22 | ] |
| 23 | |
| 24 | |
| 25 | def check_stateless_agw(): |
| 26 | num_stateful = 0 |
| 27 | for service, config, value in STATELESS_SERVICE_CONFIGS: |
| 28 | if ( |
| 29 | _check_stateless_service_config(service, config, value) |
| 30 | == magmad_pb2.CheckStatelessResponse.STATEFUL |
| 31 | ): |
| 32 | num_stateful += 1 |
| 33 | |
| 34 | if num_stateful == 0: |
| 35 | res = magmad_pb2.CheckStatelessResponse.STATELESS |
| 36 | elif num_stateful == len(STATELESS_SERVICE_CONFIGS): |
| 37 | res = magmad_pb2.CheckStatelessResponse.STATEFUL |
| 38 | else: |
| 39 | res = magmad_pb2.CheckStatelessResponse.CORRUPT |
| 40 | |
| 41 | logging.debug( |
| 42 | "Check returning %s", magmad_pb2.CheckStatelessResponse.AGWMode.Name( |
| 43 | res, |
| 44 | ), |
| 45 | ) |
| 46 | return res |
| 47 | |
| 48 | |
| 49 | def enable_stateless_agw(): |
| 50 | if check_stateless_agw() == magmad_pb2.CheckStatelessResponse.STATELESS: |
| 51 | logging.info("Nothing to enable, AGW is stateless") |
| 52 | for service, config, value in STATELESS_SERVICE_CONFIGS: |
| 53 | cfg = load_override_config(service) or {} |
| 54 | cfg[config] = value |
| 55 | save_override_config(service, cfg) |
| 56 | |
| 57 | # restart Sctpd so that eNB connections are reset and local state cleared |
| 58 | _restart_sctpd() |
| 59 | |
| 60 | |
| 61 | def disable_stateless_agw(): |
| 62 | if check_stateless_agw() == magmad_pb2.CheckStatelessResponse.STATEFUL: |
| 63 | logging.info("Nothing to disable, AGW is stateful") |
| 64 | for service, config, value in STATELESS_SERVICE_CONFIGS: |
| 65 | cfg = load_override_config(service) or {} |
| 66 | cfg[config] = not value |
| 67 | save_override_config(service, cfg) |
| 68 | |
| 69 | # restart Sctpd so that eNB connections are reset and local state cleared |
| 70 | _restart_sctpd() |
| 71 | |
| 72 | |
| 73 | def _check_stateless_service_config(service, config_name, config_value): |
| 74 | service_config = load_service_config(service) |
| 75 | if service_config.get(config_name) == config_value: |
| 76 | logging.info("STATELESS\t%s -> %s", service, config_name) |
| 77 | return magmad_pb2.CheckStatelessResponse.STATELESS |
| 78 | |
| 79 | logging.info("STATEFUL\t%s -> %s", service, config_name) |
| 80 | return magmad_pb2.CheckStatelessResponse.STATEFUL |
| 81 | |
| 82 | |
| 83 | def _restart_sctpd(): |
| 84 | logging.info("Restarting sctpd") |
| 85 | subprocess.call("service sctpd restart".split()) |