File: vnflcm_op_occs.py

package info (click to toggle)
python-tackerclient 2.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,460 kB
  • sloc: python: 11,603; makefile: 23; sh: 3
file content (353 lines) | stat: -rw-r--r-- 11,909 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from osc_lib.command import command
from osc_lib import utils
from tackerclient.i18n import _
from tackerclient.osc import sdk_utils
from tackerclient.osc import utils as tacker_osc_utils

_VNF_LCM_OP_OCC_ID = 'vnf_lcm_op_occ_id'

_MIXED_CASE_FIELDS = ['operationState', 'stateEnteredTime', 'startTime',
                      'vnfInstanceId', 'grantId', 'isAutomaticInvocation',
                      'isCancelPending', 'cancelMode', 'operationParams',
                      'resourceChanges', 'changedInfo',
                      'changedExtConnectivity']

_FORMATTERS = {
    'operationParams': tacker_osc_utils.FormatComplexDataColumn,
    'error': tacker_osc_utils.FormatComplexDataColumn,
    'resourceChanges': tacker_osc_utils.FormatComplexDataColumn,
    'changedInfo': tacker_osc_utils.FormatComplexDataColumn,
    'changedExtConnectivity': tacker_osc_utils.FormatComplexDataColumn,
    '_links': tacker_osc_utils.FormatComplexDataColumn
}

_ATTR_MAP = (
    ('id', 'id', tacker_osc_utils.LIST_BOTH),
    ('operationState', 'operationState', tacker_osc_utils.LIST_BOTH),
    ('vnfInstanceId', 'vnfInstanceId', tacker_osc_utils.LIST_BOTH),
    ('operation', 'operation', tacker_osc_utils.LIST_BOTH)
)


def _get_columns(vnflcm_op_occ_obj, action=None):

    column_map = {
        'id': 'ID',
        'operationState': 'Operation State',
        'stateEnteredTime': 'State Entered Time',
        'startTime': 'Start Time',
        'vnfInstanceId': 'VNF Instance ID',
        'operation': 'Operation',
        'isAutomaticInvocation': 'Is Automatic Invocation',
        'isCancelPending': 'Is Cancel Pending',
        'error': 'Error',
        '_links': 'Links'
    }

    if action == 'show':
        column_map.update(
            {'operationParams': 'Operation Parameters',
             'grantId': 'Grant ID',
             'resourceChanges': 'Resource Changes',
             'changedInfo': 'Changed Info',
             'cancelMode': 'Cancel Mode',
             'changedExtConnectivity': 'Changed External Connectivity'}
        )

    return sdk_utils.get_osc_show_columns_for_sdk_resource(vnflcm_op_occ_obj,
                                                           column_map)


class RollbackVnfLcmOp(command.Command):
    def get_parser(self, prog_name):
        """Add arguments to parser.

        Args:
            prog_name ([type]): program name

        Returns:
            parser([ArgumentParser]):
        """
        parser = super(RollbackVnfLcmOp, self).get_parser(prog_name)
        parser.add_argument(
            _VNF_LCM_OP_OCC_ID,
            metavar="<vnf-lcm-op-occ-id>",
            help=_('VNF lifecycle management operation occurrence ID.'))

        return parser

    def take_action(self, parsed_args):
        """Execute rollback_vnf_instance and output comment.

        Args:
            parsed_args ([Namespace]): arguments of CLI.
        """
        client = self.app.client_manager.tackerclient
        result = client.rollback_vnf_instance(parsed_args.vnf_lcm_op_occ_id)
        if not result:
            print((_('Rollback request for LCM operation %(id)s has been'
                     ' accepted') % {'id': parsed_args.vnf_lcm_op_occ_id}))


class CancelVnfLcmOp(command.ShowOne):
    _description = _("Cancel VNF Instance")

    def get_parser(self, prog_name):
        """Add arguments to parser.

        Args:
            prog_name ([type]): program name

        Returns:
            parser([ArgumentParser]):
        """
        parser = super(CancelVnfLcmOp, self).get_parser(prog_name)
        parser.add_argument(
            _VNF_LCM_OP_OCC_ID,
            metavar="<vnf-lcm-op-occ-id>",
            help=_('VNF lifecycle management operation occurrence ID.'))
        parser.add_argument(
            "--cancel-mode",
            default='GRACEFUL',
            metavar="<cancel-mode>",
            choices=['GRACEFUL', 'FORCEFUL'],
            help=_("Cancel mode can be 'GRACEFUL' or 'FORCEFUL'. "
                   "Default is 'GRACEFUL'"))
        return parser

    def take_action(self, parsed_args):
        """Execute cancel_vnf_instance and output comment.

        Args:
            parsed_args ([Namespace]): arguments of CLI.
        """
        client = self.app.client_manager.tackerclient
        result = client.cancel_vnf_instance(
            parsed_args.vnf_lcm_op_occ_id,
            {'cancelMode': parsed_args.cancel_mode})
        if not result:
            print((_('Cancel request for LCM operation %(id)s has been'
                     ' accepted') % {'id': parsed_args.vnf_lcm_op_occ_id}))


class FailVnfLcmOp(command.ShowOne):
    _description = _("Fail VNF Instance")

    def get_parser(self, prog_name):
        """Add arguments to parser.

        Args:
            prog_name ([type]): program name

        Returns:
            parser([ArgumentParser]):
        """
        parser = super(FailVnfLcmOp, self).get_parser(prog_name)
        parser.add_argument(
            _VNF_LCM_OP_OCC_ID,
            metavar="<vnf-lcm-op-occ-id>",
            help=_('VNF lifecycle management operation occurrence ID.'))
        return parser

    def take_action(self, parsed_args):
        """Execute fail_vnf_instance and output response.

        Args:
            parsed_args ([Namespace]): arguments of CLI.
        """
        client = self.app.client_manager.tackerclient
        obj = client.fail_vnf_instance(parsed_args.vnf_lcm_op_occ_id)
        display_columns, columns = _get_columns(obj)
        data = utils.get_item_properties(
            sdk_utils.DictModel(obj),
            columns, formatters=_FORMATTERS,
            mixed_case_fields=_MIXED_CASE_FIELDS)
        return (display_columns, data)


class RetryVnfLcmOp(command.Command):
    _description = _("Retry VNF Instance")

    def get_parser(self, prog_name):
        """Add arguments to parser.

        Args:
            prog_name ([type]): program name

        Returns:
            parser([ArgumentParser]):
        """

        parser = super(RetryVnfLcmOp, self).get_parser(prog_name)
        parser.add_argument(
            _VNF_LCM_OP_OCC_ID,
            metavar="<vnf-lcm-op-occ-id>",
            help=_('VNF lifecycle management operation occurrence ID.'))
        return parser

    def take_action(self, parsed_args):
        """Execute retry_vnf_instance and output comment.

        Args:
            parsed_args ([Namespace]): arguments of CLI.
        """
        client = self.app.client_manager.tackerclient
        result = client.retry_vnf_instance(parsed_args.vnf_lcm_op_occ_id)
        if not result:
            print((_('Retry request for LCM operation %(id)s has been'
                     ' accepted') % {'id': parsed_args.vnf_lcm_op_occ_id}))


class ListVnfLcmOp(command.Lister):
    _description = _("List LCM Operation Occurrences")

    def get_parser(self, program_name):
        """Add arguments to parser.

        Args:
            program_name ([type]): program name

        Returns:
            parser([ArgumentParser]):
        """
        parser = super(ListVnfLcmOp, self).get_parser(program_name)
        parser.add_argument(
            "--filter",
            metavar="<filter>",
            help=_("Attribute-based-filtering parameters"),
        )
        fields_exclusive_group = parser.add_mutually_exclusive_group(
            required=False)
        fields_exclusive_group.add_argument(
            "--fields",
            metavar="<fields>",
            help=_("Complex attributes to be included into the response"),
        )
        fields_exclusive_group.add_argument(
            "--exclude-fields",
            metavar="<exclude-fields>",
            help=_("Complex attributes to be excluded from the response"),
        )
        return parser

    def get_attributes(self, exclude=None):
        """Get attributes.

        Args:
            exclude([exclude]): a list of fields which needs to exclude.

        Returns:
            attributes([attributes]): a list of table entry definitions.
            Each entry should be a tuple consisting of
            (API attribute name, header name, listing mode).
        """
        fields = [
            {
                "key": "id",
                "value": "ID"
            },
            {
                "key": "operationState",
                "value": "Operation State"
            },
            {
                "key": "vnfInstanceId",
                "value": "VNF Instance ID"
            },
            {
                "key": "operation",
                "value": "Operation"
            }
        ]

        attributes = []
        if exclude is None:
            exclude = []

        for field in fields:
            if field['value'] not in exclude:
                attributes.extend([(field['key'], field['value'],
                                  tacker_osc_utils.LIST_BOTH)])
        return tuple(attributes)

    def take_action(self, parsed_args):
        """Execute list_vnflcm_op_occs and output response.

        Args:
            parsed_args ([Namespace]): arguments of CLI.
        """
        params = {}
        exclude_fields = []
        extra_fields = []

        if parsed_args.filter:
            params['filter'] = parsed_args.filter
        if parsed_args.fields:
            params['fields'] = parsed_args.fields
            fields = parsed_args.fields.split(',')
            for field in fields:
                extra_fields.append(field.split('/')[0])
        if parsed_args.exclude_fields:
            params['exclude-fields'] = parsed_args.exclude_fields
            fields = parsed_args.exclude_fields.split(',')
            exclude_fields.extend(fields)

        client = self.app.client_manager.tackerclient
        vnflcm_op_occs = client.list_vnf_lcm_op_occs(**params)
        headers, columns = tacker_osc_utils.get_column_definitions(
            self.get_attributes(exclude=exclude_fields),
            long_listing=True)

        dictionary_properties = (utils.get_dict_properties(
            s, columns, mixed_case_fields=_MIXED_CASE_FIELDS)
            for s in vnflcm_op_occs
        )

        return (headers, dictionary_properties)


class ShowVnfLcmOp(command.ShowOne):
    _description = _("Display Operation Occurrence details")

    def get_parser(self, program_name):
        """Add arguments to parser.

        Args:
            program_name ([type]): program name

        Returns:
            parser([ArgumentParser]):
        """
        parser = super(ShowVnfLcmOp, self).get_parser(program_name)
        parser.add_argument(
            _VNF_LCM_OP_OCC_ID,
            metavar="<vnf-lcm-op-occ-id>",
            help=_('VNF lifecycle management operation occurrence ID.'))
        return parser

    def take_action(self, parsed_args):
        """Execute show_vnf_lcm_op_occs and output response.

        Args:
            parsed_args ([Namespace]): arguments of CLI.
        """
        client = self.app.client_manager.tackerclient
        obj = client.show_vnf_lcm_op_occs(parsed_args.vnf_lcm_op_occ_id)
        display_columns, columns = _get_columns(obj, action='show')
        data = utils.get_item_properties(
            sdk_utils.DictModel(obj),
            columns, formatters=_FORMATTERS,
            mixed_case_fields=_MIXED_CASE_FIELDS)
        return (display_columns, data)