16
16
}
17
17
18
18
19
+ def is_enabled (cfg ):
20
+ return cfg .get ('state' , 'disabled' ).lower () == 'enabled'
21
+
22
+
23
+ def is_multi_instance (cfg ):
24
+ return str (cfg .get ('has_per_asic_scope' , 'False' )).lower () == 'true'
25
+
26
+
19
27
class FeatureRegistry :
20
28
""" FeatureRegistry class provides an interface to
21
29
register/de-register new feature persistently. """
@@ -27,51 +35,93 @@ def register(self,
27
35
manifest : Manifest ,
28
36
state : str = 'disabled' ,
29
37
owner : str = 'local' ):
38
+ """ Register feature in CONFIG DBs.
39
+
40
+ Args:
41
+ manifest: Feature's manifest.
42
+ state: Desired feature admin state.
43
+ owner: Owner of this feature (kube/local).
44
+ Returns:
45
+ None.
46
+ """
47
+
30
48
name = manifest ['service' ]['name' ]
31
- for table in self ._get_tables ():
32
- cfg_entries = self .get_default_feature_entries (state , owner )
33
- non_cfg_entries = self .get_non_configurable_feature_entries (manifest )
49
+ db_connectors = self ._sonic_db . get_connectors ()
50
+ cfg_entries = self .get_default_feature_entries (state , owner )
51
+ non_cfg_entries = self .get_non_configurable_feature_entries (manifest )
34
52
35
- exists , current_cfg = table .get (name )
53
+ for conn in db_connectors :
54
+ current_cfg = conn .get_entry (FEATURE , name )
36
55
37
56
new_cfg = cfg_entries .copy ()
38
57
# Override configurable entries with CONFIG DB data.
39
- new_cfg = {** new_cfg , ** dict ( current_cfg ) }
58
+ new_cfg = {** new_cfg , ** current_cfg }
40
59
# Override CONFIG DB data with non configurable entries.
41
60
new_cfg = {** new_cfg , ** non_cfg_entries }
42
61
43
- table . set ( name , list ( new_cfg . items ()) )
62
+ conn . set_entry ( FEATURE , name , new_cfg )
44
63
45
64
def deregister (self , name : str ):
46
- for table in self ._get_tables ():
47
- table ._del (name )
65
+ """ Deregister feature by name.
66
+
67
+ Args:
68
+ name: Name of the feature in CONFIG DB.
69
+ Returns:
70
+ None
71
+ """
72
+
73
+ db_connetors = self ._sonic_db .get_connectors ()
74
+ for conn in db_connetors :
75
+ conn .set_entry (FEATURE , name , None )
76
+
77
+ def update (self ,
78
+ old_manifest : Manifest ,
79
+ new_manifest : Manifest ):
80
+ """ Migrate feature configuration. It can be that non-configurable
81
+ feature entries have to be updated. e.g: "has_timer" for example if
82
+ the new feature introduces a service timer or name of the service has
83
+ changed, but user configurable entries are not changed).
84
+
85
+ Args:
86
+ old_manifest: Old feature manifest.
87
+ new_manifest: New feature manifest.
88
+ Returns:
89
+ None
90
+ """
91
+
92
+ old_name = old_manifest ['service' ]['name' ]
93
+ new_name = new_manifest ['service' ]['name' ]
94
+ db_connectors = self ._sonic_db .get_connectors ()
95
+ non_cfg_entries = self .get_non_configurable_feature_entries (new_manifest )
96
+
97
+ for conn in db_connectors :
98
+ current_cfg = conn .get_entry (FEATURE , old_name )
99
+ conn .set_entry (FEATURE , old_name , None )
100
+
101
+ new_cfg = current_cfg .copy ()
102
+ # Override CONFIG DB data with non configurable entries.
103
+ new_cfg = {** new_cfg , ** non_cfg_entries }
104
+
105
+ conn .set_entry (FEATURE , new_name , new_cfg )
48
106
49
107
def is_feature_enabled (self , name : str ) -> bool :
50
108
""" Returns whether the feature is current enabled
51
109
or not. Accesses running CONFIG DB. If no running CONFIG_DB
52
110
table is found in tables returns False. """
53
111
54
- running_db_table = self ._sonic_db .running_table ( FEATURE )
55
- if running_db_table is None :
112
+ conn = self ._sonic_db .get_running_db_connector ( )
113
+ if conn is None :
56
114
return False
57
115
58
- exists , cfg = running_db_table .get (name )
59
- if not exists :
60
- return False
61
- cfg = dict (cfg )
62
- return cfg .get ('state' ).lower () == 'enabled'
116
+ cfg = conn .get_entry (FEATURE , name )
117
+ return is_enabled (cfg )
63
118
64
119
def get_multi_instance_features (self ):
65
- res = []
66
- init_db_table = self ._sonic_db .initial_table (FEATURE )
67
- for feature in init_db_table .keys ():
68
- exists , cfg = init_db_table .get (feature )
69
- assert exists
70
- cfg = dict (cfg )
71
- asic_flag = str (cfg .get ('has_per_asic_scope' , 'False' ))
72
- if asic_flag .lower () == 'true' :
73
- res .append (feature )
74
- return res
120
+ """ Returns a list of features which run in asic namespace. """
121
+
122
+ conn = self ._sonic_db .get_initial_db_connector ()
123
+ features = conn .get_table (FEATURE )
124
+ return [feature for feature , cfg in features .items () if is_multi_instance (cfg )]
75
125
76
126
@staticmethod
77
127
def get_default_feature_entries (state = None , owner = None ) -> Dict [str , str ]:
0 commit comments