Debian Codegraph @

neutronclient/osc/v2/sfc/sfc_port_chain.py

raw
1 # Copyright (c) 2017 Huawei Technologies India Pvt.Limited.
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 import logging
17
18 from osc_lib.cli import parseractions
19 from osc_lib.command import command
20 from osc_lib import exceptions
21 from osc_lib import utils
22 from osc_lib.utils import columns as column_util
23
24 from neutronclient._i18n import _
25
26 LOG = logging.getLogger(__name__)
27
28 resource = 'port_chain'
29
30 _attr_map = (
31 ('id', 'ID', column_util.LIST_BOTH),
32 ('name', 'Name', column_util.LIST_BOTH),
33 ('port_pair_groups', 'Port Pair Groups', column_util.LIST_BOTH),
34 ('flow_classifiers', 'Flow Classifiers',
35 column_util.LIST_BOTH),
36 ('chain_parameters', 'Chain Parameters',
37 column_util.LIST_BOTH),
38 ('description', 'Description', column_util.LIST_LONG_ONLY),
39 ('project_id', 'Project', column_util.LIST_LONG_ONLY),
40 )
41
42 _attr_map_dict = {
43 'id': 'ID',
44 'name': 'Name',
45 'port_pair_groups': 'Port Pair Groups',
46 'flow_classifiers': 'Flow Classifiers',
47 'chain_parameters': 'Chain Parameters',
48 'description': 'Description',
49 'tenant_id': 'Project',
50 'project_id': 'Project',
51 }
52
53
54 class CreateSfcPortChain(command.ShowOne):
55 _description = _("Create a port chain")
56
57 def get_parser(self, prog_name):
58 parser = super(CreateSfcPortChain, self).get_parser(prog_name)
59 parser.add_argument(
60 'name',
61 metavar='<name>',
62 help=_('Name of the port chain'))
63 parser.add_argument(
64 '--description',
65 metavar='<description>',
66 help=_('Description for the port chain'))
67 parser.add_argument(
68 '--flow-classifier',
69 default=[],
70 metavar='<flow-classifier>',
71 dest='flow_classifiers',
72 action='append',
73 help=_('Add flow classifier (name or ID). '
74 'This option can be repeated.'))
75 parser.add_argument(
76 '--chain-parameters',
77 metavar='correlation=<correlation-type>,symmetric=<boolean>',
78 action=parseractions.MultiKeyValueAction,
79 optional_keys=['correlation', 'symmetric'],
80 help=_('Dictionary of chain parameters. Supports '
81 'correlation=(mpls|nsh) (default is mpls) '
82 'and symmetric=(true|false).'))
83 parser.add_argument(
84 '--port-pair-group',
85 metavar='<port-pair-group>',
86 dest='port_pair_groups',
87 required=True,
88 action='append',
89 help=_('Add port pair group (name or ID). '
90 'This option can be repeated.'))
91 return parser
92
93 def take_action(self, parsed_args):
94 client = self.app.client_manager.network
95 attrs = _get_common_attrs(self.app.client_manager, parsed_args)
96 obj = client.create_sfc_port_chain(**attrs)
97 display_columns, columns = utils.get_osc_show_columns_for_sdk_resource(
98 obj, _attr_map_dict, ['location', 'tenant_id'])
99 data = utils.get_dict_properties(obj, columns)
100 return display_columns, data
101
102
103 class DeleteSfcPortChain(command.Command):
104 _description = _("Delete a given port chain")
105
106 def get_parser(self, prog_name):
107 parser = super(DeleteSfcPortChain, self).get_parser(prog_name)
108 parser.add_argument(
109 'port_chain',
110 metavar="<port-chain>",
111 nargs='+',
112 help=_("Port chain(s) to delete (name or ID)")
113 )
114 return parser
115
116 def take_action(self, parsed_args):
117 client = self.app.client_manager.network
118 result = 0
119 for pc in parsed_args.port_chain:
120 try:
121 pc_id = client.find_sfc_port_chain(
122 pc, ignore_missing=False)['id']
123 client.delete_sfc_port_chain(pc_id)
124 except Exception as e:
125 result += 1
126 LOG.error(_("Failed to delete port chain with name "
127 "or ID '%(pc)s': %(e)s"), {'pc': pc, 'e': e})
128 if result > 0:
129 total = len(parsed_args.port_chain)
130 msg = (_("%(result)s of %(total)s port chain(s) "
131 "failed to delete.") % {'result': result,
132 'total': total})
133 raise exceptions.CommandError(msg)
134
135
136 class ListSfcPortChain(command.Lister):
137 _description = _("List port chains")
138
139 def get_parser(self, prog_name):
140 parser = super(ListSfcPortChain, self).get_parser(prog_name)
141 parser.add_argument(
142 '--long',
143 action='store_true',
144 default=False,
145 help=_("List additional fields in output")
146 )
147 return parser
148
149 def take_action(self, parsed_args):
150 client = self.app.client_manager.network
151 data = client.sfc_port_chains()
152 headers, columns = column_util.get_column_definitions(
153 _attr_map, long_listing=parsed_args.long)
154 return (headers,
155 (utils.get_dict_properties(s, columns)
156 for s in data))
157
158
159 class SetSfcPortChain(command.Command):
160 _description = _("Set port chain properties")
161
162 def get_parser(self, prog_name):
163 parser = super(SetSfcPortChain, self).get_parser(prog_name)
164 parser.add_argument(
165 '--name',
166 metavar='<name>',
167 help=_('Name of the port chain'))
168 parser.add_argument(
169 '--description',
170 metavar='<description>',
171 help=_('Description for the port chain'))
172 parser.add_argument(
173 '--flow-classifier',
174 metavar='<flow-classifier>',
175 dest='flow_classifiers',
176 action='append',
177 help=_('Add flow classifier (name or ID). '
178 'This option can be repeated.'))
179 parser.add_argument(
180 '--no-flow-classifier',
181 action='store_true',
182 help=_('Remove associated flow classifiers from the port chain'))
183 parser.add_argument(
184 '--port-pair-group',
185 metavar='<port-pair-group>',
186 dest='port_pair_groups',
187 action='append',
188 help=_('Add port pair group (name or ID). '
189 'Current port pair groups order is kept, the added port '
190 'pair group will be placed at the end of the port chain. '
191 'This option can be repeated.'))
192 parser.add_argument(
193 '--no-port-pair-group',
194 action='store_true',
195 help=_('Remove associated port pair groups from the port chain. '
196 'At least one --port-pair-group must be specified '
197 'together.'))
198 parser.add_argument(
199 'port_chain',
200 metavar='<port-chain>',
201 help=_("Port chain to modify (name or ID)"))
202 return parser
203
204 def take_action(self, parsed_args):
205 client = self.app.client_manager.network
206 pc_id = client.find_sfc_port_chain(parsed_args.port_chain,
207 ignore_missing=False)['id']
208 attrs = _get_common_attrs(self.app.client_manager, parsed_args,
209 is_create=False)
210 if parsed_args.no_flow_classifier:
211 attrs['flow_classifiers'] = []
212 if parsed_args.flow_classifiers:
213 if parsed_args.no_flow_classifier:
214 fc_list = []
215 else:
216 fc_list = client.find_sfc_port_chain(
217 parsed_args.port_chain,
218 ignore_missing=False
219 )['flow_classifiers']
220 for fc in parsed_args.flow_classifiers:
221 fc_id = client.find_sfc_flow_classifier(
222 fc,
223 ignore_missing=False)['id']
224 if fc_id not in fc_list:
225 fc_list.append(fc_id)
226 attrs['flow_classifiers'] = fc_list
227 if (parsed_args.no_port_pair_group and not
228 parsed_args.port_pair_groups):
229 message = _('At least one --port-pair-group must be specified.')
230 raise exceptions.CommandError(message)
231 if parsed_args.no_port_pair_group and parsed_args.port_pair_groups:
232 ppg_list = []
233 for ppg in parsed_args.port_pair_groups:
234 ppg_id = client.find_sfc_port_pair_group(
235 ppg, ignore_missing=False)['id']
236 if ppg_id not in ppg_list:
237 ppg_list.append(ppg_id)
238 attrs['port_pair_groups'] = ppg_list
239 if (parsed_args.port_pair_groups and
240 not parsed_args.no_port_pair_group):
241 ppg_list = client.find_sfc_port_chain(
242 parsed_args.port_chain,
243 ignore_missing=False
244 )['port_pair_groups']
245 for ppg in parsed_args.port_pair_groups:
246 ppg_id = client.find_sfc_port_pair_group(
247 ppg, ignore_missing=False)['id']
248 if ppg_id not in ppg_list:
249 ppg_list.append(ppg_id)
250 attrs['port_pair_groups'] = ppg_list
251 try:
252 client.update_sfc_port_chain(pc_id, **attrs)
253 except Exception as e:
254 msg = (_("Failed to update port chain '%(pc)s': %(e)s")
255 % {'pc': parsed_args.port_chain, 'e': e})
256 raise exceptions.CommandError(msg)
257
258
259 class ShowSfcPortChain(command.ShowOne):
260 _description = _("Display port chain details")
261
262 def get_parser(self, prog_name):
263 parser = super(ShowSfcPortChain, self).get_parser(prog_name)
264 parser.add_argument(
265 'port_chain',
266 metavar="<port-chain>",
267 help=_("Port chain to display (name or ID)")
268 )
269 return parser
270
271 def take_action(self, parsed_args):
272 client = self.app.client_manager.network
273 pc_id = client.find_sfc_port_chain(parsed_args.port_chain,
274 ignore_missing=False)['id']
275 obj = client.get_sfc_port_chain(pc_id)
276 display_columns, columns = utils.get_osc_show_columns_for_sdk_resource(
277 obj, _attr_map_dict, ['location', 'tenant_id'])
278 data = utils.get_dict_properties(obj, columns)
279 return display_columns, data
280
281
282 class UnsetSfcPortChain(command.Command):
283 _description = _("Unset port chain properties")
284
285 def get_parser(self, prog_name):
286 parser = super(UnsetSfcPortChain, self).get_parser(prog_name)
287 parser.add_argument(
288 'port_chain',
289 metavar='<port-chain>',
290 help=_("Port chain to unset (name or ID)"))
291 port_chain = parser.add_mutually_exclusive_group()
292 port_chain.add_argument(
293 '--flow-classifier',
294 action='append',
295 metavar='<flow-classifier>',
296 dest='flow_classifiers',
297 help=_('Remove flow classifier(s) from the port chain '
298 '(name or ID). This option can be repeated.'))
299 port_chain.add_argument(
300 '--all-flow-classifier',
301 action='store_true',
302 help=_('Remove all flow classifiers from the port chain'))
303 parser.add_argument(
304 '--port-pair-group',
305 metavar='<port-pair-group>',
306 dest='port_pair_groups',
307 action='append',
308 help=_('Remove port pair group(s) from the port chain '
309 '(name or ID). This option can be repeated.'))
310 return parser
311
312 def take_action(self, parsed_args):
313 client = self.app.client_manager.network
314 pc_id = client.find_sfc_port_chain(parsed_args.port_chain,
315 ignore_missing=False)['id']
316 attrs = {}
317 if parsed_args.flow_classifiers:
318 fc_list = client.find_sfc_port_chain(
319 parsed_args.port_chain, ignore_missing=False
320 )['flow_classifiers']
321 for fc in parsed_args.flow_classifiers:
322 fc_id = client.find_sfc_flow_classifier(
323 fc,
324 ignore_missing=False)['id']
325 if fc_id in fc_list:
326 fc_list.remove(fc_id)
327 attrs['flow_classifiers'] = fc_list
328 if parsed_args.all_flow_classifier:
329 attrs['flow_classifiers'] = []
330 if parsed_args.port_pair_groups:
331 ppg_list = client.find_sfc_port_chain(
332 parsed_args.port_chain,
333 ignore_missing=False)['port_pair_groups']
334 for ppg in parsed_args.port_pair_groups:
335 ppg_id = client.find_sfc_port_pair_group(
336 ppg,
337 ignore_missing=False)['id']
338 if ppg_id in ppg_list:
339 ppg_list.remove(ppg_id)
340 if ppg_list == []:
341 message = _('At least one port pair group must be'
342 ' specified.')
343 raise exceptions.CommandError(message)
344 attrs['port_pair_groups'] = ppg_list
345 try:
346 client.update_sfc_port_chain(pc_id, **attrs)
347 except Exception as e:
348 msg = (_("Failed to unset port chain '%(pc)s': %(e)s")
349 % {'pc': parsed_args.port_chain, 'e': e})
350 raise exceptions.CommandError(msg)
351
352
353 def _get_common_attrs(client_manager, parsed_args, is_create=True):
354 attrs = {}
355 client = client_manager.network
356 if parsed_args.name is not None:
357 attrs['name'] = parsed_args.name
358 if parsed_args.description is not None:
359 attrs['description'] = parsed_args.description
360 if parsed_args.port_pair_groups:
361 attrs['port_pair_groups'] = [client.find_sfc_port_pair_group(
362 ppg, ignore_missing=False)['id']
363 for ppg in parsed_args.port_pair_groups]
364 if parsed_args.flow_classifiers:
365 attrs['flow_classifiers'] = [client.find_sfc_flow_classifier(
366 fc, ignore_missing=False)['id']
367 for fc in parsed_args.flow_classifiers]
368 if is_create is True:
369 _get_attrs(attrs, parsed_args)
370 return attrs
371
372
373 def _get_attrs(attrs, parsed_args):
374 if parsed_args.chain_parameters is not None:
375 chain_params = {}
376 for chain_param in parsed_args.chain_parameters:
377 if 'correlation' in chain_param:
378 chain_params['correlation'] = chain_param['correlation']
379 if 'symmetric' in chain_param:
380 chain_params['symmetric'] = chain_param['symmetric']
381 attrs['chain_parameters'] = chain_params