Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame^] | 1 | """ |
| 2 | Copyright 2020 The Magma Authors. |
| 3 | |
| 4 | This source code is licensed under the BSD-style license found in the |
| 5 | LICENSE file in the root directory of this source tree. |
| 6 | |
| 7 | Unless required by applicable law or agreed to in writing, software |
| 8 | distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | See the License for the specific language governing permissions and |
| 11 | limitations under the License. |
| 12 | """ |
| 13 | |
| 14 | import logging |
| 15 | import subprocess |
| 16 | |
| 17 | from configuration.service_configs import ( |
| 18 | load_override_config, |
| 19 | load_service_config, |
| 20 | save_override_config, |
| 21 | ) |
| 22 | from orc8r.protos import magmad_pb2 |
| 23 | |
| 24 | STATELESS_SERVICE_CONFIGS = [ |
| 25 | ("mme", "use_stateless", True), |
| 26 | ("mobilityd", "persist_to_redis", True), |
| 27 | ("pipelined", "clean_restart", False), |
| 28 | ("pipelined", "redis_enabled", True), |
| 29 | ("sessiond", "support_stateless", True), |
| 30 | ] |
| 31 | |
| 32 | |
| 33 | def check_stateless_agw(): |
| 34 | num_stateful = 0 |
| 35 | for service, config, value in STATELESS_SERVICE_CONFIGS: |
| 36 | if ( |
| 37 | _check_stateless_service_config(service, config, value) |
| 38 | == magmad_pb2.CheckStatelessResponse.STATEFUL |
| 39 | ): |
| 40 | num_stateful += 1 |
| 41 | |
| 42 | if num_stateful == 0: |
| 43 | res = magmad_pb2.CheckStatelessResponse.STATELESS |
| 44 | elif num_stateful == len(STATELESS_SERVICE_CONFIGS): |
| 45 | res = magmad_pb2.CheckStatelessResponse.STATEFUL |
| 46 | else: |
| 47 | res = magmad_pb2.CheckStatelessResponse.CORRUPT |
| 48 | |
| 49 | logging.debug( |
| 50 | "Check returning %s", magmad_pb2.CheckStatelessResponse.AGWMode.Name( |
| 51 | res, |
| 52 | ), |
| 53 | ) |
| 54 | return res |
| 55 | |
| 56 | |
| 57 | def enable_stateless_agw(): |
| 58 | if check_stateless_agw() == magmad_pb2.CheckStatelessResponse.STATELESS: |
| 59 | logging.info("Nothing to enable, AGW is stateless") |
| 60 | for service, config, value in STATELESS_SERVICE_CONFIGS: |
| 61 | cfg = load_override_config(service) or {} |
| 62 | cfg[config] = value |
| 63 | save_override_config(service, cfg) |
| 64 | |
| 65 | # restart Sctpd so that eNB connections are reset and local state cleared |
| 66 | _restart_sctpd() |
| 67 | |
| 68 | |
| 69 | def disable_stateless_agw(): |
| 70 | if check_stateless_agw() == magmad_pb2.CheckStatelessResponse.STATEFUL: |
| 71 | logging.info("Nothing to disable, AGW is stateful") |
| 72 | for service, config, value in STATELESS_SERVICE_CONFIGS: |
| 73 | cfg = load_override_config(service) or {} |
| 74 | cfg[config] = not value |
| 75 | save_override_config(service, cfg) |
| 76 | |
| 77 | # restart Sctpd so that eNB connections are reset and local state cleared |
| 78 | _restart_sctpd() |
| 79 | |
| 80 | |
| 81 | def _check_stateless_service_config(service, config_name, config_value): |
| 82 | service_config = load_service_config(service) |
| 83 | if service_config.get(config_name) == config_value: |
| 84 | logging.info("STATELESS\t%s -> %s", service, config_name) |
| 85 | return magmad_pb2.CheckStatelessResponse.STATELESS |
| 86 | |
| 87 | logging.info("STATEFUL\t%s -> %s", service, config_name) |
| 88 | return magmad_pb2.CheckStatelessResponse.STATEFUL |
| 89 | |
| 90 | |
| 91 | def _restart_sctpd(): |
| 92 | logging.info("Restarting sctpd") |
| 93 | subprocess.call("service sctpd restart".split()) |