summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/network/cloudengine/ce_snmp_community.py
blob: 53693f621a056c791ec6fd5ec971b1f1ae7215f0 (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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
#!/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 = {'status': ['preview'],
                    'supported_by': 'community',
                    'metadata_version': '1.0'}

DOCUMENTATION = '''
---
module: ce_snmp_community
version_added: "2.4"
short_description: Manages SNMP community configuration on HUAWEI CloudEngine switches.
description:
    - Manages SNMP community configuration on HUAWEI CloudEngine switches.
author:
    - wangdezhuang (@CloudEngine-Ansible)
options:
    acl_number:
        description:
            - Access control list number.
        required: false
        default: null
    community_name:
        description:
            - Unique name to identify the community.
        required: false
        default: null
    access_right:
        description:
            - Access right read or write.
        required: false
        default: null
        choices: ['read','write']
    community_mib_view:
        description:
            - Mib view name.
        required: false
        default: null
    group_name:
        description:
            - Unique name to identify the SNMPv3 group.
        required: false
        default: null
    security_level:
        description:
            - Security level indicating whether to use authentication and encryption.
        required: false
        default: null
        choices: ['noAuthNoPriv', 'authentication', 'privacy']
    read_view:
        description:
            - Mib view name for read.
        required: false
        default: null
    write_view:
        description:
            - Mib view name for write.
        required: false
        default: null
    notify_view:
        description:
            - Mib view name for notification.
        required: false
        default: null
    state:
        description:
            - Manage the state of the resource.
        required: false
        default: present
        choices: ['present','absent']
'''

EXAMPLES = '''

- name: CloudEngine snmp community 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: "Config SNMP community"
    ce_snmp_community:
      state: present
      community_name: Wdz123456789
      access_right: write
      provider: "{{ cli }}"

  - name: "Undo SNMP community"
    ce_snmp_community:
      state: absent
      community_name: Wdz123456789
      access_right: write
      provider: "{{ cli }}"

  - name: "Config SNMP group"
    ce_snmp_community:
      state: present
      group_name: wdz_group
      security_level: noAuthNoPriv
      acl_number: 2000
      provider: "{{ cli }}"

  - name: "Undo SNMP group"
    ce_snmp_community:
      state: absent
      group_name: wdz_group
      security_level: noAuthNoPriv
      acl_number: 2000
      provider: "{{ cli }}"
'''

RETURN = '''
changed:
    description: check to see if a change was made on the device
    returned: always
    type: boolean
    sample: true
proposed:
    description: k/v pairs of parameters passed into module
    returned: always
    type: dict
    sample: {"acl_number": "2000", "group_name": "wdz_group",
             "security_level": "noAuthNoPriv", "state": "present"}
existing:
    description: k/v pairs of existing aaa server
    returned: always
    type: dict
    sample: {}
end_state:
    description: k/v pairs of aaa params after module execution
    returned: always
    type: dict
    sample: {"snmp v3 group": {"snmp_group": ["wdz_group", "noAuthNoPriv", "2000"]}}
updates:
    description: command sent to the device
    returned: always
    type: list
    sample: ["snmp-agent group v3 wdz_group noauthentication acl 2000"]
'''

from xml.etree import ElementTree
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec


# get snmp commutiny
CE_GET_SNMP_COMMUNITY_HEADER = """
    <filter type="subtree">
      <snmp xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
        <communitys>
          <community>
            <communityName></communityName>
            <accessRight></accessRight>
"""
CE_GET_SNMP_COMMUNITY_TAIL = """
          </community>
        </communitys>
      </snmp>
    </filter>
"""
# merge snmp commutiny
CE_MERGE_SNMP_COMMUNITY_HEADER = """
    <config>
      <snmp xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
        <communitys>
          <community operation="merge">
            <communityName>%s</communityName>
            <accessRight>%s</accessRight>
"""
CE_MERGE_SNMP_COMMUNITY_TAIL = """
          </community>
        </communitys>
      </snmp>
    </config>
"""
# create snmp commutiny
CE_CREATE_SNMP_COMMUNITY_HEADER = """
    <config>
      <snmp xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
        <communitys>
          <community operation="create">
            <communityName>%s</communityName>
            <accessRight>%s</accessRight>
"""
CE_CREATE_SNMP_COMMUNITY_TAIL = """
          </community>
        </communitys>
      </snmp>
    </config>
"""
# delete snmp commutiny
CE_DELETE_SNMP_COMMUNITY_HEADER = """
    <config>
      <snmp xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
        <communitys>
          <community operation="delete">
            <communityName>%s</communityName>
            <accessRight>%s</accessRight>
"""
CE_DELETE_SNMP_COMMUNITY_TAIL = """
          </community>
        </communitys>
      </snmp>
    </config>
"""

# get snmp v3 group
CE_GET_SNMP_V3_GROUP_HEADER = """
    <filter type="subtree">
      <snmp xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
        <snmpv3Groups>
          <snmpv3Group>
            <groupName></groupName>
            <securityLevel></securityLevel>
"""
CE_GET_SNMP_V3_GROUP_TAIL = """
          </snmpv3Group>
        </snmpv3Groups>
      </snmp>
    </filter>
"""
# merge snmp v3 group
CE_MERGE_SNMP_V3_GROUP_HEADER = """
    <config>
      <snmp xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
        <snmpv3Groups>
          <snmpv3Group operation="merge">
            <groupName>%s</groupName>
            <securityLevel>%s</securityLevel>
"""
CE_MERGE_SNMP_V3_GROUP_TAIL = """
          </snmpv3Group>
        </snmpv3Groups>
      </snmp>
    </filter>
"""
# create snmp v3 group
CE_CREATE_SNMP_V3_GROUP_HEADER = """
    <config>
      <snmp xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
        <snmpv3Groups>
          <snmpv3Group operation="create">
            <groupName>%s</groupName>
            <securityLevel>%s</securityLevel>
"""
CE_CREATE_SNMP_V3_GROUP_TAIL = """
          </snmpv3Group>
        </snmpv3Groups>
      </snmp>
    </filter>
"""
# delete snmp v3 group
CE_DELETE_SNMP_V3_GROUP_HEADER = """
    <config>
      <snmp xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
        <snmpv3Groups>
          <snmpv3Group operation="delete">
            <groupName>%s</groupName>
            <securityLevel>%s</securityLevel>
"""
CE_DELETE_SNMP_V3_GROUP_TAIL = """
          </snmpv3Group>
        </snmpv3Groups>
      </snmp>
    </filter>
"""


class SnmpCommunity(object):
    """ Manages SNMP community configuration """

    def netconf_get_config(self, **kwargs):
        """ Get configure through netconf """

        module = kwargs["module"]
        conf_str = kwargs["conf_str"]

        xml_str = get_nc_config(module, conf_str)

        return xml_str

    def netconf_set_config(self, **kwargs):
        """ Set configure through netconf """

        module = kwargs["module"]
        conf_str = kwargs["conf_str"]

        xml_str = set_nc_config(module, conf_str)

        return xml_str

    def check_snmp_community_args(self, **kwargs):
        """ Check snmp community args """

        module = kwargs["module"]
        result = dict()
        need_cfg = False
        result["community_info"] = []
        state = module.params['state']
        community_name = module.params['community_name']
        access_right = module.params['access_right']
        acl_number = module.params['acl_number']
        community_mib_view = module.params['community_mib_view']

        if community_name and access_right:
            if len(community_name) > 32 or len(community_name) == 0:
                module.fail_json(
                    msg='Error: The len of community_name %s is out of [1 - 32].' % community_name)

            if acl_number:
                if acl_number.isdigit():
                    if int(acl_number) > 2999 or int(acl_number) < 2000:
                        module.fail_json(
                            msg='Error: The value of acl_number %s is out of [2000 - 2999].' % acl_number)
                else:
                    if not acl_number[0].isalpha() or len(acl_number) > 32 or len(acl_number) < 1:
                        module.fail_json(
                            msg='Error: The len of acl_number %s is out of [1 - 32] or is invalid.' % acl_number)

            if community_mib_view:
                if len(community_mib_view) > 32 or len(community_mib_view) == 0:
                    module.fail_json(
                        msg='Error: The len of community_mib_view %s is out of [1 - 32].' % community_mib_view)

            conf_str = CE_GET_SNMP_COMMUNITY_HEADER
            if acl_number:
                conf_str += "<aclNumber></aclNumber>"
            if community_mib_view:
                conf_str += "<mibViewName></mibViewName>"

            conf_str += CE_GET_SNMP_COMMUNITY_TAIL
            recv_xml = self.netconf_get_config(module=module, conf_str=conf_str)

            if "<data/>" in recv_xml:
                if state == "present":
                    need_cfg = True
            else:
                xml_str = recv_xml.replace('\r', '').replace('\n', '').\
                    replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\
                    replace('xmlns="http://www.huawei.com/netconf/vrp"', "")

                root = ElementTree.fromstring(xml_str)
                community_info = root.findall("data/snmp/communitys/community")
                if community_info:
                    for tmp in community_info:
                        tmp_dict = dict()
                        for site in tmp:
                            if site.tag in ["communityName", "accessRight", "aclNumber", "mibViewName"]:
                                tmp_dict[site.tag] = site.text

                        result["community_info"].append(tmp_dict)

                if result["community_info"]:
                    for tmp in result["community_info"]:
                        if "communityName" in tmp.keys():
                            need_cfg = True

                        if "accessRight" in tmp.keys():
                            if state == "present":
                                if tmp["accessRight"] != access_right:
                                    need_cfg = True
                            else:
                                if tmp["accessRight"] == access_right:
                                    need_cfg = True

                        if acl_number:
                            if "aclNumber" in tmp.keys():
                                if state == "present":
                                    if tmp["aclNumber"] != acl_number:
                                        need_cfg = True
                                else:
                                    if tmp["aclNumber"] == acl_number:
                                        need_cfg = True

                        if community_mib_view:
                            if "mibViewName" in tmp.keys():
                                if state == "present":
                                    if tmp["mibViewName"] != community_mib_view:
                                        need_cfg = True
                                else:
                                    if tmp["mibViewName"] == community_mib_view:
                                        need_cfg = True

        result["need_cfg"] = need_cfg
        return result

    def check_snmp_v3_group_args(self, **kwargs):
        """ Check snmp v3 group args """

        module = kwargs["module"]
        result = dict()
        need_cfg = False
        result["group_info"] = []
        state = module.params['state']
        group_name = module.params['group_name']
        security_level = module.params['security_level']
        acl_number = module.params['acl_number']
        read_view = module.params['read_view']
        write_view = module.params['write_view']
        notify_view = module.params['notify_view']

        community_name = module.params['community_name']
        access_right = module.params['access_right']

        if group_name and security_level:

            if community_name and access_right:
                module.fail_json(
                    msg='Error: Community is used for v1/v2c, group_name is used for v3, do not '
                        'input at the same time.')

            if len(group_name) > 32 or len(group_name) == 0:
                module.fail_json(
                    msg='Error: The len of group_name %s is out of [1 - 32].' % group_name)

            if acl_number:
                if acl_number.isdigit():
                    if int(acl_number) > 2999 or int(acl_number) < 2000:
                        module.fail_json(
                            msg='Error: The value of acl_number %s is out of [2000 - 2999].' % acl_number)
                else:
                    if not acl_number[0].isalpha() or len(acl_number) > 32 or len(acl_number) < 1:
                        module.fail_json(
                            msg='Error: The len of acl_number %s is out of [1 - 32] or is invalid.' % acl_number)

            if read_view:
                if len(read_view) > 32 or len(read_view) < 1:
                    module.fail_json(
                        msg='Error: The len of read_view %s is out of [1 - 32].' % read_view)

            if write_view:
                if len(write_view) > 32 or len(write_view) < 1:
                    module.fail_json(
                        msg='Error: The len of write_view %s is out of [1 - 32].' % write_view)

            if notify_view:
                if len(notify_view) > 32 or len(notify_view) < 1:
                    module.fail_json(
                        msg='Error: The len of notify_view %s is out of [1 - 32].' % notify_view)

            conf_str = CE_GET_SNMP_V3_GROUP_HEADER
            if acl_number:
                conf_str += "<aclNumber></aclNumber>"
            if read_view:
                conf_str += "<readViewName></readViewName>"
            if write_view:
                conf_str += "<writeViewName></writeViewName>"
            if notify_view:
                conf_str += "<notifyViewName></notifyViewName>"

            conf_str += CE_GET_SNMP_V3_GROUP_TAIL
            recv_xml = self.netconf_get_config(module=module, conf_str=conf_str)

            if "<data/>" in recv_xml:
                if state == "present":
                    need_cfg = True
            else:
                xml_str = recv_xml.replace('\r', '').replace('\n', '').\
                    replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\
                    replace('xmlns="http://www.huawei.com/netconf/vrp"', "")

                root = ElementTree.fromstring(xml_str)
                group_info = root.findall("data/snmp/snmpv3Groups/snmpv3Group")
                if group_info:
                    for tmp in group_info:
                        tmp_dict = dict()
                        for site in tmp:
                            if site.tag in ["groupName", "securityLevel", "readViewName", "writeViewName",
                                            "notifyViewName", "aclNumber"]:
                                tmp_dict[site.tag] = site.text

                        result["group_info"].append(tmp_dict)

                if result["group_info"]:
                    for tmp in result["group_info"]:
                        if "groupName" in tmp.keys():
                            if state == "present":
                                if tmp["groupName"] != group_name:
                                    need_cfg = True
                            else:
                                if tmp["groupName"] == group_name:
                                    need_cfg = True

                        if "securityLevel" in tmp.keys():
                            if state == "present":
                                if tmp["securityLevel"] != security_level:
                                    need_cfg = True
                            else:
                                if tmp["securityLevel"] == security_level:
                                    need_cfg = True

                        if acl_number:
                            if "aclNumber" in tmp.keys():
                                if state == "present":
                                    if tmp["aclNumber"] != acl_number:
                                        need_cfg = True
                                else:
                                    if tmp["aclNumber"] == acl_number:
                                        need_cfg = True

                        if read_view:
                            if "readViewName" in tmp.keys():
                                if state == "present":
                                    if tmp["readViewName"] != read_view:
                                        need_cfg = True
                                else:
                                    if tmp["readViewName"] == read_view:
                                        need_cfg = True

                        if write_view:
                            if "writeViewName" in tmp.keys():
                                if state == "present":
                                    if tmp["writeViewName"] != write_view:
                                        need_cfg = True
                                else:
                                    if tmp["writeViewName"] == write_view:
                                        need_cfg = True

                        if notify_view:
                            if "notifyViewName" in tmp.keys():
                                if state == "present":
                                    if tmp["notifyViewName"] != notify_view:
                                        need_cfg = True
                                else:
                                    if tmp["notifyViewName"] == notify_view:
                                        need_cfg = True

        result["need_cfg"] = need_cfg
        return result

    def merge_snmp_community(self, **kwargs):
        """ Merge snmp community operation """

        module = kwargs["module"]
        community_name = module.params['community_name']
        access_right = module.params['access_right']
        acl_number = module.params['acl_number']
        community_mib_view = module.params['community_mib_view']

        conf_str = CE_MERGE_SNMP_COMMUNITY_HEADER % (
            community_name, access_right)
        if acl_number:
            conf_str += "<aclNumber>%s</aclNumber>" % acl_number
        if community_mib_view:
            conf_str += "<mibViewName>%s</mibViewName>" % community_mib_view

        conf_str += CE_MERGE_SNMP_COMMUNITY_TAIL

        recv_xml = self.netconf_set_config(module=module, conf_str=conf_str)

        if "<ok/>" not in recv_xml:
            module.fail_json(msg='Error: Merge snmp community failed.')

        community_safe_name = "******"

        cmd = "snmp-agent community %s %s" % (access_right, community_safe_name)

        if acl_number:
            cmd += " acl %s" % acl_number
        if community_mib_view:
            cmd += " mib-view %s" % community_mib_view

        return cmd

    def create_snmp_community(self, **kwargs):
        """ Create snmp community operation """

        module = kwargs["module"]
        community_name = module.params['community_name']
        access_right = module.params['access_right']
        acl_number = module.params['acl_number']
        community_mib_view = module.params['community_mib_view']

        conf_str = CE_CREATE_SNMP_COMMUNITY_HEADER % (
            community_name, access_right)
        if acl_number:
            conf_str += "<aclNumber>%s</aclNumber>" % acl_number
        if community_mib_view:
            conf_str += "<mibViewName>%s</mibViewName>" % community_mib_view

        conf_str += CE_CREATE_SNMP_COMMUNITY_TAIL

        recv_xml = self.netconf_set_config(module=module, conf_str=conf_str)

        if "<ok/>" not in recv_xml:
            module.fail_json(msg='Error: Create snmp community failed.')

        community_safe_name = "******"

        cmd = "snmp-agent community %s %s" % (access_right, community_safe_name)

        if acl_number:
            cmd += " acl %s" % acl_number
        if community_mib_view:
            cmd += " mib-view %s" % community_mib_view

        return cmd

    def delete_snmp_community(self, **kwargs):
        """ Delete snmp community operation """

        module = kwargs["module"]
        community_name = module.params['community_name']
        access_right = module.params['access_right']
        acl_number = module.params['acl_number']
        community_mib_view = module.params['community_mib_view']

        conf_str = CE_DELETE_SNMP_COMMUNITY_HEADER % (
            community_name, access_right)
        if acl_number:
            conf_str += "<aclNumber>%s</aclNumber>" % acl_number
        if community_mib_view:
            conf_str += "<mibViewName>%s</mibViewName>" % community_mib_view

        conf_str += CE_DELETE_SNMP_COMMUNITY_TAIL

        recv_xml = self.netconf_set_config(module=module, conf_str=conf_str)

        if "<ok/>" not in recv_xml:
            module.fail_json(msg='Error: Create snmp community failed.')

        community_safe_name = "******"
        cmd = "undo snmp-agent community %s %s" % (
            access_right, community_safe_name)

        return cmd

    def merge_snmp_v3_group(self, **kwargs):
        """ Merge snmp v3 group operation """

        module = kwargs["module"]
        group_name = module.params['group_name']
        security_level = module.params['security_level']
        acl_number = module.params['acl_number']
        read_view = module.params['read_view']
        write_view = module.params['write_view']
        notify_view = module.params['notify_view']

        conf_str = CE_MERGE_SNMP_V3_GROUP_HEADER % (group_name, security_level)
        if acl_number:
            conf_str += "<aclNumber>%s</aclNumber>" % acl_number
        if read_view:
            conf_str += "<readViewName>%s</readViewName>" % read_view
        if write_view:
            conf_str += "<writeViewName>%s</writeViewName>" % write_view
        if notify_view:
            conf_str += "<notifyViewName>%s</notifyViewName>" % notify_view
        conf_str += CE_MERGE_SNMP_V3_GROUP_TAIL

        recv_xml = self.netconf_set_config(module=module, conf_str=conf_str)

        if "<ok/>" not in recv_xml:
            module.fail_json(msg='Error: Merge snmp v3 group failed.')

        if security_level == "noAuthNoPriv":
            security_level_cli = "noauthentication"
        elif security_level == "authentication":
            security_level_cli = "authentication"
        elif security_level == "privacy":
            security_level_cli = "privacy"

        cmd = "snmp-agent group v3 %s %s" % (group_name, security_level_cli)

        if read_view:
            cmd += " read-view %s" % read_view
        if write_view:
            cmd += " write-view %s" % write_view
        if notify_view:
            cmd += " notify-view %s" % notify_view
        if acl_number:
            cmd += " acl %s" % acl_number

        return cmd

    def create_snmp_v3_group(self, **kwargs):
        """ Create snmp v3 group operation """

        module = kwargs["module"]
        group_name = module.params['group_name']
        security_level = module.params['security_level']
        acl_number = module.params['acl_number']
        read_view = module.params['read_view']
        write_view = module.params['write_view']
        notify_view = module.params['notify_view']

        conf_str = CE_CREATE_SNMP_V3_GROUP_HEADER % (
            group_name, security_level)
        if acl_number:
            conf_str += "<aclNumber>%s</aclNumber>" % acl_number
        if read_view:
            conf_str += "<readViewName>%s</readViewName>" % read_view
        if write_view:
            conf_str += "<writeViewName>%s</writeViewName>" % write_view
        if notify_view:
            conf_str += "<notifyViewName>%s</notifyViewName>" % notify_view
        conf_str += CE_CREATE_SNMP_V3_GROUP_TAIL

        recv_xml = self.netconf_set_config(module=module, conf_str=conf_str)

        if "<ok/>" not in recv_xml:
            module.fail_json(msg='Error: Create snmp v3 group failed.')

        if security_level == "noAuthNoPriv":
            security_level_cli = "noauthentication"
        elif security_level == "authentication":
            security_level_cli = "authentication"
        elif security_level == "privacy":
            security_level_cli = "privacy"

        cmd = "snmp-agent group v3 %s %s" % (group_name, security_level_cli)

        if read_view:
            cmd += " read-view %s" % read_view
        if write_view:
            cmd += " write-view %s" % write_view
        if notify_view:
            cmd += " notify-view %s" % notify_view
        if acl_number:
            cmd += " acl %s" % acl_number

        return cmd

    def delete_snmp_v3_group(self, **kwargs):
        """ Delete snmp v3 group operation """

        module = kwargs["module"]
        group_name = module.params['group_name']
        security_level = module.params['security_level']
        acl_number = module.params['acl_number']
        read_view = module.params['read_view']
        write_view = module.params['write_view']
        notify_view = module.params['notify_view']

        conf_str = CE_DELETE_SNMP_V3_GROUP_HEADER % (
            group_name, security_level)
        if acl_number:
            conf_str += "<aclNumber>%s</aclNumber>" % acl_number
        if read_view:
            conf_str += "<readViewName>%s</readViewName>" % read_view
        if write_view:
            conf_str += "<writeViewName>%s</writeViewName>" % write_view
        if notify_view:
            conf_str += "<notifyViewName>%s</notifyViewName>" % notify_view
        conf_str += CE_DELETE_SNMP_V3_GROUP_TAIL

        recv_xml = self.netconf_set_config(module=module, conf_str=conf_str)

        if "<ok/>" not in recv_xml:
            module.fail_json(msg='Error: Delete snmp v3 group failed.')

        if security_level == "noAuthNoPriv":
            security_level_cli = "noauthentication"
        elif security_level == "authentication":
            security_level_cli = "authentication"
        elif security_level == "privacy":
            security_level_cli = "privacy"

        cmd = "undo snmp-agent group v3 %s %s" % (
            group_name, security_level_cli)

        return cmd


def main():
    """ main function """

    argument_spec = dict(
        state=dict(choices=['present', 'absent'], default='present'),
        acl_number=dict(type='str'),
        community_name=dict(type='str', no_log=True),
        access_right=dict(choices=['read', 'write']),
        community_mib_view=dict(type='str'),
        group_name=dict(type='str'),
        security_level=dict(
            choices=['noAuthNoPriv', 'authentication', 'privacy']),
        read_view=dict(type='str'),
        write_view=dict(type='str'),
        notify_view=dict(type='str')
    )

    argument_spec.update(ce_argument_spec)
    required_together = [("community_name", "access_right"), ("security_level", "group_name")]
    module = AnsibleModule(
        argument_spec=argument_spec,
        required_together=required_together,
        supports_check_mode=True
    )

    changed = False
    proposed = dict()
    existing = dict()
    end_state = dict()
    updates = []

    state = module.params['state']
    acl_number = module.params['acl_number']
    community_name = module.params['community_name']
    community_mib_view = module.params['community_mib_view']
    access_right = module.params['access_right']
    group_name = module.params['group_name']
    security_level = module.params['security_level']
    read_view = module.params['read_view']
    write_view = module.params['write_view']
    notify_view = module.params['notify_view']

    snmp_community_obj = SnmpCommunity()

    if not snmp_community_obj:
        module.fail_json(msg='Error: Init module failed.')

    snmp_community_rst = snmp_community_obj.check_snmp_community_args(
        module=module)
    snmp_v3_group_rst = snmp_community_obj.check_snmp_v3_group_args(
        module=module)

    # get proposed
    proposed["state"] = state
    if acl_number:
        proposed["acl_number"] = acl_number
    if community_name:
        proposed["community_name"] = community_name
    if community_mib_view:
        proposed["community_mib_view"] = community_mib_view
    if access_right:
        proposed["access_right"] = access_right
    if group_name:
        proposed["group_name"] = group_name
    if security_level:
        proposed["security_level"] = security_level
    if read_view:
        proposed["read_view"] = read_view
    if write_view:
        proposed["write_view"] = write_view
    if notify_view:
        proposed["notify_view"] = notify_view

    # state exist snmp community config
    exist_tmp = dict()
    for item in snmp_community_rst:
        if item != "need_cfg":
            exist_tmp[item] = snmp_community_rst[item]

    if exist_tmp:
        existing["snmp community"] = exist_tmp
    # state exist snmp v3 group config
    exist_tmp = dict()
    for item in snmp_v3_group_rst:
        if item != "need_cfg":
            exist_tmp[item] = snmp_v3_group_rst[item]

    if exist_tmp:
        existing["snmp v3 group"] = exist_tmp

    if state == "present":
        if snmp_community_rst["need_cfg"]:
            if len(snmp_community_rst["community_info"]) != 0:
                cmd = snmp_community_obj.merge_snmp_community(module=module)
                changed = True
                updates.append(cmd)
            else:
                cmd = snmp_community_obj.create_snmp_community(module=module)
                changed = True
                updates.append(cmd)

        if snmp_v3_group_rst["need_cfg"]:
            if len(snmp_v3_group_rst["group_info"]):
                cmd = snmp_community_obj.merge_snmp_v3_group(module=module)
                changed = True
                updates.append(cmd)
            else:
                cmd = snmp_community_obj.create_snmp_v3_group(module=module)
                changed = True
                updates.append(cmd)

    else:
        if snmp_community_rst["need_cfg"]:
            cmd = snmp_community_obj.delete_snmp_community(module=module)
            changed = True
            updates.append(cmd)
        if snmp_v3_group_rst["need_cfg"]:
            cmd = snmp_community_obj.delete_snmp_v3_group(module=module)
            changed = True
            updates.append(cmd)

    # state end snmp community config
    snmp_community_rst = snmp_community_obj.check_snmp_community_args(
        module=module)
    end_tmp = dict()
    for item in snmp_community_rst:
        if item != "need_cfg":
            exist_tmp[item] = snmp_community_rst[item]
    if end_tmp:
        end_state["snmp community"] = end_tmp
    # state exist snmp v3 group config
    snmp_v3_group_rst = snmp_community_obj.check_snmp_v3_group_args(
        module=module)
    end_tmp = dict()
    for item in snmp_v3_group_rst:
        if item != "need_cfg":
            exist_tmp[item] = snmp_v3_group_rst[item]
    if end_tmp:
        end_state["snmp v3 group"] = end_tmp

    results = dict()
    results['proposed'] = proposed
    results['existing'] = existing
    results['changed'] = changed
    results['end_state'] = end_state
    results['updates'] = updates

    module.exit_json(**results)


if __name__ == '__main__':
    main()