@@ -136,17 +136,49 @@ def get_asic_id_from_name(asic_name):
136
136
raise ValueError ('Unknown asic namespace name {}' .format (asic_name ))
137
137
138
138
139
+ def get_current_namespace ():
140
+ """
141
+ This API returns the network namespace in which it is
142
+ invoked. In case of global namepace the API returns None
143
+ """
144
+
145
+ net_namespace = None
146
+ command = ["/bin/ip netns identify" , str (os .getpid ())]
147
+ proc = subprocess .Popen (command ,
148
+ stdout = subprocess .PIPE ,
149
+ shell = True ,
150
+ stderr = subprocess .STDOUT )
151
+ try :
152
+ stdout , stderr = proc .communicate ()
153
+ if proc .returncode != 0 :
154
+ raise RuntimeError (
155
+ "Command {} failed with stderr {}" .format (command , stderr )
156
+ )
157
+ if stdout .rstrip ('\n ' ) != "" :
158
+ net_namespace = stdout .rstrip ('\n ' )
159
+ except OSError as e :
160
+ raise OSError ("Error running command {}" .format (command ))
161
+
162
+ return net_namespace
163
+
164
+
139
165
def get_namespaces_from_linux ():
140
166
"""
141
167
In a multi asic platform, each ASIC is in a Linux Namespace.
142
- This method returns list of all the Namespace present on the device
168
+ This method returns the asic namespace under which this is invoked,
169
+ if namespace is None (global namespace) it returns list of all
170
+ the Namespace present on the device
143
171
144
172
Note: It is preferable to use this function only when config_db is not
145
173
available. When configdb is available use get_all_namespaces()
146
174
147
175
Returns:
148
176
List of the namespaces present in the system
149
177
"""
178
+ current_ns = get_current_namespace ()
179
+ if current_ns :
180
+ return [current_ns ]
181
+
150
182
ns_list = []
151
183
for path in glob .glob (NAMESPACE_PATH_GLOB ):
152
184
ns = os .path .basename (path )
0 commit comments