summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/network/cloudengine/ce_netstream_aging.py
blob: bdb478bcd1a7add4d7a0792b1467a5e97fa58233 (plain)
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#!/usr/bin/python
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
#
ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['preview'],
                    'supported_by': 'community'}

DOCUMENTATION = """
---
module: ce_netstream_aging
version_added: "2.4"
short_description: Manages timeout mode of NetStream on HUAWEI CloudEngine switches.
description:
    - Manages timeout mode of NetStream on HUAWEI CloudEngine switches.
author: YangYang (@QijunPan)
options:
    timeout_interval:
        description:
            - Netstream timeout interval.
              If is active type the interval is 1-60.
              If is inactive ,the interval is 5-600.
        default: 30
    type:
        description:
            - Specifies the packet type of netstream timeout active interval.
        choices: ['ip', 'vxlan']
    state:
        description:
            - Specify desired state of the resource.
        choices: ['present', 'absent']
        default: present
    timeout_type:
        description:
            - Netstream timeout type.
        choices: ['active', 'inactive', 'tcp-session', 'manual']
    manual_slot:
        description:
            -  Specifies the slot number of netstream manual timeout.
"""

EXAMPLES = '''
- name: netstream aging module test
  hosts: cloudengine
  connection: local
  gather_facts: no
  vars:
    cli:
      host: "{{ inventory_hostname }}"
      port: "{{ ansible_ssh_port }}"
      username: "{{ username }}"
      password: "{{ password }}"
      transport: cli

  tasks:

  - name: Configure netstream ip timeout active interval , the interval is 40 minutes.
    ce_netstream_aging:
      timeout_interval: 40
      type: ip
      timeout_type: active
      state: present
      provider: "{{ cli }}"

  - name: Configure netstream vxlan timeout active interval , the interval is 40 minutes.
    ce_netstream_aging:
      timeout_interval: 40
      type: vxlan
      timeout_type: active
      active_state: present
      provider: "{{ cli }}"

  - name: Delete netstream ip timeout active interval , set the ip timeout interval to 30 minutes.
    ce_netstream_aging:
      type: ip
      timeout_type: active
      state: absent
      provider: "{{ cli }}"

  - name: Delete netstream vxlan timeout active interval , set the vxlan timeout interval to 30 minutes.
    ce_netstream_aging:
      type: vxlan
      timeout_type: active
      state: absent
      provider: "{{ cli }}"

  - name: Enable netstream ip tcp session timeout.
    ce_netstream_aging:
      type: ip
      timeout_type: tcp-session
      state: present
      provider: "{{ cli }}"

  - name: Enable netstream vxlan tcp session timeout.
    ce_netstream_aging:
      type: vxlan
      timeout_type: tcp-session
      state: present
      provider: "{{ cli }}"

  - name: Disable netstream ip tcp session timeout.
    ce_netstream_aging:
      type: ip
      timeout_type: tcp-session
      state: absent
      provider: "{{ cli }}"

  - name: Disable netstream vxlan tcp session timeout.
    ce_netstream_aging:
      type: vxlan
      timeout_type: tcp-session
      state: absent
      provider: "{{ cli }}"
'''

RETURN = '''
proposed:
    description: k/v pairs of parameters passed into module
    returned: verbose mode
    type: dict
    sample: {"timeout_interval": "40",
             "type": "ip",
             "state": "absent",
             "timeout_type": active}
existing:
    description: k/v pairs of existing configuration
    returned: verbose mode
    type: dict
    sample: {"active_timeout": [
            {
                "ip": "40",
                "vxlan": 30
            }
        ],
        "inactive_timeout": [
            {
                "ip": 30,
                "vxlan": 30
            }
        ],
        "tcp_timeout": [
            {
                "ip": "disable",
                "vxlan": "disable"
            }
        ]}
end_state:
    description: k/v pairs of configuration after module execution
    returned: verbose mode
    type: dict
    sample: {"active_timeout": [
            {
                "ip": 30,
                "vxlan": 30
            }
        ],
        "inactive_timeout": [
            {
                "ip": 30,
                "vxlan": 30
            }
        ],
        "tcp_timeout": [
            {
                "ip": "disable",
                "vxlan": "disable"
            }
        ]}
updates:
    description: commands sent to the device
    returned: always
    type: list
    sample: ["undo netstream timeout ip active 40"]
changed:
    description: check to see if a change was made on the device
    returned: always
    type: bool
    sample: true
'''

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.network.cloudengine.ce import exec_command, load_config
from ansible.module_utils.network.cloudengine.ce import ce_argument_spec


class NetStreamAging(object):
    """
    Manages netstream aging.
    """

    def __init__(self, argument_spec):
        self.spec = argument_spec
        self.module = None
        self.init_module()

        # module input info
        self.timeout_interval = self.module.params['timeout_interval']
        self.type = self.module.params['type']
        self.state = self.module.params['state']
        self.timeout_type = self.module.params['timeout_type']
        self.manual_slot = self.module.params['manual_slot']

        # host info
        self.host = self.module.params['host']
        self.username = self.module.params['username']
        self.port = self.module.params['port']

        # state
        self.changed = False
        self.updates_cmd = list()
        self.commands = list()
        self.results = dict()
        self.proposed = dict()
        self.existing = dict()
        self.end_state = dict()

        # local parameters
        self.existing["active_timeout"] = list()
        self.existing["inactive_timeout"] = list()
        self.existing["tcp_timeout"] = list()
        self.end_state["active_timeout"] = list()
        self.end_state["inactive_timeout"] = list()
        self.end_state["tcp_timeout"] = list()
        self.active_changed = False
        self.inactive_changed = False
        self.tcp_changed = False

    def init_module(self):
        """init module"""

        self.module = AnsibleModule(argument_spec=self.spec, supports_check_mode=True)

    def cli_load_config(self, commands):
        """load config by cli"""

        if not self.module.check_mode:
            load_config(self.module, commands)

    def cli_add_command(self, command, undo=False):
        """add command to self.update_cmd and self.commands"""

        if undo and command.lower() not in ["quit", "return"]:
            cmd = "undo " + command
        else:
            cmd = command

        self.commands.append(cmd)
        if command.lower() not in ["quit", "return"]:
            self.updates_cmd.append(cmd)

    def get_exist_timer_out_para(self):
        """Get exist netstream timeout parameters"""

        active_tmp = dict()
        inactive_tmp = dict()
        tcp_tmp = dict()
        active_tmp["ip"] = "30"
        active_tmp["vxlan"] = "30"
        inactive_tmp["ip"] = "30"
        inactive_tmp["vxlan"] = "30"
        tcp_tmp["ip"] = "absent"
        tcp_tmp["vxlan"] = "absent"

        cmd = "display current-configuration | include ^netstream timeout"
        rc, out, err = exec_command(self.module, cmd)
        if rc != 0:
            self.module.fail_json(msg=err)
        config = str(out).strip()
        if config:
            config = config.lstrip()
            config_list = config.split('\n')
            for config_mem in config_list:
                config_mem = config_mem.lstrip()
                config_mem_list = config_mem.split(' ')
                if config_mem_list[2] == "ip":
                    if config_mem_list[3] == "active":
                        active_tmp["ip"] = config_mem_list[4]
                    if config_mem_list[3] == "inactive":
                        inactive_tmp["ip"] = config_mem_list[4]
                    if config_mem_list[3] == "tcp-session":
                        tcp_tmp["ip"] = "present"
                if config_mem_list[2] == "vxlan":
                    if config_mem_list[4] == "active":
                        active_tmp["vxlan"] = config_mem_list[5]
                    if config_mem_list[4] == "inactive":
                        inactive_tmp["vxlan"] = config_mem_list[5]
                    if config_mem_list[4] == "tcp-session":
                        tcp_tmp["vxlan"] = "present"
        self.existing["active_timeout"].append(active_tmp)
        self.existing["inactive_timeout"].append(inactive_tmp)
        self.existing["tcp_timeout"].append(tcp_tmp)

    def get_end_timer_out_para(self):
        """Get end netstream timeout parameters"""

        active_tmp = dict()
        inactive_tmp = dict()
        tcp_tmp = dict()
        active_tmp["ip"] = "30"
        active_tmp["vxlan"] = "30"
        inactive_tmp["ip"] = "30"
        inactive_tmp["vxlan"] = "30"
        tcp_tmp["ip"] = "absent"
        tcp_tmp["vxlan"] = "absent"
        cmd = "display current-configuration | include ^netstream timeout"
        rc, out, err = exec_command(self.module, cmd)
        if rc != 0:
            self.module.fail_json(msg=err)
        config = str(out).strip()
        if config:
            config = config.lstrip()
            config_list = config.split('\n')
            for config_mem in config_list:
                config_mem = config_mem.lstrip()
                config_mem_list = config_mem.split(' ')
                if len(config_mem_list) > 4 and config_mem_list[2] == "ip":
                    if config_mem_list[3] == "active":
                        active_tmp["ip"] = config_mem_list[4]
                    if config_mem_list[3] == "inactive":
                        inactive_tmp["ip"] = config_mem_list[4]
                    if config_mem_list[3] == "tcp-session":
                        tcp_tmp["ip"] = "present"
                if len(config_mem_list) > 5 and config_mem_list[2] == "vxlan":
                    if config_mem_list[4] == "active":
                        active_tmp["vxlan"] = config_mem_list[5]
                    if config_mem_list[4] == "inactive":
                        inactive_tmp["vxlan"] = config_mem_list[5]
                    if config_mem_list[4] == "tcp-session":
                        tcp_tmp["vxlan"] = "present"
        self.end_state["active_timeout"].append(active_tmp)
        self.end_state["inactive_timeout"].append(inactive_tmp)
        self.end_state["tcp_timeout"].append(tcp_tmp)

    def check_params(self):
        """Check all input params"""

        # interval check
        if not str(self.timeout_interval).isdigit():
            self.module.fail_json(
                msg='Error: Timeout interval should be numerical.')
        if self.timeout_type == "active":
            if int(self.timeout_interval) < 1 or int(self.timeout_interval) > 60:
                self.module.fail_json(
                    msg="Error: Active interval should between 1 - 60 minutes.")
        if self.timeout_type == "inactive":
            if int(self.timeout_interval) < 5 or int(self.timeout_interval) > 600:
                self.module.fail_json(
                    msg="Error: Inactive interval should between 5 - 600 seconds.")
        if self.timeout_type == "manual":
            if not self.manual_slot:
                self.module.fail_json(
                    msg="Error: If use manual timeout mode,slot number is needed.")
            if not str(self.manual_slot).isdigit():
                self.module.fail_json(
                    msg='Error: Slot number should be numerical.')

    def get_proposed(self):
        """get proposed info"""

        if self.timeout_interval:
            self.proposed["timeout_interval"] = self.timeout_interval
        if self.timeout_type:
            self.proposed["timeout_type"] = self.timeout_type
        if self.type:
            self.proposed["type"] = self.type
        if self.state:
            self.proposed["state"] = self.state
        if self.manual_slot:
            self.proposed["manual_slot"] = self.manual_slot

    def get_existing(self):
        """get existing info"""
        active_tmp = dict()
        inactive_tmp = dict()
        tcp_tmp = dict()

        self.get_exist_timer_out_para()

        if self.timeout_type == "active":
            for active_tmp in self.existing["active_timeout"]:
                if self.state == "present":
                    if str(active_tmp[self.type]) != self.timeout_interval:
                        self.active_changed = True
                else:
                    if self.timeout_interval != "30":
                        if str(active_tmp[self.type]) != "30":
                            if str(active_tmp[self.type]) != self.timeout_interval:
                                self.module.fail_json(
                                    msg='Error: The specified active interval do not exist.')
                    if str(active_tmp[self.type]) != "30":
                        self.timeout_interval = active_tmp[self.type]
                        self.active_changed = True
        if self.timeout_type == "inactive":
            for inactive_tmp in self.existing["inactive_timeout"]:
                if self.state == "present":
                    if str(inactive_tmp[self.type]) != self.timeout_interval:
                        self.inactive_changed = True
                else:
                    if self.timeout_interval != "30":
                        if str(inactive_tmp[self.type]) != "30":
                            if str(inactive_tmp[self.type]) != self.timeout_interval:
                                self.module.fail_json(
                                    msg='Error: The specified inactive interval do not exist.')
                    if str(inactive_tmp[self.type]) != "30":
                        self.timeout_interval = inactive_tmp[self.type]
                        self.inactive_changed = True
        if self.timeout_type == "tcp-session":
            for tcp_tmp in self.existing["tcp_timeout"]:
                if str(tcp_tmp[self.type]) != self.state:
                    self.tcp_changed = True

    def operate_time_out(self):
        """configure timeout parameters"""

        cmd = ""
        if self.timeout_type == "manual":
            if self.type == "ip":
                self.cli_add_command("quit")
                cmd = "reset netstream cache ip slot %s" % self.manual_slot
                self.cli_add_command(cmd)
            elif self.type == "vxlan":
                self.cli_add_command("quit")
                cmd = "reset netstream cache vxlan inner-ip slot %s" % self.manual_slot
                self.cli_add_command(cmd)

        if not self.active_changed and not self.inactive_changed and not self.tcp_changed:
            if self.commands:
                self.cli_load_config(self.commands)
                self.changed = True
            return

        if self.active_changed or self.inactive_changed:
            if self.type == "ip":
                cmd = "netstream timeout ip %s %s" % (self.timeout_type, self.timeout_interval)
            elif self.type == "vxlan":
                cmd = "netstream timeout vxlan inner-ip %s %s" % (self.timeout_type, self.timeout_interval)
            if self.state == "absent":
                self.cli_add_command(cmd, undo=True)
            else:
                self.cli_add_command(cmd)
        if self.timeout_type == "tcp-session" and self.tcp_changed:
            if self.type == "ip":
                if self.state == "present":
                    cmd = "netstream timeout ip tcp-session"
                else:
                    cmd = "undo netstream timeout ip tcp-session"

            elif self.type == "vxlan":
                if self.state == "present":
                    cmd = "netstream timeout vxlan inner-ip tcp-session"
                else:
                    cmd = "undo netstream timeout vxlan inner-ip tcp-session"
            self.cli_add_command(cmd)
        if self.commands:
            self.cli_load_config(self.commands)
            self.changed = True

    def get_end_state(self):
        """get end state info"""

        self.get_end_timer_out_para()

    def work(self):
        """worker"""

        self.check_params()
        self.get_existing()
        self.get_proposed()
        self.operate_time_out()
        self.get_end_state()
        self.results['changed'] = self.changed
        self.results['proposed'] = self.proposed
        self.results['existing'] = self.existing
        self.results['end_state'] = self.end_state
        if self.changed:
            self.results['updates'] = self.updates_cmd
        else:
            self.results['updates'] = list()

        self.module.exit_json(**self.results)


def main():
    """Module main"""

    argument_spec = dict(
        timeout_interval=dict(required=False, type='str', default='30'),
        type=dict(required=False, choices=['ip', 'vxlan']),
        state=dict(required=False, choices=['present', 'absent'], default='present'),
        timeout_type=dict(required=False, choices=['active', 'inactive', 'tcp-session', 'manual']),
        manual_slot=dict(required=False, type='str'),
    )
    argument_spec.update(ce_argument_spec)
    module = NetStreamAging(argument_spec)
    module.work()


if __name__ == '__main__':
    main()