blob: fb24924f5228b1142ed63b347978d291dd427653 [file] [log] [blame]
Wei-Yu Chen49950b92021-11-08 19:19:18 +08001"""
2Copyright 2020 The Magma Authors.
3
4This source code is licensed under the BSD-style license found in the
5LICENSE file in the root directory of this source tree.
6
7Unless required by applicable law or agreed to in writing, software
8distributed under the License is distributed on an "AS IS" BASIS,
9WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10See the License for the specific language governing permissions and
11limitations under the License.
12"""
13
14import logging
15import subprocess
16
17from configuration.service_configs import (
18 load_override_config,
19 load_service_config,
20 save_override_config,
21)
22from orc8r.protos import magmad_pb2
23
24STATELESS_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
33def 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
57def 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
69def 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
81def _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
91def _restart_sctpd():
92 logging.info("Restarting sctpd")
93 subprocess.call("service sctpd restart".split())