#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright: (c) 2019, F5 Networks Inc.
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

DOCUMENTATION = r'''
---
module: bigip_profile_sip
short_description: Manage SIP profiles on a BIG-IP
description:
  - Manage SIP profiles on a BIG-IP system.
version_added: "1.0.0"
options:
  name:
    description:
      - Specifies the name of the SIP profile to manage.
    type: str
    required: True
  parent:
    description:
      - Specifies the profile from which this profile inherits settings.
      - When creating a new profile, if this parameter is not specified, the default
        is the system-supplied C(sip) profile.
    type: str
  community:
    description:
      - When the C(dialog_aware) is C(yes) and the configuration requires multiple SIP virtual server-profile pairings,
        this string value indicates whether the pair belongs to the same SIP proxy functional group.
    type: str
  description:
    description:
      - Description of the profile.
      - To remove the entry completely, set a value of C('').
    type: str
  dialog_aware:
    description:
      - When C(yes), the system gathers SIP dialog information and automatically forwards SIP messages belonging to
        the known SIP dialog.
    type: bool
  enable_sip_firewall:
    description:
      - Specifies whether the Advanced Firewall Manager (AFM) policy is enabled.
      - When C(yes), the SIP Security settings configured in the DoS Profile in AFM apply to the virtual servers that
        use this profile.
    type: bool
  insert_record_route_header:
    description:
      - When C(yes), inserts a Record-Route SIP header, which indicates the next hop for the following SIP request
        messages.
    type: bool
  insert_via_header:
    description:
      - When C(yes), inserts a Via header in the forwarded SIP request.
      - Via headers indicate the path taken through proxy devices and transports used. The response message uses this
        routing information.
    type: bool
  user_via_header:
    description:
      - When C(insert_via_header) is C(yes), specifies the Via value the system inserts as the top Via header in a
        SIP REQUEST message.
      - "The valid value must include SIP protocol and sent_by settings, for example: C(SIP/2.0/UDP 10.10.10.10:5060)."
      - To remove the entry completely, set a value of C('').
    type: str
  log_profile:
    description:
      - Specifies the logging settings the publisher uses to send log messages.
      - The format of the name can be either be prepended by partition (C(/Common/foo)), or specified
        just as an object name (C(foo)).
      - To remove the entry. set a value of C(''), however the profile C(log_publisher)
        must also be set as C('').
    type: str
  log_publisher:
    description:
      - Specifies the publisher defined to log messages.
      - Format of the name can be either be prepended by partition (C(/Common/foo)), or specified
        just as an object name (C(foo)).
      - To remove the entry. set a value of C(''), however the profile C(log_profile)
        must also be set as C('').
    type: str
  secure_via_header:
    description:
      - When checked (enabled), inserts a secure Via header in the forwarded SIP request.
      - A secure Via header indicates where the message originated.
      - This parameter causes the inserted Via header to specify Transport Layer Security. For this option to take
        effect, C(insert_via_header) must be set to (yes).
    type: bool
  security:
    description:
      - "When C(yes). enables the use of enhanced Horizontal Security Layer (HSL) security checking."
    type: bool
  terminate_on_bye:
    description:
      - When C(yes), closes a connection when a BYE transaction finishes.
      - A BYE transaction is a message an application sends to another application when it is ready to close the
        connection between the two.
    type: bool
  max_size:
    description:
      - Specifies the maximum SIP message size that the BIG-IP system accepts.
      - The accepted value range is C(0 - 4294967295) bytes.
    type: int
  partition:
    description:
      - Device partition to manage resources on.
    type: str
    default: Common
  state:
    description:
      - When C(present), ensures the profile exists.
      - When C(absent), ensures the profile is removed.
    type: str
    choices:
      - present
      - absent
    default: present
extends_documentation_fragment: f5networks.f5_modules.f5
author:
  - Wojciech Wypior (@wojtek0806)
'''

EXAMPLES = r'''
- name: Create a SIP profile
  bigip_profile_sip:
    name: foo
    parent: sip
    log_profile: alg_log
    log_publisher: foo-publisher
    description: this is a new profile
    security: yes
    provider:
      password: secret
      server: lb.mydomain.com
      user: admin
  delegate_to: localhost

- name: Update SIP profile
  bigip_profile_sip:
    name: foo
    insert_record_route_header: yes
    enable_sip_firewall: yes
    insert_via_header: yes
    user_via_header: "SIP/2.0/UDP 10.10.10.10:5060"
    provider:
      password: secret
      server: lb.mydomain.com
      user: admin
  delegate_to: localhost

- name: Delete a SIP profile
  bigip_profile_sip:
    name: foo
    state: absent
    provider:
      password: secret
      server: lb.mydomain.com
      user: admin
  delegate_to: localhost
'''

RETURN = r'''
description:
  description: Description of the profile.
  returned: changed
  type: str
  sample: "custom description"
community:
  description: Indicates whether the pair belongs to the same SIP proxy functional group.
  returned: changed
  type: str
  sample: foo_community
parent:
  description: Specifies the profile from which this profile inherits settings.
  returned: changed
  type: str
  sample: /Common/sip
dialog_aware:
  description: Specifies if the system gathers SIP dialog information.
  returned: changed
  type: bool
  sample: no
enable_sip_firewall:
  description: Specifies whether the Advanced Firewall Manager policy is enabled.
  returned: changed
  type: bool
  sample: yes
insert_record_route_header:
  description: Specifies if the system will insert a Record-Route SIP header.
  returned: changed
  type: bool
  sample: yes
insert_via_header:
  description: Specifies if the system will insert a Via header in the forwarded SIP request.
  returned: changed
  type: bool
  sample: yes
user_via_header:
  description: The value the system inserts as the top Via header in a SIP REQUEST message.
  returned: changed
  type: str
  sample: "SIP/2.0/UDP 10.10.10.10:5060"
log_profile:
  description: The logging settings the publisher uses to send log messages.
  returned: changed
  type: str
  sample: "/Common/alg_profile"
log_publisher:
  description: The publisher defined to log messages.
  returned: changed
  type: str
  sample: "/Common/foo_publisher"
secure_via_header:
  description: Specifies if the system will insert a secure Via header in the forwarded SIP request.
  returned: changed
  type: bool
  sample: no
security:
  description: Enables the use of enhanced Horizontal Security Layer security checking.
  returned: changed
  type: bool
  sample: yes
terminate_on_bye:
  description: Specifies if the system will close a connection when a BYE transaction finishes.
  returned: changed
  type: bool
  sample: no
max_size:
  description: Specifies if the system will close a connection when a BYE transaction finishes.
  returned: changed
  type: bool
  sample: no
'''
from datetime import datetime

from ansible.module_utils.basic import (
    AnsibleModule, env_fallback
)

from ..module_utils.bigip import F5RestClient
from ..module_utils.common import (
    F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, flatten_boolean, fq_name
)
from ..module_utils.icontrol import tmos_version
from ..module_utils.teem import send_teem


class Parameters(AnsibleF5Parameters):
    api_map = {
        'defaultsFrom': 'parent',
        'dialogAware': 'dialog_aware',
        'enableSipFirewall': 'enable_sip_firewall',
        'insertRecordRouteHeader': 'insert_record_route_header',
        'insertViaHeader': 'insert_via_header',
        'userViaHeader': 'user_via_header',
        'logProfile': 'log_profile',
        'logPublisher': 'log_publisher',
        'secureViaHeader': 'secure_via_header',
        'terminateOnBye': 'terminate_on_bye',
        'maxSize': 'max_size',
    }

    api_attributes = [
        'community',
        'description',
        'defaultsFrom',
        'dialogAware',
        'enableSipFirewall',
        'insertRecordRouteHeader',
        'insertViaHeader',
        'logProfile',
        'logPublisher',
        'secureViaHeader',
        'security',
        'terminateOnBye',
        'userViaHeader',
        'maxSize',

    ]

    returnables = [
        'description',
        'community',
        'parent',
        'dialog_aware',
        'enable_sip_firewall',
        'insert_record_route_header',
        'insert_via_header',
        'user_via_header',
        'log_profile',
        'log_publisher',
        'secure_via_header',
        'security',
        'terminate_on_bye',
        'max_size',
    ]

    updatables = [
        'description',
        'community',
        'parent',