Debian Codegraph @

neutronclient/osc/v2/sfc/sfc_port_pair.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_pair'
29
30 _attr_map = (
31 ('id', 'ID', column_util.LIST_BOTH),
32 ('name', 'Name', column_util.LIST_BOTH),
33 ('ingress', 'Ingress Logical Port', column_util.LIST_BOTH),
34 ('egress', 'Egress Logical Port', column_util.LIST_BOTH),
35 ('service_function_parameters', 'Service Function Parameters',
36 column_util.LIST_LONG_ONLY),
37 ('description', 'Description', column_util.LIST_LONG_ONLY),
38 ('project_id', 'Project', column_util.LIST_LONG_ONLY),
39 )
40
41 _attr_map_dict = {
42 'id': 'ID',
43 'name': 'Name',
44 'description': 'Description',
45 'ingress': 'Ingress Logical Port',
46 'egress': 'Egress Logical Port',
47 'service_function_parameters': 'Service Function Parameters',
48 'tenant_id': 'Project',
49 'project_id': 'Project',
50 }
51
52
53 class CreateSfcPortPair(command.ShowOne):
54 _description = _("Create a port pair")
55
56 def get_parser(self, prog_name):
57 parser = super(CreateSfcPortPair, self).get_parser(prog_name)
58 parser.add_argument(
59 'name',
60 metavar='<name>',
61 help=_('Name of the port pair'))
62 parser.add_argument(
63 '--description',
64 metavar='<description>',
65 help=_('Description for the port pair'))
66 parser.add_argument(
67 '--service-function-parameters',
68 metavar='correlation=<correlation-type>,weight=<weight>',
69 action=parseractions.MultiKeyValueAction,
70 optional_keys=['correlation', 'weight'],
71 help=_('Dictionary of service function parameters. '
72 'Currently, correlation=(None|mpls|nsh) and weight '
73 'are supported. Weight is an integer that influences '
74 'the selection of a port pair within a port pair group '
75 'for a flow. The higher the weight, the more flows will '
76 'hash to the port pair. The default weight is 1.'))
77 parser.add_argument(
78 '--ingress',
79 metavar='<ingress>',
80 required=True,
81 help=_('Ingress neutron port (name or ID)'))
82 parser.add_argument(
83 '--egress',
84 metavar='<egress>',
85 required=True,
86 help=_('Egress neutron port (name or ID)'))
87 return parser
88
89 def take_action(self, parsed_args):
90 client = self.app.client_manager.network
91 attrs = _get_common_attrs(self.app.client_manager, parsed_args)
92 obj = client.create_sfc_port_pair(**attrs)
93 display_columns, columns = utils.get_osc_show_columns_for_sdk_resource(
94 obj, _attr_map_dict, ['location', 'tenant_id'])
95 data = utils.get_dict_properties(obj, columns)
96 return display_columns, data
97
98
99 class DeleteSfcPortPair(command.Command):
100 _description = _("Delete a given port pair")
101
102 def get_parser(self, prog_name):
103 parser = super(DeleteSfcPortPair, self).get_parser(prog_name)
104 parser.add_argument(
105 'port_pair',
106 metavar="<port-pair>",
107 nargs='+',
108 help=_("Port pair(s) to delete (name or ID)")
109 )
110 return parser
111
112 def take_action(self, parsed_args):
113 client = self.app.client_manager.network
114 result = 0
115 for pp in parsed_args.port_pair:
116 try:
117 port_pair_id = client.find_sfc_port_pair(
118 pp, ignore_missing=False)['id']
119 client.delete_sfc_port_pair(port_pair_id)
120 except Exception as e:
121 result += 1
122 LOG.error(_("Failed to delete port pair with name "
123 "or ID '%(port_pair)s': %(e)s"),
124 {'port_pair': pp, 'e': e})
125 if result > 0:
126 total = len(parsed_args.port_pair)
127 msg = (_("%(result)s of %(total)s port pair(s) "
128 "failed to delete.") % {'result': result,
129 'total': total})
130 raise exceptions.CommandError(msg)
131
132
133 class ListSfcPortPair(command.Lister):
134 _description = _("List port pairs")
135
136 def get_parser(self, prog_name):
137 parser = super(ListSfcPortPair, self).get_parser(prog_name)
138 parser.add_argument(
139 '--long',
140 action='store_true',
141 help=_("List additional fields in output")
142 )
143 return parser
144
145 def take_action(self, parsed_args):
146 client = self.app.client_manager.network
147 data = client.sfc_port_pairs()
148 headers, columns = column_util.get_column_definitions(
149 _attr_map, long_listing=parsed_args.long)
150 return (headers,
151 (utils.get_dict_properties(
152 s, columns,
153 ) for s in data))
154
155
156 class SetSfcPortPair(command.Command):
157 _description = _("Set port pair properties")
158
159 def get_parser(self, prog_name):
160 parser = super(SetSfcPortPair, self).get_parser(prog_name)
161 parser.add_argument(
162 '--name',
163 metavar='<name>',
164 help=_('Name of the port pair'))
165 parser.add_argument(
166 '--description',
167 metavar='<description>',
168 help=_('Description for the port pair'))
169 parser.add_argument(
170 'port_pair',
171 metavar='<port-pair>',
172 help=_("Port pair to modify (name or ID)")
173 )
174 return parser
175
176 def take_action(self, parsed_args):
177 client = self.app.client_manager.network
178 port_pair_id = client.find_sfc_port_pair(
179 parsed_args.port_pair, ignore_missing=False
180 )['id']
181 attrs = _get_common_attrs(self.app.client_manager, parsed_args,
182 is_create=False)
183 try:
184 client.update_sfc_port_pair(port_pair_id, **attrs)
185 except Exception as e:
186 msg = (_("Failed to update port pair '%(port_pair)s': %(e)s")
187 % {'port_pair': parsed_args.port_pair, 'e': e})
188 raise exceptions.CommandError(msg)
189
190
191 class ShowSfcPortPair(command.ShowOne):
192 _description = _("Display port pair details")
193
194 def get_parser(self, prog_name):
195 parser = super(ShowSfcPortPair, self).get_parser(prog_name)
196 parser.add_argument(
197 'port_pair',
198 metavar='<port-pair>',
199 help=_("Port pair to display (name or ID)")
200 )
201 return parser
202
203 def take_action(self, parsed_args):
204 client = self.app.client_manager.network
205 port_pair_id = client.find_sfc_port_pair(
206 parsed_args.port_pair, ignore_missing=False
207 )['id']
208 obj = client.get_sfc_port_pair(port_pair_id)
209 display_columns, columns = utils.get_osc_show_columns_for_sdk_resource(
210 obj, _attr_map_dict, ['location', 'tenant_id'])
211 data = utils.get_dict_properties(obj, columns)
212 return display_columns, data
213
214
215 def _get_common_attrs(client_manager, parsed_args, is_create=True):
216 attrs = {}
217 if parsed_args.name is not None:
218 attrs['name'] = parsed_args.name
219 if parsed_args.description is not None:
220 attrs['description'] = parsed_args.description
221 if is_create:
222 _get_attrs(client_manager, attrs, parsed_args)
223 return attrs
224
225
226 def _get_attrs(client_manager, attrs, parsed_args):
227 client = client_manager.network
228 if parsed_args.ingress is not None:
229 attrs['ingress'] = client.find_port(
230 parsed_args.ingress, ignore_missing=False
231 )['id']
232 if parsed_args.egress is not None:
233 attrs['egress'] = client.find_port(
234 parsed_args.egress, ignore_missing=False
235 )['id']
236 if parsed_args.service_function_parameters is not None:
237 attrs['service_function_parameters'] = _get_service_function_params(
238 parsed_args.service_function_parameters)
239
240
241 def _get_service_function_params(sf_params):
242 attrs = {}
243 for sf_param in sf_params:
244 if 'correlation' in sf_param:
245 if sf_param['correlation'] == 'None':
246 attrs['correlation'] = None
247 else:
248 attrs['correlation'] = sf_param['correlation']
249 if 'weight' in sf_param:
250 attrs['weight'] = sf_param['weight']
251 return attrs